Skip to content
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

fix: stop pass through request params for function redirects #4897

Merged
merged 4 commits into from Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/utils/proxy.js
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
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' },
danez marked this conversation as resolved.
Show resolved Hide resolved
],
})
.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