-
Notifications
You must be signed in to change notification settings - Fork 29
fix(vtex): sanitize query params to prevent NaN crashes #1546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -161,20 +161,27 @@ export interface Props { | |||||||||||||
| } | ||||||||||||||
| const searchArgsOf = (props: Props, url: URL) => { | ||||||||||||||
| const hideUnavailableItems = props.hideUnavailableItems; | ||||||||||||||
| const VALID_SIM_BEHAVIORS = new Set(["default", "skip", "only1P"]); | ||||||||||||||
| const rawSim = url.searchParams.get("simulationBehavior"); | ||||||||||||||
| const simulationBehavior = | ||||||||||||||
| url.searchParams.get("simulationBehavior") as SimulationBehavior || | ||||||||||||||
| props.simulationBehavior || "default"; | ||||||||||||||
| (rawSim && VALID_SIM_BEHAVIORS.has(rawSim) | ||||||||||||||
| ? rawSim | ||||||||||||||
| : props.simulationBehavior || "default") as SimulationBehavior; | ||||||||||||||
| const countFromSearchParams = url.searchParams.get("PS"); | ||||||||||||||
| const count = Number(countFromSearchParams ?? props.count ?? 12); | ||||||||||||||
| const rawCount = countFromSearchParams ? Number(countFromSearchParams) : NaN; | ||||||||||||||
| const count = Number.isFinite(rawCount) && rawCount > 0 | ||||||||||||||
| ? Math.min(Math.floor(rawCount), 50) | ||||||||||||||
| : Number(props.count ?? 12); | ||||||||||||||
| const query = props.query ?? url.searchParams.get("q") ?? ""; | ||||||||||||||
| const currentPageoffset = props.pageOffset ?? 1; | ||||||||||||||
| const rawPage = url.searchParams.get("page") | ||||||||||||||
| ? Number(url.searchParams.get("page")) | ||||||||||||||
| : NaN; | ||||||||||||||
| const parsedPage = Number.isFinite(rawPage) && rawPage >= currentPageoffset | ||||||||||||||
| ? rawPage - currentPageoffset | ||||||||||||||
| : 0; | ||||||||||||||
|
Comment on lines
+180
to
+182
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents
Suggested change
|
||||||||||||||
| const page = props.page ?? | ||||||||||||||
| Math.min( | ||||||||||||||
| url.searchParams.get("page") | ||||||||||||||
| ? Number(url.searchParams.get("page")) - currentPageoffset | ||||||||||||||
| : 0, | ||||||||||||||
| VTEX_MAX_PAGES - currentPageoffset, | ||||||||||||||
| ); | ||||||||||||||
| Math.min(parsedPage, VTEX_MAX_PAGES - currentPageoffset); | ||||||||||||||
|
Comment on lines
+177
to
+184
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 180 uses Proposed fix- const parsedPage = Number.isFinite(rawPage) && rawPage >= currentPageoffset
+ const parsedPage = Number.isInteger(rawPage) && rawPage >= currentPageoffset
? rawPage - currentPageoffset
: 0;🤖 Prompt for AI Agents |
||||||||||||||
| const sort = (url.searchParams.get("sort")) ?? | ||||||||||||||
| LEGACY_TO_IS[url.searchParams.get("O") ?? ""] ?? | ||||||||||||||
| props.sort ?? | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,8 @@ | ||||||||||||||||||||||||||||||||||
| import { LegacyItem } from "../../utils/types.ts"; | ||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||
| removeNonLatin1Chars, | ||||||||||||||||||||||||||||||||||
| removeScriptChars, | ||||||||||||||||||||||||||||||||||
| } from "../../../utils/normalize.ts"; | ||||||||||||||||||||||||||||||||||
| import type { Filter, ProductListingPage } from "../../../commerce/types.ts"; | ||||||||||||||||||||||||||||||||||
| import { STALE } from "../../../utils/fetch.ts"; | ||||||||||||||||||||||||||||||||||
| import { AppContext } from "../../mod.ts"; | ||||||||||||||||||||||||||||||||||
|
|
@@ -194,7 +198,10 @@ const loader = async ( | |||||||||||||||||||||||||||||||||
| const filtersBehavior = props.filters || "dynamic"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const countFromSearchParams = url.searchParams.get("PS"); | ||||||||||||||||||||||||||||||||||
| const count = Number(countFromSearchParams ?? props.count ?? 12); | ||||||||||||||||||||||||||||||||||
| const rawCount = countFromSearchParams ? Number(countFromSearchParams) : NaN; | ||||||||||||||||||||||||||||||||||
| const count = Number.isFinite(rawCount) && rawCount > 0 | ||||||||||||||||||||||||||||||||||
| ? Math.min(Math.floor(rawCount), 50) | ||||||||||||||||||||||||||||||||||
| : Number(props.count ?? 12); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const maybeMap = props.map || url.searchParams.get("map") || undefined; | ||||||||||||||||||||||||||||||||||
| let maybeTerm = props.term || url.pathname || ""; | ||||||||||||||||||||||||||||||||||
|
|
@@ -204,18 +211,25 @@ const loader = async ( | |||||||||||||||||||||||||||||||||
| maybeTerm = result?.possiblePaths[0] ?? maybeTerm; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const pageParam = url.searchParams.get("page") | ||||||||||||||||||||||||||||||||||
| ? Number(url.searchParams.get("page")) - currentPageoffset | ||||||||||||||||||||||||||||||||||
| const rawPage = url.searchParams.get("page") | ||||||||||||||||||||||||||||||||||
| ? Number(url.searchParams.get("page")) | ||||||||||||||||||||||||||||||||||
| : NaN; | ||||||||||||||||||||||||||||||||||
| const pageParam = Number.isFinite(rawPage) && rawPage >= currentPageoffset | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents |
||||||||||||||||||||||||||||||||||
| ? rawPage - currentPageoffset | ||||||||||||||||||||||||||||||||||
| : 0; | ||||||||||||||||||||||||||||||||||
| const page = props.page || pageParam; | ||||||||||||||||||||||||||||||||||
| const O: LegacySort = (url.searchParams.get("O") as LegacySort) ?? | ||||||||||||||||||||||||||||||||||
| const page = props.page || Math.min(pageParam, MAX_ALLOWED_PAGES); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+214
to
+220
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 217 validates only Proposed fix- const pageParam = Number.isFinite(rawPage) && rawPage >= currentPageoffset
+ const pageParam = Number.isInteger(rawPage) && rawPage >= currentPageoffset
? rawPage - currentPageoffset
: 0;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| const rawSort = (url.searchParams.get("O") as LegacySort) ?? | ||||||||||||||||||||||||||||||||||
| IS_TO_LEGACY[url.searchParams.get("sort") ?? ""] ?? | ||||||||||||||||||||||||||||||||||
| props.sort ?? | ||||||||||||||||||||||||||||||||||
| sortOptions[0].value as LegacySort; | ||||||||||||||||||||||||||||||||||
| const isSortValid = sortOptions.some((opt) => opt.value === rawSort); | ||||||||||||||||||||||||||||||||||
| const O = isSortValid ? rawSort : sortOptions[0].value as LegacySort; | ||||||||||||||||||||||||||||||||||
| const fq = [ | ||||||||||||||||||||||||||||||||||
| ...new Set([ | ||||||||||||||||||||||||||||||||||
| ...(props.fq ? [props.fq] : []), | ||||||||||||||||||||||||||||||||||
| ...url.searchParams.getAll("fq"), | ||||||||||||||||||||||||||||||||||
| ...url.searchParams.getAll("fq").map((v) => | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents |
||||||||||||||||||||||||||||||||||
| removeScriptChars(removeNonLatin1Chars(v)) | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+230
to
+232
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 230–232 strips characters commonly used by legitimate VTEX filter syntax (e.g. separators/ranges/accents), which can silently alter queries and return wrong or empty PLPs. Use narrower sanitization (script tags/control chars) instead of broad character removal. Safer direction- ...url.searchParams.getAll("fq").map((v) =>
- removeScriptChars(removeNonLatin1Chars(v))
- ),
+ ...url.searchParams.getAll("fq")
+ .map((v) =>
+ v
+ .replace(/<\/?script\b[^>]*>/gi, "")
+ .replace(/[\u0000-\u001F\u007F]/g, "")
+ .trim()
+ )
+ .filter(Boolean),📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| ]), | ||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||
| const _from = page * count; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normalize
simulationBehaviorconsistently in loader and cache key.Line 166–169 correctly normalizes invalid
simulationBehaviorto default, butcacheKeystill includes raw query values (Line 475–478). Equivalent requests can generate different cache keys, enabling cache fragmentation with junk params.Proposed fix (outside this hunk: cacheKey section)
🤖 Prompt for AI Agents