fix(music): normalise pasted cookies so /song/url stops answering 400

A cookie file on a live deployment held this:

  MUSIC_A_T=1456043741268; Max-Age=2147483647; Expires=Thu, 25 Nov 2094 ...;
  Path=/api/clientlog;;MUSIC_R_T=...; Max-Age=...; Path=/eapi/feedback; ...

That is a `Set-Cookie` header pasted whole. The cookie is then handed to the
sidecar as `?cookie=<the whole thing>`, so `Max-Age`, `Expires` and `Path` are
read as cookie NAMES and the string grows to several KB before URL-encoding.

Measured on that box (data/logs/bot.log):

- 34 of 34 `/song/url/v1` calls failed with `ERR_BAD_REQUEST` (HTTP 400);
- the same failure is still happening today, e.g. 17:14 and 17:20 local;
- every one surfaced to the user as 「无法播放(区域/版权限制)」.

So the message people were chasing for weeks is not a copyright signal at all
— it is the sidecar rejecting a malformed request.

`normalizeCookie()` drops attribute pairs, the `Set-Cookie:` prefix, CR/LF and
duplicate names, and keeps only real `name=value` pairs. It is applied:

- on `save()`, so nothing malformed reaches the file again;
- on `load()`, and **written back when it differs** — an existing deployment is
  repaired in place without asking anyone to scan a QR code again;
- in `setCookie()` of all four providers, including Kugou, which parses into a
  map and would otherwise store `max-age` as a device field.

jellyfin is excluded: its blob is JSON (accessToken/userId/deviceId), not a
cookie header, and normalising it would shred the payload. A test pins that.

Valid cookies are untouched — `normalizeCookie` is idempotent and byte-exact on
a clean string, and a clean file is not rewritten.

This does not overlap with the play-URL retry PR: that one retries a genuine
`url: null`, this one stops the request from being rejected before it is sent.
Both are needed — retrying a 400 just gets four more 400s.
This commit is contained in:
jiayuLian committed 2026-11-08 17:41:58 +08:00
1 parent 17008ae52e
commit 4ac3309e34
8 files changed
+295 -11

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
import { describe, expect, it } from "vitest";
import { isNormalizedCookie, normalizeCookie } from "./cookie-normalize.js";
/**
* The exact shape found in a live deployment's data/cookies/netease.json. Every
* `/song/url/v1` call answered 400 while this was stored, and the user saw
* 「无法播放(区域/版权限制)」 on every play attempt.
*/
const HEADER_BLOB = [
"MUSIC_A_T=1456043741268; Max-Age=2147483647; Expires=Thu, 25 Nov 2094 21:48:40 GMT; Path=/api/clientlog;",
";MUSIC_R_T=1456043923317; Max-Age=2147483647; Expires=Thu, 25 Nov 2094 21:48:40 GMT; Path=/eapi/feedback;",
";NMTID=00Oyuu_9EnDq_rMBEMIuKakmlo4dukAAAGht0p3Kw; Max-Age=315360000; Expires=Tue, 04 Nov 2036 18:34:33 GMT; Path=/;",
";MUSIC_U=00ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef0123456; Max-Age=1296000; Expires=Mon, 23 Nov 2026 09:32:11 GMT; Path=/;",
";__csrf=8f14e45fceea167a5a36dedd4bea2543; Max-Age=1296000; Expires=Mon, 23 Nov 2026 09:32:11 GMT; Path=/;",
].join("");
describe("normalizeCookie", () => {
it("keeps only the real cookie pairs from a pasted Set-Cookie blob", () => {
expect(normalizeCookie(HEADER_BLOB)).toBe(
"MUSIC_A_T=1456043741268; MUSIC_R_T=1456043923317; NMTID=00Oyuu_9EnDq_rMBEMIuKakmlo4dukAAAGht0p3Kw; " +
"MUSIC_U=00ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef0123456; " +
"__csrf=8f14e45fceea167a5a36dedd4bea2543",
);
});
it("drops every attribute name so it cannot be read as a cookie", () => {
const out = normalizeCookie(HEADER_BLOB);
for (const attr of ["Max-Age", "Expires", "Path", "Domain", "Secure", "HttpOnly", "SameSite"]) {
expect(out).not.toContain(`${attr}=`);
}
});
it("shrinks the query string enough to stop the 400", () => {
// The real blob repeats one attribute set per request path, so it grows
// several KB and the re-encoded query is what the sidecar rejects. The
// fixture is the same shape at a smaller scale; what matters is that the
// bulk (attributes) is gone and only the payload survives.
expect(normalizeCookie(HEADER_BLOB).length).toBeLessThan(HEADER_BLOB.length * 0.6);
});
it("leaves an already-valid cookie untouched", () => {
const valid = "MUSIC_U=abc123; __csrf=def456; NMTID=xyz";
expect(normalizeCookie(valid)).toBe(valid);
expect(isNormalizedCookie(valid)).toBe(true);
});
it("is idempotent", () => {
const once = normalizeCookie(HEADER_BLOB);
expect(normalizeCookie(once)).toBe(once);
});
it("strips a pasted `Set-Cookie:` / `Cookie:` prefix", () => {
expect(normalizeCookie("Set-Cookie: MUSIC_U=abc; Path=/")).toBe("MUSIC_U=abc");
expect(normalizeCookie("cookie: MUSIC_U=abc")).toBe("MUSIC_U=abc");
});
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("");
});
it("removes CR/LF left inside a value by a multi-line paste", () => {
expect(normalizeCookie("MUSIC_U=abc\r\ndef; __csrf=x")).toBe("MUSIC_U=abcdef; __csrf=x");
});
it("keeps the last value when a name repeats", () => {
expect(normalizeCookie("MUSIC_A_T=1; MUSIC_A_T=2")).toBe("MUSIC_A_T=2");
});
it("keeps '=' inside a value (base64 padding)", () => {
expect(normalizeCookie("MUSIC_U=abc==; Path=/")).toBe("MUSIC_U=abc==");
});
it("drops nameless fragments and empty values", () => {
expect(normalizeCookie(";; =x; MUSIC_U=abc; empty=;")).toBe("MUSIC_U=abc");
});
it("handles the empty string", () => {
expect(normalizeCookie("")).toBe("");
});
});