-
Notifications
You must be signed in to change notification settings - Fork 0
fix: resolve relative URLs in client-side fetch/XHR patches #5
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,22 +116,42 @@ export const uppercaseScript = ` | |
| } | ||
| }, true); | ||
|
|
||
| // resolve any URL to a proxied URL | ||
| function resolveForProxy(url) { | ||
| if (!url || url.startsWith(PROXY_PREFIX) || url.startsWith('data:') || url.startsWith('blob:') || url.startsWith('javascript:') || url.startsWith('#')) { | ||
| return url; | ||
| } | ||
| if (url.startsWith('http://') || url.startsWith('https://')) { | ||
| return PROXY_PREFIX + url; | ||
| } | ||
| // relative URL — resolve against the target origin | ||
| var path = window.location.pathname; | ||
| if (path.startsWith(PROXY_PREFIX)) { | ||
| var targetUrl = path.slice(PROXY_PREFIX.length) + window.location.search; | ||
| try { | ||
| var resolved = new URL(url, targetUrl).href; | ||
| return PROXY_PREFIX + resolved; | ||
| } catch(e) {} | ||
| } | ||
| return url; | ||
|
Comment on lines
+120
to
+136
|
||
| } | ||
|
|
||
| // patch fetch | ||
| var origFetch = window.fetch; | ||
| window.fetch = function(input, init) { | ||
| if (typeof input === 'string' && (input.startsWith('http://') || input.startsWith('https://'))) { | ||
| input = PROXY_PREFIX + input; | ||
| } else if (input instanceof Request && (input.url.startsWith('http://') || input.url.startsWith('https://')) && !input.url.includes(PROXY_PREFIX)) { | ||
| input = new Request(PROXY_PREFIX + input.url, input); | ||
| if (typeof input === 'string') { | ||
| input = resolveForProxy(input); | ||
| } else if (input instanceof Request && !input.url.includes(PROXY_PREFIX)) { | ||
| input = new Request(resolveForProxy(input.url), input); | ||
|
Comment on lines
+144
to
+145
|
||
| } | ||
| return origFetch.call(this, input, init); | ||
| }; | ||
|
|
||
| // patch XHR | ||
| var origOpen = XMLHttpRequest.prototype.open; | ||
| XMLHttpRequest.prototype.open = function(method, url) { | ||
| if (typeof url === 'string' && (url.startsWith('http://') || url.startsWith('https://')) && !url.includes(PROXY_PREFIX)) { | ||
| url = PROXY_PREFIX + url; | ||
| if (typeof url === 'string') { | ||
| url = resolveForProxy(url); | ||
| } | ||
| return origOpen.apply(this, [method, url, ...Array.prototype.slice.call(arguments, 2)]); | ||
| }; | ||
|
|
||
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.
resolveForProxy()claims to handle already-proxied/browse/paths, but it only checksurl.startsWith(PROXY_PREFIX). If application code constructs an absolute proxied URL (e.g.new URL('/api', location.href).toString()on a proxied page ->https://<proxy-origin>/browse/https://<target>/api), this function treats it as an absolutehttps://URL and prefixes again, producing a broken double-proxy URL. Consider detecting proxy-origin absolute URLs whosepathnamestarts withPROXY_PREFIXand returning them unchanged (or normalized to a relative/browse/...), before thehttp(s)branch.