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.

First, whenever we write a query against the node table, we must use a dynamic<br />

query, built using db_select(). Second, we must call the addTag() method on<br />

the query object and give it a tag of node_access. For example:<br />

$result = db_select('node')<br />

->fields('node')<br />

->condition('type', 'page')<br />

->addTag('node_access')<br />

->execute();<br />

Appendix B<br />

In the preceding code, we are selecting all the data in the node table for nodes of type<br />

"page". The extra tag, however, allows <strong>Drupal</strong> to alter the query before it is run and<br />

also to filter out nodes that the current user does not have access to view.<br />

Always use a dynamic query with a tag of node_access when<br />

querying the node table so that <strong>Drupal</strong> can apply additional<br />

security filters that are necessary.<br />

See http://drupal.org/node/310077 for more details on how query tagging and<br />

query alteration work, and how some other common tags work.<br />

Handling insecure code<br />

Sooner or later, you will stumble across code that is insecure. It could be a module<br />

that you have written, or a contributed module that you have downloaded from<br />

<strong>Drupal</strong>.org, or possibly even <strong>Drupal</strong> core itself. No code is perfectly secure and no<br />

developer is perfect, either, so it will happen. It's just a question of when and what<br />

we do about it.<br />

Regardless of whose code it is, the way to handle it is more or less the same:<br />

• Don't panic.<br />

• Investigate the module in question (whether ours or not) to determine<br />

exactly what the problem is. Sometimes it's a very simple bug that is easily<br />

fixed, and at other times it's very subtle. We need to figure out if it affects just<br />

one version of the module or multiple versions, or if it applies only to certain<br />

configurations (say, only on Apache web servers, only on Windows OS),<br />

among other things.<br />

• If we are able to do so, put together a patch that fixes the issue but do not<br />

post it to the issue queue.<br />

• Don't panic.<br />

[ 379 ]

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

Saved successfully!

Ooh no, something went wrong!