Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions Framework/Backend/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
* @param {object} [connectIdConfig] - configuration of OpenID Connect
*/
constructor(httpConfig, jwtConfig, connectIdConfig = null) {
this.logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'framework'}/server`);

assert(httpConfig, 'Missing config');
assert(httpConfig.port, 'Missing config value: port');
httpConfig.tls = !httpConfig.tls ? false : httpConfig.tls;
Expand Down Expand Up @@ -89,7 +91,6 @@
* @value {object.lastLoggedTimestamp} - timestamp of the last log of the same error by the same key
*/
this._jwtErrorsByIp = new Map();
this.logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'framework'}/server`);
}

/**
Expand Down Expand Up @@ -142,14 +143,15 @@
/**
* Configures Helmet rules to increase web app security
* @param {object} config - configuration of HTTP server
* @param {string} config.hostname whitelisted hostname for websocket connection
* @param {number} config.port secure port number
* @param {list} config.iframeCsp list of URLs for frame-src CSP
* @param {boolean} config.allow allow unsafe-eval in CSP
* @param {boolean} config.allowIframeCsp allow blob: URLs in img-src (image previews/downloads)
* @param {list} [config.extraConnectSrc] sources appended to the default connect-src CSP
* @param {string} config.hostname - whitelisted hostname for websocket connection
* @param {number} config.port - secure port number
* @param {string[]} [config.iframeCsp] - list of URLs for frame-src CSP
* @param {boolean} [config.allow] - allow unsafe-eval in CSP
* @param {boolean} [config.allowBlobImages] - allow blob: URLs in img-src (image previews/downloads)
* @param {boolean} [config.allowIframeCsp] - Deprecated: Use `allowBlobImages` instead
* @param {string[]} [config.extraConnectSrc] - sources appended to the default connect-src CSP
*/
configureHelmet({ hostname, port, iframeCsp = [], allow = false, allowIframeCsp = false, extraConnectSrc = [] }) {
configureHelmet({ hostname, port, iframeCsp = [], allow = false, allowBlobImages, allowIframeCsp, extraConnectSrc = [] }) {
// Sets "X-Frame-Options: DENY" (doesn't allow to be in any iframe)
this.app.use(helmet.frameguard({ action: 'deny' }));
// Sets "Strict-Transport-Security: max-age=5184000 (60 days) (stick to HTTPS)
Expand All @@ -164,12 +166,18 @@
this.app.use(helmet.hidePoweredBy());
// Disable DNS prefetching
this.app.use(helmet.dnsPrefetchControl());

if (allowIframeCsp !== undefined) {
this.logger.warnMessage('http.allowIframeCsp is deprecated, use http.allowBlobImages');
}
const blobImages = allowBlobImages ?? allowIframeCsp ?? false;

// Disables external resources
this.app.use(helmet.contentSecurityPolicy({
directives: {
/* eslint-disable */
defaultSrc: ["'self'", "data:", hostname + ':*'],
...(allowIframeCsp && { imgSrc: ["'self'", "data:", "blob:"] }),
...(blobImages && { imgSrc: ["'self'", "data:", "blob:"] }),
scriptSrc: ["'self'", ...(allow ? ["'unsafe-eval'"] : [])],
styleSrc: ["'self'", "'unsafe-inline'"],
connectSrc: ["'self'", 'http://' + hostname + ':' + port, 'https://' + hostname, 'wss://' + hostname, 'ws://' + hostname + ':' + port,
Expand Down Expand Up @@ -237,7 +245,7 @@

/**
* Ensures important information is saved in an error.
* @param {*} err - The error to serialize.

Check warning on line 248 in Framework/Backend/http/server.js

View workflow job for this annotation

GitHub Actions / Test on windows-latest

Prefer a more specific type to `*`

Check warning on line 248 in Framework/Backend/http/server.js

View workflow job for this annotation

GitHub Actions / Tests & coverage on ubuntu-latest

Prefer a more specific type to `*`

Check warning on line 248 in Framework/Backend/http/server.js

View workflow job for this annotation

GitHub Actions / Tests on macos-latest

Prefer a more specific type to `*`
*/
function serializeError(err) {
if (err && typeof err === 'object') {
Expand Down
3 changes: 2 additions & 1 deletion Framework/docs/guide/http-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Where:
* [`LIMIT`] - controls the maximum request body size. Defaults to '100kb'.
* [`iframeCsp`] - list of URLs allowed in the `frame-src` CSP directive (default: `[]`)
* [`allow`] - adds `'unsafe-eval'` to `script-src` (default: `false`)
* [`allowIframeCsp`] - adds `blob:` to `img-src` so blob-based images can be previewed/downloaded; despite the name, unrelated to iframes (default: `false`)
* [`allowBlobImages`] - adds `blob:` to `img-src` so blob-based images can be previewed/downloaded (default: `false`)
* [`allowIframeCsp`] - **deprecated**, use `allowBlobImages`;
* [`extraConnectSrc`] - sources appended to the default `connect-src` CSP directive (default: `[]`)
* [`JWT_CONF`] - JWT module config, see [JWT module](json-tokens.md)
* [`OPENID_CONF`] - OpenID config, see [OpenID Connect module](openid.md)
Expand Down
Loading