Sub topic 12
Security: Content Security Policy and inline styles
This chapter assumes you can write all three kinds of CSS (the style attribute, the style element, and a linked file) and that you know what an HTTP response header is. It covers how a server can tell the browser to refuse some of those kinds.
What a Content Security Policy does
A Content Security Policy (CSP) is a rule set that a server sends in an HTTP response header. The browser enforces it for that page. The policy lists which sources the page may load content from, such as scripts, images, and styles.
The directive that controls CSS is style-src. If a page has no style-src, the browser falls back to default-src. If neither is present, styles are not restricted.
Sites use CSP to limit the damage of injection attacks. Suppose an attacker gets markup into your page. CSS can then hide buttons, overlay a fake login form, or leak data through selectors that request attacker-controlled URLs. A strict policy stops injected styles from being applied.
The strict policy
This header allows stylesheets only from the page's own origin:
Content-Security-Policy: style-src 'self'Under this policy:
| Method | Result | Reason |
|---|---|---|
| `<link rel="stylesheet" href="css/site.css">` | Allowed | The file comes from the same origin |
| `<link rel="stylesheet" href="https://cdn.example/x.css">` | Blocked | Another origin, not listed |
| `<style>` element in the page | Blocked | Inline content is not covered by `'self'` |
| `style="..."` attribute | Blocked | Inline content is not covered by `'self'` |
Blocked styles are silently ignored. The page still loads, but it looks unstyled. The browser console reports a message that names the violated directive.
Allowing inline styles safely
If you need a style element under a strict policy, you have three options.
- Nonce. The server generates a random value for each response. It puts the value in the header and in the element.
- Hash. The header contains a SHA-256 (or SHA-384/512) hash of the exact text inside the
styleelement. The browser hashes the element's content and compares. 'unsafe-inline'. Allows all inline styles. It works, but it removes most of the protection for styles.
A nonce looks like this:
Content-Security-Policy: style-src 'self' 'nonce-Zm9vYmFyMTIzNDU2'<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Nonce example</title>
<style nonce="Zm9vYmFyMTIzNDU2">
p { color: darkblue; }
</style>
</head>
<body>
<p>This style element is allowed because its nonce matches the header.</p>
</body>
</html>The nonce must be unguessable and different on every response. A fixed nonce is no better than 'unsafe-inline', because an attacker can copy it.
A hash is useful when the inline CSS never changes, since you can compute it once at build time. Any edit to the content, even whitespace, changes the hash and the style is blocked.
Style attributes are the hard case
A nonce attaches to an element, and a style attribute is not an element. A nonce therefore cannot allow style="...". You can allow specific attributes by hash with the 'unsafe-hashes' keyword, but this is awkward to maintain. In practice, a site with strict CSP moves its attribute styles into classes in a stylesheet.
You can also split the directive. style-src-elem controls <style> and <link>, and style-src-attr controls style attributes. This lets you keep a strict element policy while handling attributes separately.
Common errors
- Adding
'unsafe-inline'to silence the console. This fixes the symptom and drops the protection. Move the CSS into a linked file instead. - Forgetting an external origin. A font or CDN stylesheet from another domain needs that origin listed in
style-src. - Reusing a nonce. A cached page with a baked-in nonce defeats the purpose.
- Testing only in your own browser cache. Open the console on a clean load and check for CSP violation messages.
Link to the choice of method
External stylesheets are the easiest method to use with CSP, since 'self' covers them. This is one reason strict sites prefer them. Internal styles need a nonce or hash. Inline attributes cost the most to support.
Exercise
A page sends Content-Security-Policy: style-src 'self'. It has a linked css/site.css, a <style> element in the head, and a <p style="color: red;">. Which of the three still apply, and what is the smallest safe change that makes the <style> element work again?
Show answer
Only the linked css/site.css applies, because it comes from the same origin. The <style> element and the style attribute are blocked. The smallest safe change is to add a per-response random nonce to the header ('nonce-...') and the same value as the nonce attribute on the <style> element. The <p> attribute stays blocked, so move its color into a rule in a stylesheet.