Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Subresource Integrity Generator #12

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
dist
42 changes: 42 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const gulp = require('gulp');
const { series } = require('gulp');
const sriHash = require('gulp-sri-hash');
const { exec } = require('child_process');
const hashstream = require('inline-csp-hash');

/* remove all integrity and crossorigin tags from *.html files in website directory */
exec(`
for file in website/*.html; do
sed 's/ integrity="[^"]*"//g' $file | sed 's/ crossorigin="[^"]*"//g' > "$file.tmp" && mv "$file.tmp" $file
done
`);

exports.default = series(
function add_integrity_and_csp_checks() {
return gulp.src('./**/*.html')
// do not modify contents of any referenced css- and js-files after this task...
.pipe(sriHash({
algo: 'sha512', // use strong hashing
// prefix: '/assets', // no trailing slash
// selector: 'link[href]', // limit selector
cacheParser: false, // no cache
relative: true // assets reside relative to html file
}))
.pipe(hashstream({
what: 'script',
hash: 'sha512', // use strong hashing
replace_cb: (s, hashes) => s.replace(/script-src localhost:\* https:[^;]*/, "script-src localhost:* https: " + hashes.join(" "))
}))
.pipe(hashstream({
what: 'style',
hash: 'sha512', // use strong hashing
replace_cb: (s, hashes) => s.replace(/style-src localhost:\* https:[^;]*/, "style-src localhost:* https: " + hashes.join(" "))
}))
// ... manipulating html files further, is perfectly fine
.pipe(gulp.dest('./')) // output
;
},
function publish_for_http(){
return exec('./publish_for_http.sh');
}
);
Loading