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

add workaround to URL parser for Cloudflare Reverse Proxy bug #388

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions lib/cors-anywhere.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ function onProxyResponse(proxy, proxyReq, proxyRes, req, res) {
* @return {object} URL parsed using url.parse
*/
function parseURL(req_url) {
//Cloudflare reverse proxy has a bug of collapsing multiple /s to one
// https://community.cloudflare.com/t/worker-url-obj-parses-double-backslash-diff-from-chrome/210046
// https://community.cloudflare.com/t/bug-inconsistent-url-behaviour/98044
// https://community.cloudflare.com/t/worker-fetch-mangles-location-header-url-on-redirect-from-origin/311984
//so just make https://fooapp.herokuapp.com/http:/example.com/index.html
//parse correctly

//handling a corrupted good URL such as
//https://fooapp.herokuapp.com///example.com/index.html
//which corrupts to
//https://fooapp.herokuapp.com//example.com/index.html is skipped
//since its a much more rare URL format and unknown if it was really
//a corrupt protocol relative or a true server relative
//(unsupported, this is a proxy, we don't have endpoints or content)
req_url = req_url.replace(/^http(s?):\/(?!\/)/, 'http$1://');
var match = req_url.match(/^(?:(https?:)?\/\/)?(([^\/?]+?)(?::(\d{0,5})(?=[\/?]|$))?)([\/?][\S\s]*|$)/i);
// ^^^^^^^ ^^^^^^^^ ^^^^^^^ ^^^^^^^^^^^^
// 1:protocol 3:hostname 4:port 5:path + query string
Expand Down