Skip to content

everything: question — a non-numeric GZIP_MAX_FETCH_SIZE silently disables the documented cap (failing test attached) #5642

Done

Description

@reservoir-cmyk

docs/features.md documents GZIP_MAX_FETCH_SIZE (default 10 MiB) as the cap on what gzip-file-as-resource will fetch. The module parses the variable with Number(...) at load time (tools/gzip-file-as-resource.ts:40-43), so a non-numeric value becomes NaN — and since every comparison against NaN is false, the cap silently stops applying and an over-cap fetch succeeds. There is no attacker path here (the value is the operator's own environment), so this is a documentation-versus-code question rather than a vulnerability report: the attached test pins the documented behaviour, and it fails on the current code. That failure is the question.

The question

The size cap documented for GZIP_MAX_FETCH_SIZE silently stops applying when the variable holds a non-numeric value: the module parses it with Number(...), the result is NaN, and every comparison against NaN is false, so an over-cap fetch succeeds. The attached test pins the documented behavior and fails on the current code. Is the intended behavior (a) fall back to the default on an unparseable value, (b) fail at startup, or (c) treat the variable as operator's-responsibility and document that invalid values disable the cap? The same Number(...) pattern parses GZIP_MAX_FETCH_TIME_MILLIS next to it. Happy to adjust the test to whichever behavior is intended.

Proposed test

As a new file beside src/everything/__tests__/gzip-limits.test.ts (the variable is read at module load, so the test needs its own file, in the same env-stubbing style). The first test is a control — a small fetch still succeeds under the same env. The second test is the one that fails:

/**
 * The documented-cap question, as a test. docs/features.md documents
 * GZIP_MAX_FETCH_SIZE (default 10 MiB) as the cap on what
 * gzip-file-as-resource will fetch.
 * The tool parses the variable with Number(...) at module load
 * (tools/gzip-file-as-resource.ts:40-43), so a non-numeric value becomes
 * NaN and every size comparison against it is false: the cap silently
 * stops applying. This file sets the variable to a non-numeric value
 * before importing the server (vitest gives each test file its own
 * module graph) and asserts the DOCUMENTED behavior — an over-cap fetch
 * is refused. On the pinned code the second test FAILS, which is the
 * point: it is the question, in executable form. The fixture is on
 * loopback, so it also sets GZIP_ALLOW_PRIVATE_IPS.
 */
import { createServer as createHttpServer, type Server } from "node:http";
import { once } from "node:events";
import {
  afterAll,
  afterEach,
  beforeAll,
  describe,
  expect,
  it,
  vi,
} from "vitest";
import type { Session } from "./harness.js";

const TEN_MB = 10 * 1024 * 1024;

let harness: typeof import("./harness.js");
let http: Server;
let base: string;

beforeAll(async () => {
  vi.stubEnv("GZIP_MAX_FETCH_SIZE", "not-a-number");
  vi.stubEnv("GZIP_ALLOW_PRIVATE_IPS", "1");
  harness = await import("./harness.js");

  http = createHttpServer((req, res) => {
    if (req.url === "/big") {
      res.writeHead(200, { "content-length": String(TEN_MB + 1) });
      res.end(Buffer.alloc(TEN_MB + 1, 0x61));
      return;
    }
    res.end("small");
  });
  http.listen(0);
  await once(http, "listening");
  const address = http.address();
  if (address === null || typeof address === "string")
    throw new Error("no port");
  base = `http://127.0.0.1:${address.port}`;
});

afterAll(async () => {
  http.closeAllConnections();
  await new Promise((resolve) => http.close(resolve));
});

const sessions: Session[] = [];

afterEach(async () => {
  await Promise.all(sessions.splice(0).map((s) => s.close()));
});

async function open(): Promise<Session> {
  const s = await harness.connect();
  sessions.push(s);
  return s;
}

describe("gzip-file-as-resource with a non-numeric GZIP_MAX_FETCH_SIZE", () => {
  it("still fetches a small file (the tool itself works)", async () => {
    const s = await open();
    const result = await s.client.callTool({
      name: "gzip-file-as-resource",
      arguments: { name: "small.txt.gz", data: `${base}/small` },
    });
    expect(result.isError).toBeFalsy();
  });

  it("refuses a fetch larger than the documented 10 MiB cap", async () => {
    const s = await open();
    const result = await s.client.callTool({
      name: "gzip-file-as-resource",
      arguments: { name: "big.bin.gz", data: `${base}/big` },
    });
    // Documented expectation: the cap applies, so this is a tool error.
    // Pinned behavior (2026-11-11): the fetch SUCCEEDS — the non-numeric
    // value disabled the cap. This test is the question attached to the
    // report, not a passing pin.
    expect(result.isError).toBe(true);
  });
});

Verification

Ran against a byte-identical copy of v2/main @ 6cfff62 (Node 24, vitest 4.1.12): 1 passed (the small-fetch control) / 1 failed (the over-cap fetch succeeded instead of being refused). The failure is the question in executable form, not a broken run — it fails by succeeding. Full everything suite in the same tree: 481 passed, with this test the only failure. Happy to adjust the test to whichever behaviour is intended.

Prompt used

"In src/everything, the gzip fetch caps come from environment variables parsed once at module load. Write a test that starts the server (in the style of tests/gzip-limits.test.ts, which stubs env before importing the harness) with GZIP_MAX_FETCH_SIZE set to a non-numeric value, serves a body whose Content-Length is one byte over the documented 10 MiB default from a loopback fixture (with GZIP_ALLOW_PRIVATE_IPS=1), and asserts the fetch is refused — the behavior the docs describe. Add a control showing a small fetch still succeeds under the same env. The point of the test is the question it asks; on the current code the refusal assertion fails."

Activity

  1. A7mdMe commented on Nov 22, 2026

    @A7mdMe

    This is a great finding and question. The test suite clearly demonstrates the silent failure mode.

    In production MCP deployments and containerized multi-agent environments, Option (a) with a startup/load diagnostic warning is almost universally preferred over (b) or (c) for the following reasons:

    Architectural Evaluation of the Options

    1. Why not (c) (Treat as operator-responsibility / silent disable)?

      • In cloud and orchestrator configurations, environment variables are frequently set via Helm charts, Docker Compose, or .env files where unexpanded template variables (e.g. "${MAX_FETCH_SIZE}"), empty strings (""), or intuitive human-readable values like "10MB" or "30s" occur.
      • Because all relational comparisons against NaN in JavaScript (payloadSize > NaN and payloadSize < NaN) evaluate to false, the protection cap is silently and completely removed. A memory-safety cap should never fail-open due to a parsing mistake.
    2. Why not (b) (Hard crash at startup)?

      • While failing fast is clean in theory, a container crash-loop on startup due to a minor syntax quirk (e.g. trailing whitespace or an unrecognized string) can prevent other essential, non-gzip tools in the same server process from being served to the AI client.
    3. Why (a) (Fail-closed fallback to documented default + stderr warning) is optimal:

      • It maintains fail-closed security invariants: the server process guarantees that unbounded memory allocation cannot occur regardless of malformed configuration.
      • It preserves server availability.
      • Emitting a one-line warning on process.stderr (never stdout, to protect JSON-RPC stdio framing) alerts operators during deployment without breaking client initialization.

    Additional Consideration: Human-Readable Unit Suffixes

    In addition to pure integers, operators frequently pass strings with units ("10MB", "500KB", "30s"). Standard Number() evaluates all of these to NaN.

    Here is a lightweight, zero-dependency helper pattern that parses unit multipliers, clamps safely to bounds, and guarantees fail-closed fallback to the documented default:

    const BYTE_UNITS: Record<string, number> = {
      b: 1,
      kb: 1024,
      kib: 1024,
      mb: 1024 * 1024,
      mib: 1024 * 1024,
      gb: 1024 * 1024 * 1024,
      gib: 1024 * 1024 * 1024,
    };
    
    export function parseByteLimitSafely(
      rawValue: string | undefined | null,
      defaultValue: number,
      min: number = 0,
      max: number = Number.MAX_SAFE_INTEGER
    ): number {
      if (!rawValue || typeof rawValue !== "string") {
        return defaultValue;
      }
    
      const str = rawValue.trim().toLowerCase();
      const match = str.match(/^(\d+(?:\.\d+)?)\s*([a-z]+)?$/);
      if (!match) {
        if (process.env.NODE_ENV !== "test") {
          process.stderr.write(
            `[mcp:warning] Invalid byte size "${rawValue}". Falling back to default (${defaultValue} bytes).\n`
          );
        }
        return defaultValue;
      }
    
      const num = parseFloat(match[1]);
      const unit = match[2];
      const multiplier = unit ? BYTE_UNITS[unit] : 1;
    
      if (multiplier === undefined || Number.isNaN(num) || !Number.isFinite(num) || num < 0) {
        return defaultValue;
      }
    
      const total = Math.floor(num * multiplier);
      return Math.min(Math.max(total, min), max);
    }

    Applying this to tools/gzip-file-as-resource.ts allows the test in the OP to pass immediately when expecting Option (a) behavior, while also safely handling intuitive operator values like "10 MiB" or "10MB".


    Disclosure: I maintain the open-source mcp-production-kit and the Production MCP Server Hardening Checklist, where we document and test fail-closed limit parsing and runtime edge cases across the MCP ecosystem.

  2. added
    bug
    server-everything
    on Nov 30, 2026
  3. cliffhall commented on Nov 30, 2026

    @cliffhall
    Member

    Triage: Priority Low (total 5)

    • Severity 2: a non-numeric GZIP_MAX_FETCH_SIZE silently disables the documented cap; the value is the operator's own configuration
    • Urgency 2: wanted eventually, nothing blocked on it
    • Bonuses: +1 bug label (relabelled from a question: the documented behaviour and the code disagree)

    Board: Servers V2 (#43), Status Incoming, awaiting maintainer review (no milestone yet).

  4. added this to the v2.7.0 milestone on Nov 30, 2026
  5. self-assigned this
    on Nov 30, 2026
  6. cliffhall commented on Nov 30, 2026

    @cliffhall
    Member

    Fixed by #5703, merged into v2/main; ships in v2.7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugserver-everythingv2

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions