|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | import express, { Request, Response, NextFunction } from 'express' |
| 11 | +import rateLimit from 'express-rate-limit' |
| 12 | +import helmet from 'helmet' |
11 | 13 | import { createServer } from 'http' |
12 | 14 | import { WebSocketServer, WebSocket } from 'ws' |
13 | 15 | import path from 'path' |
@@ -38,7 +40,36 @@ const snapshots = new Map<string, { name: string; elements: CanvasElement[]; cre |
38 | 40 |
|
39 | 41 | const app = express() |
40 | 42 | const httpServer = createServer(app) |
41 | | -const wss = new WebSocketServer({ server: httpServer, path: '/ws' }) |
| 43 | +const wss = new WebSocketServer({ |
| 44 | + server: httpServer, |
| 45 | + path: '/ws', |
| 46 | + maxPayload: 64 * 1024, |
| 47 | + verifyClient: (info, callback) => { |
| 48 | + const origin = info.origin || info.req.headers.origin |
| 49 | + const host = info.req.headers.host |
| 50 | + |
| 51 | + if (!origin && !host) { |
| 52 | + callback(true) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + if (origin) { |
| 57 | + try { |
| 58 | + const url = new URL(origin) |
| 59 | + const allowedHosts = ['localhost', '127.0.0.1', host?.split(':')[0]] |
| 60 | + if (!allowedHosts.includes(url.hostname)) { |
| 61 | + callback(false, 403, 'Forbidden: invalid origin') |
| 62 | + return |
| 63 | + } |
| 64 | + } catch { |
| 65 | + callback(false, 403, 'Forbidden: invalid origin') |
| 66 | + return |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + callback(true) |
| 71 | + }, |
| 72 | +}) |
42 | 73 |
|
43 | 74 | /** Active WebSocket clients */ |
44 | 75 | const wsClients = new Set<WebSocket>() |
@@ -134,11 +165,34 @@ function requestScreenshot(format: 'png' | 'svg', background: boolean): Promise< |
134 | 165 |
|
135 | 166 | // ─── Middleware ─────────────────────────────────────────────────────────────── |
136 | 167 |
|
137 | | -app.use(express.json()) |
| 168 | +app.use(helmet({ |
| 169 | + contentSecurityPolicy: false, |
| 170 | + crossOriginEmbedderPolicy: false, |
| 171 | +})) |
| 172 | + |
| 173 | +app.use(express.json({ limit: '10kb' })) |
| 174 | + |
| 175 | +const apiLimiter = rateLimit({ |
| 176 | + windowMs: 60 * 1000, |
| 177 | + limit: 60, |
| 178 | + standardHeaders: 'draft-8', |
| 179 | + legacyHeaders: false, |
| 180 | + message: { success: false, error: 'Too many requests, please try again later' }, |
| 181 | +}) |
| 182 | + |
| 183 | +app.use('/api/', apiLimiter) |
138 | 184 |
|
139 | 185 | // CORS — allow Vite dev server on :5173 during development |
140 | 186 | app.use((_req: Request, res: Response, next: NextFunction) => { |
141 | | - res.setHeader('Access-Control-Allow-Origin', '*') |
| 187 | + const origin = _req.headers.origin |
| 188 | + const allowedOrigins = ['http://localhost:5173', 'http://127.0.0.1:5173', 'http://localhost:3000', 'http://127.0.0.1:3000'] |
| 189 | + |
| 190 | + if (origin && allowedOrigins.includes(origin)) { |
| 191 | + res.setHeader('Access-Control-Allow-Origin', origin) |
| 192 | + } else if (!origin) { |
| 193 | + res.setHeader('Access-Control-Allow-Origin', '*') |
| 194 | + } |
| 195 | + |
142 | 196 | res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS') |
143 | 197 | res.setHeader('Access-Control-Allow-Headers', 'Content-Type') |
144 | 198 | next() |
|
0 commit comments