Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing security headers #2362

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 23 additions & 2 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ import { GIMessage } from '~/shared/domains/chelonia/GIMessage.js'

const { CONTRACTS_VERSION, GI_VERSION } = process.env

const securityHeaders = {
// This is the most likely to break things now; it may need changing when sandboxing or federation is implemented.
'Content-Security-Policy': "default-src 'none'; script-src 'unsafe-eval'; script-src-elem 'self'; script-src-attr 'none'; style-src 'self'; style-src-elem 'self'; style-src-attr 'none'; img-src 'self' blob:; font-src 'self'; connect-src 'self'; media-src 'self'; prefetch-src 'self'; worker-src 'self'; frame-ancestors 'none'; form-action 'self'; upgrade-insecure-requests; manifest-src 'self'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause issues with the emoji images, since those are loaded externally. See #2334. Either img-src will need to be expanded, or the sheet be embedded locally (preferred solution).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is using img-src 'self' blob: https://unpkg.com OK for now? Otherwise, installing and redistributing emoji sheets via dist/assets seems a bit tricky regarding e.g. licensing issues.

Copy link
Member

@corrideat corrideat Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of solving the CSP issue, yes (although you need to check manually, it could interfere with Cross-Origin-Embedder-Policy as well).

The preferred (to me) solution would be embedding the sheet. There could be some licensing concerns, but I'm not sure those concerns are alleviated by hotlinking. This is best left as a question to a lawyer or to the copyright holder. (Sidenote: fonts designs generally aren't under copyright in the US, but I'm guessing emoji designs likely are, and even if they weren't, there's other places to consider).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative solution is to drop the Apple emojis and use an emoji set with a free license. E.g., https://github.com/mozilla/fxemoji

'X-Content-Type-Options': 'nosniff',
'Referrer-Policy': 'strict-origin-when-cross-origin',
// List generated using https://www.permissionspolicy.com.
'Permissions-Policy': 'accelerometer=(), ambient-light-sensor=(), autoplay=(), battery=(), camera=(), cross-origin-isolated=(self), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(self), execution-while-out-of-viewport=(self), fullscreen=(self), geolocation=(), gyroscope=(), keyboard-map=(), magnetometer=(), microphone=(), midi=(), navigation-override=(self), payment=(), picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(self), xr-spatial-tracking=(), clipboard-read=(), clipboard-write=(self), gamepad=(), speaker-selection=()',
// Don't share context with potentially untrusted sites.
'Cross-Origin-Opener-Policy': 'same-origin',
// Block loading the page in a frame.
'X-Frame-Options': 'deny',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be DENY (uppercase)

// Blocks embedding cross-origin resources that don't explicitly allow it.
'Cross-Origin-Embedder-Policy': 'require-corp',
// Block hotlinking.
'Cross-Origin-Resource-Policy': 'same-origin'
}

const hapi = new Hapi.Server({
// debug: false, // <- Hapi v16 was outputing too many unnecessary debug statements
// // v17 doesn't seem to do this anymore so I've re-enabled the logging
Expand Down Expand Up @@ -52,9 +69,13 @@ hapi.ext({
// but custom headers can be manually added using `.output.headers`.
// See https://hapi.dev/module/boom/api/.
if (typeof request.response.header === 'function') {
request.response.header('X-Frame-Options', 'deny')
for (const [name, value] of Object.entries(securityHeaders)) {
request.response.header(name, value)
}
} else {
request.response.output.headers['X-Frame-Options'] = 'deny'
for (const [name, value] of Object.entries(securityHeaders)) {
request.response.output.headers[name] = value
}
}
} catch (err) {
console.warn(chalk.yellow('[backend] Could not set X-Frame-Options header:', err.message))
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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