Skip to content

Commit 685e7a3

Browse files
authored
Remove unsafe eval (#16704)
* Remove unsafe eval * Actually, we're not using this anyways * Reset package-lock, I have no idea why this keeps changing * Update csp.js * Update server.js
1 parent 87fb2ce commit 685e7a3

4 files changed

Lines changed: 25 additions & 6 deletions

File tree

javascripts/fake-hogan.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This module overrides "Hogan" that instantsearch.js uses
2+
// Hogan uses `new Function`,
3+
// so we can't use it with our content security policy.
4+
// Turns out, we use all our own templates anyway,
5+
// so we just have to shim out Hogan so it doesn't error!
6+
7+
export default {
8+
compile (template) {
9+
return {
10+
render (data) {
11+
return ''
12+
}
13+
}
14+
}
15+
}

middleware/csp.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ module.exports = contentSecurityPolicy({
3030
],
3131
scriptSrc: [
3232
"'self'",
33-
'data:',
34-
"'unsafe-eval'", // exception for Algolia instantsearch
35-
"'unsafe-inline'"
33+
'data:'
3634
],
3735
frameSrc: [ // exceptions for GraphQL Explorer
3836
'https://graphql-explorer.githubapp.com', // production env

tests/rendering/server.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ describe('server', () => {
5353
expect(csp.get('img-src').includes('octodex.github.com')).toBe(true)
5454

5555
expect(csp.get('script-src').includes("'self'")).toBe(true)
56-
expect(csp.get('script-src').includes("'unsafe-eval'")).toBe(true) // exception for Algolia instantsearch
57-
expect(csp.get('script-src').includes("'unsafe-inline'")).toBe(true)
5856

5957
expect(csp.get('style-src').includes("'self'")).toBe(true)
6058
expect(csp.get('style-src').includes("'unsafe-inline'")).toBe(true)

webpack.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const CopyWebpackPlugin = require('copy-webpack-plugin')
44
const { EnvironmentPlugin } = require('webpack')
55

66
module.exports = {
7+
devtool: 'source-map', // this prevents webpack from using eval
78
entry: './javascripts/index.js',
89
output: {
910
filename: 'index.js',
@@ -70,5 +71,12 @@ module.exports = {
7071
]
7172
}),
7273
new EnvironmentPlugin(['NODE_ENV'])
73-
]
74+
],
75+
resolve: {
76+
alias: {
77+
// Hogan uses `new Function` which breaks content security policy
78+
// Turns out, we aren't even using it anyways!
79+
'hogan.js': path.resolve(__dirname, 'javascripts/fake-hogan.js')
80+
}
81+
}
7482
}

0 commit comments

Comments
 (0)