Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions vnda/loaders/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ const API_PATHS = [
const decoSiteMapUrl = "/sitemap/deco.xml";

const VNDA_HOST_HEADER = "X-Shop-Host";
const VNDA_ALLOWED_COOKIES = [
"vnda_cart_id",
"cart_id",
"ahoy_visit",
"ahoy_visitor",
];
export interface Props {
/** @description ex: /p/fale-conosco */
pagesToProxy?: string[];
Expand Down Expand Up @@ -100,6 +106,7 @@ function loader(
: internalDomain,
host: url.hostname,
customHeaders,
allowedCookies: VNDA_ALLOWED_COOKIES,
},
},
}));
Expand All @@ -123,6 +130,7 @@ function loader(
url: `https://api.vnda.com.br/`,
host: url.hostname,
customHeaders,
allowedCookies: VNDA_ALLOWED_COOKIES,
},
},
}));
Expand Down
25 changes: 25 additions & 0 deletions website/handlers/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ export const removeCFHeaders = (headers: Headers) => {
}
});
};
export const filterCookies = (headers: Headers, allowed: string[]) => {
const cookie = headers.get("cookie");
if (!cookie) return;
const filtered = cookie
.split(";")
.map((c) => c.trim())
.filter((c) => {
const name = c.split("=")[0]?.trim();
return name && allowed.includes(name);
})
.join("; ");
if (filtered) {
headers.set("cookie", filtered);
} else {
headers.delete("cookie");
}
};
/**
* @title {{{key}}} - {{{value}}}
*/
Expand Down Expand Up @@ -86,6 +103,10 @@ export interface Props {
removeDirtyCookies?: boolean;
excludeHeaders?: string[];
pathsThatRequireSameReferer?: string[];
/**
* @description Only forward cookies whose names match this list. When set, all other cookies are stripped from the proxied request.
*/
allowedCookies?: string[];
}
/**
* @title Proxy
Expand All @@ -104,6 +125,7 @@ export default function Proxy({
replaces,
removeDirtyCookies = false,
pathsThatRequireSameReferer = [],
allowedCookies,
}: Props): Handler {
return async (req, _ctx) => {
const url = new URL(req.url);
Expand All @@ -127,6 +149,9 @@ export default function Proxy({
if (removeDirtyCookies) {
removeDirtyCookiesFn(headers);
}
if (allowedCookies && allowedCookies.length > 0) {
filterCookies(headers, allowedCookies);
}
if (isFreshCtx<DecoSiteState>(_ctx)) {
_ctx?.state?.monitoring?.logger?.log?.("proxy sent headers", headers);
}
Expand Down
Loading