From 017f05cbb9bc097b7ca7b05ab80983e41c05aae3 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 17:52:37 +0800 Subject: [PATCH] gcp-ai tests: the network guard lets another package's loopback fetch through MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gcp-ai's test/network.mjs (from #307) replaced globalThis.fetch with one that refuses every call. bun test runs every package in one process, so the guard outlived gcp-ai's files: with #384 merged on top, all eight Search1API tests that fetch their own 127.0.0.1 fixture failed with "Unexpected network fetch in test: http://127.0.0.1:…/search" in the repo-wide run, and passed alone. fetch now refuses only a non-loopback host, as the socket, http and dns hooks beside it already did. The real fetch stays inside the module, never on globalThis. Run (sandbox HOME under TMPDIR, HTTPS_PROXY at a dead port): - bun test packages/gcp-ai: 40 pass, 1 skip, 0 fail. - A script importing the guard: a Bun.serve loopback fetch answers "ok"; https://example.com is refused and the process exits 1. - bun test (whole repo) with this file over #384's merge: 2647 pass, 1 skip, 0 fail. Without it: 2638 pass, 9 fail (8 Search1API, plus one gcp-ai Chrome test that lost its CDP connection, which passed in the next run). - bun scripts/check.mjs: every package ok. --- packages/gcp-ai/test/network.mjs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/gcp-ai/test/network.mjs b/packages/gcp-ai/test/network.mjs index bbd28b8..a33c477 100644 --- a/packages/gcp-ai/test/network.mjs +++ b/packages/gcp-ai/test/network.mjs @@ -5,20 +5,30 @@ import net from "node:net" import tls from "node:tls" import { syncBuiltinESMExports } from "node:module" -// Importable by tests and as --import: never retain the real fetch for a test -// to restore. Record violations too, since production fallbacks catch errors. +// Importable by tests and as --import: the real fetch stays in this module, +// never on globalThis for a test to restore. Record violations too, since +// production fallbacks catch errors. const unexpected = [] function refuse(kind, target) { const message = `Unexpected network ${kind} in test: ${target}` unexpected.push(message) throw new Error(message) } -globalThis.fetch = (input) => refuse("fetch", input?.url ?? input) const local = (host) => { const value = String(host ?? "localhost").replace(/^\[|\]$/g, "").toLowerCase() return value === "localhost" || value === "::1" || /^127(?:\.\d{1,3}){3}$/.test(value) } +// bun test runs every package's files in one process, so this outlives +// gcp-ai's tests: another package's fetch to its own loopback server goes on. +const realFetch = globalThis.fetch +globalThis.fetch = (input, init) => { + const target = input?.url ?? input + let host + try { host = new URL(String(target)).hostname } catch {} + if (host === undefined || !local(host)) refuse("fetch", target) + return realFetch(input, init) +} function check(kind, host) { if (!local(host)) refuse(kind, host) }