Files
SnapOtter/tests/unit/shared/subprocess-limit.test.ts
T
SnapOtterandGitHub 079fcd2631 fix(security): close the gaps a full 2.0 re-audit left open (#620)
Follow-up to a full re-audit of the 2.0 tree. Most prior findings were already
fixed; this closes the ones that were not:

- SAML assertion replay: validateInResponseTo ifPresent plus a Redis-backed
  CacheProvider, so a captured signed assertion cannot be replayed. ifPresent
  keeps IdP-initiated SSO working.
- MFA login challenge burned after 5 wrong TOTP codes.
- api_keys.key_prefix indexed; the per-request lookup was a full table scan.
- MAX_AI_JOBS_PER_USER caps a user's in-flight single-file AI jobs (the AI pool
  runs at concurrency 1). Batch and pipeline AI stay uncapped.
- MAX_WORKSPACE_SIZE_GB enforced instead of being dead config.
- SUBPROCESS_MEMORY_LIMIT_MB (default off) for the native media and doc engines;
  not applied to the AI sidecar.
- SVG sanitizer closes unquoted and whitespace-prefixed javascript: hrefs and
  the animateTransform/animateMotion/handler/mpath elements.
- Windows-style paths stripped from error output to match the Sentry scrubber.
- Postgres and Redis compose services get cap_drop plus pids_limit and cpus.
- .env.example ships MAX_SVG_SIZE_MB=50 (0 disabled the cap).

Adds security-focused unit and integration tests. typecheck, biome, and the
full unit and integration suites pass.
2026-07-23 00:18:16 +08:00

44 lines
1.8 KiB
TypeScript

import { afterEach, describe, expect, it } from "vitest";
import { wrapWithMemoryLimit } from "../../../packages/shared/src/subprocess-limit.js";
const KEY = "SUBPROCESS_MEMORY_LIMIT_MB";
const orig = process.env[KEY];
describe("wrapWithMemoryLimit", () => {
afterEach(() => {
if (orig === undefined) delete process.env[KEY];
else process.env[KEY] = orig;
});
it("returns the command unchanged when the limit is unset (default)", () => {
delete process.env[KEY];
expect(wrapWithMemoryLimit("ffmpeg", ["-i", "a.mp4"])).toEqual(["ffmpeg", ["-i", "a.mp4"]]);
});
it("returns the command unchanged when the limit is 0 or non-numeric", () => {
process.env[KEY] = "0";
expect(wrapWithMemoryLimit("gs", ["-dSAFER"])).toEqual(["gs", ["-dSAFER"]]);
process.env[KEY] = "not-a-number";
expect(wrapWithMemoryLimit("gs", ["-dSAFER"])).toEqual(["gs", ["-dSAFER"]]);
});
it("wraps in an ulimit -v sh shim (limit in KB) when a positive MB limit is set", () => {
process.env[KEY] = "512";
const [bin, args] = wrapWithMemoryLimit("ffmpeg", ["-i", "in.mp4", "out.mp4"]);
expect(bin).toBe("/bin/sh");
expect(args[0]).toBe("-c");
expect(args[1]).toContain("ulimit -v");
expect(args[1]).toContain('exec "$@"');
// sh -c <script> sh <kb> <realbin> <realargs...>
expect(args.slice(2)).toEqual(["sh", String(512 * 1024), "ffmpeg", "-i", "in.mp4", "out.mp4"]);
});
it("passes user args positionally so a crafted arg is never re-parsed by the shell", () => {
process.env[KEY] = "256";
const [, args] = wrapWithMemoryLimit("gs", ["-sOutputFile=/x/$(whoami).pdf"]);
// The metacharacter-laden value is a positional param, not part of the script body.
expect(args[1]).not.toContain("whoami");
expect(args).toContain("-sOutputFile=/x/$(whoami).pdf");
});
});