Mitigation Strategy: Leverage Drupal's Form API and Render API Rigorously
-
Mitigation Strategy: Utilize Drupal's Form API and Render API for all form building and content output.
-
Description:
- Form Building (Form API): Use the Form API's structured arrays (
#type
,#title
,#required
, etc.) for all form creation. Avoid manual HTML construction. - Form Processing (Form API): Employ Form API validation (
#element_validate
) and submit handlers. Sanitize input within these handlers using Drupal core functions like\Drupal\Component\Utility\Html::escape()
and\Drupal\Component\Utility\Xss::filter()
. - Output Rendering (Render API): Use render arrays for all content output. Define output structure with keys like
#markup
,#theme
,#allowed_tags
. Avoid direct HTML printing. - Twig Templates (Core Integration): Within Twig templates, use Drupal core's functions and filters (e.g.,
{{ content.field_name }}
,{{ url('route_name') }}
). Minimize use of the|raw
filter, and only after verifying input safety.
- Form Building (Form API): Use the Form API's structured arrays (
-
Threats Mitigated:
- Cross-Site Scripting (XSS) - Severity: High: Prevents injection of malicious JavaScript.
- HTML Injection - Severity: High: Prevents injection of arbitrary HTML.
- Other Injection Attacks - Severity: Medium: Reduces risk by enforcing proper escaping.
-
Impact:
- XSS: Risk significantly reduced (near elimination with correct implementation).
- HTML Injection: Risk significantly reduced (near elimination with correct implementation).
- Other Injection Attacks: Risk moderately reduced.
-
Currently Implemented:
- Core
user
module forms (login, registration) use the Form API. - Core
node
module content display uses the Render API. - Basic Twig templates use core functions.
- Core
-
Missing Implementation: (Examples - would be specific to your project)
- Any custom module or theme that manually constructs HTML forms or outputs content without using the Form API or Render API.
Mitigation Strategy: Utilize Drupal's Text Formats and Filters (Core System)
-
Mitigation Strategy: Configure and enforce appropriate text formats and filters for user-generated content using Drupal's core system.
-
Description:
- Access Core Configuration: Use the Drupal admin interface (
Configuration > Content authoring > Text formats and editors
). - Restrict "Full HTML": Assign "Full HTML" only to trusted administrator roles.
- Configure "Restricted/Basic HTML": Customize these core formats to allow only a safe subset of HTML tags and attributes. Disallow dangerous tags (
<script>
,<object>
, etc.) and attributes (onload
,onerror
). - Assign Formats to Fields (Core Functionality): When creating content types and fields, assign the appropriate core text format to each field.
- Regular Review (Within Core): Periodically review and update the core text format configurations.
- Access Core Configuration: Use the Drupal admin interface (
-
Threats Mitigated:
- Cross-Site Scripting (XSS) - Severity: High: Limits malicious JavaScript injection.
- HTML Injection - Severity: High: Restricts allowed HTML.
- Malicious File Uploads (Indirectly) - Severity: Medium: Prevents embedding of malicious files via restricted tags.
-
Impact:
- XSS: Risk significantly reduced.
- HTML Injection: Risk significantly reduced.
- Malicious File Uploads: Risk moderately reduced.
-
Currently Implemented:
- "Restricted HTML" is the default for core comment fields.
- "Basic HTML" is used for core body fields in "Article".
- "Full HTML" is restricted to the "Administrator" role.
-
Missing Implementation: (Examples - would be specific to your project)
- Any situation where a custom module or content type bypasses the core text format system.
Mitigation Strategy: Employ Drupal's \Drupal\Component\Utility\Html::escape()
and Related Functions (Core Utility)
-
Mitigation Strategy: Use Drupal core's helper functions for escaping when absolutely necessary to handle raw HTML outside of the Render API.
-
Description:
- Identify Risky Code: Find code that directly manipulates HTML strings or outputs content outside the Render/Form APIs.
- Use
Html::escape()
(Core): For general HTML escaping, use\Drupal\Component\Utility\Html::escape()
. - Use
Xss::filter()
(Core): For more complex filtering with a limited set of allowed tags, use\Drupal\Component\Utility\Xss::filter()
. - Use
UrlHelper::filterBadProtocol()
(Core): For URLs, use\Drupal\Component\Utility\UrlHelper::filterBadProtocol()
. - Context-Specific Escaping (Core Awareness): Be mindful of the output context (HTML, attributes, JavaScript, CSS) and use the appropriate core escaping function.
-
Threats Mitigated:
- Cross-Site Scripting (XSS) - Severity: High: Escapes special characters.
- HTML Injection - Severity: High: Sanitizes user input.
- URL Manipulation - Severity: Medium: Filters bad URL protocols.
-
Impact:
- XSS: Risk significantly reduced (when used correctly).
- HTML Injection: Risk significantly reduced (when used correctly).
- URL Manipulation: Risk moderately reduced.
-
Currently Implemented:
- Used in some core modules where direct HTML manipulation is unavoidable.
-
Missing Implementation: (Examples - would be specific to your project)
- Any custom code that directly outputs HTML without using these core functions when the Render API cannot be used.
Mitigation Strategy: Strict Adherence to Drupal's Permission System (Core)
-
Mitigation Strategy: Define granular permissions and regularly audit user roles and permissions using Drupal's core permission system.
-
Description:
- Define Custom Permissions (Core API): For custom modules, define specific permissions in the module's
.permissions.yml
file. Use thepermission_callbacks
key for dynamic permissions. - Assign Permissions to Roles (Core UI): Use the Drupal core UI to create/modify user roles and assign the appropriate core-defined permissions.
- Regular Audit (Core UI): Periodically review user roles and permissions within the core UI.
- Test Permissions (Core Functionality): Test permission configurations using Drupal's user system.
- Define Custom Permissions (Core API): For custom modules, define specific permissions in the module's
-
Threats Mitigated:
- Unauthorized Access - Severity: High: Prevents unauthorized access.
- Privilege Escalation - Severity: High: Prevents gaining higher privileges.
- Information Disclosure - Severity: Medium to High: Prevents unauthorized viewing of information.
-
Impact:
- Unauthorized Access: Risk significantly reduced.
- Privilege Escalation: Risk significantly reduced.
- Information Disclosure: Risk significantly reduced.
-
Currently Implemented:
- Basic permissions are defined for core modules.
- Core user roles ("Administrator," "Authenticated User") are configured.
-
Missing Implementation: (Examples - would be specific to your project)
- Custom modules that don't define permissions in
.permissions.yml
or rely on overly broad core permissions.
- Custom modules that don't define permissions in
Mitigation Strategy: Use of Entity Access API (Core)
-
Mitigation Strategy: Always use the core Entity Access API (
$entity->access()
) to check user permissions before entity operations. -
Description:
- Identify Entity Operations: Locate code interacting with core content entities (nodes, users, taxonomy terms, files).
- Use
$entity->access()
(Core API): Before any operation (view, create, update, delete), use$entity->access('operation_name')
. - Handle Access Denied (Core Logic): If
$entity->access()
returnsFALSE
, handle the denial appropriately (error message, redirect). - Consider Access Control Handlers (Core Extension): For complex logic, define custom core access control handlers for your entities.
- Do Not Bypass (Core Principle): Never bypass the Entity Access API.
-
Threats Mitigated:
- Unauthorized Access - Severity: High: Prevents unauthorized entity access.
- Privilege Escalation - Severity: High: Prevents unauthorized operations.
- Information Disclosure - Severity: Medium to High: Prevents unauthorized viewing of entity data.
-
Impact:
- Unauthorized Access: Risk significantly reduced.
- Privilege Escalation: Risk significantly reduced.
- Information Disclosure: Risk significantly reduced.
-
Currently Implemented:
- Used consistently in core modules (e.g.,
node
,user
).
- Used consistently in core modules (e.g.,
-
Missing Implementation: (Examples - would be specific to your project)
- Any custom code that interacts with core entities without using
$entity->access()
.
- Any custom code that interacts with core entities without using
Mitigation Strategy: Route Access Checks with Custom Requirements (Core Routing)
-
Mitigation Strategy: Use appropriate route access checks and define custom access checkers (within the core system) for complex logic.
-
Description:
- Review Route Definitions: Examine
.routing.yml
files. - Use Built-in Checks (Core): For simple access, use core checks like
_permission
,_role
,_user_is_logged_in
. - Define Custom Access Checkers (Core Services): For complex logic, create custom access checkers as services implementing
\Drupal\Core\Routing\Access\AccessInterface
. - Implement
access()
Method (Core Interface): Implement theaccess()
method to return an\Drupal\Core\Access\AccessResult
. - Reference in Route Definition (Core YAML): Use the
_custom_access
key in.routing.yml
to reference your custom checker. - Test (Core Functionality): Thoroughly test route access.
- Review Route Definitions: Examine
-
Threats Mitigated:
- Unauthorized Access - Severity: High: Prevents unauthorized route access.
- Privilege Escalation - Severity: High: Prevents accessing routes requiring higher privileges.
-
Impact:
- Unauthorized Access: Risk significantly reduced.
- Privilege Escalation: Risk significantly reduced.
-
Currently Implemented:
- Basic checks (
_permission
,_role
) are used in core.
- Basic checks (
-
Missing Implementation: (Examples - would be specific to your project)
- Custom modules that don't use appropriate core route access checks or don't define custom checkers when needed.
Mitigation Strategy: Validate File Uploads (Using Core File API)
-
Mitigation Strategy: Rigorously validate all file uploads using Drupal's core file validation system.
-
Description:
- Use Drupal's File Validation (Core API): When using the Form API, use the
#upload_validators
property. - Check File Extension (Core Validation): Validate against a whitelist of allowed extensions.
- Check MIME Type (Core Validation): Use Drupal's
file_validate_mime_type()
function. - Check File Size (Core Validation): Use Drupal's
file_validate_size()
function. - Rename Files (Core Functions): Use Drupal's file system functions to generate unique file names.
- Restrict Access (Core File API): Use Drupal's file access control.
- Use Drupal's managed file system (Core API): Use file field and related APIs.
- Use Drupal's File Validation (Core API): When using the Form API, use the
-
Threats Mitigated:
- Malicious File Upload - Severity: High: Prevents malicious file execution.
- Cross-Site Scripting (XSS) - Severity: High: Prevents XSS via uploaded files.
- Denial-of-Service (DoS) - Severity: Medium: Prevents excessively large file uploads.
-
Impact:
- Malicious File Upload: Risk significantly reduced.
- XSS: Risk significantly reduced.
- DoS: Risk moderately reduced.
-
Currently Implemented:
- Basic extension and size validation in core.
-
Missing Implementation: (Examples - would be specific to your project)
- Custom modules that handle file uploads without using the core
#upload_validators
or related core functions.
- Custom modules that handle file uploads without using the core