mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-02 19:02:50 +08:00
fix(music): handle multiple cookie headers and invalid saved values
This commit is contained in:
1 parent
4ac3309e34
commit
51debb915c
4 files changed
+14
-5
No files matched your search
@@ -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("");
|
||||
});
|
||||
});
|
||||
+2
-1
@@ -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 "";
|
||||
}
|
||||
|
||||
@@ -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("");
|
||||
});
|
||||
|
||||
@@ -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<string, string>();
|
||||
for (const fragment of text.split(";")) {
|
||||
|
||||
Reference in new issue
Block a user