It takes quite a bit, but once you've mastered a technique or two, it becomes simple habit. Essentially, you should be checking every bit of data that comes in and making sure it contains only what should be allowed. This includes checking lengths of fields (never rely on HTML's 'maxlength' attribute to limit input length) and making sure that any hidden fields contain what you think they do.

The second you find yourself stuffing data into hidden fields, you're most likely wanting to implement a system that maintains user sessions. This way any important information is placed on your server (in some sort of database) where the user can't modify it manually.

You should try restricting the characters used in fields as much as possible (for example, if you are accepting phone numbers, you might try restricting the field to only allow digits, spaces, hyphens and parentheses). And then come checkboxes, radio buttons, select fields. You need to check that the values coming in through these are valid. You might only put 'Chocolate' and 'Vanilla' as options to a select field, but someone can copy the html over and insert their own 'Strawberry' flavour if they'd like.

It's hard to give a full-blown tutorial without writing a book on the subject. Validating data is trivial but at the same time can be a lot of work in some situations. One final note: if you're ever going to use javascript to validate form fields (which is Evil in itself), you still need to validate data on the server side of things. Relying on javascript validation is even worse than relying on the 'maxlength' attribute, because even legit users who are not trying to hack your script apart will be able to submit invalid data if they don't have javascript enabled.

So long post made short: you need to check all incoming data to make sure it is exactly what you are expecting. It's that simple :)


In reply to Re: CGI form data validation by Anonymous Monk
in thread CGI form data validation by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.