Skip to content

Commit 041bd4d

Browse files
committed
fix(self-host): strip trusted header on public routes, content-type for assets, remove broken batch shim
1 parent 2bd3afd commit 041bd4d

6 files changed

Lines changed: 20 additions & 22 deletions

File tree

Caddyfile

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,31 @@
2222

2323
handle /api/* {
2424
uri strip_prefix /api
25-
reverse_proxy backend:8787
25+
reverse_proxy backend:8787 {
26+
header_up X-Forwarded-Email ""
27+
header_up X-Forwarded-Name ""
28+
}
2629
}
2730

2831
handle /chat/* {
2932
uri strip_prefix /chat
30-
reverse_proxy chat:8788
33+
reverse_proxy chat:8788 {
34+
header_up X-Forwarded-Email ""
35+
header_up X-Forwarded-Name ""
36+
}
3137
}
3238

3339
handle /assets/* {
34-
reverse_proxy backend:8787
40+
reverse_proxy backend:8787 {
41+
header_up X-Forwarded-Email ""
42+
header_up X-Forwarded-Name ""
43+
}
3544
}
3645

3746
handle {
38-
reverse_proxy web:3000
47+
reverse_proxy web:3000 {
48+
header_up X-Forwarded-Email ""
49+
header_up X-Forwarded-Name ""
50+
}
3951
}
4052
}

backend/src/db/index.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
import { drizzle as drizzleD1 } from 'drizzle-orm/d1';
2-
import { drizzle as drizzleSqlite } from 'drizzle-orm/better-sqlite3';
32
import type { Env } from '../types';
43

54
type D1Db = ReturnType<typeof drizzleD1>;
65

7-
function withBatch(db: object): D1Db {
8-
if ('batch' in db) return db as D1Db;
9-
return Object.assign(db, {
10-
async batch(queries: unknown[]) {
11-
const results: unknown[] = [];
12-
for (const query of queries) results.push(await (query as { execute: () => Promise<unknown> }).execute());
13-
return results;
14-
},
15-
}) as D1Db;
16-
}
17-
186
export function createDb(env: Env): D1Db | null {
19-
if (env.SQLITE_DB) return withBatch(drizzleSqlite(env.SQLITE_DB));
207
if (env.DB) return drizzleD1(env.DB);
218
return null;
229
}

backend/src/lib/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function getRuntimeConfig(env: Env): RuntimeConfig {
3030
serviceName: parsed.SERVICE_NAME,
3131
commitSha: parsed.COMMIT_SHA,
3232
orderTimeZone: parsed.ORDER_TIME_ZONE,
33-
databaseMode: env.SQLITE_DB ? 'sqlite' : env.DB ? 'd1' : 'unconfigured',
33+
databaseMode: env.DB ? 'd1' : 'unconfigured',
3434
hasPublicMenuBucket: Boolean(env.PUBLIC_MENU_BUCKET),
3535
auth: {
3636
issuer: parsed.ACCESS_TEAM_DOMAIN,

backend/src/self-host/fs-bucket.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export function createFsBucket(root: string): R2Bucket {
6060
async get(key: string) {
6161
try {
6262
const data = await readFile(safePath(root, key));
63-
return object(root, key, data);
63+
const ext = key.slice(key.lastIndexOf('.')).toLowerCase();
64+
const contentType = ({ '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.webp': 'image/webp', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.json': 'application/json', '.pdf': 'application/pdf' } as Record<string, string>)[ext];
65+
return object(root, key, data, contentType);
6466
} catch {
6567
return null;
6668
}

backend/src/self-host/server.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const env: Env = {
2222
COMMIT_SHA: process.env.COMMIT_SHA || 'self-host',
2323
ORDER_TIME_ZONE: process.env.ORDER_TIME_ZONE || 'UTC',
2424
SELF_HOST_AUTH_HEADER: process.env.SELF_HOST_AUTH_HEADER || 'x-forwarded-email',
25-
SQLITE_DB: sqlite,
2625
DB: createD1Compat(sqlite),
2726
PUBLIC_MENU_BUCKET: createFsBucket(bucketDir),
2827
R2_PUBLIC_URL: process.env.R2_PUBLIC_URL || (process.env.PUBLIC_URL ? `${process.env.PUBLIC_URL.replace(/\/$/, '')}/assets` : ''),

backend/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { AuthUser } from './middleware/auth';
22
import type { StaffSession } from './lib/staff';
33
import type { createDb } from './db/index';
4-
import type { SqliteDatabase } from './self-host/sqlite';
54

65
export interface Env {
76
APP_ENV?: string;
@@ -11,7 +10,6 @@ export interface Env {
1110
ALLOWED_ORIGINS?: string;
1211
ALLOWED_HOST_SUFFIXES?: string;
1312
DB?: D1Database;
14-
SQLITE_DB?: SqliteDatabase;
1513
PUBLIC_MENU_BUCKET?: R2Bucket;
1614
R2_PUBLIC_URL?: string;
1715
ACCESS_TEAM_DOMAIN?: string;

0 commit comments

Comments
 (0)