-
Notifications
You must be signed in to change notification settings - Fork 8.4k
hack/releaser: preserve query string on CloudFront redirects #25393
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
Open
dvdksn
wants to merge
1
commit into
docker:main
Choose a base branch
from
dvdksn:lambda-redirects-preserve-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| 'use strict'; | ||
|
|
||
| // Tests for cloudfront-lambda-redirects.js. | ||
| // | ||
| // The function source is a Go text/template (see aws.go) with two | ||
| // placeholders that are filled with JSON at build time. These tests render | ||
| // the template the same way aws.go does, then load and exercise the handler. | ||
| // | ||
| // Run with: node --test hack/releaser/cloudfront-lambda-redirects.test.js | ||
|
|
||
| const { test } = require('node:test'); | ||
| const assert = require('node:assert/strict'); | ||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
| const Module = require('node:module'); | ||
|
|
||
| const SRC = path.join(__dirname, 'cloudfront-lambda-redirects.js'); | ||
|
|
||
| const REDIRECTS = { | ||
| '/old/page/': '/new/page/', | ||
| '/target-with-query/': '/dest/?ref=docs', | ||
| }; | ||
|
|
||
| const REDIRECTS_PREFIXES = [ | ||
| { prefix: 'keep/', strip: false }, | ||
| { prefix: 'strip/', strip: true }, | ||
| ]; | ||
|
|
||
| // Render the template the same way getLambdaFunctionZip in aws.go does, then | ||
| // evaluate it as a CommonJS module so we can call handler() directly. | ||
| function loadHandler() { | ||
| const rendered = fs | ||
| .readFileSync(SRC, 'utf8') | ||
| // Function replacers avoid String.replace's special handling of `$` | ||
| // sequences in the replacement, in case a redirect target contains one. | ||
| .replace('{{.RedirectsJSON}}', () => JSON.stringify(REDIRECTS)) | ||
| .replace('{{.RedirectsPrefixesJSON}}', () => JSON.stringify(REDIRECTS_PREFIXES)); | ||
|
|
||
| const m = new Module(SRC); | ||
| m._compile(rendered, SRC); | ||
| return m.exports.handler; | ||
| } | ||
|
|
||
| const handler = loadHandler(); | ||
|
|
||
| // invoke wraps the callback-style handler in a promise. | ||
| function invoke({ uri, querystring = '', accept = 'text/html' }) { | ||
| const request = { | ||
| uri, | ||
| querystring, | ||
| headers: { accept: [{ key: 'Accept', value: accept }] }, | ||
| }; | ||
| const event = { Records: [{ cf: { request } }] }; | ||
| return new Promise((resolve, reject) => { | ||
| handler(event, {}, (err, result) => { | ||
| if (err) return reject(err); | ||
| resolve({ result, request }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function locationOf(result) { | ||
| return result.headers.location[0].value; | ||
| } | ||
|
|
||
| test('exact redirect preserves the query string', async () => { | ||
| const { result } = await invoke({ | ||
| uri: '/old/page/', | ||
| querystring: 'utm_source=newsletter&utm_campaign=launch', | ||
| }); | ||
| assert.equal(result.status, '301'); | ||
| assert.equal(locationOf(result), '/new/page/?utm_source=newsletter&utm_campaign=launch'); | ||
| }); | ||
|
|
||
| test('exact redirect without a query string is unchanged', async () => { | ||
| const { result } = await invoke({ uri: '/old/page/' }); | ||
| assert.equal(result.status, '301'); | ||
| assert.equal(locationOf(result), '/new/page/'); | ||
| }); | ||
|
|
||
| test('exact redirect appends with & when target already has a query string', async () => { | ||
| const { result } = await invoke({ | ||
| uri: '/target-with-query/', | ||
| querystring: 'utm_medium=email', | ||
| }); | ||
| assert.equal(locationOf(result), '/dest/?ref=docs&utm_medium=email'); | ||
| }); | ||
|
|
||
| test('prefix redirect (strip) preserves the query string', async () => { | ||
| const { result } = await invoke({ | ||
| uri: '/strip/some/path', | ||
| querystring: 'utm_source=x', | ||
| }); | ||
| assert.equal(result.status, '301'); | ||
| assert.equal(locationOf(result), '/some/path?utm_source=x'); | ||
| }); | ||
|
|
||
| test('prefix redirect (no strip) preserves the query string', async () => { | ||
| const { result } = await invoke({ | ||
| uri: '/keep/anything', | ||
| querystring: 'utm_source=x', | ||
| }); | ||
| assert.equal(result.status, '301'); | ||
| assert.equal(locationOf(result), '/?utm_source=x'); | ||
| }); | ||
|
|
||
| test('directory rewrite passes the request through with query string intact', async () => { | ||
| const { result, request } = await invoke({ | ||
| uri: '/some/page', | ||
| querystring: 'utm_source=x', | ||
| }); | ||
| // No redirect response: the handler forwards the (mutated) request. | ||
| assert.equal(result, request); | ||
| assert.equal(request.uri, '/some/page/index.html'); | ||
| assert.equal(request.querystring, 'utm_source=x'); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
thought: I'd personally default to allow-listing this (e.g. just support UTM parameters)
(I can't see any exploitative angles at the moment, would be just in fear of unknown-unknowns)
suggestion: Manipulate this with
URLinstead of doing raw string concatenationIf
locationwere something likehttps://foo.com?x=123#some-header, this would yield things likehttps://foo.com?x=123#some-header&utm_source=....I'd personally parse
locationinto aURLandrequest.querystringinto aURLSearchParams, then merge the latter into the former's search params, then read its.hrefto get the final URL.