Skip to content

Commit bb47140

Browse files
committed
fix: no csrf token in set-cookie
1 parent bff509a commit bb47140

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

.changeset/metal-shirts-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"leetcode-query": patch
3+
---
4+
5+
Works with no cookie presented in the response

src/credential-cn.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ async function get_csrf() {
1616
query: "query nojGlobalData {\n siteRegion\n chinaHost\n websocketUrl\n}\n",
1717
}),
1818
});
19-
const cookies_raw = res.headers.get("set-cookie") as string;
19+
const cookies_raw = res.headers.get("set-cookie");
20+
if (!cookies_raw) {
21+
return undefined;
22+
}
2023

2124
const csrf_token = parse_cookie(cookies_raw).csrftoken;
2225
return csrf_token;

src/credential.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import type { ICredential } from "./types";
44
import { parse_cookie } from "./utils";
55

66
async function get_csrf() {
7-
const cookies_raw = (await fetch(BASE_URL, {
7+
const cookies_raw = await fetch(BASE_URL, {
88
headers: {
99
"user-agent": USER_AGENT,
1010
},
11-
}).then((res) => res.headers.get("set-cookie"))) as string;
11+
}).then((res) => res.headers.get("set-cookie"));
12+
if (!cookies_raw) {
13+
return undefined;
14+
}
1215

1316
const csrf_token = parse_cookie(cookies_raw).csrftoken;
1417
return csrf_token;

src/leetcode-cn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class LeetCodeCN extends EventEmitter {
127127
this.emit("receive-graphql", res.clone());
128128

129129
if (res.headers.has("set-cookie")) {
130-
const cookies = parse_cookie(res.headers.get("set-cookie") as string);
130+
const cookies = parse_cookie(res.headers.get("set-cookie") || "");
131131

132132
if (cookies["csrftoken"]) {
133133
this.credential.csrf = cookies["csrftoken"];

src/leetcode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export class LeetCode extends EventEmitter {
372372
this.emit("receive-graphql", res.clone());
373373

374374
if (res.headers.has("set-cookie")) {
375-
const cookies = parse_cookie(res.headers.get("set-cookie") as string);
375+
const cookies = parse_cookie(res.headers.get("set-cookie") || "");
376376

377377
if (cookies["csrftoken"]) {
378378
this.credential.csrf = cookies["csrftoken"];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy