XWX-AI commited on
Commit
4574c25
·
1 Parent(s): 2c58405

fix(server): 413 entity.too.large干净JSON响应+content-length盲区观测(PostHog 09-09韩国413=HTML错误页暴露) + requestTimeout=0根除Grok慢上传408(09-06,渲染超时已有应用层动态上限兜底,不动server.timeout防大文件渲染回退) + Claude Imagine widget无前缀设计令牌补齐(SVG var(--text-warning)等消失根因)

Browse files
Files changed (2) hide show
  1. server.js +34 -1
  2. templates/widget-render.html +42 -1
server.js CHANGED
@@ -1815,13 +1815,46 @@ app.post('/api/render_charts', async (req, res) => {
1815
  // 会抛 entity.parse.failed 错误。外部探针/扫描器会定期向公开接口发送这类
1816
  // 非法 JSON。若不兜底,Express 默认错误处理器会把完整堆栈打到 stdout
1817
  // (刷屏 HF LOG)并在响应中暴露技术栈。这里返回干净的 400,不打印堆栈。
 
 
 
 
 
 
 
1818
  app.use((err, req, res, next) => {
1819
  if (err && err.type === 'entity.parse.failed') {
1820
  return res.status(400).json({ error: 'Invalid JSON body' });
1821
  }
 
 
 
 
 
 
1822
  next(err);
1823
  });
1824
 
1825
- app.listen(port, () => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1826
  console.log(`Server listening at http://localhost:${port}`);
1827
  });
 
 
 
 
1815
  // 会抛 entity.parse.failed 错误。外部探针/扫描器会定期向公开接口发送这类
1816
  // 非法 JSON。若不兜底,Express 默认错误处理器会把完整堆栈打到 stdout
1817
  // (刷屏 HF LOG)并在响应中暴露技术栈。这里返回干净的 400,不打印堆栈。
1818
+ //
1819
+ // entity.too.large(413)同理:超过 express.json({ limit: '50mb' }) 的请求
1820
+ // 会被 body-parser 在读取阶段拒绝,同样走 Express 默认错误处理器 → 堆栈
1821
+ // 刷屏 + HTML 错误页暴露 /app/node_modules 路径(PostHog 2026-09-09 韩国用户
1822
+ // 的 413 事件 errmsg 即为该 HTML 页;HF LOG 6 天出现 15 次堆栈)。
1823
+ // 这里返回干净的 413 JSON,并记录 content-length(此前 413 在到达路由前就被
1824
+ // 拒绝,服务端日志完全不知道是谁/多大的请求——补上这条盲区观测)。
1825
  app.use((err, req, res, next) => {
1826
  if (err && err.type === 'entity.parse.failed') {
1827
  return res.status(400).json({ error: 'Invalid JSON body' });
1828
  }
1829
+ if (err && err.type === 'entity.too.large') {
1830
+ const len = req.headers['content-length'];
1831
+ const lenMB = len ? (Number(len) / (1024 * 1024)).toFixed(1) + 'MB' : 'unknown';
1832
+ console.warn(`[HTTP-413] ${req.method} ${req.path} content-length=${lenMB} (limit 50MiB)`);
1833
+ return res.status(413).json({ error: 'payload_too_large', limit_bytes: 50 * 1024 * 1024 });
1834
+ }
1835
  next(err);
1836
  });
1837
 
1838
+ // ─── Server-level timeouts (PostHog 2026-09-06 Grok 408 root cause) ─────────
1839
+ // Node >= 18 defaults server.requestTimeout to 300s. It covers RECEIVING the
1840
+ // request, not just processing it: a slow mobile uplink uploading a multi-MB
1841
+ // base64 JSON payload gets "HTTP/1.1 408 Request Timeout, Connection: close"
1842
+ // (empty body) at ~5:20 even though rendering never started. Reproduced in
1843
+ // temp/test/2026-09-06/ (local Docker + prod hf.space, slow-drip POST → 408).
1844
+ //
1845
+ // Fix: disable the protocol-level receive deadline — rendering duration is
1846
+ // already bounded per-request by the app-layer dynamic timeouts (setContent /
1847
+ // waitForNetworkIdle / pdfTimeout, see /api/generate_pdf), so a hung request
1848
+ // cannot leak resources.
1849
+ //
1850
+ // ⚠️ Do NOT set server.timeout (socket inactivity): during a long render the
1851
+ // request is fully received and NO bytes flow on the socket for minutes
1852
+ // (measured: 7MB HTML renders in >1183s, memo 24) — an idle timeout would
1853
+ // kill those connections and regress large-file rendering. Half-open sockets
1854
+ // (client vanished mid-upload) are reaped by the TCP stack / HF ingress.
1855
+ const server = app.listen(port, () => {
1856
  console.log(`Server listening at http://localhost:${port}`);
1857
  });
1858
+ server.requestTimeout = 0; // no 5-min cap on slow body uploads (408 fix)
1859
+ server.headersTimeout = 60000; // headers must still arrive promptly (Node default, explicit)
1860
+ server.keepAliveTimeout = 65000; // idle-after-response: > proxy idle norms; long renders unaffected (active request)
templates/widget-render.html CHANGED
@@ -38,7 +38,48 @@ svg .box { fill: #f9f9f9; stroke: #e5e5e5; }
38
  stroke on text causes fill+stroke double rendering → looks like bold. */
39
  svg text { stroke: none !important; }
40
 
41
- /* Claude CSS variable fallbacks */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  :root {
43
  --color-text-primary: #212121;
44
  --color-text-secondary: #6b7280;
 
38
  stroke on text causes fill+stroke double rendering → looks like bold. */
39
  svg text { stroke: none !important; }
40
 
41
+ /* Claude "Imagine" widget design tokens (unprefixed scheme).
42
+ Widgets reference var(--text-warning), var(--text-secondary), var(--surface-1),
43
+ etc. — normally provided by claude.ai via tokens.vanilla.css. Without them,
44
+ stroke="var(--text-warning)" computes to none → SVG lines disappear. */
45
+ :root {
46
+ --surface-0: #ffffff;
47
+ --surface-1: #fcfcfb;
48
+ --surface-2: #ffffff;
49
+ --text-primary: #0b0b0b;
50
+ --text-secondary: #52514e;
51
+ --text-muted: #898781;
52
+ --text-accent: #2a78d6;
53
+ --text-info: #3266ad;
54
+ --text-success: #265b19;
55
+ --text-warning: #5a4815;
56
+ --text-danger: #7f2c28;
57
+ --bg-accent: #d6e4f6;
58
+ --bg-info: #d6e4f6;
59
+ --bg-success: #e9f1dc;
60
+ --bg-warning: #f6eedf;
61
+ --bg-danger: #f7ecec;
62
+ --border: rgba(11, 11, 11, 0.10);
63
+ --border-strong: rgba(31, 30, 29, 0.3);
64
+ --border-stronger: rgba(31, 30, 29, 0.4);
65
+ --border-accent: #4682d5;
66
+ --border-info: #4682d5;
67
+ --border-success: #437426;
68
+ --border-warning: #805c1f;
69
+ --border-danger: #a73d39;
70
+ --radius: 8px;
71
+ --pad-sm: 8px;
72
+ --pad-md: 12px;
73
+ --pad-lg: 16px;
74
+ --pad-xl: 24px;
75
+ --gap-xs: 4px;
76
+ --gap-sm: 8px;
77
+ --gap-md: 12px;
78
+ --gap-lg: 16px;
79
+ --gap-xl: 24px;
80
+ }
81
+
82
+ /* Claude CSS variable fallbacks (prefixed scheme) */
83
  :root {
84
  --color-text-primary: #212121;
85
  --color-text-secondary: #6b7280;