Module Development

Concept: Writing Secure Code for Drupal 8, 9, 10, and 11

In the world of Drupal development, ensuring the security of custom code is paramount. Drupal's security standards and features offer a robust foundation, but developers must also adhere to security best practices to safeguard against vulnerabilities. From sanitizing output to prevent cross-site scripting (XSS) attacks to using Drupal's database abstraction layer to avert SQL injection, Drupal empowers developers to write secure code. But it's still up to you, the developer, to do so.

In this tutorial, we'll:

  • Provide an overview of these practices.
  • Introduces additional resources to increase your security expertise.

By the end of this tutorial, you should be able to articulate some of the common security risks and mitigation strategies for Drupal modules.

Goal

Learn best practices for developing secure custom code within Drupal.

Prerequisites

Follow Drupal's coding standards

In addition to rules about how to format your code, Drupal's coding standards also include recommendations for using Drupal's internal APIs to help write secure code. We'll go into this more in Lint Your Code with PHP_CodeSniffer.

Sanitize output to prevent XSS

Drupal’s Twig templating engine automatically filters output to prevent XSS attacks. Developers should avoid using the | raw filter in Twig templates unless absolutely necessary and ensure proper use of the t() function and Html::escape() for manual output sanitization.

We discussed the t() function in Output Translatable Strings.

Prevent SQL injection

Drupal’s Entity API and database abstraction layer offer built-in protections against SQL injection. Developers should use these tools instead of crafting raw SQL queries.

See Concept: Entity API and Data Storage.

CSRF protection

Drupal includes built-in CSRF protection for routes that perform actions, significantly reducing the risk of cross-site request forgeries. Developers should ensure that any custom routes performing actions leverage this protection.

Use Drupal's API for data validation

Use Drupal's Form API and validation mechanisms to ensure user input is properly validated. This will reduce the risk of injection attacks and data tampering.

See Add Custom Validation to User Entities and Validate User Input for the Settings Form.

Regular module and core updates

Keeping Drupal core and contributed modules updated is crucial for security. Updates often include patches for security vulnerabilities.

See 13.3. Concept: Security and Regular Updates.

Learning and implementing security practices

Recap

Writing secure code in Drupal is crucial for maintaining the integrity and trustworthiness of Drupal sites. By following Drupal's security best practices and using its robust security features, developers can protect sites from common vulnerabilities.

Further your understanding

  • Explore Drupal’s API documentation for functions that help sanitize output and validate input.
  • Review the security advisories posted on Drupal.org to understand common vulnerabilities and learn how they were addressed.

Additional resources

Drupal Module Developer Guide