mirror of
https://github.com/gethomepage/homepage.git
synced 2026-05-18 19:40:58 +08:00
Fix: allow explicit cookie header overwrite (#6672)
This commit is contained in:
@@ -2,13 +2,13 @@ import { Cookie, CookieJar } from "tough-cookie";
|
|||||||
|
|
||||||
const cookieJar = new CookieJar();
|
const cookieJar = new CookieJar();
|
||||||
|
|
||||||
export function setCookieHeader(url, params) {
|
export function setCookieHeader(url, params, { overwrite = false } = {}) {
|
||||||
// add cookie header, if we have one in the jar
|
// add cookie header, if we have one in the jar
|
||||||
const existingCookie = cookieJar.getCookieStringSync(url.toString());
|
const existingCookie = cookieJar.getCookieStringSync(url.toString());
|
||||||
if (existingCookie) {
|
if (existingCookie) {
|
||||||
params.headers = params.headers ?? {};
|
params.headers = params.headers ?? {};
|
||||||
const cookieHeader = params.cookieHeader ?? "Cookie";
|
const cookieHeader = params.cookieHeader ?? "Cookie";
|
||||||
if (!params.headers[cookieHeader]) {
|
if (overwrite || !params.headers[cookieHeader]) {
|
||||||
params.headers[cookieHeader] = existingCookie;
|
params.headers[cookieHeader] = existingCookie;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,4 +54,17 @@ describe("utils/proxy/cookie-jar", () => {
|
|||||||
|
|
||||||
expect(params.headers.Cookie).toBe("manual=1");
|
expect(params.headers.Cookie).toBe("manual=1");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("overwrites an existing cookie header when requested", async () => {
|
||||||
|
const { addCookieToJar, setCookieHeader } = await import("./cookie-jar");
|
||||||
|
|
||||||
|
const url = new URL("http://example5.test/path");
|
||||||
|
addCookieToJar(url, { "set-cookie": ["sid=1; Path=/"] });
|
||||||
|
|
||||||
|
const params = { headers: { Cookie: "stale=1" } };
|
||||||
|
setCookieHeader(url, params, { overwrite: true });
|
||||||
|
|
||||||
|
expect(params.headers.Cookie).toContain("sid=1");
|
||||||
|
expect(params.headers.Cookie).not.toContain("stale=1");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ export default function createUnifiProxyHandler({
|
|||||||
}
|
}
|
||||||
|
|
||||||
addCookieToJar(url, responseHeaders);
|
addCookieToJar(url, responseHeaders);
|
||||||
setCookieHeader(url, params);
|
setCookieHeader(url, params, { overwrite: true });
|
||||||
|
|
||||||
[status, contentType, data, responseHeaders] = await httpProxy(url, params);
|
[status, contentType, data, responseHeaders] = await httpProxy(url, params);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ function addCookieHandler(url, params) {
|
|||||||
// handle cookies during redirects
|
// handle cookies during redirects
|
||||||
params.beforeRedirect = (options, responseInfo) => {
|
params.beforeRedirect = (options, responseInfo) => {
|
||||||
addCookieToJar(options.href, responseInfo.headers);
|
addCookieToJar(options.href, responseInfo.headers);
|
||||||
setCookieHeader(options.href, options);
|
setCookieHeader(options.href, options, { overwrite: true });
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -348,7 +348,9 @@ describe("utils/proxy/http httpProxy", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
expect(cookieJar.addCookieToJar).toHaveBeenCalledWith("http://example.com/redirect", { "set-cookie": ["a=b"] });
|
expect(cookieJar.addCookieToJar).toHaveBeenCalledWith("http://example.com/redirect", { "set-cookie": ["a=b"] });
|
||||||
expect(cookieJar.setCookieHeader).toHaveBeenCalledWith("http://example.com/redirect", expect.any(Object));
|
expect(cookieJar.setCookieHeader).toHaveBeenCalledWith("http://example.com/redirect", expect.any(Object), {
|
||||||
|
overwrite: true,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("supports gzip-compressed responses", async () => {
|
it("supports gzip-compressed responses", async () => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import getServiceWidget from "utils/config/service-helpers";
|
import getServiceWidget from "utils/config/service-helpers";
|
||||||
import createLogger from "utils/logger";
|
import createLogger from "utils/logger";
|
||||||
import { asJson, formatApiCall, sanitizeErrorURL } from "utils/proxy/api-helpers";
|
import { asJson, formatApiCall, sanitizeErrorURL } from "utils/proxy/api-helpers";
|
||||||
import { addCookieToJar } from "utils/proxy/cookie-jar";
|
import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar";
|
||||||
import { httpProxy } from "utils/proxy/http";
|
import { httpProxy } from "utils/proxy/http";
|
||||||
import widgets from "widgets/widgets";
|
import widgets from "widgets/widgets";
|
||||||
|
|
||||||
@@ -57,6 +57,7 @@ export default async function frigateProxyHandler(req, res, map) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addCookieToJar(url, loginResponseHeaders);
|
addCookieToJar(url, loginResponseHeaders);
|
||||||
|
setCookieHeader(url, params, { overwrite: true });
|
||||||
// Retry original request with cookie set
|
// Retry original request with cookie set
|
||||||
[status, , data] = await httpProxy(url, params);
|
[status, , data] = await httpProxy(url, params);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const { httpProxy, getServiceWidget, cookieJar, logger } = vi.hoisted(() => ({
|
|||||||
getServiceWidget: vi.fn(),
|
getServiceWidget: vi.fn(),
|
||||||
cookieJar: {
|
cookieJar: {
|
||||||
addCookieToJar: vi.fn(),
|
addCookieToJar: vi.fn(),
|
||||||
|
setCookieHeader: vi.fn(),
|
||||||
},
|
},
|
||||||
logger: {
|
logger: {
|
||||||
debug: vi.fn(),
|
debug: vi.fn(),
|
||||||
@@ -130,6 +131,9 @@ describe("widgets/frigate/proxy", () => {
|
|||||||
await frigateProxyHandler(req, res);
|
await frigateProxyHandler(req, res);
|
||||||
|
|
||||||
expect(cookieJar.addCookieToJar).toHaveBeenCalled();
|
expect(cookieJar.addCookieToJar).toHaveBeenCalled();
|
||||||
|
expect(cookieJar.setCookieHeader).toHaveBeenCalledWith("http://frigate/api/stats", expect.any(Object), {
|
||||||
|
overwrite: true,
|
||||||
|
});
|
||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
expect(res.body).toEqual({ num_cameras: 2, uptime: 123, version: "1.0" });
|
expect(res.body).toEqual({ num_cameras: 2, uptime: 123, version: "1.0" });
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -86,6 +86,9 @@ describe("widgets/unifi/proxy", () => {
|
|||||||
expect(httpProxy).toHaveBeenCalledTimes(4);
|
expect(httpProxy).toHaveBeenCalledTimes(4);
|
||||||
expect(httpProxy.mock.calls[1][0].toString()).toContain("/proxy/network/api/self");
|
expect(httpProxy.mock.calls[1][0].toString()).toContain("/proxy/network/api/self");
|
||||||
expect(cookieJar.addCookieToJar).toHaveBeenCalled();
|
expect(cookieJar.addCookieToJar).toHaveBeenCalled();
|
||||||
|
expect(cookieJar.setCookieHeader).toHaveBeenLastCalledWith(expect.any(URL), expect.any(Object), {
|
||||||
|
overwrite: true,
|
||||||
|
});
|
||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
expect(res.body).toEqual(Buffer.from("data"));
|
expect(res.body).toEqual(Buffer.from("data"));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user