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 nonce csp capability #316

Open
wants to merge 1 commit into
base: v1.x/staging
Choose a base branch
from
Open
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
31 changes: 28 additions & 3 deletions lib/webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ function getPresetHeader(name, preset, req) {
return `default-src 'self' ${req.hostname};`;
} else if (preset == 'frame-strict') {
return `frame-src 'self' ${req.hostname};`;
} else if (preset == 'script-nonce') {
let nonce=crypto.randomBytes(16).toString('hex');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether the approach with a random string would be suitable for HA mode when there are multiple app-servers running.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, probably not.

req.nonce=nonce;
return `script-src 'nonce-${nonce}' 'strict-dynamic' 'unsafe-eval'`;
}
break;
}
Expand Down Expand Up @@ -1425,7 +1429,9 @@ WebApp.prototype = {
res.redirect(rootPage);
});
}
this.expressApp.use(rootPage, express.static(webdir));
if (fs.existsSync(webdir)) {
this.expressApp.use(rootPage, express.static(webdir));
}
},

installCommonMiddleware() {
Expand Down Expand Up @@ -1840,9 +1846,28 @@ WebApp.prototype = {
}
middleware.push(commonMiddleware.customHeaderInjection(headersArray));
}
middleware.push(expressStaticGzip(path.join(plugin.location, '/web'),
let webMiddleware = middleware.slice();
webMiddleware.push(expressStaticGzip(path.join(plugin.location, '/web'),
{enableBrotli: true, orderPreference: ['br', 'gzip']}));
this.pluginRouter.use(url, middleware);

//Used to replace nonce template with server value
if (fs.existsSync(path.join(plugin.location, 'web', 'index.html')) && plugin.webContent.headers) {
let indexMiddleware = middleware.slice();
indexMiddleware.push((req,res,next)=>{
console.log(`nonce=${req.nonce} and path=${req.path}`);
if (req.nonce && (req.path == '/' || req.path == '/index.html')) {
fs.readFile(path.join(plugin.location, 'web', 'index.html'), {encoding: 'utf8'}, (err, data)=> {
res.status(200).send(data.replace('nonce="{{nonce}}', 'nonce="'+req.nonce));
});
} else { next() }
});
indexMiddleware.push(expressStaticGzip(path.join(plugin.location, '/web'),
{enableBrotli: true, orderPreference: ['br', 'gzip']}));
this.pluginRouter.use(url, indexMiddleware);
//but if no template candidate, skip that for performance.
} else {
this.pluginRouter.use(url, webMiddleware);
}
}
if (plugin.pluginType === "library") {
if (plugin.libraryVersion) {
Expand Down