fix: reject malformed tlon sse event ids · openclaw/openclaw@8b180fe
steipete
·
2026-05-29
·
via Recent Commits to openclaw:main
File tree
extensions/tlon/src/urbit
| Original file line number | Diff line number | Diff line change |
|---|
@@ -182,6 +182,22 @@ describe("UrbitSSEClient", () => {
|
182 | 182 | ); |
183 | 183 | }); |
184 | 184 | |
| 185 | +it("ignores malformed event ids when deciding whether to ack", async () => { |
| 186 | +const mockUrbitFetch = vi.mocked(urbitFetch); |
| 187 | +mockUrbitFetch.mockResolvedValue({ |
| 188 | +response: { ok: true, status: 200 } as unknown as Response, |
| 189 | +finalUrl: "https://example.com", |
| 190 | +release: vi.fn().mockResolvedValue(undefined), |
| 191 | +}); |
| 192 | +const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123"); |
| 193 | + |
| 194 | +client.processEvent('id: 25abc\ndata: {"json":{"ok":true}}'); |
| 195 | +await Promise.resolve(); |
| 196 | + |
| 197 | +expect(mockUrbitFetch).not.toHaveBeenCalled(); |
| 198 | +expect((client as unknown as { lastHeardEventId: number }).lastHeardEventId).toBe(-1); |
| 199 | +}); |
| 200 | + |
185 | 201 | it("tracks lastHeardEventId and ackThreshold", () => { |
186 | 202 | const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123"); |
187 | 203 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,15 @@ function parseUrbitSsePayload(data: string): { id?: number; json?: unknown; resp
|
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
| 34 | +function parseUrbitSseEventId(value: string): number | null { |
| 35 | +const trimmed = value.trim(); |
| 36 | +if (!/^\d+$/.test(trimmed)) { |
| 37 | +return null; |
| 38 | +} |
| 39 | +const parsed = Number(trimmed); |
| 40 | +return Number.isSafeInteger(parsed) ? parsed : null; |
| 41 | +} |
| 42 | + |
34 | 43 | export class UrbitSSEClient { |
35 | 44 | url: string; |
36 | 45 | cookie: string; |
@@ -257,7 +266,7 @@ export class UrbitSSEClient {
|
257 | 266 | |
258 | 267 | for (const line of lines) { |
259 | 268 | if (line.startsWith("id: ")) { |
260 | | -eventId = Number.parseInt(line.slice(4), 10); |
| 269 | +eventId = parseUrbitSseEventId(line.slice(4)); |
261 | 270 | } |
262 | 271 | if (line.startsWith("data: ")) { |
263 | 272 | data = line.slice(6); |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。