DEV Community

Cover image for Secure Drupal: Best Practices for Enterprise Sites
Balasaranya Varadhalingam for AddWeb Solution Pvt Ltd

Posted on • Edited on

Secure Drupal: Best Practices for Enterprise Sites

“Small habits, when repeated daily, become big results.”
- James Clear, author of Atomic Habits

Index

  1. Introduction
  2. Phase 1: The Development Period – Building a Secure Foundation
  3. Phase 2: The Review Period – Automating Security with a CI/CD Pipeline
  4. Phase 3: The Deployment Stage – Hardening The Live Environment
  5. Key Takeaways
  6. Stats
  7. Interesting Facts
  8. FAQs
  9. Conclusion

1. Introduction

Strengthening Security from Development to Deployment.
Drupal is one of the most powerful content management platforms used by enterprise organizations. It offers flexibility, scalability, and a strong foundation for building complex digital experiences. However, its open-source nature and modular structure also demand a proactive approach to security. With the increasing sophistication of cyber threats, a "set it and forget it" approach to security is no longer viable. A truly secure website isn’t the result of a last-minute checklist; it’s the outcome of a security-first mindset integrated into every stage of the development lifecycle.
Enterprise sites, in particular, attract more attention from malicious actors because of the value of their data and visibility. That’s why it’s important to build security into the project from the very beginning right from development and continue applying best practices throughout the site’s lifecycle.

In this post, I’ll walk through how we can secure our Drupal sites across all three main stages:

  • Development
  • Review
  • Deployment We’ll also see how certain Drupal modules and tools can make this process easier and more effective.

2. Phase 1: The Development Period – Building a Secure Foundation

Security starts the moment the first line of code is written. In fact, the choices we make during development shape how well the site can defend itself in the future. If we ignore security early on, we might end up patching issues later, which is harder and riskier. But when we build the site with safety in mind from the beginning, we make everything stronger and easier to manage in the long run.

One of the most important things to do in the early stages is to install and configure the Security Kit (SecKit) module. Think of SecKit as a digital shield that adds layers of protection around your site. It helps the browser understand what it should and shouldn’t accept so even if someone tries something malicious, the browser won’t allow it. Instead of writing code first and worrying about vulnerabilities later, we can use SecKit to guide our development process. With it turned on early, we’re writing code that already respects important safety rules. This approach saves us time, avoids bugs later, and makes the project cleaner overall.

Now, Let’s explore how SecKit helps during development:

Protection Against Cross-Site Scripting (XSS)

  • XSS is one of the most common attacks on websites. It happens when someone manages to sneak harmful scripts (like JavaScript) into forms, comments, or URLs. These scripts can steal user data, show fake messages, or redirect users to another site.
  • SecKit helps reduce this risk by controlling what kinds of content are allowed. For example, it can stop the browser from running scripts unless they come from safe sources. It also sets security headers that tell the browser, “Don’t allow this page to do certain things.”

  • By applying these restrictions during development, we’re building our features in a way that naturally blocks harmful behavior without breaking normal site functionality.

Cross-Site Request Forgery (CSRF) Protection

  • CSRF is another sneaky trick attackers use to fool logged-in users into doing something they didn’t mean to. Imagine you're logged into a Drupal admin panel in one tab, and you visit a shady website in another. That website might silently submit a request to change your password without you clicking anything.
  • SecKit helps by telling the browser that all actions (like submitting forms or saving content) must come from users who actually interacted with the site. It blocks suspicious requests that try to act on your behalf without your knowledge.

Defending Against Clickjacking
Clickjacking is a trick where attackers hide your site inside an invisible frame (iframe) on their page. They then place fake buttons on top, so when someone thinks they’re clicking on “Play Video,” they’re actually clicking “Delete Account” on your site.

  • To stop this, SecKit sends a rule to the browser: “Don’t allow this page to be embedded anywhere.” That simple rule prevents outsiders from hijacking your site's interface.
  • When we enable this setting during development, we don’t have to worry later about which pages are safe and which aren’t. It applies protection across the board.

Configuring SecKit from day one forces developers to work within a secure framework, making it a foundational part of the site’s architecture.

“The future depends on what you do today.” - Mahatma Gandhi

3. Phase 2: The Review Period – Automating Security with a CI/CD Pipeline

After the development work begins, we reach a critical point in the review phase. This is where code is double-checked, tested, and filtered before it reaches the live site. But instead of doing all the checking manually, we can automate most of it using a smart CI/CD (Continuous Integration and Continuous Deployment) pipeline.
Think of this phase as the site’s first security checkpoint. It catches problems while they are still easy to fix. The goal here is simple: stop bad code before it becomes part of the project.

What is a CI/CD Pipeline?

A CI/CD pipeline is a series of automated steps that run whenever someone writes new code or makes changes. It’s like a robot assistant that checks your work every time you save it. These steps can do many things from checking code quality to scanning for security risks to running all the tests that simulate user activity.
By putting security checks into this pipeline, we create a strong defense system that works in the background while we build features.

Step 1: Static Code Analysis with PHP_CodeSniffer (phpcs)
One of the first things we can add to our pipeline is PHP_CodeSniffer. This tool reads through the code and compares it with Drupal’s coding standards. It doesn’t just look for formatting it also highlights risky patterns that could later cause security issues.

For example, if a developer forgets to sanitize user input, or directly prints something without filtering it, phpcs will flag it. It acts like a teacher reviewing your homework pointing out anything that doesn’t match Drupal’s best practices.
This step helps every developer on the team write consistent and safer code, even if they are new to Drupal.

Behind the Scenes:
When a developer pushes code to Git, the CI system automatically runs phpcs. If there are violations, the pipeline stops and gives feedback. The developer can then fix the issues and resubmit. This keeps problems out of the codebase before they grow.

Step 2: Scan for Vulnerabilities in Dependencies
Modern Drupal sites use many third-party libraries : modules, composer packages, and plugins. These tools save time, but they can also bring hidden risks. If one of them has a known vulnerability, attackers can use that weak point to access or damage the site.

To catch these kinds of problems, we can use a Composer Audit. This tool compares our project’s dependencies with a database of reported vulnerabilities. If any of the packages are known to be unsafe, the pipeline will show a warning right away.
This is like checking the ingredients of a recipe to make sure none of them are expired or harmful.

Step 3: Automated Testing – Stop Bugs Before They Go Live
Even if the code is written cleanly and libraries are safe, mistakes can still happen. A small change in one area might accidentally break something else. That’s why we run automated tests in the CI/CD pipeline.
These tests include:

  • Unit tests that check small pieces of code
  • Kernel tests that look at how parts of Drupal interact
  • Browser tests that simulate real user behavior (like logging in, submitting forms, etc.)

If any of these tests fail, the code doesn’t move forward. It stays in review until the issue is fixed.
This step gives us confidence that every change we make keeps the site working as expected. It also reduces the chance of introducing bugs that might affect users or create new security problems.

By automating this process, we avoid rushing through reviews or missing something important. We also save time, because developers don’t need to manually review every detail.

4. Phase 3: The Deployment Stage – Hardening The Live Environment

Once the code has been built and reviewed, we reach the final phase: deployment. This is where the website leaves the comfort of development and enters the real world. It now faces real users, real traffic and real threats.

At this stage, our job is to lock down everything around the site. Think of it like moving into a new house: even if the house is well-built, we still need to add locks, cameras, and other safety tools to protect it.

Step 1: Keep Non-Production Environments Hidden
Before we even talk about the live site, we need to talk about non-production environments like dev, staging, UAT, and testing. These are often copies of the real site, used for trying out features or fixing bugs. But they can be a big risk if left open.

Search engines can accidentally index them. Attackers can use them to study our code. Sometimes, these sites even contain real data used for testing.
To stop this, we can use the Shield module. It adds a basic username and password prompt that appears before the Drupal site loads. This is a simple but effective wall that keeps outsiders away.

Set a simple username and password to block public access to staging or test sites.

Step 2: Apply a Strict Content Security Policy (CSP)
Even though we might have installed Security Kit (SecKit) earlier, the most powerful feature Content Security Policy (CSP) comes into full play during deployment.
CSP is a set of rules sent from the server to the browser. These rules tell the browser which sources are allowed to load content like scripts, styles, images, fonts, or iframes. If something tries to load from an unapproved location, the browser blocks it automatically.
For example, if someone tries to inject a malicious JavaScript file from an unknown website, CSP stops it from running. It’s like telling the browser, “Only accept food from this kitchen, not from the stranger at the door.”

To make CSP effective:

  • List only the domains you truly need (your CDN, analytics tool, font provider, etc.)
  • Avoid using unsafe-inline or wildcards unless absolutely required
  • Test the policy using report-only mode before enforcing it

A solid CSP setup is one of the best ways to block Cross-Site Scripting (XSS) attacks, which remain a major threat across the web.

Step 3: Secure the Server Environment
Beyond Drupal itself, the server plays a huge role in the site’s overall safety. If the server is not properly configured, even the cleanest code can be compromised.

Here are key areas to harden:

  • File and Folder Permissions

    • Files on the server should be readable by the web server, but not writable by the public. Directories that don’t need to execute scripts should block execution. Avoid overly permissive settings like 777, which allow anyone to read, write, and execute.
  • A safe setup might look like:

    • Files: 644
    • Directories: 755
    • No PHP execution allowed in /sites/default/files
  • Use HTTPS Everywhere

    • Every page should be served over HTTPS. This encrypts the connection between the server and the user’s browser, keeping data safe in transit. It also helps build trust, since modern browsers now mark HTTP-only sites as “Not Secure.”
    • Redirect all HTTP traffic to HTTPS automatically using your .htaccess file or web server configuration.
  • Keep Software Updated

    • Security patches are released regularly for PHP, database engines, and the operating system itself. Running old versions can leave open doors that attackers already know how to exploit.
  • Backup Strategy

    • Disasters happen : servers crash, databases get corrupted, or someone deletes content by mistake. That’s why backups are not optional.
    • Set up regular automated backups that include:
      • Database dumps
      • Public and private files
      • Configurations
    • Test restoring from backups occasionally so you know they actually work.

When a Drupal site is deployed, it becomes visible to the world but also vulnerable to it. What we do during this phase has long-term effects on the stability, safety, and success of the project.

5. Key Takeaways

  • Start security early with tools like SecKit
  • Automate checks using CI/CD pipelines
  • Protect dev environments using Shield
  • Use CSP and headers to reduce browser-side attacks
  • Regularly patch and back up the server

6. Stats

  1. 71% of breaches in enterprise websites are caused by vulnerabilities that had patches available. Source: Verizon 2023 Data Breach Investigations Report
  2. Drupal has over 40,000 contributed modules, and more than 1,000 have security advisories.Source: Drupal.org Security Advisories

7. Interesting Facts

  • The U.S. White House, NASA, and European Commission websites have used Drupal due to its strong security features.
  • The Security Kit (SecKit) module allows configuration of over 30 security headers directly from the admin UI.
  • Clickjacking prevention using X-Frame-Options is one of the simplest yet most overlooked browser defenses.

8. FAQs

Q1 : Can security headers conflict with frontend features?
A: Yes, misconfigured CSP or X-Frame-Options can block legitimate scripts or iframes. Use “report-only” mode to test policies safely.

Q2: What should I use to hide staging sites?
A: The Shield module is ideal for adding simple HTTP authentication to non-public environments.

Q3: How often should I update modules and server packages?
A: At least monthly or whenever a security update is released.

9. Conclusion

Security isn't a one-time task, it's something we keep working on as the site grows. At AddWeb Solution, we believe that safety should be part of every step, starting from the very first idea all the way to live deployment and beyond. By thinking about security while planning the site, checking code automatically during reviews, and setting up a strong and safe live environment, we don’t leave room for guesswork. Each phase builds a stronger layer of protection. When these layers work together, the site becomes much harder to attack and much easier to trust.
This kind of proactive approach not only protects data and users, but also helps build lasting confidence in the brand behind the website. That’s what sets enterprise Drupal projects apart and why security should always be part of the journey, not just the destination.

“It’s not the will to win that matters, everyone has that. It’s the will to prepare to win that matters..” - Paul “Bear” Bryant

About the Author:Balasaranya Varadhalingam, Software Engineer (Drupal) at AddWebSolution, specializes in building secure and scalable Drupal solutions for enterprise clients.

Top comments (0)