Skip to content

Commit 5ce50fc

Browse files
guitavanoclaude
andauthored
fix(vnda): filter proxy cookies to prevent RequestHeaderSectionTooLarge (#1633)
Add allowedCookies option to proxy handler and configure VNDA proxy to only forward VNDA-relevant cookies (vnda_cart_id, cart_id, ahoy_visit, ahoy_visitor), stripping analytics/tracking cookies that inflate headers beyond the 8KB limit. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e878c5c commit 5ce50fc

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

vnda/loaders/proxy.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ const API_PATHS = [
3838
const decoSiteMapUrl = "/sitemap/deco.xml";
3939

4040
const VNDA_HOST_HEADER = "X-Shop-Host";
41+
const VNDA_ALLOWED_COOKIES = [
42+
"vnda_cart_id",
43+
"cart_id",
44+
"ahoy_visit",
45+
"ahoy_visitor",
46+
];
4147
export interface Props {
4248
/** @description ex: /p/fale-conosco */
4349
pagesToProxy?: string[];
@@ -100,6 +106,7 @@ function loader(
100106
: internalDomain,
101107
host: url.hostname,
102108
customHeaders,
109+
allowedCookies: VNDA_ALLOWED_COOKIES,
103110
},
104111
},
105112
}));
@@ -123,6 +130,7 @@ function loader(
123130
url: `https://api.vnda.com.br/`,
124131
host: url.hostname,
125132
customHeaders,
133+
allowedCookies: VNDA_ALLOWED_COOKIES,
126134
},
127135
},
128136
}));

website/handlers/proxy.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ export const removeCFHeaders = (headers: Headers) => {
2424
}
2525
});
2626
};
27+
export const filterCookies = (headers: Headers, allowed: string[]) => {
28+
const cookie = headers.get("cookie");
29+
if (!cookie) return;
30+
const filtered = cookie
31+
.split(";")
32+
.map((c) => c.trim())
33+
.filter((c) => {
34+
const name = c.split("=")[0]?.trim();
35+
return name && allowed.includes(name);
36+
})
37+
.join("; ");
38+
if (filtered) {
39+
headers.set("cookie", filtered);
40+
} else {
41+
headers.delete("cookie");
42+
}
43+
};
2744
/**
2845
* @title {{{key}}} - {{{value}}}
2946
*/
@@ -86,6 +103,10 @@ export interface Props {
86103
removeDirtyCookies?: boolean;
87104
excludeHeaders?: string[];
88105
pathsThatRequireSameReferer?: string[];
106+
/**
107+
* @description Only forward cookies whose names match this list. When set, all other cookies are stripped from the proxied request.
108+
*/
109+
allowedCookies?: string[];
89110
}
90111
/**
91112
* @title Proxy
@@ -104,6 +125,7 @@ export default function Proxy({
104125
replaces,
105126
removeDirtyCookies = false,
106127
pathsThatRequireSameReferer = [],
128+
allowedCookies,
107129
}: Props): Handler {
108130
return async (req, _ctx) => {
109131
const url = new URL(req.url);
@@ -127,6 +149,9 @@ export default function Proxy({
127149
if (removeDirtyCookies) {
128150
removeDirtyCookiesFn(headers);
129151
}
152+
if (allowedCookies && allowedCookies.length > 0) {
153+
filterCookies(headers, allowedCookies);
154+
}
130155
if (isFreshCtx<DecoSiteState>(_ctx)) {
131156
_ctx?.state?.monitoring?.logger?.log?.("proxy sent headers", headers);
132157
}

0 commit comments

Comments
 (0)