The base-uri Directive
What you'll learn: How to prevent malicious <base> tag injection attacks by controlling which URLs can be used as document base URLs with the base-uri CSP directive.
What's the Problem?
In HTML, the <base> tag sets the base URL for all relative URLs in a document. If an attacker can inject a malicious <base> tag (perhaps through an XSS vulnerability or unvalidated content), they can redirect all your relative links and scripts to their own server. Imagine a welcome email feature where user input accidentally gets rendered as HTML—suddenly <script src="app.js"> loads from the attacker's domain instead of yours!
Think of it like this: The <base> tag is like setting the "home address" for your document. If someone malicious changes that address, all your relative directions ("go two blocks over") now lead somewhere completely different.
How base-uri Protects You
The base-uri directive restricts which URLs can appear in the <base> tag's href attribute. Most sites never need to change the base URL dynamically, so you can lock this down tight:
Content-Security-Policy: base-uri 'self'
This allows <base> tags only if they point to your own origin. For maximum security, many sites use:
Content-Security-Policy: base-uri 'none'
This completely disallows any <base> tags from taking effect—essentially saying "our document has no dynamic base URL."
Common Configuration
Content-Security-Policy: default-src 'self'; base-uri 'self'; script-src 'self'
Note that base-uri does not fall back to default-src—you must set it explicitly if you want protection.
Key Takeaway: Use base-uri 'self' or base-uri 'none' to prevent attackers from hijacking your document's base URL and redirecting all relative resources to malicious servers.