Skip to content

Find code by asking what it does. A CLI for coding agents that uses Jev to discover relevant files and source context.

fix(auth): reject header-invalid keys before saving

Liu Jiebd99d46f42e8
diff --git a/apps/cli/src/auth.ts b/apps/cli/src/auth.ts
index c582f96..32e2f0a 100644
--- a/apps/cli/src/auth.ts
+++ b/apps/cli/src/auth.ts
@@ -1,6 +1,7 @@
import { chmod, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { randomUUID } from "node:crypto";
+import { validateHeaderValue } from "node:http";
import { isCancel, password, select, text } from "@clack/prompts";
import {
customProviderId,
@@ -21,6 +22,11 @@ function validateKey(raw: string): string {
if (!key || /\s/.test(key) || Buffer.byteLength(key) > 8192) {
throw new CliError("Provide one non-empty API key without whitespace (maximum 8 KiB).");
}
+ try {
+ validateHeaderValue("Authorization", `Bearer ${key}`);
+ } catch {
+ throw new CliError("Provide an API key containing only valid HTTP header characters.");
+ }
return key;
}
diff --git a/apps/cli/test/auth.test.ts b/apps/cli/test/auth.test.ts
index a9faed5..21b5f37 100644
--- a/apps/cli/test/auth.test.ts
+++ b/apps/cli/test/auth.test.ts
@@ -79,6 +79,45 @@ testInDocker("installed auth saves privately, bounds stdin, and never echoes key
});
});
+for (const [label, input] of [
+ ["NUL", "invalid-fake\0secret\n"],
+ ["non-Latin-1", "invalid-fake☃secret\n"],
+ ["control character", "invalid-fake\x01secret\n"],
+ ["DEL", "invalid-fake\x7fsecret\n"],
+ ["malformed UTF-8", Buffer.concat([Buffer.from("invalid-fake"), Buffer.from([0xff])])],
+] as const) {
+ testInDocker(`auth rejects ${label} without replacing saved credentials`, async () => {
+ await withCli(async ({ home, run }) => {
+ const directory = join(home, "jevgrep");
+ const file = join(directory, "credentials.json");
+ const original = '{ "provider": "vercel", "apiKey": "existing-fake-secret" }\n';
+ await mkdir(directory, { recursive: true });
+ await writeFile(file, original);
+ const result = await run(["auth", "--provider", "vercel", "--stdin"], input);
+ expect(result.code).toBe(1);
+ expect(result.stderr).toBe("");
+ expect(result.stdout).toContain("API key");
+ expect(result.stdout).not.toContain("invalid-fake");
+ expect(result.stdout).not.toContain("existing-fake-secret");
+ expect(await readFile(file, "utf8")).toBe(original);
+ });
+ });
+}
+
+testInDocker("auth accepts a Latin-1 key and trims surrounding whitespace", async () => {
+ await withCli(async ({ home, run }) => {
+ const key = "fake-latin-é-secret";
+ const result = await run(["auth", "--provider", "vercel", "--stdin"], ` \t${key}\r\n`);
+ expect(result).toMatchObject({ code: 0, stderr: "" });
+ expect(result.stdout).toContain("key saved");
+ expect(result.stdout).not.toContain(key);
+ expect(JSON.parse(await readFile(join(home, "jevgrep", "credentials.json"), "utf8"))).toEqual({
+ provider: "vercel",
+ apiKey: key,
+ });
+ });
+});
+
testInDocker(
"custom auth saves the endpoint and doctor verifies it without echoing the key",
async () => {
diff --git a/apps/cli/test/cli.ts b/apps/cli/test/cli.ts
index 43c9348..6bafd60 100644
--- a/apps/cli/test/cli.ts
+++ b/apps/cli/test/cli.ts
@@ -11,14 +11,14 @@ export async function withCli(
home: string;
run: (
args: string[],
- input?: string,
+ input?: string | Buffer,
env?: NodeJS.ProcessEnv,
) => Promise<{ stdout: string; stderr: string; code: number }>;
}) => Promise<void>,
) {
const home = await mkdtemp(join(tmpdir(), "jevgrep-cli-"));
const cli = process.env.JEVGREP_TEST_CLI ?? join(import.meta.dir, "../dist/bin/index.js");
- const run = async (args: string[], input = "", env: NodeJS.ProcessEnv = {}) => {
+ const run = async (args: string[], input: string | Buffer = "", env: NodeJS.ProcessEnv = {}) => {
const child = Bun.spawn(["node", cli, ...args], {
cwd: home,
env: {