Skip to content

Commit

Permalink
fix: stop pass through request params for function redirects (#4897)
Browse files Browse the repository at this point in the history
* fix: stop pass through request params for function redirects

* chore: remove only modifier from test

* test: check if splat is being passed as query param in function redirects

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
tinfoil-knight and kodiakhq[bot] authored Aug 15, 2022
1 parent ec1a05e commit 9e1e55b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
7 changes: 3 additions & 4 deletions src/utils/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,9 @@ const serveRedirect = async function ({ match, options, proxy, req, res }) {
// construct destination URL from redirect rule match
const dest = new URL(match.to, `${reqUrl.protocol}//${reqUrl.host}`)

// We pass through request params in one of the following cases:
// 1. The redirect rule doesn't have any query params
// 2. This is a function redirect https://github.com/netlify/cli/issues/1605
if ([...dest.searchParams].length === 0 || isFunction(options.functionsPort, stripOrigin(dest))) {
// We pass through request params if the redirect rule
// doesn't have any query params
if ([...dest.searchParams].length === 0) {
dest.searchParams.forEach((_, key) => {
dest.searchParams.delete(key)
})
Expand Down
31 changes: 19 additions & 12 deletions tests/integration/200.command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ testMatrix.forEach(({ args }) => {
})
.withRedirectsFile({
redirects: [
{ from: `/api/*`, to: `/.netlify/functions/echo?a=1&a=2`, status: '200' },
{ from: `/foo`, to: `/`, status: '302' },
{ from: `/bar`, to: `/?a=1&a=2`, status: '302' },
{ from: `/test id=:id`, to: `/?param=:id` },
{ from: '/api/*', to: '/.netlify/functions/echo?a=1&a=2', status: '200' },
{ from: '/foo', to: '/', status: '302' },
{ from: '/bar', to: '/?a=1&a=2', status: '302' },
{ from: '/test id=:id', to: '/?param=:id' },
{ from: '/baz/*', to: '/.netlify/functions/echo?query=:splat' },
],
})
.withFunction({
Expand All @@ -53,15 +54,18 @@ testMatrix.forEach(({ args }) => {
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async (server) => {
const [fromFunction, queryPassthrough, queryInRedirect, withParamMatching] = await Promise.all([
got(`${server.url}/api/test?foo=1&foo=2&bar=1&bar=2`).json(),
got(`${server.url}/foo?foo=1&foo=2&bar=1&bar=2`, { followRedirect: false }),
got(`${server.url}/bar?foo=1&foo=2&bar=1&bar=2`, { followRedirect: false }),
got(`${server.url}/test?id=1`, { followRedirect: false }),
])
const [fromFunction, queryPassthrough, queryInRedirect, withParamMatching, functionWithSplat] =
await Promise.all([
got(`${server.url}/api/test?foo=1&foo=2&bar=1&bar=2`).json(),
got(`${server.url}/foo?foo=1&foo=2&bar=1&bar=2`, { followRedirect: false }),
got(`${server.url}/bar?foo=1&foo=2&bar=1&bar=2`, { followRedirect: false }),
got(`${server.url}/test?id=1`, { followRedirect: false }),
got(`${server.url}/baz/abc`).json(),
])

// query params should be taken from the request
t.deepEqual(fromFunction.multiValueQueryStringParameters, { foo: ['1', '2'], bar: ['1', '2'] })
// query params should be taken from redirect rule for functions
// eslint-disable-next-line id-length
t.deepEqual(fromFunction.multiValueQueryStringParameters, { a: ['1', '2'] })

// query params should be passed through from the request
t.is(queryPassthrough.headers.location, '/?foo=1&foo=2&bar=1&bar=2')
Expand All @@ -71,6 +75,9 @@ testMatrix.forEach(({ args }) => {

// query params should be taken from the redirect rule
t.is(withParamMatching.headers.location, '/?param=1')

// splat should be passed as query param in function redirects
t.deepEqual(functionWithSplat.queryStringParameters, { query: 'abc' })
})
})
})
Expand Down

1 comment on commit 9e1e55b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 222 MB

Please sign in to comment.