@netlify/plugin-csp-nonce 1.2.5 → 1.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,7 +60,7 @@ When true, adds `'unsafe-eval'` to the CSP for easier adoption. Set to `false` t
60
60
 
61
61
  #### `path`
62
62
 
63
- _Default: `"/_"`.\*
63
+ _Default: `"/*"`._
64
64
 
65
65
  The glob expressions of path(s) that should invoke the CSP nonce edge function. Can be a string or array of strings.
66
66
 
package/index.js CHANGED
@@ -3,10 +3,17 @@ import fs, { copyFileSync } from "fs";
3
3
 
4
4
  const SITE_ID = "321a7119-6008-49a8-9d2f-e20602b1b349";
5
5
 
6
- export const onPreBuild = async ({ inputs, netlifyConfig, utils }) => {
6
+ export const onPreBuild = async ({
7
+ inputs,
8
+ netlifyConfig,
9
+ utils,
10
+ constants,
11
+ }) => {
7
12
  const config = JSON.stringify(inputs, null, 2);
8
13
  const { build } = netlifyConfig;
9
14
 
15
+ const { INTERNAL_EDGE_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC } = constants;
16
+
10
17
  // DISABLE_CSP_NONCE is undocumented (deprecated), but still supported
11
18
  // -> superseded by CSP_NONCE_DISTRIBUTION
12
19
  if (build.environment.DISABLE_CSP_NONCE === "true") {
@@ -35,25 +42,30 @@ export const onPreBuild = async ({ inputs, netlifyConfig, utils }) => {
35
42
  ? "./src"
36
43
  : "./node_modules/@netlify/plugin-csp-nonce/src";
37
44
 
38
- const edgeFunctionsDir = build.edge_functions || "./netlify/edge-functions";
39
45
  // make the directory in case it actually doesn't exist yet
40
- await utils.run.command(`mkdir -p ${edgeFunctionsDir}`);
41
- console.log(` Writing nonce edge function to ${edgeFunctionsDir}...`);
46
+ await utils.run.command(`mkdir -p ${INTERNAL_EDGE_FUNCTIONS_SRC}`);
47
+ console.log(
48
+ ` Writing nonce edge function to ${INTERNAL_EDGE_FUNCTIONS_SRC}...`,
49
+ );
42
50
  copyFileSync(
43
51
  `${basePath}/__csp-nonce.ts`,
44
- `${edgeFunctionsDir}/__csp-nonce.ts`
52
+ `${INTERNAL_EDGE_FUNCTIONS_SRC}/__csp-nonce.ts`,
53
+ );
54
+ fs.writeFileSync(
55
+ `${INTERNAL_EDGE_FUNCTIONS_SRC}/__csp-nonce-inputs.json`,
56
+ config,
45
57
  );
46
- fs.writeFileSync(`${edgeFunctionsDir}/__csp-nonce-inputs.json`, config);
47
58
 
48
59
  // if no reportUri in config input, deploy function on site's behalf
49
60
  if (!inputs.reportUri) {
50
- const functionsDir = build.functions || "./netlify/functions";
51
61
  // make the directory in case it actually doesn't exist yet
52
- await utils.run.command(`mkdir -p ${functionsDir}`);
53
- console.log(` Writing violations logging function to ${functionsDir}...`);
62
+ await utils.run.command(`mkdir -p ${INTERNAL_FUNCTIONS_SRC}`);
63
+ console.log(
64
+ ` Writing violations logging function to ${INTERNAL_FUNCTIONS_SRC}...`,
65
+ );
54
66
  copyFileSync(
55
67
  `${basePath}/__csp-violations.ts`,
56
- `${functionsDir}/__csp-violations.ts`
68
+ `${INTERNAL_FUNCTIONS_SRC}/__csp-violations.ts`,
57
69
  );
58
70
  } else {
59
71
  console.log(` Using ${inputs.reportUri} as report-uri directive...`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@netlify/plugin-csp-nonce",
4
- "version": "1.2.5",
4
+ "version": "1.2.7",
5
5
  "description": "Use a nonce for the script-src and style-src directives of your Content Security Policy.",
6
6
  "main": "index.js",
7
7
  "repository": {
@@ -90,15 +90,16 @@ const handler = async (request: Request, context: Context) => {
90
90
  .map((directive) => {
91
91
  // prepend our rules for any existing directives
92
92
  const d = directive.trim();
93
- if (d.startsWith("script-src")) {
94
- return d.replace("script-src", scriptSrc);
93
+ // intentionally add trailing space to avoid mangling `script-src-elem`
94
+ if (d.startsWith("script-src ")) {
95
+ return d.replace("script-src ", scriptSrc);
95
96
  }
96
97
  // intentionally omit report-uri: theirs should take precedence
97
98
  return d;
98
99
  })
99
100
  .filter(Boolean);
100
101
  // push our rules if the directives don't exist yet
101
- if (!directives.find((d) => d.startsWith("script-src"))) {
102
+ if (!directives.find((d) => d.startsWith("script-src "))) {
102
103
  directives.push(scriptSrc);
103
104
  }
104
105
  if (!directives.find((d) => d.startsWith("report-uri"))) {