Skip to content

Commit c9a91c4

Browse files
author
Jason Barry
authored
fix: log unnecessary invocations (#28)
1 parent a602df8 commit c9a91c4

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ First, add the dependency:
1616
npm i @netlify/plugin-csp-nonce
1717
```
1818

19-
Then, include the plugin inside your `netlify.toml`:
19+
Then, include the plugin inside your `netlify.toml`:
2020

2121
```
2222
# netlify.toml
2323
[[plugins]]
2424
package = "@netlify/plugin-csp-nonce"
2525
```
2626

27-
2827
## Configuration options
2928

30-
Sample configuration:
29+
Sample configuration:
3130

3231
```
3332
# netlify.toml
@@ -41,31 +40,35 @@ Sample configuration:
4140
]
4241
```
4342

44-
#### `reportOnly`
45-
*Default: `true`*.
43+
#### `reportOnly`
44+
45+
_Default: `true`_.
4646

4747
When true, uses the `Content-Security-Policy-Report-Only` header instead of the `Content-Security-Policy` header. Setting `reportOnly` to `true` is useful for testing the CSP with real production traffic without actually blocking resources. Be sure to monitor your logging function to observe potential violations.
4848

4949
#### `reportUri`
50-
*Default: `undefined`*.
5150

52-
The relative or absolute URL to report any violations. If left undefined, violations are reported to the `__csp-violations` function, which this plugin deploys. If your site already has a `report-uri` directive defined in its CSP header, then that value will take precedence.
51+
_Default: `undefined`_.
52+
53+
The relative or absolute URL to report any violations. If left undefined, violations are reported to the `__csp-violations` function, which this plugin deploys. If your site already has a `report-uri` directive defined in its CSP header, then that value will take precedence.
5354

5455
#### `unsafeEval`
55-
*Default: `true`.*
56+
57+
_Default: `true`._
5658

5759
When true, adds `'unsafe-eval'` to the CSP for easier adoption. Set to `false` to have a safer policy if your code and code dependencies does not use `eval()`.
5860

5961
#### `path`
60-
*Default: `"/*"`.*
62+
63+
_Default: `"/_"`.\*
6164

6265
The glob expressions of path(s) that should invoke the CSP nonce edge function. Can be a string or array of strings.
6366

6467
#### `excludedPath`
65-
*Default: `[]`*
6668

67-
The glob expressions of path(s) that _should not_ invoke the CSP nonce edge function. Must be an array of strings. This value gets spread with common non-html filetype extensions (`*.css`, `*.js`, `*.svg`, etc).
69+
_Default: `[]`_
6870

71+
The glob expressions of path(s) that _should not_ invoke the CSP nonce edge function. Must be an array of strings. This value gets spread with common non-html filetype extensions (`*.css`, `*.js`, `*.svg`, etc).
6972

7073
## Debugging
7174

@@ -80,6 +83,8 @@ To further limit invocations, add globs to the `excludedPath` configuration opti
8083

8184
Requests that invoke the nonce edge function will contain a `x-debug-csp-nonce: invoked` response header. Use this to determine if unwanted paths are invoking the edge function, and add those paths to the `excludedPath` array.
8285

86+
Also, monitor the edge function logs in the Netlify UI. If the edge function is invoked but the response is not transformed, the request's path will be logged.
87+
8388
### Not transforming as expected
8489

8590
If your HTML does not contain the `nonce` attribute on the `<script>` tags that you expect, ensure that all of these criteria are met:
@@ -91,10 +96,6 @@ If your HTML does not contain the `nonce` attribute on the `<script>` tags that
9196

9297
### Quickly enabling and disabling
9398

94-
You may want to quickly enable/disable the plugin while monitoring violation reports. You can do so without modifying code.
99+
You may want to quickly enable/disable the plugin while monitoring violation reports. You can do so without modifying code.
95100

96101
Simply set the `DISABLE_CSP_NONCE` environment variable to `true`, and your next deploy will skip running the plugin. Setting to `false` will re-enable the plugin. The environment variable needs to be scoped to `Builds`.
97-
98-
99-
100-

src/__csp-nonce.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const handler = async (request: Request, context: Context) => {
3535
.startsWith("text/html");
3636
const shouldTransformResponse = isGET && isHTMLRequest && isHTMLResponse;
3737
if (!shouldTransformResponse) {
38+
// @ts-expect-error
39+
console.log(`Unnecessary invocation for ${request.path || request.url}`);
3840
return response;
3941
}
4042

0 commit comments

Comments
 (0)