Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): stop a null net.fetch init from leaking a fetch slot

Yudhi Armyndharisb6a9c6e87d7b
diff --git a/src/core/plugins/plugin-net.spec.ts b/src/core/plugins/plugin-net.spec.ts
index 3f3ff8f32..8ec3bff0d 100644
--- a/src/core/plugins/plugin-net.spec.ts
+++ b/src/core/plugins/plugin-net.spec.ts
@@ -122,6 +122,18 @@ describe('performPluginFetch', () => {
performPluginFetch('https://api.example.com/t', {}, { fetch: fakeSafeFetch(cannedResponse('{}', {}), sink) }),
).resolves.toMatchObject({ ok: true });
});
+
+ it('treats a null init as no options and never holds a concurrency slot for it', async () => {
+ // A sandboxed plugin can send `null` across the worker bridge; the `= {}` default only covers undefined.
+ const sink: { init?: RequestInit } = {};
+ const fetcher = fakeSafeFetch(cannedResponse('{}', {}), sink);
+ for (let i = 0; i < 17; i++) {
+ await expect(performPluginFetch('https://api.example.com/t', null, { fetch: fetcher })).resolves.toMatchObject({
+ ok: true,
+ });
+ }
+ expect(sink.init?.method).toBe('GET');
+ });
});
describe('effectiveNetAllow', () => {
diff --git a/src/core/plugins/plugin-net.ts b/src/core/plugins/plugin-net.ts
index 604cb19fb..f29ab4c34 100644
--- a/src/core/plugins/plugin-net.ts
+++ b/src/core/plugins/plugin-net.ts
@@ -91,10 +91,19 @@ export function isNetHostAllowed(allow: string[] | undefined, url: string): bool
*/
export async function performPluginFetch(
url: string,
- init: PluginNetRequestInit = {},
+ init: PluginNetRequestInit | null = {},
deps: { fetch?: typeof withSafeFetch } = {},
): Promise<PluginNetResponse> {
const safeFetch = deps.fetch ?? withSafeFetch;
+ // A worker can send `null` (the default only covers undefined). Everything that reads the options
+ // runs before the slot is reserved: a throw between the increment and the try would leak the slot.
+ const opts = init ?? {};
+ // Coerce a non-finite timeoutMs (a string/object/NaN from the untrusted worker) to the default
+ // instead of letting it flow through as NaN — `Math.max('abc', 1)` is NaN, and AbortSignal.timeout(NaN)
+ // throws a RangeError, silently defeating the documented default + hard-cap clamp.
+ const requested =
+ typeof opts.timeoutMs === 'number' && Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : DEFAULT_TIMEOUT_MS;
+ const timeoutMs = Math.min(Math.max(requested, 1), MAX_TIMEOUT_MS);
// Reject-when-full BEFORE reserving a slot, so total concurrent host-side buffering stays bounded to
// MAX_INFLIGHT_FETCHES × MAX_BODY_BYTES. Check + increment are synchronous (single event-loop turn),
// so no interleaving can overshoot the cap; the slot is released in the finally below.
@@ -102,20 +111,13 @@ export async function performPluginFetch(
throw new Error(`too many concurrent plugin net.fetch calls (max ${MAX_INFLIGHT_FETCHES}); retry shortly`);
}
inFlightFetches++;
- // Coerce a non-finite timeoutMs (a string/object/NaN from the untrusted worker) to the default
- // instead of letting it flow through as NaN — `Math.max('abc', 1)` is NaN, and AbortSignal.timeout(NaN)
- // throws a RangeError, silently defeating the documented default + hard-cap clamp.
- const requested =
- typeof init.timeoutMs === 'number' && Number.isFinite(init.timeoutMs) ? init.timeoutMs : DEFAULT_TIMEOUT_MS;
- const timeoutMs = Math.min(Math.max(requested, 1), MAX_TIMEOUT_MS);
-
try {
return await safeFetch<PluginNetResponse>(
url,
{
- method: init.method ?? 'GET',
- headers: init.headers,
- body: init.body,
+ method: opts.method ?? 'GET',
+ headers: opts.headers,
+ body: opts.body,
signal: AbortSignal.timeout(timeoutMs),
},
async response => {