diff --git a/src/utils/httpUtils.ts b/src/utils/httpUtils.ts index b7771734..8f9f239a 100644 --- a/src/utils/httpUtils.ts +++ b/src/utils/httpUtils.ts @@ -1,9 +1,13 @@ import axios, { AxiosRequestConfig, AxiosPromise } from "axios"; import { omit } from "lodash"; import { globalState } from "../globalState"; +import { getUrl } from "../shared"; import { DialogType, promptForOpenOutputChannel } from "./uiUtils"; -const referer = "vscode-lc-extension"; +function extractCsrfToken(cookie: string): string { + const match = cookie.match(/csrftoken=([^;]+)/); + return match ? match[1] : ""; +} export function LcAxios(path: string, settings?: AxiosRequestConfig): AxiosPromise { const cookie = globalState.getCookie(); @@ -14,15 +18,21 @@ export function LcAxios(path: string, settings?: AxiosRequestConfig): A ); return Promise.reject("Failed to obtain the cookie."); } + + const baseUrl = getUrl("base"); + const csrfToken = extractCsrfToken(cookie); + return axios(path, { headers: { - referer, + "Origin": baseUrl, + "Referer": baseUrl, + "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", "content-type": "application/json", - cookie, + "cookie": cookie, + "X-CSRFToken": csrfToken, + "X-Requested-With": "XMLHttpRequest", ...(settings && settings.headers), }, - xsrfCookieName: "csrftoken", - xsrfHeaderName: "X-CSRFToken", ...(settings && omit(settings, "headers")), }); }