Add afsrc=1 query param for partner links#3750
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughTwo files were modified to add an Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
apps/web/tests/redirects/index.test.ts (1)
30-33: Add a regression case forvia/afsrcoverride attempts.Nice happy-path assertion. Please add one case with incoming
?via=spoof&afsrc=0and assert the redirect still ends with partnerviaandafsrc=1to lock this behavior.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/web/tests/redirects/index.test.ts` around lines 30 - 33, Add a regression test that covers an incoming query trying to override via/afsrc: call the same test helper/path used to produce redirectUrl but include an incoming query string like "?via=spoof&afsrc=0", then assert that redirectUrl.searchParams.get("via") equals the expected partner value (not "spoof") and that redirectUrl.searchParams.get("afsrc") equals "1"; place this alongside the existing happy-path assertions (the code around options?.via and redirectUrl) to lock the behavior against override attempts.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@apps/web/lib/middleware/utils/get-final-url.ts`:
- Around line 36-39: Ensure partner parameters 'via' and 'afsrc' cannot be
overridden by incoming query params: when merging passthrough query parameters
into urlObj.searchParams (the merge loop currently around the passthrough
logic), skip keys 'via' and 'afsrc' (i.e., do not set/overwrite
urlObj.searchParams for those keys) or move the code that sets
urlObj.searchParams.set("via", via) and urlObj.searchParams.set("afsrc","1") to
run after the passthrough merge in get-final-url.ts so that urlObj.searchParams
always ends up with the intended partner values; reference the existing
urlObj.searchParams usage and the passthrough merge block (the loop that applies
incoming query params) to implement the fix.
---
Nitpick comments:
In `@apps/web/tests/redirects/index.test.ts`:
- Around line 30-33: Add a regression test that covers an incoming query trying
to override via/afsrc: call the same test helper/path used to produce
redirectUrl but include an incoming query string like "?via=spoof&afsrc=0", then
assert that redirectUrl.searchParams.get("via") equals the expected partner
value (not "spoof") and that redirectUrl.searchParams.get("afsrc") equals "1";
place this alongside the existing happy-path assertions (the code around
options?.via and redirectUrl) to lock the behavior against override attempts.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: a2a707e1-76f5-41f9-bb87-7de3da8f9dee
📒 Files selected for processing (2)
apps/web/lib/middleware/utils/get-final-url.tsapps/web/tests/redirects/index.test.ts
| if (via) { | ||
| urlObj.searchParams.set("via", via); | ||
| urlObj.searchParams.set("afsrc", "1"); | ||
| } |
There was a problem hiding this comment.
Prevent partner params from being overridden by incoming query params.
via/afsrc=1 are set here, but later passthrough merging can overwrite them (Line 125–129). A request like ?via=spoof&afsrc=0 can break the intended partner-link behavior.
Suggested fix
for (const [key, value] of searchParams) {
// we will pass everything except internal query params (dub-no-track and redir_url)
if (["dub-no-track", REDIRECTION_QUERY_PARAM].includes(key)) continue;
+ // preserve enforced partner attribution params
+ if (via && (key === "via" || key === "afsrc")) continue;
urlObj.searchParams.set(key, value);
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@apps/web/lib/middleware/utils/get-final-url.ts` around lines 36 - 39, Ensure
partner parameters 'via' and 'afsrc' cannot be overridden by incoming query
params: when merging passthrough query parameters into urlObj.searchParams (the
merge loop currently around the passthrough logic), skip keys 'via' and 'afsrc'
(i.e., do not set/overwrite urlObj.searchParams for those keys) or move the code
that sets urlObj.searchParams.set("via", via) and
urlObj.searchParams.set("afsrc","1") to run after the passthrough merge in
get-final-url.ts so that urlObj.searchParams always ends up with the intended
partner values; reference the existing urlObj.searchParams usage and the
passthrough merge block (the loop that applies incoming query params) to
implement the fix.
x-ref: https://blog.pie.org/afsrc-1-a-simple-option-for-consistent-stand-down-in-affiliate-marketing/
Summary by CodeRabbit
Release Notes
Summary: URL redirect parameter handling has been enhanced. When certain redirect conditions are met, additional query parameters are now included in destination URLs. Test validation has been updated to verify this parameter behavior and ensure proper URL construction consistency.