18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Filtering<br />

The following guidelines then should guide us as to how to properly screen<br />

incoming data:<br />

Appendix B<br />

• If we know in advance that a given piece of data is supposed to be numeric,<br />

and logically would not make sense otherwise, cast it to an integer or a float<br />

in order to filter out unsafe strings.<br />

• If we know in advance that there are a fixed number of possible values<br />

a piece of data could hold, outrightly reject any values that are not one of<br />

those on the allowed list. In most cases, <strong>Drupal</strong>'s form API does this for us.<br />

• Do not filter out HTML tags when saving textual data to the database,<br />

as whether those tags are appropriate or not depends on where they get<br />

displayed later.<br />

• Treat any textual data, even if it comes from our own database, as unsafe<br />

until filtered at display time.<br />

<strong>Drupal</strong> provides a number of tools for filtering textual data. The most important are:<br />

• filter_xss() will strip out all HTML tags except those in a specified list.<br />

The default list is reasonably safe.<br />

• filter_xss_admin() is the same as filter_xss(), but with a very<br />

permissive tag list. It is best used for text that we know will only be<br />

entered by a trusted administrator.<br />

• check_markup() will filter a piece of text according to a specified set of filter<br />

rules, which are configured through the administrative UI as text formats.<br />

• check_plain() takes a more sledgehammer approach to a piece of text.<br />

Rather than removing untrustworthy HTML tags, it escapes all HTML<br />

tags so that they will appear literally in the browser. See below for more<br />

on escaping.<br />

Escaping HTML<br />

Escaping, by contrast, does not remove content from a piece of data but encodes it<br />

in a format that another system expects to avoid confusing that system. For instance,<br />

if we want to print HTML tags to the page such that the user can see them, we need<br />

to escape the < and > characters using HTML entity codes. The two most common<br />

systems that <strong>Drupal</strong> will be sending data to are an SQL database and the web<br />

browser, and both require different approaches.<br />

[ 377 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!