Skip to content

feat(security): add sandboxed mfe container and dynamic csp generator#627

Open
Dopezapha wants to merge 2 commits into
SoroLabs:mainfrom
Dopezapha:feat/mfe-container-and-csp-generator
Open

feat(security): add sandboxed mfe container and dynamic csp generator#627
Dopezapha wants to merge 2 commits into
SoroLabs:mainfrom
Dopezapha:feat/mfe-container-and-csp-generator

Conversation

@Dopezapha

Copy link
Copy Markdown
Contributor

Closes #600
Closes #576

PR Description

This pull request implements two security-focused frontend features for SoroTask: a sandboxed Micro-Frontend (MFE) container and a dynamic Content Security Policy (CSP) header generator. Both features have been designed to enforce strict security boundaries, handle edge cases resiliently, provide observability hooks, and satisfy 100% of their respective acceptance criteria.

Issue #600: [Frontend] Develop Secure Micro-Frontend (MFE) Container Application

Implementation & Fixes Details

  • Resilient Heartbeat & Timeout Mechanism: Since standard error events on HTML <iframe> elements do not fire reliably for cross-origin network errors, 404s, or remote application crashes, a custom handshake design was implemented. The container initializes a configurable timer (initTimeoutMs, default 5 seconds) on mount. The child micro-frontend must notify the parent container by posting a MFE_READY or HEARTBEAT message within this time frame. Once received, the timer is cleared, and the loading spinner is unmounted. If the timer runs out before receiving a valid heartbeat, the container unmounts the iframe and renders the fallback UI.
  • Explicit Error Handover: Whitelisted child micro-frontends can explicitly post an MFE_ERROR message to indicate an internal application failure. Upon receipt of this message, the container immediately unmounts the iframe and presents the fallback UI.
  • Strict Origin Whitelisting: Any incoming message event is filtered against a whitelisted array of origins. Messages from unauthorized origins are logged as warnings and rejected immediately.
  • Sandboxed Execution Boundary: The iframe sandbox attributes are restricted to allow-scripts allow-same-origin allow-forms allow-popups to prevent top-level navigation, parent hijacking, or other elevated access.
  • Referrer Policy: Set to no-referrer to prevent passing authentication tokens or sensitive path metadata in outbound request headers.

Acceptance Criteria Verification

  • Feature implemented according to requirements: Yes. Sandboxed boundary, origin validation, and the heartbeat-based error handling are fully operational.
  • Unit and integration tests passing: Yes. Jest tests cover loaders, error signaling, timeout fallback, whitelists, and event cleanup.
  • Security review completed: Yes. Applied strict sandbox limitations, referrer headers, and origin check boundaries.
  • Comprehensive documentation written: Yes. Time/space complexities and architectural choices have been documented in frontend/docs/mfe-container.md.

Issue #576: [Frontend] Implement Content Security Policy (CSP) Dynamic Generator

Implementation & Fixes Details

  • Directive Dictionary Builder: Programmatically builds policy string using a dictionary of core directives (including default-src 'self', object-src 'none', frame-ancestors 'none', and upgrade-insecure-requests).
  • Dynamic Nonce Injection: Supports generating script-src policies using cryptographically secure script nonces ('nonce-<token>') to allow trust-anchored inline scripts when using strict-dynamic.
  • Environment-Aware Exceptions: Automatically checks if process.env.NODE_ENV is set to development (or reads a passed override flag) to append 'unsafe-eval' and 'unsafe-inline', which are required for Vite/Webpack hot module reloading.
  • Observabilities & Violation Reporting: Supports appending the report-uri directive to the CSP header, allowing browsers to post JSON violation reports to the specified endpoint when a policy breach occurs.
  • Directive Merging & De-duplication: Features an extraDirectives parameter allowing developers to merge additional application-specific paths or domains while automatically deduplicating existing array values.

Acceptance Criteria Verification

  • Feature implemented according to requirements: Yes. Programmatic generator supporting nonces, environment flags, extra directive merges, and report-uri violation tracking.
  • Unit and integration tests passing: Yes. Jest tests cover basic header outputs, nonces, dev-mode alterations, custom directives, and report-uri formatting.
  • Security review completed: Yes. Defaulted to strict directives, disabled object-src/frame-ancestors, and enforced upgrade-insecure-requests.
  • Comprehensive documentation written: Yes. Documented generator structure and O(D + E) complexity in frontend/docs/csp-generator.md.

Changes Made

  • Created frontend/components/mfe-container.tsx containing the sandboxed iframe container and postMessage/heartbeat validation logic.
  • Created frontend/components/__tests__/mfe-container.test.tsx verifying sandbox attribute assignment, loading indicators, trusted message handling, untrusted message rejection, timeout fallback, and message-driven error handling.
  • Created frontend/lib/csp-generator.ts with options for script nonces, dev mode bypasses, custom directives, and report-uri violation tracking.
  • Created frontend/lib/__tests__/csp-generator.test.ts testing base generation, nonce injection, development flags, custom directive merging, and report-uri construction.
  • Created frontend/docs/mfe-container.md and frontend/docs/csp-generator.md to house the architectural and security documentation for each issue.
  • Modified frontend/jest.config.js to include the components and lib directories in Jest's matching patterns so that the new test suites run.

Testing

  • Jest tests executed successfully using:
    npx jest components/__tests__/mfe-container.test.tsx lib/__tests__/csp-generator.test.ts --coverage --collectCoverageFrom='components/mfe-container.tsx' --collectCoverageFrom='lib/csp-generator.ts' --coverageReporters=text
  • Result: Both files successfully passed verification with >97% code coverage:
    PASS  components/__tests__/mfe-container.test.tsx
    PASS  lib/__tests__/csp-generator.test.ts
    

@drips-wave

drips-wave Bot commented Jun 26, 2026

Copy link
Copy Markdown

@Dopezapha Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Frontend] Develop Secure Micro-Frontend (MFE) Container Application [Frontend] Implement Content Security Policy (CSP) Dynamic Generator

1 participant