Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/fix-web-image-csp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

web: Fix uploaded and persisted images failing to display on non-loopback server connections.
8 changes: 5 additions & 3 deletions packages/kap-server/src/middleware/securityHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* Headers:
* - `X-Content-Type-Options: nosniff` — stop MIME sniffing.
* - `Referrer-Policy: no-referrer` — never leak the URL to third parties.
* - `Content-Security-Policy: default-src 'self'` — the bundled Web UI is
* same-origin, so `'self'` covers it; tighten later if needed.
* - `Content-Security-Policy` — the bundled Web UI is same-origin. Images
* additionally allow `data:` for persisted base64 content and `blob:` for
* local attachment previews and authenticated media responses.
* - `Strict-Transport-Security` — ONLY when `opts.tls === true`. In this
* phase TLS is terminated by a reverse proxy (Caddy/nginx), so `start.ts`
* passes `tls: false` and HSTS is omitted here; the proxy is responsible
Expand All @@ -25,6 +26,7 @@ export interface SecurityHeadersOptions {
}

const HSTS_VALUE = 'max-age=31536000';
const CONTENT_SECURITY_POLICY = "default-src 'self'; img-src 'self' data: blob:";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Allow blob media sources for video previews

On non-loopback binds this relaxes only img-src; media-src is still absent and therefore falls back to default-src 'self' (MDN), so blob: URLs remain blocked for <video>/<audio>. The web UI creates blob URLs for authenticated uploaded videos in AuthMedia.vue and for local video attachment previews in Composer.vue, so users viewing video attachments over the exposed server still get broken previews unless this policy also includes media-src 'self' data: blob:.

Useful? React with 👍 / 👎.


/**
* Build the `onSend` hook. Returns the payload unchanged so Fastify continues
Expand All @@ -36,7 +38,7 @@ export function createSecurityHeadersHook(
return async (_req, reply, payload) => {
reply.header('X-Content-Type-Options', 'nosniff');
reply.header('Referrer-Policy', 'no-referrer');
reply.header('Content-Security-Policy', "default-src 'self'");
reply.header('Content-Security-Policy', CONTENT_SECURITY_POLICY);
if (opts.tls === true) {
reply.header('Strict-Transport-Security', HSTS_VALUE);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/kap-server/test/hostExposure.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ describe('real password path (verifyPassword)', () => {
});
expect(res.status).toBe(200);
expect(res.headers.get('x-content-type-options')).toBe('nosniff');
expect(res.headers.get('content-security-policy')).toBe("default-src 'self'");
expect(res.headers.get('content-security-policy')).toBe(
"default-src 'self'; img-src 'self' data: blob:",
);
});

it('accepts the persistent token on a public bind', async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/kap-server/test/securityExposure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ describe('server-v2 exposure hardening hooks', () => {
expect(res.statusCode).toBe(200);
expect(res.headers['x-content-type-options']).toBe('nosniff');
expect(res.headers['referrer-policy']).toBe('no-referrer');
expect(res.headers['content-security-policy']).toBe("default-src 'self'");
expect(res.headers['content-security-policy']).toBe(
"default-src 'self'; img-src 'self' data: blob:",
);
expect(res.headers['strict-transport-security']).toBeUndefined();
});

Expand Down
Loading