This document is designed as a security spec that supports:
This document is designed as a security spec that supports:
It is intentionally written as a set of normative requirements (“MUST/SHOULD/MAY”) plus audit rules (what bad patterns look like, how to detect them, and how to fix/mitigate them).
Notes:
unsafe-inline/unsafe-eval without justification, removing origin checks for postMessage, switching to innerHTML for convenience, accepting arbitrary redirects/URLs, or turning off sanitization).<meta http-equiv=...> only simulates a subset of headers; don’t assume other security headers exist just because a meta tag exists.) ([MDN Web Docs][1])When asked to write new frontend JS/TS code or modify existing code:
innerHTML, navigation to javascript: URLs, dynamic code execution via eval/Function, unsafe postMessage, unsafe third-party script loading, etc.). ([OWASP Cheat Sheet Series][2])While working anywhere in a frontend repo (even if the user did not ask for a security scan):
When the user asks to “scan”, “audit”, or “hunt for vulns”:
Recommended audit order:
index.html, server-rendered templates), script/style includes, and any CSP delivery (header vs meta). ([W3C][3])innerHTML, document.write, insertAdjacentHTML, event-handler attributes) and their data sources (URL params/hash, storage, postMessage, API responses). ([OWASP Cheat Sheet Series][2])window.location*, link targets, URL allowlists) including javascript: URL hazards. ([MDN Web Docs][4])postMessage, iframe embed patterns, sandboxing). ([MDN Web Docs][5])window/document named properties. ([OWASP Cheat Sheet Series][8])Examples include:
location.href, location.search, location.hash, document.baseURI, new URLSearchParams(location.search), routing fragments. ([OWASP Cheat Sheet Series][2])postMessage event data (event.data) and metadata (event.origin) from other windows/frames. ([MDN Web Docs][5])localStorage, sessionStorage, IndexedDB (contents can be attacker-influenced via XSS or local machine access; never treat as “trusted”). ([OWASP Cheat Sheet Series][6])A sink is any API/operation that can execute script or interpret attacker-controlled strings as HTML/JS/URL in a security-sensitive way. High-signal sinks include:
innerHTML, outerHTML, insertAdjacentHTML, document.write, document.writeln. ([OWASP Cheat Sheet Series][2])eval, new Function, setTimeout("..."), setInterval("..."). ([MDN Web Docs][10])javascript:) via setters like Location.href/window.location (and via link href if attacker-controlled). ([MDN Web Docs][4])setAttribute("onclick", "..."). ([OWASP Cheat Sheet Series][2])For each issue found, output:
This is the smallest baseline that prevents common frontend JS/TS security misconfigurations. Some items are “in repo” (HTML/JS) and some may live at the server/edge.
<meta http-equiv="Content-Security-Policy" ...> tag when you cannot set headers (e.g., purely static hosting constraints). ([MDN Web Docs][1])<meta http-equiv>, MUST understand the limitations:report-uri, frame-ancestors, and sandbox. ([W3C][3])Practical baseline goals:
unsafe-inline and unsafe-eval (they significantly weaken CSP’s value against XSS). ([MDN Web Docs][10])postMessage communications to explicit origins, and validate both origin and message shape. ([MDN Web Docs][5])Each rule contains: required practice, insecure patterns, detection hints, and remediation.
innerHTML and friends)Severity: Critical if you can prove attacker-controlled input can reach these APIs; otherwise Medium
Required:
innerHTML, outerHTML, and insertAdjacentHTML as dangerous sinks when their input can contain untrusted data. ([OWASP Cheat Sheet Series][2])textContent for text. ([OWASP Cheat Sheet Series][2])document.createElement, appendChild, setAttribute for non-event-handler attributes. ([OWASP Cheat Sheet Series][2])Insecure patterns:
el.innerHTML = userInputel.insertAdjacentHTML('beforeend', userInput)el.outerHTML = userInputDetection hints:
.innerHTML, .outerHTML, insertAdjacentHTML(.Fix:
textContent for plain text. ([OWASP Cheat Sheet Series][2])TrustedHTML reaches sinks where supported. ([MDN Web Docs][11])Mitigation:
require-trusted-types-for 'script'). ([MDN Web Docs][10])False positive notes:
document.write / document.writeln (XSS + document clobbering hazards)Severity: Critical if you can prove attacker-controlled input can reach these APIs; otherwise Medium
Required:
document.write() and document.writeln() in production code (they are XSS vectors and can be abused with crafted HTML even if some browsers block injected <script> in certain situations). ([MDN Web Docs][13])TrustedHTML) where supported. ([MDN Web Docs][14])Insecure patterns:
document.write(userInput)document.writeln(getParam('q'))Detection hints:
document.write(, document.writeln(. ([OWASP Cheat Sheet Series][2])Fix:
createElement, appendChild) or safe text insertion (textContent). ([OWASP Cheat Sheet Series][2])Mitigation:
eval, new Function, string timeouts)Severity: Critical if you can prove attacker-controlled input can reach these APIs; otherwise Medium
Required:
eval()new Function(...)setTimeout("...") / setInterval("...") with string arguments ([MDN Web Docs][10])unsafe-eval unless there is a documented, reviewed justification and compensating controls. ([MDN Web Docs][10])Insecure patterns:
eval(userInput)new Function("return " + userInput)()setTimeout(userInput, 0) where userInput is a stringDetection hints:
eval(, new Function, setTimeout(", setInterval(".Fix:
JSON.parse) instead of eval for JSON. ([OWASP Cheat Sheet Series][2])Mitigation:
eval()-like APIs by default, and avoid unsafe-eval. ([MDN Web Docs][10])setAttribute("onclick", "..."))Severity: High
Required:
setAttribute("on…", string) or similar patterns with untrusted data; this coerces strings into executable code in the event-handler context. ([OWASP Cheat Sheet Series][2])addEventListener with function references.Insecure patterns:
el.setAttribute("onclick", userInput)el.onclick = userControlledString (string assignment)Detection hints:
.setAttribute("on, .onclick =, .onmouseover =, etc.Fix:
addEventListener("click", () => { ... }).window.location / location.replace)Severity: Low (High if you can prove an attacker can fully control the URL)
IMPORTANT: This can cause a lot of false positives. Please perform extra analysis to determine if the url is fully attacker controlled. If not fully attacker controlled, then this is informational at best.
NOTE: It may be important functionality to be able to redirect to any given url. If that is the goal of the feature, then at a minimum, ensure it checks the schema even if the origin is allowed to be anything.
Required:
window.location = ...location.href = ...location.assign(...)location.replace(...) ([MDN Web Docs][4])javascript: URLs (and generally other script-bearing/active schemes), especially when input is derived from URL params, storage, or messages. ([MDN Web Docs][4]). Only allow http: and https:.https: and optionally http: for localhost dev). ([OWASP Cheat Sheet Series][8])Insecure patterns:
location.replace(getParam("next"))window.location = userSuppliedUrllocation.assign(window.redirectTo || "/") where redirectTo can be clobbered or attacker-set ([OWASP Cheat Sheet Series][8])Detection hints:
window.location, location.href, location.assign, location.replace.next, returnTo, redirect, url, continue.javascript: literal usage. ([MDN Web Docs][4])Fix:
new URL(value, location.origin) and then enforce:url.protocol in { "https:" } (and only include http: in explicit dev-only code paths),url.origin equals location.origin for internal redirects, or in a strict allowlist for external redirects,Mitigation:
False positive notes:
IMPORTANT: This can cause a lot of false positives. Please perform extra analysis to determine if the url is fully attacker controlled. If not fully attacker controlled, then this is informational at best.
href, src, etc.)Severity: Low (High if you can prove an attacker can fully control the URL)
IMPORTANT: This can cause a lot of false positives. Please perform extra analysis to determine if the url is fully attacker controlled. If not fully attacker controlled, then this is informational at best.
Required:
a.href, img.src, script.src, iframe.src, form.action, link.href.javascript: and other active schemes) when values can be attacker-influenced. ([MDN Web Docs][4])a.href = url.toString()) after parsing and validation, rather than string concatenation.Insecure patterns:
link.href = getParam("u")el.setAttribute("href", userInput) without validationDetection hints:
.href =, .src =, .action =, setAttribute("href", setAttribute("src".javascript: / data: usage in URLs. ([MDN Web Docs][4])IMPORTANT: This can cause a lot of false positives. Please perform extra analysis to determine if the url is fully attacker controlled. If not fully attacker controlled, then this is informational at best.
Fix:
new URL(...) and validate:<script src> at all (treat as code execution). ([OWASP Cheat Sheet Series][8])Severity: Medium to High (depends on threat model; High when handling untrusted content)
NOTE: It is most important to set the CSP's script-src. All other directives are not as important and can generally be excluded for the ease of development.
Required:
<meta http-equiv="Content-Security-Policy" ...> when headers are not available. ([MDN Web Docs][1])report-uri, frame-ancestors, sandbox). ([W3C][3])unsafe-inline as a “quick fix” for CSP issues unless explicitly required and reviewed (it defeats much of CSP’s purpose). ([MDN Web Docs][10])unsafe-eval unless explicitly required and reviewed (it allows eval-like APIs that are commonly abused). ([MDN Web Docs][10])Insecure patterns:
script-src 'unsafe-inline' and/or script-src 'unsafe-eval' without strong justification. ([MDN Web Docs][10])frame-ancestors (it will be ignored in meta). ([W3C][3])Detection hints:
<meta http-equiv="Content-Security-Policy".Content-Security-Policy header.<script> tags you want governed. ([W3C][3])Fix:
frame-ancestors) at the server/edge, not in meta. ([W3C][3])Severity: Medium
NOTE: It is most important to set the CSP's script-src. All other directives are not as important and can generally be excluded for the ease of development.
Required:
Insecure patterns:
onclick="..." handlers.unsafe-eval.Detection hints:
<script> blocks with inline code, onclick=", onload=", etc.unsafe-inline or unsafe-eval. ([MDN Web Docs][10])Fix:
Severity: Low
Required:
require-trusted-types-for 'script' to make many DOM XSS sinks reject raw strings. ([MDN Web Docs][11])trusted-types directive to restrict which policies can be created (reduces policy sprawl and improves auditability). ([MDN Web Docs][16])Insecure patterns:
Detection hints:
require-trusted-types-for and trusted-types.trustedTypes.createPolicy( and inspect policy implementations. ([MDN Web Docs][11])Fix:
createHTML that sanitizes).trusted-types <policyName...>.TrustedHTML / TrustedScriptURL as appropriate. ([MDN Web Docs][11])postMessage must use strict origin validation and explicit targetOriginSeverity: Medium (High if dangerous behavior can be triggered via postMessage)
Required:
targetOrigin (not *) to avoid sending data to an unexpected origin after redirects or window origin changes. ([MDN Web Docs][5])event.origin exactly against an allowlist of expected origins (no substring matching). ([OWASP Cheat Sheet Series][6])event.source (expected window reference) when applicable. ([MDN Web Docs][5])event.data structure (schema/shape) and treat it purely as data (never evaluate it as code and never insert into DOM with innerHTML). ([OWASP Cheat Sheet Series][6])Insecure patterns:
otherWindow.postMessage(payload, "*")window.addEventListener("message", (e) => { doSomething(e.data) }) with no origin checkif (e.origin.includes("trusted.com")) (substring checks)el.innerHTML = e.data ([OWASP Cheat Sheet Series][6])Detection hints:
postMessage(, addEventListener("message", onmessage =.event.origin. ([OWASP Cheat Sheet Series][6])Fix:
const ALLOWED = new Set(["https://app.example.com", "https://accounts.example.com"]); NOTE: For ease of development, you can use the current page's origin window.location.origin as a safe default origin.
if (!ALLOWED.has(event.origin)) return;event.data with a strict schema and reject unknown/extra fields.targetOrigin. ([OWASP Cheat Sheet Series][6])Mitigation:
Severity: Low
Required:
localStorage (or sessionStorage) if compromise would matter; a single XSS can exfiltrate everything in storage. ([OWASP Cheat Sheet Series][6])HttpOnly for session identifiers (JS cannot set HttpOnly, so avoid storing session IDs in JS-accessible storage). ([OWASP Cheat Sheet Series][6])Insecure patterns:
localStorage.setItem("access_token", token)localStorage.setItem("session", sessionId)localStorage is “trusted because same-origin.”Detection hints:
localStorage.getItem, localStorage.setItem, sessionStorage.*.token, jwt, session, auth, refresh. ([OWASP Cheat Sheet Series][6])Fix:
Severity: Low
Required:
Insecure patterns:
script-src *). ([MDN Web Docs][10])Detection hints:
<script src="https://..."> and tag manager snippets.script-src sources for wildcards or overly broad domains.document.createElement("script"), script.src = ..., appendChild(script). ([OWASP Cheat Sheet Series][8])Fix:
script-src to the smallest set of trusted sources.Severity: Low
Required:
Insecure patterns:
<script src="https://cdn.example.com/lib.js"></script> with no integrity.latest or unpinned third-party resources.Detection hints:
<script src="https:// and <link rel="stylesheet" href="https:// without integrity=.integrity is present and uses strong hashes (sha256/384/512 are typical). ([MDN Web Docs][12])Fix:
integrity="sha384-..." (or appropriate) and ensure proper CORS mode where needed.window/document named properties)Severity: Medium to High (can become Critical if it enables script loading or javascript: navigation)
Required:
window.someName / document.someName lookups that can be clobbered by injected HTML elements with matching id/name. ([OWASP Cheat Sheet Series][8])let x = window.redirectTo || "/safe"; location.assign(x); where redirectTo could be clobbered to an <a> element whose href is attacker-controlled (including javascript:). ([OWASP Cheat Sheet Series][8])getElementById) rather than named property access. ([OWASP Cheat Sheet Series][8])id/name collisions. ([OWASP Cheat Sheet Series][8])Insecure patterns:
const cfg = window.config || {}; used for security-sensitive URLs.const redirect = window.redirectTo || "/"; location.assign(redirect); ([OWASP Cheat Sheet Series][8])window.* config values without strict validation.Detection hints:
window. and document. used as config stores (especially || fallback patterns).location.assign/replace with variables that come from window/document properties.createElement('script')) where .src comes from a non-local variable. ([OWASP Cheat Sheet Series][8])Fix:
window/document) and pass it explicitly.When actively scanning, use these high-signal patterns:
.innerHTML, .outerHTML, insertAdjacentHTML(document.write(, document.writeln( ([OWASP Cheat Sheet Series][2])window.location, location.href, location.assign, location.replacejavascript: literals (and other suspicious schemes like data:text/html) ([MDN Web Docs][4])eval(, new Function, setTimeout(", setInterval(" ([MDN Web Docs][10]).setAttribute("on, .onclick =, .onload = with strings ([OWASP Cheat Sheet Series][2])postMessage:postMessage( with "*" as targetOriginaddEventListener("message" without strict event.origin allowlist checks ([MDN Web Docs][5])localStorage.setItem( / getItem(, sessionStorage.*token, jwt, session, auth, refresh ([OWASP Cheat Sheet Series][6])Content-Security-Policy header config (server/edge)<meta http-equiv="Content-Security-Policy" ...>unsafe-inline or unsafe-evalrequire-trusted-types-for / trusted-types directives ([MDN Web Docs][1])<script src="https://..."> without integrity=window.<name> || ... and document.<name> || ... patternswindow/document properties as config sources ([OWASP Cheat Sheet Series][8])Always try to confirm:
Primary standards / platform docs:
<meta> delivery restrictions; unsupported directives in meta CSP): https://www.w3.org/TR/CSP2/ ([W3C][3])unsafe-inline/unsafe-eval, eval blocking): https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP ([MDN Web Docs][10])<meta http-equiv> (CSP via meta and warning about meta-based security headers): https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/meta/http-equiv ([MDN Web Docs][1])frame-ancestors (and note it’s not supported in <meta>): https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/frame-ancestors ([MDN Web Docs][18])DOM XSS and dangerous sinks:
textContent): https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html ([OWASP Cheat Sheet Series][2])innerHTML (security considerations): https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML ([MDN Web Docs][19])insertAdjacentHTML (security considerations): https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML ([MDN Web Docs][20])document.write() / document.writeln() (security considerations): https://developer.mozilla.org/en-US/docs/Web/API/Document/write and https://developer.mozilla.org/en-US/docs/Web/API/Document/writeln ([MDN Web Docs][13])URL scheme hazards:
javascript: URLs (execution on navigation; discouraged; references window.location): https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/javascript ([MDN Web Docs][4])Trusted Types:
Element.innerHTML and Location.href setters; goals and limitations): https://www.w3.org/TR/trusted-types/ ([W3C][15])require-trusted-types-for directive: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/require-trusted-types-for ([MDN Web Docs][11])trusted-types directive: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/trusted-types ([MDN Web Docs][16])Cross-window messaging:
window.postMessage (security guidance: specify targetOrigin; validate origin): https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage ([MDN Web Docs][5])innerHTML): https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html ([OWASP Cheat Sheet Series][6])Third-party scripts and integrity:
https://cheatsheetseries.owasp.org/cheatsheets/Third_Party_Javascript_Management_Cheat_Sheet.html ([OWASP Cheat Sheet Series][7])https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Subresource_Integrity ([MDN Web Docs][12])https://www.w3.org/TR/sri-2/ ([W3C][21])DOM clobbering:
location.assign and javascript:): https://cheatsheetseries.owasp.org/cheatsheets/DOM_Clobbering_Prevention_Cheat_Sheet.html ([OWASP Cheat Sheet Series][8])[1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/meta/http-equiv "https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/meta/http-equiv" [2]: https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html "https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html" [3]: https://www.w3.org/TR/CSP2/ "Content Security Policy Level 2" [4]: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/javascript "javascript: URLs - URIs | MDN" [5]: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage "https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage" [6]: https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html "https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html" [7]: https://cheatsheetseries.owasp.org/cheatsheets/Third_Party_Javascript_Management_Cheat_Sheet.html "https://cheatsheetseries.owasp.org/cheatsheets/Third_Party_Javascript_Management_Cheat_Sheet.html" [8]: https://cheatsheetseries.owasp.org/cheatsheets/DOM_Clobbering_Prevention_Cheat_Sheet.html "https://cheatsheetseries.owasp.org/cheatsheets/DOM_Clobbering_Prevention_Cheat_Sheet.html" [9]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/rel/noopener "https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/rel/noopener" [10]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP "https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP" [11]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/require-trusted-types-for "https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/require-trusted-types-for" [12]: https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Subresource_Integrity "https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Subresource_Integrity" [13]: https://developer.mozilla.org/en-US/docs/Web/API/Document/write "https://developer.mozilla.org/en-US/docs/Web/API/Document/write" [14]: https://developer.mozilla.org/en-US/docs/Web/API/Document/writeln "https://developer.mozilla.org/en-US/docs/Web/API/Document/writeln" [15]: https://www.w3.org/TR/trusted-types/ "https://www.w3.org/TR/trusted-types/" [16]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/trusted-types "https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/trusted-types" [18]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/frame-ancestors "https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/frame-ancestors" [19]: https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML "https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML" [20]: https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML "https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML" [21]: https://www.w3.org/TR/sri-2/ "https://www.w3.org/TR/sri-2/"