Skip to content

Commit d939de1

Browse files
committed
Fix CSP for disqus and Google Analytics
This commit should fix existing problems with Disqus and Google Analytics enabled in the meta-yaml section of a note. Before this commit they were blocked by the strict CSP. It's still possible to disable the added directives using `addDisqus` and `addGoogleAnalytics` in the `csp` config section. They are enabled by default to prevent breaking changes. Signed-off-by: Sheogorath <sheogorath@shivering-isles.com>
1 parent d2cce76 commit d939de1

6 files changed

Lines changed: 25 additions & 9 deletions

File tree

config.json.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"directives": {
2828
},
2929
"upgradeInsecureRequests": "auto"
30-
"addDefaults": true
30+
"addDefaults": true,
31+
"addDisqus": true,
32+
"addGoogleAnalytics": true
3133
},
3234
"db": {
3335
"username": "",

lib/config/default.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module.exports = {
1818
directives: {
1919
},
2020
addDefaults: true,
21+
addDisqus: true,
22+
addGoogleAnalytics: true,
2123
upgradeInsecureRequests: 'auto',
2224
reportURI: undefined
2325
},

lib/csp.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var CspStrategy = {}
55

66
var defaultDirectives = {
77
defaultSrc: ['\'self\''],
8-
scriptSrc: ['\'self\'', 'vimeo.com', 'https://gist.github.com', 'www.slideshare.net', 'https://query.yahooapis.com', 'https://*.disqus.com', '\'unsafe-eval\''],
8+
scriptSrc: ['\'self\'', 'vimeo.com', 'https://gist.github.com', 'www.slideshare.net', 'https://query.yahooapis.com', '\'unsafe-eval\''],
99
// ^ TODO: Remove unsafe-eval - webpack script-loader issues https://github.com/hackmdio/hackmd/issues/594
1010
imgSrc: ['*'],
1111
styleSrc: ['\'self\'', '\'unsafe-inline\'', 'https://assets-cdn.github.com'], // unsafe-inline is required for some libs, plus used in views
@@ -22,11 +22,23 @@ var cdnDirectives = {
2222
fontSrc: ['https://cdnjs.cloudflare.com', 'https://fonts.gstatic.com']
2323
}
2424

25+
var disqusDirectives = {
26+
scriptSrc: ['https://*.disqus.com', 'https://*.disquscdn.com'],
27+
styleSrc: ['https://*.disquscdn.com'],
28+
fontSrc: ['https://*.disquscdn.com']
29+
}
30+
31+
var googleAnalyticsDirectives = {
32+
scriptSrc: ['https://www.google-analytics.com']
33+
}
34+
2535
CspStrategy.computeDirectives = function () {
2636
var directives = {}
2737
mergeDirectives(directives, config.csp.directives)
2838
mergeDirectivesIf(config.csp.addDefaults, directives, defaultDirectives)
2939
mergeDirectivesIf(config.useCDN, directives, cdnDirectives)
40+
mergeDirectivesIf(config.csp.addDisqus, directives, disqusDirectives)
41+
mergeDirectivesIf(config.csp.addGoogleAnalytics, directives, googleAnalyticsDirectives)
3042
if (!areAllInlineScriptsAllowed(directives)) {
3143
addInlineScriptExceptions(directives)
3244
}

lib/response.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ function showPublishNote (req, res, next) {
226226
lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null,
227227
robots: meta.robots || false, // default allow robots
228228
GA: meta.GA,
229-
disqus: meta.disqus
229+
disqus: meta.disqus,
230+
cspNonce: res.locals.nonce
230231
}
231232
return renderPublish(data, res)
232233
}).catch(function (err) {

public/views/shared/disqus.ejs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<div id="disqus_thread"></div>
2-
<script>
2+
<script nonce="<%= cspNonce %>">
33
var disqus_config = function () {
44
this.page.identifier = window.location.pathname.split('/').slice(-1)[0];
55
};
66
(function() {
77
var d = document, s = d.createElement('script');
8-
s.src = '//<%= disqus %>.disqus.com/embed.js';
8+
s.src = 'https://<%= disqus %>.disqus.com/embed.js';
99
s.setAttribute('data-timestamp', +new Date());
1010
(d.head || d.body).appendChild(s);
1111
})();
1212
</script>
1313
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
14-

public/views/shared/ga.ejs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% if(typeof GA !== 'undefined' && GA) { %>
2-
<script>
2+
<script nonce="<%= cspNonce %>">
33
(function (i, s, o, g, r, a, m) {
44
i['GoogleAnalyticsObject'] = r;
55
i[r] = i[r] || function () {
@@ -10,9 +10,9 @@
1010
a.async = 1;
1111
a.src = g;
1212
m.parentNode.insertBefore(a, m)
13-
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
13+
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
1414
1515
ga('create', '<%= GA %>', 'auto');
1616
ga('send', 'pageview');
1717
</script>
18-
<% } %>
18+
<% } %>

0 commit comments

Comments
 (0)