()
| 5 | import router from '../routes' |
| 6 | |
| 7 | export const createWebApp = (): Express => { |
| 8 | const app = express() |
| 9 | app |
| 10 | .disable('x-powered-by') |
| 11 | .use((req, res, next) => { |
| 12 | const settings = createSettings() |
| 13 | const nonce = randomBytes(16).toString('base64') |
| 14 | res.locals.nonce = nonce |
| 15 | |
| 16 | const relayUrl = new URL(settings.info.relay_url) |
| 17 | const webRelayUrl = new URL(relayUrl.toString()) |
| 18 | webRelayUrl.protocol = relayUrl.protocol === 'wss:' ? 'https:' : ':' |
| 19 | |
| 20 | const directives = { |
| 21 | 'img-src': ["'self'", 'data:', 'https://cdn.zebedee.io/an/nostr/'], |
| 22 | 'connect-src': ["'self'", settings.info.relay_url as string, webRelayUrl.toString()], |
| 23 | 'default-src': ["'self'"], |
| 24 | 'script-src-attr': [`'nonce-${nonce}'`], |
| 25 | 'script-src': [ |
| 26 | "'self'", |
| 27 | `'nonce-${nonce}'`, |
| 28 | 'https://cdn.jsdelivr.net/npm/', |
| 29 | 'https://unpkg.com/', |
| 30 | 'https://cdnjs.cloudflare.com/ajax/libs/', |
| 31 | ], |
| 32 | 'style-src': ["'self'", 'https://cdn.jsdelivr.net/npm/'], |
| 33 | 'font-src': ["'self'", 'https://cdn.jsdelivr.net/npm/'], |
| 34 | } |
| 35 | |
| 36 | const csp = Object.entries(directives) |
| 37 | .map(([key, values]) => `${key} ${values.join(' ')}`) |
| 38 | .join('; ') |
| 39 | res.setHeader('Content-Security-Policy', csp) |
| 40 | return next() |
| 41 | }) |
| 42 | .use('/favicon.ico', express.static('./resources/favicon.ico')) |
| 43 | .use('/css', express.static('./resources/css')) |
| 44 | |
| 45 | app.use(router) |
| 46 | |
| 47 | return app |
| 48 | } |
no test coverage detected