18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Security<br />

Always assume that incoming data is insecure until and<br />

unless it's been processed to ensure that it is safe.<br />

Most security vulnerabilities come from not properly checking whether a piece of<br />

data is what we expect it to be. Remember also that there are many of types of input.<br />

• Any information coming from the user, either in the GET query or a POST<br />

request, is input and may be insecure.<br />

• Any cookies the user sends are input, and may be faked.<br />

• Any files read from disk are input, which may contain data we do not<br />

expect. Even if not malicious, unexpected data formats can pose a serious<br />

security risk.<br />

• Any data stored in a database that we didn't write to may be insecure.<br />

• Even data stored in <strong>Drupal</strong>'s own database may be insecure, unless we know<br />

for certain that we cleaned it before saving it.<br />

Data does not have to be malicious in order to be a<br />

security threat.<br />

Also remember that our website may not be the target of an attack. A very common<br />

tactic is to post comments on a website that contains JavaScript that, when viewed,<br />

will take over a user's web browser or trick them into visiting another site that will<br />

download malicious code to their computer. Although our site is not harmed, it is<br />

still used as a way to attack another user. We don't want that.<br />

Filtering versus escaping<br />

There are two ways of dealing with potentially insecure data, namely, filtering<br />

and escaping.<br />

Filtering involves stripping out portions of the data that could be trouble, or forcing<br />

the data into a simpler, safer form. Common practice in PHP is to filter all input into<br />

the system unequivocally. <strong>Drupal</strong> takes a slightly different tactic, as for certain pieces<br />

of content, such as a node body, we may not know the proper format in advance. If<br />

a user changes a text format setting, for instance, we don't want the user's previous<br />

text to be lost, just filtered differently. Instead, we filter it on output.<br />

[ 376 ]

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

Saved successfully!

Ooh no, something went wrong!