惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
I
InfoQ
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
量子位
博客园 - 叶小钗
月光博客
月光博客
IT之家
IT之家
G
Google Developers Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(discord): bound REST response body to prevent OOM flo...
Alix-007 · 2026-06-28 · via Recent Commits to openclaw:main
Original file line numberDiff line numberDiff line change

@@ -692,6 +692,78 @@ describe("RequestClient", () => {

692692

expect(metrics.invalidRequestCountByStatus).toEqual({ 403: 1 });

693693

});

694694
695+

it("bounds oversized REST response bodies instead of buffering them unbounded", async () => {

696+

const encoder = new TextEncoder();

697+

let pullCount = 0;

698+

let cancelCount = 0;

699+

const fetchSpy = vi.fn(

700+

async () =>

701+

new Response(

702+

new ReadableStream<Uint8Array>({

703+

pull(controller) {

704+

pullCount += 1;

705+

// Flood far past the cap so an unbounded reader would OOM.

706+

controller.enqueue(encoder.encode("x".repeat(4 * 1024 * 1024)));

707+

},

708+

cancel() {

709+

cancelCount += 1;

710+

},

711+

}),

712+

{ status: 200 },

713+

),

714+

);

715+

const client = new RequestClient("test-token", { fetch: fetchSpy, queueRequests: false });

716+
717+

await expect(client.get("/channels/c1/messages")).rejects.toThrow(

718+

/Discord REST response body exceeds 8388608 bytes/,

719+

);

720+

// The reader was cancelled at the cap rather than draining the whole flood:

721+

// only a handful of 4 MiB chunks are pulled before the cap is hit.

722+

expect(cancelCount).toBe(1);

723+

expect(pullCount).toBeLessThanOrEqual(4);

724+

});

725+
726+

it("aborts stalled REST response bodies after the idle timeout", async () => {

727+

const encoder = new TextEncoder();

728+

let cancelReason: unknown;

729+

const fetchSpy = vi.fn(

730+

async () =>

731+

new Response(

732+

new ReadableStream<Uint8Array>({

733+

start(controller) {

734+

// Emit a partial chunk, then stall forever so the idle timeout

735+

// (request timeout) must fire and cancel the stream.

736+

controller.enqueue(encoder.encode("partial payload"));

737+

},

738+

cancel(reason) {

739+

cancelReason = reason;

740+

},

741+

}),

742+

{ status: 200 },

743+

),

744+

);

745+

const client = new RequestClient("test-token", {

746+

fetch: fetchSpy,

747+

queueRequests: false,

748+

timeout: 50,

749+

});

750+
751+

await expect(client.get("/channels/c1/messages")).rejects.toThrow(

752+

"Discord REST response stalled: no data received for 50ms",

753+

);

754+

expect(cancelReason).toBeInstanceOf(Error);

755+

expect((cancelReason as Error).message).toBe(

756+

"Discord REST response stalled: no data received for 50ms",

757+

);

758+

});

759+
760+

it("still parses normal-sized REST response payloads under the cap", async () => {

761+

const fetchSpy = vi.fn(async () => createJsonResponse({ id: "channel", name: "general" }));

762+

const client = new RequestClient("test-token", { fetch: fetchSpy, queueRequests: false });

763+
764+

await expect(client.get("/channels/c1")).resolves.toEqual({ id: "channel", name: "general" });

765+

});

766+
695767

it("serializes message multipart uploads with payload_json", () => {

696768

const headers = new Headers();

697769

const body = serializeRequestBody(

Original file line numberDiff line numberDiff line change

@@ -6,6 +6,7 @@ import {

66

parseFiniteNumber,

77

resolveTimerTimeoutMs,

88

} from "openclaw/plugin-sdk/number-runtime";

9+

import { readResponseWithLimit } from "openclaw/plugin-sdk/response-limit-runtime";

910

import { serializeRequestBody } from "./rest-body.js";

1011

import {

1112

DiscordError,

@@ -89,6 +90,24 @@ const defaultLaneOptions: Record<RestRequestPriority, { staleAfterMs?: number; w

8990

background: { staleAfterMs: 20_000, weight: 1 },

9091

};

9192
93+

// Cap the REST response body well above any legitimate Discord JSON payload

94+

// (bulk message/member fetches stay in the low hundreds of KB) so a controlled

95+

// or hijacked endpoint cannot flood the body into an unbounded buffer (OOM).

96+

const DISCORD_REST_RESPONSE_BODY_MAX_BYTES = 8 * 1024 * 1024;

97+
98+

async function readResponseBodyText(response: Response, idleTimeoutMs: number): Promise<string> {

99+

const buffer = await readResponseWithLimit(response, DISCORD_REST_RESPONSE_BODY_MAX_BYTES, {

100+

chunkTimeoutMs: idleTimeoutMs,

101+

onOverflow: ({ size }) =>

102+

new Error(

103+

`Discord REST response body exceeds ${DISCORD_REST_RESPONSE_BODY_MAX_BYTES} bytes (received ${size})`,

104+

),

105+

onIdleTimeout: ({ chunkTimeoutMs }) =>

106+

new Error(`Discord REST response stalled: no data received for ${chunkTimeoutMs}ms`),

107+

});

108+

return buffer.toString("utf8");

109+

}

110+
92111

function coerceResponseBody(raw: string): unknown {

93112

if (!raw) {

94113

return undefined;

@@ -249,7 +268,7 @@ export class RequestClient {

249268

body: await normalizeFetchBody(body, headers),

250269

signal,

251270

});

252-

const text = await response.text();

271+

const text = await readResponseBodyText(response, this.options.timeout ?? 15_000);

253272

const parsed = coerceResponseBody(text);

254273

this.scheduler.recordResponse(routeKey, path, response, parsed);

255274

if (response.status === 204) {