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: correctly allow redirects to external URLs #5005

Merged
merged 3 commits into from
Aug 30, 2022
Merged

fix: correctly allow redirects to external URLs #5005

merged 3 commits into from
Aug 30, 2022

Conversation

danez
Copy link
Contributor

@danez danez commented Aug 26, 2022

🎉 Thanks for submitting a pull request! 🎉

Summary

#4109 change the behavior so that every redirect to an external URL was actually executed as a rewrite. The tests only checked the response body which is identical no matter if redirect or rewrite.

This is now fixed and the tests adapted to actually check if a redirect is happening.

Fixes #2710
Fixes #1242

@danez danez added the type: bug code to address defects in shipped code label Aug 26, 2022
@danez danez requested review from tinfoil-knight and a team August 26, 2022 16:03
@danez danez self-assigned this Aug 26, 2022
@github-actions
Copy link

github-actions bot commented Aug 26, 2022

📊 Benchmark results

Comparing with 1e5f8b0

Package size: 222 MB

(no change)

^  220 MB  220 MB  220 MB  226 MB  221 MB  221 MB  221 MB  221 MB  222 MB  222 MB  222 MB  222 MB  222 MB 
│   ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

@@ -70,7 +70,7 @@ const proxyToExternalUrl = function ({ dest, destURL, req, res }) {
pathRewrite: () => destURL,
...(Buffer.isBuffer(req.originalBody) && { buffer: toReadableStream(req.originalBody) }),
})
return handler(req, res, {})
return handler(req, res, () => {})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I only noticed this because a request was not matching and http-proxy-rewrite was actually calling next (the third parameter)

next is not a function

@@ -185,7 +185,7 @@ const serveRedirect = async function ({ match, options, proxy, req, res }) {

const staticFile = await getStatic(decodeURIComponent(reqUrl.pathname), options.publicFolder)
if (staticFile) {
req.url = encodeURIComponent(staticFile) + reqUrl.search
req.url = encodeURI(staticFile) + reqUrl.search
Copy link
Contributor Author

@danez danez Aug 26, 2022

Choose a reason for hiding this comment

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

This fixes a problem where static URLs are escaped to %2Findex.html. This was introduced in #2665 but even now after changing the to escapeUri the tests still work.

body: 'param=value',
followRedirect: false,
}),
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just replaced with got instead of custom HTTP request.

t.is(response3, '<html><h1>not-foo')
})
})
})
Copy link
Contributor Author

@danez danez Aug 26, 2022

Choose a reason for hiding this comment

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

These tests are all duplicates and are actually also in 500.command.dev.test.js

checked by comparing the two files.

tinfoil-knight
tinfoil-knight previously approved these changes Aug 27, 2022
Copy link
Contributor

@tinfoil-knight tinfoil-knight left a comment

Choose a reason for hiding this comment

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

[nit]
The commit message is of type fix & mentions fix again in the commit body. It can just be fix: redirects to external URLs


Everything else LGTM.

tests/integration/0.command.dev.test.js Outdated Show resolved Hide resolved
tests/integration/0.command.dev.test.js Show resolved Hide resolved
@danez danez changed the title fix: fix redirects to external URLs fix: correctly allow redirects to external URLs Aug 29, 2022
@danez danez requested a review from tinfoil-knight August 29, 2022 12:24
@danez
Copy link
Contributor Author

danez commented Aug 29, 2022

About the commit message, it is true that the commit message on its own sounds weird with the double fix, but in the changelog the semantic type will be removed and would just read redirects to external URLs, which is a little undescriptive for a changelog message :)
But I changed the PR title now and reworded it, so it will read better.

Copy link
Contributor

@tinfoil-knight tinfoil-knight left a comment

Choose a reason for hiding this comment

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

LGTM!

Nice catch on the duplicate tests. Weird that they were in the repo for so long.

@danez danez added the automerge Add to Kodiak auto merge queue label Aug 30, 2022
Copy link
Contributor

@khendrikse khendrikse left a comment

Choose a reason for hiding this comment

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

lets go boi

@kodiakhq kodiakhq bot merged commit afd67d5 into main Aug 30, 2022
@kodiakhq kodiakhq bot deleted the redirects branch August 30, 2022 14:22
khendrikse pushed a commit that referenced this pull request Sep 9, 2022
* fix: fix redirects to external URLs

* chore: fix nit from review

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Add to Kodiak auto merge queue type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary url escaping breaks request paths Bug: re-write rules are not applied for non static servers
4 participants