Skip to content

Commit

Permalink
feat: add secure middleware, move CSP to header (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz authored Aug 12, 2024
1 parent 7135040 commit 49a1d52
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 0 additions & 4 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
<link rel="apple-touch-icon" href="/icon-512.png" />
<meta name="theme-color" content="#000000" />
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; img-src 'self' https://uploads.getalby-assets.com https://getalby.com;"
/>
</head>
<body class="min-h-screen">
<div id="root" class="min-h-screen"></div>
Expand Down
16 changes: 10 additions & 6 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default defineConfig(({ command }) => ({
globPatterns: ["**/*.{js,css,html,png,svg,ico}"],
},
}),
...(command === "serve" ? [insertDevCSPNoncePlugin] : []),
...(command === "serve" ? [insertDevCSPPlugin] : []),
],
server: {
proxy: {
Expand All @@ -64,19 +64,23 @@ export default defineConfig(({ command }) => ({
html:
command === "serve"
? {
cspNonce: "PLACEHOLDER",
cspNonce: "DEVELOPMENT",
}
: undefined,
}));

const insertDevCSPNoncePlugin: Plugin = {
name: "transform-html",
const DEVELOPMENT_NONCE = "'nonce-DEVELOPMENT'";

const insertDevCSPPlugin: Plugin = {
name: "dev-csp",
transformIndexHtml: {
enforce: "pre",
transform(html) {
return html.replace(
"default-src 'self'",
"default-src 'self' 'nonce-PLACEHOLDER'"
"<head>",
`<head>
<!-- DEV-ONLY CSP - when making changes here, also update the CSP header in http_service.go (without the nonce!) -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' ${DEVELOPMENT_NONCE}; img-src 'self' https://uploads.getalby-assets.com https://getalby.com;"/>`
);
},
},
Expand Down
6 changes: 6 additions & 0 deletions http/http_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func NewHttpService(svc service.Service, eventPublisher events.EventPublisher) *
func (httpSvc *HttpService) RegisterSharedRoutes(e *echo.Echo) {
e.HideBanner = true

e.Use(middleware.SecureWithConfig(middleware.SecureConfig{
ContentTypeNosniff: "nosniff",
XFrameOptions: "DENY",
ContentSecurityPolicy: "default-src 'self'; img-src 'self' https://uploads.getalby-assets.com https://getalby.com;",
ReferrerPolicy: "no-referrer",
}))
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogStatus: true,
Expand Down

0 comments on commit 49a1d52

Please sign in to comment.