Basiscursus door Michaël Zenner Drupal Minimum elementen voor een module Een eerste module Een eerste theme Theming binnen modules Hooks Menusystem Users, nodes en comments Form API 1
2
Core Modules HOOKS Themes Nodes Blocks De core (kern) is de verzameling van modules, templates en databankschema's die standaard deel uitmaakt van Drupal. Je vindt de coremodules via Beheren Site constructie > Modules (http://www.example.com/admin/build/modu les). 3
Een module is een toepassing binnen een Drupalwebsite die bepaalde functionaliteiten biedt. Vb: Zoeken, blogs, forums, gebruikersbeheer, statistieken, meertaligheid, Google Analytics: het zijn allemaal modules die je aan of uit kunt zetten in je website. Een module bestaat uit één of meerdere PHP bestanden soms aangevuld met CSS stijlbladen. Hooks mag je zien als een manier om in te haken op een Drupal actie. Vb: Een user meld zich aan op de site; dan start drupal de hook_user Om op dit moment iets uit te voeren zal je in je module de functie: examplemodule_user() user() gebruiken Je zal dus de placeholder hook vervangen door de modulenaam: examplemodule_user 4
Een template is een verzameling PHP, CSS bestanden en afbeeldingen die samen het ontwerp van je website bepalen. De Nederlandse vertaling van het woord template is enigzins verwarrend omdat een template in de Engelstalige documentatie theme heet vandaar ik doorheen de opleiding ook van theme zal spreken. Een node (Engels voor knoop) is een inhoudselement van je website. bit Een pagina, een nieuwsartikel, een blogpost, een forumbericht of een recept: in een Drupalwebsite zijn het allemaal nodes. Een node bestaat minimaal uit een titel en een stuk tekst (de body) en is identificeerbaar door een uniek nummer. > > NID (node id) Elke node in het systeem kun je bekijken aan de hand van zijn id (via de url http://www.example.com/node/1234). 5
Een blok (in het Engels block) ) is een navigatie of inhoudselement dat in een regio van de template getoond kan worden. Je kiest zelf of en in welke regio een blok zichtbaar is. Alle menu's van je website zijn blokken. De meeste modules bevatten ook blokken, we zullen in een eerste modulevoorbeeld een block maken. Bestanden 6
Includes Misc Modules Profiles Scripts Sites Themes Index.php > The PHP page that serves all page requests on a Drupal installation. In de folders /sites/all; / ;/sites/default;/ /sites/sitenaam plaats je contributed modules en themes. Folders zelf te maken: contrib,contrib_patched, custom Settings.php (copy van default.settings.php) Files 7
Modulenaam.info ; $Id$ name = "Page example" description = "Een voorbeeld van menu hook" core = 6.x package = Opleiding Modulenaam.module Bevat de phpcode van die module 8
Versions 4.6 6 hook_block($op = 'list', $delta = 0, $edit = array()) Hiermee declareer je blocks binnen je module http://api.drupal.org/api/function/hook_block /6 9
hook_menu() _ () Definitie van een menuitem en de pagina callbacks. 10
Themenaam.info Page.tpl.php Node.tpl.php Block.tpl.php Styles en javascript Custom tpl s tplsvb node onsnieuws.tpl.phponsnieuws php 11
hook_theme($existing, $type, $theme, _ ( g, yp,, $path) Registreer een module s theme implementatie 12
Swentel & mzenner Original presentation: Neil Drumm, Matt Cheney, Ezra Gildesgame, Greg Knaddison write secure code eat your vegetables floss daily 13
be afraid 14
drupal core is really good with security. drupal contrib is hit or miss. not fully policed. custom code is often the worst. an important difference: http versus https http data can be read by anyone (this includes cookie data) to be secure: 1. SSL Certificate http://drupal.org/project/securepages http://drupal.org/project/securepages_prevent_hijack 15
Take actions on your behalf without you knowing. Probe entire network. Pwn your site Pwn other sites used by visitors to your site In short: pwn the world. (scared enough yet?) do not: drupal _ set_ message("surprise ". $tainted); do: drupal_set_message(t("surprise @s", array('@s' => $tainted))); @ - filtered % - filtered with emphasis! - no filter, use for image tags or other pre-filtered data 16
do not: print '<a href="'. $tainted. '">'. $more_ unsafe. '</a>'; do: print l($more_unsafe, $tainted); It's the lowercase letter L. Filters text Filters URLs for "safe" protocols drupal_set_title($tainted, CHECK_PLAIN); o in D7.x only Form select options Not automatically filtered drupal_set_message Form checkbox & radio options watchdog (depends on use) and more (do your research) 17
do: check_plain Taxonomy terms, content types For content that should not contain HTML Turns & into & do: check_markup($data, $filter) Node/comment bodies, profile text fields, etc. i.e. contain markup But...admins can mess up the filters :( do: filter_xss_admin($data) Site mission, messages from admin settings Contain markup, providing an input selector is overkill Lets through lots of stuff... Try to insert some javascript alerts in your forms.. 18
node_access() http://api.drupal.org/api/function/node_access/6 user_access() http://api.drupal.org/api/function/user_access/6 19
Understand hook_menu access control. Like we seen in the example -> page_example_perm 'access arguments' => array('access foo'), db_rewrite_sql() db_rewrite_sql() provides a method for modules to extend your SQL queries. For example, a module which controls access to nodes will need to limit the results of your queries, removing any nodes for which a visitor does not have the required set of access permissions. If you do not make use of db_rewrite_sql(), access control modules won't be able to modify or extend your SQL queries, and you may inadvertently expose content that is meant to be restricted. It's good practice to always make use of db_rewrite_sql(). 20
Be sure to test. function my_form_submit($form, &$form_state) { if (!user_ access('administer foo')) { // Deny them access. drupal_access_denied(); } $stuff = $form['la']['dee']['dah']; db_query("delete from {bar} WHERE baz = %s", $stuff); } 21
function my_form_submit($form, &$form_state) { if (!user_ access('administer foo')) { // Deny them access. drupal_access_denied(); } $stuff = $form['la']['dee']['dah']; db_query("delete from {bar} WHERE baz = %s", $stuff); } User Impersonation 22
global $user; $user->uid =... nor global $user; $user = user_load(1); global $user; $temp_user = $user; session_save_session(false); $user = user_load(1); // Action here. $user = $temp_user; session_save_session(true); 23
$account = user_load(1); What I prefer the easy way Do Email security@drupal.org Be responsive Do not Tell others about the issue Commit security fixes Try to make a release Post an issue on Drupal.org 24
Basic premise: Everything may enter the database, it is YOU who decides what goes to the screen. SQL attacks: db_query: variable placeholders Node access: db_rewrite_sql Forms: no direct post confirmation forms instead of direct links (csrf) -> Cross site request forgery, also known as a one click attack or session riding and abbreviated as CSRF Returning output: check_ markup vs check_plain vs filter_xss vs filter_xss_admin Beware of $_POST : not sanitized from FAPI (eg: selections, hidden, values) Validation always done server side, do not rely on javascript alone 25
CCK: try to use $content from cck Implement cck formatters Template: cck direct use: $value >field['safe'] instead of $value >field['value'] Template: cck: content_format(); Other: check_markup vs check_plain vs filter_xss vs filter_xss_admin Setup sane defaults Do not set FULL HTML Setup sane defaults: Do not set FULL HTML as default (unless you want sweet spanking) User better formats module to set defaults per role 26
Tools: burp (spider between request & browser) XSS attacks CSRF attacks Documentation: Writing secure code http://drupal.org/writing-secure-code p g Hardening APIs UI improvements Education 27
http://drupal.org/security/secure-configuration http://drupal.org/writing-secure-code p g http://drupal.org/security-team http://groups.drupal.org/security-scanner-component-andbest-practices http://www.owasp.org http://crackingdrupal.com 28