Skip to content

Commit e2ac8d5

Browse files
committed
fix(sandbox): use Buffer.byteLength for env var value size limit
validateEnvVarValue checked value.length (UTF-16 code units) against the 32768-byte limit, so multi-byte CJK values like "值".repeat(11000) passed the check despite exceeding 33 KB in UTF-8. Switch to Buffer.byteLength(value, "utf8") so the limit matches the actual byte count the OS and child processes see.
1 parent 61b85aa commit e2ac8d5

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/agents/sandbox/sanitize-env-vars.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,27 @@ describe("sanitizeEnvVars", () => {
105105
expect(result.allowed).toEqual({ SAFE_SECRET: "ok" });
106106
expect(result.blocked).toStrictEqual(["NULL_SECRET"]);
107107
});
108+
109+
it("warns on multi-byte values whose UTF-8 byte length exceeds the limit", () => {
110+
// Each CJK character is 3 UTF-8 bytes; 11000 chars × 3 bytes = 33000 bytes > 32768.
111+
const multiByteValue = "值".repeat(11000);
112+
expect(multiByteValue.length).toBe(11000); // UTF-16 length is below 32768
113+
expect(Buffer.byteLength(multiByteValue, "utf8")).toBe(33000); // but UTF-8 exceeds limit
114+
115+
const result = sanitizeEnvVars({ MULTIBYTE: multiByteValue });
116+
expect(result.allowed).toEqual({ MULTIBYTE: multiByteValue });
117+
expect(result.warnings).toContain("MULTIBYTE: Value exceeds maximum length");
118+
});
119+
120+
it("allows ASCII values at the byte limit boundary", () => {
121+
// Use characters outside the base64 charset to avoid triggering the
122+
// base64-credential heuristic, which runs before the length check.
123+
const atLimit = "a!b!".repeat(8192); // 8192*4 = 32768 bytes, contains !
124+
const overLimit = atLimit + "x"; // 32769 bytes
125+
126+
expect(sanitizeEnvVars({ AT_LIMIT: atLimit }).warnings).toStrictEqual([]);
127+
expect(sanitizeEnvVars({ OVER_LIMIT: overLimit }).warnings).toContain(
128+
"OVER_LIMIT: Value exceeds maximum length",
129+
);
130+
});
108131
});

src/agents/sandbox/sanitize-env-vars.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ export type EnvSanitizationOptions = {
5353
customAllowedPatterns?: ReadonlyArray<RegExp>;
5454
};
5555

56+
const MAX_ENV_VAR_VALUE_BYTES = 32768;
57+
5658
/** Returns a warning or block reason for environment values that look unsafe to forward. */
5759
export function validateEnvVarValue(value: string): string | undefined {
5860
if (value.includes("\0")) {
5961
return "Contains null bytes";
6062
}
61-
if (value.length > 32768) {
63+
if (Buffer.byteLength(value, "utf8") > MAX_ENV_VAR_VALUE_BYTES) {
6264
return "Value exceeds maximum length";
6365
}
6466
if (/^[A-Za-z0-9+/=]{80,}$/.test(value)) {

0 commit comments

Comments
 (0)