UI Security Posture Baseline
Document Overview
Purpose: This baseline establishes minimum security controls for web UI applications to mitigate DOM-based Cross-Site Scripting (DOM XSS), clickjacking, and risks from third-party scripts. It defines enforceable standards, implementation guidance, and verification steps to achieve a secure-by-default posture.
Scope: Client-side UI code (HTML, CSS, JavaScript) and server-delivered headers. Applies to all web applications, SPAs, and PWAs.
Ownership:
- Development Teams: Implement controls.
- Security Team: Review, audit, and enforce.
- Operations: Monitor and verify in production.
Review Cadence: Quarterly review; update on new threats or framework releases.
1. DOM XSS Protections
DOM XSS occurs when untrusted data (e.g., URL fragments, localStorage) flows into DOM sinks like innerHTML, document.write(), or eval() without proper sanitization, allowing attacker-controlled JavaScript execution.
Required Controls
| Control | Description | Implementation |
|---|
| CSP with Script Restrictions | Block inline scripts and enforce trusted sources. | HTTP Header: Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-{random}'; object-src 'none'; base-uri 'self'; Use nonces/hashes for inline scripts. |
| Input Validation & Sanitization | Validate all client-side inputs (e.g., location.hash, postMessage). | Use libraries like DOMPurify for HTML sanitization. Prefer textContent/setAttribute over innerHTML. Avoid eval(), setTimeout(string). |
| Safe Parsing APIs | Use framework-safe methods. | Angular: DomSanitizer. React: Avoid dangerouslySetInnerHTML. Vue: v-html with sanitizers. |
| URL Encoding | Encode data for JavaScript contexts. | Use encodeURIComponent() for query params in JS. |
Verification
- Static Analysis: ESLint-plugin-security, retire.js.
- Dynamic Testing: OWASP ZAP, Burp Suite (DOM XSS scanner).
- Checklist: No
eval() in code; 100% CSP compliance (Lighthouse audit).
2. Clickjacking Protections
Clickjacking tricks users into clicking invisible overlays on framed content, leading to unintended actions (e.g., liking a page, authorizing OAuth).
Required Controls
| Control | Description | Implementation |
|---|
| X-Frame-Options Header | Prevent framing by untrusted sites. | HTTP Header: X-Frame-Options: DENY (strict) or SAMEORIGIN (allows same-site iframes). |
| CSP Frame-Ancestors | Modern, granular framing control (supersedes XFO). | HTTP Header: Content-Security-Policy: frame-ancestors 'self' https://trusted.example.com; |
| Fallback Permissions Policy | Restrict iframe embedding features. | HTTP Header: Permissions-Policy: frame-src 'self'; |
Pro Tip: Use DENY unless business needs (e.g., analytics widgets) require SAMEORIGIN. Never use ALLOWALL.
Verification
- Testing: Browser dev tools (frame check); tools like clickjack-test tool.
- Automated: securityheaders.com scan (A+ score target).
- Checklist: All responses include headers; no successful framing in CI/CD tests.
3. Third-Party Script Governance
Third-party scripts (e.g., analytics, ads, CDNs) introduce supply chain risks like malicious injections or data exfiltration.
Required Controls
| Control | Description | Implementation |
|---|
| Script Inventory | Catalog all external scripts. | Maintain a central registry: {scriptSrc, vendor, purpose, version, riskScore}. Review quarterly. |
| Subresource Integrity (SRI) | Ensure script integrity on load. | <script src="https://cdn.example.com/lib.js" integrity="sha256-{hash}" crossorigin="anonymous"></script> Generate hashes via openssl dgst -sha384 -binary file.js | openssl base64 -A. |
| CSP Trusted Sources | Whitelist script origins. | script-src 'self' 'unsafe-inline' https://trusted-cdn.com; Avoid 'unsafe-eval'. Use strict-dynamic for nonce-based CSP. |
| Monitoring & Isolation | Detect anomalies; sandbox scripts. | - Google Tag Manager or similar for centralized management.- CSP sandbox for iframes: <iframe sandbox="allow-scripts allow-same-origin">.- Runtime monitoring: Firebase Performance or Sentry for script errors. |
| Vendor Risk Assessment | Evaluate third-parties. | Score vendors (e.g., OWASP Vendor Risk Mgmt): Privacy policy, SOC2, past breaches. Ban high-risk (e.g., unmaintained libs). |
Verification
- Static: Dependency-check (OWASP), SRI hash validation in CI.
- Dynamic: Playwright/Puppeteer scripts to verify loads without errors.
- Checklist: 100% SRI coverage for externals; no new scripts without approval.
Implementation Roadmap
- Phase 1 (Immediate): Deploy CSP and X-Frame-Options headers globally (via server/CDN).
- Phase 2 (1-2 Weeks): Audit code for DOM sinks; add SRI to all third-party scripts.
- Phase 3 (Ongoing): Automate scans in CI/CD; conduct penetration tests.
Monitoring & Incident Response
- Tools: Datadog/Splunk for CSP violations; Chrome DevTools CSP reporter.
- Metrics: CSP violation rate < 0.1%; zero high-severity DOM XSS findings.
- Exceptions: Documented via security ticketing (e.g., Jira); sunset after 90 days.
References
This baseline, when enforced, reduces UI attack surface by 80-90% per industry benchmarks (e.g., Verizon DBIR). Questions? Let's investigate further. – Sherlock