Skip to content

Commit

Permalink
Inline scripts CSP for login
Browse files Browse the repository at this point in the history
* Protect the login page a little bit more

Post OpenUserJS#944 OpenUserJS#1867
  • Loading branch information
Martii committed Dec 7, 2021
1 parent 9413bed commit c92f016
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
22 changes: 22 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var isDbg = require('../libs/debug').isDbg;
var async = require('async');
var _ = require('underscore');
var url = require('url');
var crypto = require('crypto');

//--- Model inclusions
var Discussion = require('../models/discussion').Discussion;
Expand Down Expand Up @@ -261,6 +262,11 @@ exports.register = function (aReq, aRes) {
//

Strategy.find({}, function (aErr, aAvailableStrategies) {
var SECRET = process.env.HCAPTCHA_SECRET_KEY;
var SITEKEY = process.env.HCAPTCHA_SITE_KEY;
var defaultCSP = ' \'self\'';
var captchaCSP = (SECRET ? ' hcaptcha.com *.hcaptcha.com' : '');

if (aErr || !aAvailableStrategies) {
statusCodePage(aReq, aRes, aNext, {
statusCode: 503,
Expand All @@ -277,6 +283,11 @@ exports.register = function (aReq, aRes) {
});
});

options.hasCaptcha = (SECRET ? true : false);

options.nonce = crypto.randomBytes(512).toString('hex');
defaultCSP += ' \'nonce-' + options.nonce + '\'';

// Insert an empty default strategy at the beginning
// NOTE: Safari always autoselects an option when disabled
options.strategies.unshift({'strat': '', 'display': '(default preferred authentication)'});
Expand All @@ -286,10 +297,21 @@ exports.register = function (aReq, aRes) {
return aStrategy.display;
});


aRes.header('Cache-Control', 'no-cache, no-store, must-revalidate');
aRes.header('Pragma', 'no-cache');
aRes.header('Expires', '0');

//
aRes.header('Content-Security-Policy',
'default-src' + defaultCSP +
'; connect-src' + defaultCSP + captchaCSP +
'; frame-src' + defaultCSP + captchaCSP +
'; style-src' + defaultCSP + captchaCSP +
'; script-src' + defaultCSP + captchaCSP +
''
);

aRes.render('pages/loginPage', options);
}
});
Expand Down
2 changes: 1 addition & 1 deletion views/includes/scripts/formControlClear.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script type="text/javascript">
<script type="text/javascript"{{#nonce}} nonce="{{nonce}}"{{/nonce}}>
(function () {
'use strict';

Expand Down
2 changes: 1 addition & 1 deletion views/includes/scripts/googleAnalytics.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script type="text/javascript">
<script type="text/javascript"{{#nonce}} nonce="{{nonce}}"{{/nonce}}>>
(function () {

if (!{{DNT}}) {
Expand Down
2 changes: 1 addition & 1 deletion views/includes/scripts/hideReminders.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script type="text/javascript">
<script type="text/javascript"{{#nonce}} nonce="{{nonce}}"{{/nonce}}>>
(function () {

var events = 'focus resize scroll';
Expand Down
2 changes: 1 addition & 1 deletion views/includes/scripts/loginEcho.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script type="text/javascript">
<script type="text/javascript"{{#nonce}} nonce="{{nonce}}"{{/nonce}}>
(function () {

// NOTE: Keep in sync with helper
Expand Down
2 changes: 1 addition & 1 deletion views/pages/loginPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>{{title}}</title>
{{> includes/head.html }}
{{#hasCaptcha}}
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
<script src="https://js.hcaptcha.com/1/api.js" async="async" defer="defer"></script>
{{/hasCaptcha}}
</head>
<body>
Expand Down

0 comments on commit c92f016

Please sign in to comment.