Files
cellxgene/client/configuration/webpack/cspHashPlugin.js
T
Bruce Martin f42f5151a6 Refactor build and CSP headers for Safari compat (#1442)
* add unsafe-inline directive to style-src

* debugging - turn on csp reporting

* revert reporting only csp

* do not inline JS and CSS in build

* enable HTTPs only when in production mode

* remove debug printf

* fix clean target

* revert force_https removal
2020-05-04 12:47:35 -07:00

64 lines
1.8 KiB
JavaScript

const cheerio = require("cheerio");
const crypto = require("crypto");
HtmlWebpackPlugin = require("html-webpack-plugin");
class CspHashPlugin {
constructor(opts) {
this.opts = { ...opts };
}
apply(compiler) {
compiler.hooks.compilation.tap("CspHashPlugin", (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
"CspHashPlugin",
(data, cb) => {
const { filename } = this.opts;
const $ = cheerio.load(data.html, { decodeEntities: false });
if (filename) {
const results = {};
results["script-hashes"] = $("script:not([src]):not([no-csp-hash])")
.map((i, elmt) => this.digest($(elmt).html()))
.get();
results["style-hashes"] = $("style:not([href]):not([no-csp-hash])")
.map((i, elmt) => this.digest($(elmt).html()))
.get();
const json = JSON.stringify(results);
compilation.assets[filename] = {
source: () => json,
size: () => json.length,
};
}
// remove no-csp-hash attributes
let foundOne = false;
$("script[no-csp-hash]").each((i, elmt) => {
$(elmt).removeAttr("no-csp-hash");
foundOne = true;
});
$("style[no-csp-hash]").each((i, elmt) => {
$(elmt).removeAttr("no-csp-hash");
foundOne = true;
});
if (foundOne) data.html = $.html();
// Tell webpack to move on
cb(null, data);
}
);
});
}
digest(str) {
const hash = crypto
.createHash("sha256")
.update(str, "utf8")
.digest("base64");
return "sha256-" + hash;
}
}
module.exports = CspHashPlugin;