11import express from 'express'
22import helmet from 'helmet'
3+ import { randomBytes } from 'crypto'
34
45import { createSettings } from './settings-factory'
56import router from '../routes'
@@ -10,20 +11,19 @@ export const createWebApp = () => {
1011 . disable ( 'x-powered-by' )
1112 . use ( ( req , res , next ) => {
1213 const settings = createSettings ( )
14+ const nonce = randomBytes ( 16 ) . toString ( 'base64' )
15+ res . locals . nonce = nonce
1316
1417 const relayUrl = new URL ( settings . info . relay_url )
1518 const webRelayUrl = new URL ( relayUrl . toString ( ) )
1619 webRelayUrl . protocol = ( relayUrl . protocol === 'wss:' ) ? 'https:' : ':'
1720
1821 const directives = {
19- /**
20- * TODO: Remove 'unsafe-inline'
21- */
2222 'img-src' : [ "'self'" , 'data:' , 'https://cdn.zebedee.io/an/nostr/' ] ,
2323 'connect-src' : [ "'self'" , settings . info . relay_url as string , webRelayUrl . toString ( ) ] ,
2424 'default-src' : [ "'self'" ] ,
25- 'script-src-attr' : [ "'unsafe-inline'" ] ,
26- 'script-src' : [ "'self'" , "'unsafe-inline'" , 'https://cdn.jsdelivr.net/npm/' , 'https://unpkg.com/' , 'https://cdnjs.cloudflare.com/ajax/libs/' ] ,
25+ 'script-src-attr' : [ `'nonce- ${ nonce } '` ] ,
26+ 'script-src' : [ "'self'" , `'nonce- ${ nonce } '` , 'https://cdn.jsdelivr.net/npm/' , 'https://unpkg.com/' , 'https://cdnjs.cloudflare.com/ajax/libs/' ] ,
2727 'style-src' : [ "'self'" , 'https://cdn.jsdelivr.net/npm/' ] ,
2828 'font-src' : [ "'self'" , 'https://cdn.jsdelivr.net/npm/' ] ,
2929 }
@@ -32,7 +32,8 @@ export const createWebApp = () => {
3232 } )
3333 . use ( '/favicon.ico' , express . static ( './resources/favicon.ico' ) )
3434 . use ( '/css' , express . static ( './resources/css' ) )
35- . use ( router )
35+
36+ app . use ( router )
3637
3738 return app
3839}
0 commit comments