Skip to content

Commit

Permalink
Merge pull request #236 from bcgov/bugfix/apicsp
Browse files Browse the repository at this point in the history
Add CSP rules to support redocs rendering
  • Loading branch information
kyle1morel authored Dec 20, 2023
2 parents 3e47e16 + f5aabdb commit ace4c12
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 71 deletions.
13 changes: 1 addition & 12 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ const app = express();
app.use(compression());
app.use(cors(DEFAULTCORS));
app.use(express.urlencoded({ extended: true }));
app.use(
helmet({
contentSecurityPolicy: {
directives: {
'default-src': [
"'self'", // eslint-disable-line
new URL(config.get('keycloak.serverUrl')).origin
]
}
}
})
);
app.use(helmet());

// Skip if running tests
if (process.env.NODE_ENV !== 'test') {
Expand Down
116 changes: 58 additions & 58 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/src/docs/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const docs = {
</head>
<body>
<redoc spec-url='/api/${version}/docs/api-spec.yaml' />
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"></script>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"></script>
</body>
</html>`
};
Expand Down
12 changes: 12 additions & 0 deletions app/src/routes/v1/docs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const config = require('config');
const router = require('express').Router();
const { readFileSync } = require('fs');
const helmet = require('helmet');
const yaml = require('js-yaml');
const { join } = require('path');

Expand All @@ -16,6 +17,17 @@ function getSpec() {
return spec;
}

router.use(
helmet({
contentSecurityPolicy: {
directives: {
'img-src': ['data:', 'https://cdn.redoc.ly'],
'script-src': ['blob:', 'https://cdn.redoc.ly']
}
}
})
);

/** OpenAPI Docs */
router.get('/', (_req, res) => {
const docs = require('../../docs/docs');
Expand Down

0 comments on commit ace4c12

Please sign in to comment.