Skip to content

Commit

Permalink
Fix bug in URL generation for _headers file
Browse files Browse the repository at this point in the history
  • Loading branch information
cramforce committed Nov 6, 2021
1 parent c32358e commit ef5b2a4
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions _11ty/apply-csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,36 @@ const addCspHash = async (rawContent, outputPath) => {
# [end csp headers]`;
}
const oldCustomHeaders = headers.match(regExp)[2].toString();
const CSPPolicy = `Content-Security-Policy: ${CSP.apply().regular.replace("HASHES", hashes.join(" "))}`;
// write headers for full path (/blog/index.html) and pretty url (/blog/)
// 404.html require a different pattern.
const newCustomHeaders = (outputPath != "_site/404.html")
? oldCustomHeaders.concat(
"\n", filePath, "\n ", CSPPolicy,
"\n", filePathPrettyURL, "\n ", CSPPolicy)
: oldCustomHeaders.concat(
"\n", "/404.html", "\n ", CSPPolicy,
"\n", "/*", "\n ", CSPPolicy);
fs.writeFileSync(headersPath, headers.replace(regExp, `$1${newCustomHeaders}\n$3`));
const CSPPolicy = `Content-Security-Policy: ${CSP.apply().regular.replace(
"HASHES",
hashes.join(" ")
)}`;
let newCustomHeaders = oldCustomHeaders.concat(
"\n",
filePath,
"\n ",
CSPPolicy
);
if (filePath != filePathPrettyURL) {
newCustomHeaders = newCustomHeaders.concat(
"\n",
filePathPrettyURL,
"\n ",
CSPPolicy
);
}
fs.writeFileSync(
headersPath,
headers.replace(regExp, `$1${newCustomHeaders}\n$3`)
);
} catch (error) {
console.log("[apply-csp] Something went wrong with the creation of the csp headers\n", error);
console.log(
"[apply-csp] Something went wrong with the creation of the csp headers\n",
error
);
}
}


return content;
};

Expand Down

0 comments on commit ef5b2a4

Please sign in to comment.