diff --git a/src/music/auth.test.ts b/src/music/auth.test.ts index 92b33b2..9f1c278 100644 --- a/src/music/auth.test.ts +++ b/src/music/auth.test.ts @@ -67,4 +67,9 @@ describe("cookie store", () => { fs.writeFileSync(path.join(dir, "qq.json"), "{not json", "utf-8"); expect(store.load("qq")).toBe(""); }); + + it("ignores a well-formed JSON file with a non-string cookie", () => { + fs.writeFileSync(path.join(dir, "qq.json"), JSON.stringify({ cookie: { invalid: true } })); + expect(createCookieStore(dir).load("qq")).toBe(""); + }); }); diff --git a/src/music/auth.ts b/src/music/auth.ts index c632e3f..a2f5b53 100644 --- a/src/music/auth.ts +++ b/src/music/auth.ts @@ -47,7 +47,8 @@ export function createCookieStore(cookieDir: string): CookieStore { let stored: string; try { const data = JSON.parse(fs.readFileSync(filePath, "utf-8")); - stored = data.cookie ?? ""; + if (typeof data?.cookie !== "string") return ""; + stored = data.cookie; } catch { return ""; } diff --git a/src/music/cookie-normalize.test.ts b/src/music/cookie-normalize.test.ts index 46e9edd..66a7bf8 100644 --- a/src/music/cookie-normalize.test.ts +++ b/src/music/cookie-normalize.test.ts @@ -54,6 +54,11 @@ describe("normalizeCookie", () => { expect(normalizeCookie("cookie: MUSIC_U=abc")).toBe("MUSIC_U=abc"); }); + it("keeps each cookie from multiple pasted Set-Cookie lines", () => { + expect(normalizeCookie("Set-Cookie: MUSIC_U=abc; Path=/\r\nSet-Cookie: __csrf=def; HttpOnly")) + .toBe("MUSIC_U=abc; __csrf=def"); + }); + it("empties out a blob that carries no real cookie at all", () => { expect(normalizeCookie("Max-Age=1; Expires=Thu, 25 Nov 2094 21:48:40 GMT; Path=/")).toBe(""); }); diff --git a/src/music/cookie-normalize.ts b/src/music/cookie-normalize.ts index 98f3c0b..f98b680 100644 --- a/src/music/cookie-normalize.ts +++ b/src/music/cookie-normalize.ts @@ -50,7 +50,7 @@ const TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; /** * Turn whatever was pasted into a syntactically valid `Cookie:` header value. * - * - drops a leading `Set-Cookie:` / `Cookie:` prefix (people copy the whole line) + * - handles pasted `Set-Cookie:` / `Cookie:` header lines * - drops attribute pairs (`Max-Age`, `Expires`, `Path`, ...) * - drops fragments without `=` (`;;`, trailing separators) * - drops values that are empty, which the sidecar reads as "unset anyway" @@ -64,9 +64,7 @@ const TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; export function normalizeCookie(raw: string): string { if (!raw) return ""; - let text = raw.trim(); - const prefix = /^(?:set-)?cookie\s*:\s*/i.exec(text); - if (prefix) text = text.slice(prefix[0].length); + const text = raw.trim().replace(/^[ \t]*(?:set-)?cookie[ \t]*:[ \t]*/gim, ";"); const pairs = new Map(); for (const fragment of text.split(";")) {