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.

<strong>Drupal</strong> Permissions and Security<br />

Using AJAX in other contexts<br />

While form handling of AJAX provides both a tidy API and a security check, we are<br />

not so lucky when using other AJAX callbacks. To quote Greg Knaddison, member<br />

of the <strong>Drupal</strong> security team and author of Cracking <strong>Drupal</strong>, the definitive work on<br />

<strong>Drupal</strong> security:<br />

[I]t is often tempting when building a rich AJAX feature to slip back into creating<br />

a CSRF vulnerability via GET requests….However, because this practice of taking<br />

action in response to GET requests is not as common or standard as the form<br />

system, there is no way to provide this protection automatically or easily.<br />

Cracking <strong>Drupal</strong>, pg 18.<br />

To understand the point, let's look at a typical AJAX menu callback use case.<br />

Suppose we want a module that allows users to add or delete items from a list via a<br />

dynamic AJAX callback. The module might set up something like the following:<br />

function example_menu() {<br />

$items = array();<br />

$items['example-ajax/%item/add'] = array(<br />

'title' => 'Example AJAX add to list',<br />

'page callback' => 'example_ajax_add',<br />

'page arguments' => array(1),<br />

'access arguments' => array('add to my list'),<br />

'type' => MENU_CALLBACK,<br />

);<br />

return $items;<br />

}<br />

function example_ajax_add($item) {<br />

// Do something.<br />

}<br />

Looking at the preceding code, several issues should be immediately apparent:<br />

• The default access callback user_access() is probably insufficient, since we<br />

are managing a per-user list<br />

• The permission add to my list provides no means to check if the user is the<br />

owner of the list being edited<br />

• Simply trying to hide the menu item from the site navigation (through the<br />

use of the MENU_CALLBACK property) will not prevent other users (or even<br />

search engine crawlers) from eventually finding the page<br />

[ 236 ]

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

Saved successfully!

Ooh no, something went wrong!