Skip to content

Commit dad2fe6

Browse files
committed
Legger til csp
1 parent 3448ee1 commit dad2fe6

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

package-lock.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@navikt/aksel-icons": "5.15.1",
2424
"@navikt/ds-css": "5.15.1",
2525
"@navikt/ds-react": "5.15.1",
26+
"csp-header": "5.2.1",
2627
"lodash.debounce": "4.0.8",
2728
"react": "18.2.0",
2829
"react-dom": "18.2.0",

server/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"type": "module",
66
"scripts": {
77
"build": "esbuild src/server.ts --bundle --platform=node --packages=external --outfile=dist/server/server.cjs",
8-
"start": "node ./dist/server/server.cjs",
9-
"dev": "npm run build && concurrently \"npm run build -- --watch\" \"nodemon -w ./dist/server -w ../.env ./dist/server/server.cjs\""
8+
"start": "node -r dotenv/config ./dist/server/server.cjs dotenv_config_path=../.env",
9+
"nodemon-start": "nodemon -r dotenv/config -w ./dist/server -w ../.env ./dist/server/server.cjs dotenv_config_path=../.env",
10+
"dev": "npm run build && concurrently \"npm run build -- --watch\" \"npm run nodemon-start\""
1011
},
1112
"dependencies": {
1213
"@opensearch-project/opensearch": "2.5.0",

server/src/cms/CmsArchiveSite.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import mime from 'mime';
66
import { HtmlRenderer } from '../site/ssr/htmlRenderer';
77
import { transformQueryToContentSearchParams } from '../opensearch/queries/contentSearch';
88
import { CmsArchiveCategoriesService } from './CmsArchiveCategoriesService';
9+
import { cspMiddleware } from '../routing/csp';
910

1011
export type CmsArchiveSiteConfig = {
1112
name: string;
@@ -106,7 +107,7 @@ export class CmsArchiveSite {
106107
}
107108

108109
private setupSiteRoutes(router: Router, htmlRenderer: HtmlRenderer) {
109-
router.get('/:versionKey?', async (req, res) => {
110+
router.get('/:versionKey?', cspMiddleware, async (req, res) => {
110111
const rootCategories = this.cmsArchiveCategoriesService.getRootCategories();
111112

112113
const appContext = {
@@ -121,7 +122,7 @@ export class CmsArchiveSite {
121122
return res.send(html);
122123
});
123124

124-
router.get('/html/:versionKey', async (req, res) => {
125+
router.get('/html/:versionKey', cspMiddleware, async (req, res) => {
125126
const { versionKey } = req.params;
126127

127128
const version = await this.cmsArchiveContentService.getContentVersion(versionKey);

server/src/routing/csp.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { RequestHandler } from 'express';
2+
import { DATA, getCSP, SELF, UNSAFE_EVAL, UNSAFE_INLINE } from 'csp-header';
3+
4+
const HMR_HOST = 'localhost:24678';
5+
const NAV_CDN_HOST = 'https://cdn.nav.no';
6+
7+
const csp = getCSP({
8+
directives: {
9+
'default-src': [SELF, DATA],
10+
'script-src': [SELF, UNSAFE_INLINE, UNSAFE_EVAL],
11+
'script-src-elem': [SELF, UNSAFE_INLINE],
12+
'style-src': [SELF, UNSAFE_INLINE],
13+
'style-src-elem': [SELF, UNSAFE_INLINE],
14+
'font-src': [SELF, DATA, NAV_CDN_HOST],
15+
'connect-src': [
16+
SELF,
17+
...(process.env.NODE_ENV === 'development'
18+
? [`ws://${HMR_HOST}`, `http://${HMR_HOST}`]
19+
: []),
20+
],
21+
},
22+
});
23+
24+
export const cspMiddleware: RequestHandler = (req, res, next) => {
25+
res.setHeader('Content-Security-Policy', csp);
26+
next();
27+
};

server/src/server.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import dotenv from 'dotenv';
2-
3-
dotenv.config({ path: '../.env' });
4-
51
import express from 'express';
62
import compression from 'compression';
73
import { setupErrorHandlers } from './routing/errorHandlers';

0 commit comments

Comments
 (0)