Skip to content

Commit 1786975

Browse files
authored
Merge pull request #1 from mdarrik/add-unsafe-styles-input
Adds "Unsafe Styles" Configuration
2 parents 1d465da + 7b69b52 commit 1786975

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,38 @@ While static sites are fairly secure, there's still a risk of Cross Site Scripti
66

77
CSP Headers can also help you get better scores on some automated tests, like [Web Page Test](https://www.webpagetest.org/)
88

9+
## Installation
10+
Currently, this plugin is not available in the Netlify UI. To install it, perform the following steps:
11+
12+
1. Add it to your Netlify.toml by adding the following to the Netlify.toml. This plugin should go after any other plugins that modify your html pages. This will prevent hashes from getting mismatched.
13+
```toml
14+
[[plugins]]
15+
package = "netlify-plugin-csp-headers"
16+
```
17+
1. Install the plugin in your package.json using either npm or yarn.
18+
```bash
19+
npm install -D netlify-plugin-csp-headers
20+
```
21+
```bash
22+
yarn add -D netlify-plugin-csp-headers
23+
```
24+
1. Deploy your site. If you have deploy previews turned on, it's probably best to test this plugin in a deploy preview. This can help you make sure you don't accidentally break your site when you turn on CSP headers. Typically, this involves making the above changes in a pull request.
25+
26+
## Inputs/Options
27+
28+
|Input | Environment Variable | Allowed Values | Description
29+
--- | --- | --- | ---
30+
|`unsafeStyles` | `CSP_HEADERS_UNSAFE_STYLES` | `true`, `false` | A value of `true` removes the style tag hashes from your inline styles. This way any post-processing modifications/runtime styles still work on your site.
31+
32+
933

1034
## Warning about Netlify Asset Optimizations
1135

1236
To improve the security of inline script & style tags, it takes a hash of the contents. This can stop attackers from modifying them after you've deployed your site. It also prevents new ones from being added. However, this also means that Netlify's Asset Optimization can break your site. Because Asset Optimization changes the URLs of static assets like fonts after your build is complete, it makes the hashes no longer match the ones generated by this plugin. This causes your browser to block those inline assets. Unfortunately, I haven't come up with a good way around this since the URLs are randomly generated. Unfortunately, even if you only use the Pretty URLs optimization, self hosted font urls will still get replaced. To get around this, I currently see 3 options:
1337

1438
1. Move all `<style>` tags with font declarations to an external file. This will add additional network requests to your page load, and may cause performance to drop slightly.
1539
1. Turn off all optimizations (including pretty urls 😢). This will stop Netlify from changing anything about your code. You'll also be responsible for optimizing all of your own assets. It may also prevent "pretty urls" from working correctly on your site (so pages might be at `https://example.com/route/index.html` instead of `https://example.com/route/`).
16-
1. Add the environment variable `CSP_HEADERS_UNSAFE_STYLE` with a value of `true` in your Netlify UI Dashboard. The plugin will then not include any hashes for style tags in the CSP headers. This is probably mostly safe. However, there are some risks of malicious `<style>` elements, especially around images.
40+
1. Add the environment variable `CSP_HEADERS_UNSAFE_STYLE` with a value of `true` in your Netlify UI Dashboard. The plugin will then not include any hashes for style tags in the CSP headers. This is probably mostly safe. However, there are some risks of malicious `<style>` elements, especially around images. The default CSP header added by the site should prevent at least some of the risks associated with images.
1741

1842

1943

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ const isScriptTag = require('hast-util-is-javascript');
99
const isStyleTag = require('hast-util-is-css-style')
1010
const isCssLink = require('hast-util-is-css-link')
1111

12+
let unsafeInlineStyles = process.env.CSP_HEADERS_UNSAFE_STYLES ?? false
13+
1214
module.exports= {
13-
onPostBuild: async function({constants, utils}) {
15+
onPostBuild: async function({constants, utils, inputs}) {
16+
unsafeInlineStyles = inputs.unsafeStyles ?? unsafeInlineStyles
1417
try {
1518
const htmlFiles = await getHtmlFilesFromDir(constants.PUBLISH_DIR)
1619
const cspHashesPromises = htmlFiles.map(file => processHtmlFile(file));
@@ -88,6 +91,6 @@ function generateRedirectString({filePath, hashes}, publishPath) {
8891
const url = filePath.replace(publishPath, '').replace(/^\/index.html/, '/');
8992
return (
9093
`${url}
91-
Content-Security-Policy: default-src 'self'; script-src 'self' 'strict-dynamic' 'unsafe-inline' ${hashes['script'].join(" ")}; style-src 'self' 'unsafe-inline' ${hashes['style'].join(' ')};
94+
Content-Security-Policy: default-src 'self'; script-src 'self' 'strict-dynamic' 'unsafe-inline' ${hashes['script'].join(" ")}; style-src 'self' 'unsafe-inline' ${unsafeInlineStyles ? '' : hashes['style'].join(' ')};
9295
`)
9396
}

manifest.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
name: netlify-plugin-csp-hash
1+
name: netlify-plugin-csp-hash
2+
inputs:
3+
- name: unsafeStyles
4+
description: Value of "true" allows unsafe-inline styles in your site. This leaves the style tag hashes out of your CSP headers. This gets around issues from Netlify transforming urls and other things after the plugin runs. Can instead use CSP_HEADERS_UNSAFE_STYLES environment variable.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "netlify-plugin-csp-headers",
3-
"version": "0.0.1-alpha.04",
3+
"version": "0.0.1-alpha.05",
44
"main": "index.js",
55
"repository": "https://github.com/mdarrik/netlify-plugin-csp-hash.git",
66
"author": "Darrik <30670444+mdarrik@users.noreply.github.com>",

0 commit comments

Comments
 (0)