Skip to content

Commit 077ad2c

Browse files
author
Contributor
committed
fix: correct HTTP headers for LeetCode API authentication
The login flow was failing because the HTTP request headers sent to LeetCode's GraphQL API were incorrect: - 'referer' was set to 'vscode-lc-extension' instead of a valid URL - Missing 'Origin' header required by LeetCode's CORS policy - Missing 'User-Agent' header causing Cloudflare to block requests - CSRF token was not being extracted from cookie and sent as X-CSRFToken - Removed xsrfCookieName/xsrfHeaderName which only work in browser context Fixes #478
1 parent 586b3e4 commit 077ad2c

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

src/utils/httpUtils.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import axios, { AxiosRequestConfig, AxiosPromise } from "axios";
22
import { omit } from "lodash";
33
import { globalState } from "../globalState";
4+
import { getUrl } from "../shared";
45
import { DialogType, promptForOpenOutputChannel } from "./uiUtils";
56

6-
const referer = "vscode-lc-extension";
7+
function extractCsrfToken(cookie: string): string {
8+
const match = cookie.match(/csrftoken=([^;]+)/);
9+
return match ? match[1] : "";
10+
}
711

812
export function LcAxios<T = any>(path: string, settings?: AxiosRequestConfig): AxiosPromise<T> {
913
const cookie = globalState.getCookie();
@@ -14,15 +18,21 @@ export function LcAxios<T = any>(path: string, settings?: AxiosRequestConfig): A
1418
);
1519
return Promise.reject("Failed to obtain the cookie.");
1620
}
21+
22+
const baseUrl = getUrl("base");
23+
const csrfToken = extractCsrfToken(cookie);
24+
1725
return axios(path, {
1826
headers: {
19-
referer,
27+
"Origin": baseUrl,
28+
"Referer": baseUrl,
29+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
2030
"content-type": "application/json",
21-
cookie,
31+
"cookie": cookie,
32+
"X-CSRFToken": csrfToken,
33+
"X-Requested-With": "XMLHttpRequest",
2234
...(settings && settings.headers),
2335
},
24-
xsrfCookieName: "csrftoken",
25-
xsrfHeaderName: "X-CSRFToken",
2636
...(settings && omit(settings, "headers")),
2737
});
2838
}

0 commit comments

Comments
 (0)