-
Notifications
You must be signed in to change notification settings - Fork 0
fix: resolve relative URLs in link click handler #8
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 |
|---|---|---|
|
|
@@ -96,32 +96,28 @@ export const uppercaseScript = ` | |
| if (!link) return; | ||
| var href = link.getAttribute('href'); | ||
| if (!href) return; | ||
| // let proxy-rewritten links work naturally in the iframe | ||
| // already proxied — just tell parent the real URL | ||
| if (href.startsWith(PROXY_PREFIX)) { | ||
| // extract the real URL and tell the parent to update the URL bar | ||
| var realUrl = href.slice(PROXY_PREFIX.length); | ||
| try { | ||
| window.top.postMessage({ type: 'navigate', url: realUrl }, '*'); | ||
| } catch(ex) {} | ||
| try { window.top.postMessage({ type: 'navigate', url: realUrl }, '*'); } catch(ex) {} | ||
| return; | ||
| } | ||
| // absolute external link not yet rewritten | ||
| if (href.startsWith('http://') || href.startsWith('https://')) { | ||
| // resolve via the same logic as fetch/XHR | ||
| var resolved = resolveForProxy(href); | ||
| if (resolved !== href) { | ||
| e.preventDefault(); | ||
| var proxied = PROXY_PREFIX + href; | ||
| try { | ||
| window.top.postMessage({ type: 'navigate', url: href }, '*'); | ||
| } catch(ex) {} | ||
| window.location.href = proxied; | ||
| var navigateUrl = resolved.startsWith(PROXY_PREFIX) ? resolved.slice(PROXY_PREFIX.length) : href; | ||
| try { window.top.postMessage({ type: 'navigate', url: navigateUrl }, '*'); } catch(ex) {} | ||
| window.location.href = resolved; | ||
| } | ||
| }, 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('#')) { | ||
| if (!url || url.startsWith(PROXY_PREFIX) || url.startsWith('data:') || url.startsWith('blob:') || url.startsWith('javascript:') || url.startsWith('#') || url.startsWith('mailto:') || url.startsWith('tel:')) { | ||
| return url; | ||
|
Comment on lines
116
to
118
|
||
| } | ||
| if (url.startsWith('http://') || url.startsWith('https://')) { | ||
| if (url.startsWith('http://') || url.startsWith('https://') || url.startsWith('//')) { | ||
| return PROXY_PREFIX + url; | ||
| } | ||
|
Comment on lines
+120
to
122
|
||
| // relative URL — resolve against the target origin | ||
|
|
||
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.
This test asserts on raw substrings in the generated HTML (e.g.,
resolveForProxy(href)), which is brittle to formatting/minification changes and doesn’t actually validate that relative clicks navigate correctly. If possible, make the assertion more structural (e.g., regex that tolerates whitespace) and/or add a focused unit test around the URL resolution behavior used by the click handler (relative, protocol-relative, mailto/tel).