Mitigation Strategy: Strictly Avoid Manual DOM Manipulation
-
Description:
- Establish a Coding Standard: Create a clear, documented coding standard that explicitly prohibits direct DOM manipulation using methods like
innerHTML
,outerHTML
,insertAdjacentHTML
, etc., within Preact components. This is crucial because Preact relies on its virtual DOM for efficient and secure rendering. - Code Review Enforcement: Implement mandatory code reviews. Reviewers should specifically check for any violations of the "no direct DOM manipulation" rule within component logic.
- Education and Training: Train developers on the dangers of bypassing Preact's rendering and the benefits of using JSX and the component lifecycle.
- Use of Linting Tools: Integrate ESLint with rules like
react/no-danger
(adaptable for Preact) andno-unsanitized/method
to automatically detect and flag potential violations during Preact component development. - Refactoring Existing Code: If direct DOM manipulation is found within existing Preact components, prioritize refactoring it to use Preact's JSX and component structure.
- Exception Handling (Rare and Justified Cases): If direct DOM manipulation is absolutely unavoidable within a Preact context (e.g., integrating with a legacy library that requires it and cannot be wrapped), create a dedicated, isolated Preact component for this interaction. Within this specific Preact component, implement rigorous input sanitization using a library like
DOMPurify
before any DOM interaction. Document the reason for this exception clearly and ensure it's isolated from the rest of the Preact application.
- Establish a Coding Standard: Create a clear, documented coding standard that explicitly prohibits direct DOM manipulation using methods like
-
Threats Mitigated:
- Component Injection (XSS - Preact Specific): (Severity: High) - Prevents attackers from injecting malicious code by bypassing Preact's virtual DOM and rendering process. This is a Preact-specific threat because it exploits the framework's rendering mechanism.
- Unexpected Application Behavior (Preact Specific): (Severity: Medium) - Reduces conflicts between Preact's virtual DOM and manual DOM changes, ensuring consistent rendering.
-
Impact:
- Component Injection (XSS): Risk reduction: Very High. This is the primary defense against this Preact-specific XSS vector.
- Unexpected Application Behavior: Risk reduction: High. Improves Preact component stability.
-
Currently Implemented:
- Example: "Coding standards (section 3.2) prohibit direct DOM manipulation within Preact components. ESLint rule
react/no-danger
is enabled. Code reviews are mandatory."
- Example: "Coding standards (section 3.2) prohibit direct DOM manipulation within Preact components. ESLint rule
-
Missing Implementation:
- Example: "Refactoring of
LegacyPreactWidget.js
is required; it usesinnerHTML
within itsrender
method."
- Example: "Refactoring of
Mitigation Strategy: Prop Type Validation and Enforcement (Preact's propTypes
)
-
Description:
- Define Prop Types: For every Preact component, define
propTypes
to specify the expected data type for each prop. Use specific types and custom validators where possible. - Custom Validators: For props requiring specific formats, create custom validator functions within Preact's
propTypes
. - Runtime Enforcement (Development Mode): Ensure that
propTypes
validation is enabled in development mode. Preact will log warnings if prop types are violated. - Regular Audits: Periodically review the
propTypes
definitions to ensure they are up-to-date.
- Define Prop Types: For every Preact component, define
-
Threats Mitigated:
- Component Injection (Indirectly - Preact Specific): (Severity: Medium) - Reduces the chance of unexpected data types leading to vulnerabilities within Preact's rendering.
- Unexpected Application Behavior (Preact Specific): (Severity: Medium) - Prevents errors caused by incorrect prop values passed to Preact components.
-
Impact:
- Component Injection (Indirectly): Risk reduction: Medium. Strengthens Preact component robustness.
- Unexpected Application Behavior: Risk reduction: High. Improves Preact component reliability.
-
Currently Implemented:
- Example: "All Preact components have
propTypes
defined. Custom validators are used for email and URL props inUserForm.js
."
- Example: "All Preact components have
-
Missing Implementation:
- Example: "Legacy Preact components need
propTypes
added."
- Example: "Legacy Preact components need
Mitigation Strategy: Minimize and Sanitize dangerouslySetInnerHTML
(Preact Specific)
-
Description:
- Avoidance as Primary Strategy: Avoid using Preact's
dangerouslySetInnerHTML
whenever possible. Explore alternatives like parsing HTML into a Preact-compatible structure or using Preact components for rendering. - Strict Sanitization (If Unavoidable within Preact): If
dangerouslySetInnerHTML
is absolutely necessary within a Preact component, useDOMPurify
.- Configuration: Configure
DOMPurify
strictly. - Regular Updates: Keep
DOMPurify
updated.
- Configuration: Configure
- Input Validation (Before Preact Rendering): Validate data before it's considered for use with Preact's
dangerouslySetInnerHTML
. - Code Review and Documentation: Any use of Preact's
dangerouslySetInnerHTML
should be documented and scrutinized during code reviews.
- Avoidance as Primary Strategy: Avoid using Preact's
-
Threats Mitigated:
- XSS via
dangerouslySetInnerHTML
(Preact Specific): (Severity: High) - Directly mitigates XSS through malicious HTML within Preact's rendering context. - Bypass of Sanitization: (Severity: High)
- XSS via
-
Impact:
- XSS via
dangerouslySetInnerHTML
: Risk reduction: Very High (with sanitization). Avoidance is best. - Bypass of Sanitization: Risk reduction: Medium.
- XSS via
-
Currently Implemented:
- Example: "Preact's
dangerouslySetInnerHTML
is not currently used. A policy prohibits its use."
- Example: "Preact's
-
Missing Implementation:
- Example: "If a future requirement necessitates its use, a detailed plan, including sanitization, must be created."
Mitigation Strategy: Secure Context API Usage (Preact Specific)
-
Description:
- Context Scope Limitation: Identify the Preact components that need access to data. Create a context provider only at the highest level in the Preact component tree where all those components are descendants.
- Multiple Contexts: Create separate Preact contexts for different data categories.
- Data Minimization: Store only the minimum necessary data within each Preact context.
- Read-Only Context (If Possible): If a Preact context only needs to provide data, consider making it read-only.
- Code Reviews: Examine how Preact's Context API is being used during code reviews.
- Documentation: Clearly document the purpose and scope of each Preact context.
-
Threats Mitigated:
- Unintentional Data Exposure (Preact Specific): (Severity: Medium to High) - Prevents sensitive data from being accidentally accessed by Preact components that shouldn't have access.
- Debugging-Related Leaks: (Severity: Low to Medium)
- Component Injection (Indirectly - Preact Specific): (Severity: Low) If a malicious component is injected, limiting Preact context access reduces data exposure.
-
Impact:
- Unintentional Data Exposure: Risk reduction: High.
- Debugging-Related Leaks: Risk reduction: Medium.
- Component Injection (Indirectly): Risk Reduction: Low.
-
Currently Implemented:
- Example: "Separate Preact contexts are used for authentication (
AuthContext
) and UI theme (ThemeContext
).AuthContext
only provides a user ID."
- Example: "Separate Preact contexts are used for authentication (
-
Missing Implementation:
- Example: "Code review needed to ensure all Preact context providers are at the appropriate level. Documentation for
AnalyticsContext
needs clarification."
- Example: "Code review needed to ensure all Preact context providers are at the appropriate level. Documentation for