Repository navigation
Fix SSH browser loopback fetches across ports #3820
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6901f5a
Expose missing remote loopback browser runtime bridge
austinywang a83009a
Route remote browser loopback APIs through the proxy alias
austinywang daaf40d
Broaden remote loopback browser bridge coverage
austinywang 9be789e
Centralize remote loopback alias ownership
austinywang ef2350e
Keep loopback bridge constants in sync
austinywang e73eb8e
Restore generated bridge script return
austinywang 1ba943d
Separate remote loopback runtime bridge ownership
austinywang a38ae27
Preserve TLS hostnames in remote loopback bridge
austinywang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import Foundation | ||
|
|
||
| enum RemoteLoopbackRuntimeBridge { | ||
| static let runtimeBridgeScriptSource: String = { | ||
| let exactLoopbackHostLiterals = RemoteLoopbackProxyAlias.exactLoopbackHosts | ||
| .sorted() | ||
| .map(javaScriptStringLiteral) | ||
| .joined(separator: ", ") | ||
| return """ | ||
| (() => { | ||
| const aliasHost = \(javaScriptStringLiteral(RemoteLoopbackProxyAlias.aliasHost)); | ||
| const canonicalLoopbackHost = \(javaScriptStringLiteral(RemoteLoopbackProxyAlias.canonicalLoopbackHost)); | ||
| const exactLoopbackHosts = new Set([\(exactLoopbackHostLiterals)]); | ||
| const normalizeHost = (host) => { | ||
| let value = String(host || '').trim().toLowerCase(); | ||
| if (!value) return ''; | ||
| if (value.endsWith('.')) value = value.slice(0, -1); | ||
| if (value.startsWith('[') && value.endsWith(']')) { | ||
| value = value.slice(1, -1); | ||
| } | ||
| return value; | ||
| }; | ||
| const normalizedAliasHost = normalizeHost(aliasHost); | ||
| const currentHost = normalizeHost(window.location.hostname); | ||
| let effectiveHost = currentHost; | ||
| if (!effectiveHost && window.location.protocol === 'about:') { | ||
| try { | ||
| effectiveHost = normalizeHost(new URL(document.baseURI).hostname); | ||
| } catch (_) {} | ||
| } | ||
| if (effectiveHost !== normalizedAliasHost && !effectiveHost.endsWith(`.${normalizedAliasHost}`)) { | ||
| return true; | ||
| } | ||
| if (window.__cmuxRemoteLoopbackRuntimeBridgeInstalled) return true; | ||
| window.__cmuxRemoteLoopbackRuntimeBridgeInstalled = true; | ||
|
|
||
| const loopbackAliasHost = (host) => { | ||
| const normalizedHost = normalizeHost(host); | ||
| if (exactLoopbackHosts.has(normalizedHost)) { | ||
| return aliasHost; | ||
| } | ||
| const suffix = `.${canonicalLoopbackHost}`; | ||
| if (normalizedHost.endsWith(suffix) && normalizedHost.length > suffix.length) { | ||
| return `${normalizedHost.slice(0, -suffix.length)}.${aliasHost}`; | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| const rewriteLoopbackURL = (input) => { | ||
| if (typeof input !== 'string' && !(input instanceof URL)) { | ||
| return input; | ||
| } | ||
| const original = input instanceof URL ? input.href : input; | ||
| let parsed; | ||
| try { | ||
| parsed = new URL(original, document.baseURI); | ||
| } catch { | ||
| return input; | ||
| } | ||
| // Only rewrite cleartext HTTP/WebSocket requests. TLS-bearing `https:` and | ||
| // `wss:` validate certificates against the URL hostname, so aliasing them | ||
| // would change SNI/certificate expectations for localhost dev servers. | ||
| if (parsed.protocol !== 'http:' && parsed.protocol !== 'ws:') { | ||
| return input; | ||
| } | ||
| const rewrittenHost = loopbackAliasHost(parsed.hostname); | ||
| if (!rewrittenHost) { | ||
| return input; | ||
| } | ||
| parsed.hostname = rewrittenHost; | ||
| return parsed.href; | ||
| }; | ||
|
|
||
| Object.defineProperty(window, '__cmuxRewriteRemoteLoopbackURL', { | ||
| value: rewriteLoopbackURL, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| const nativeFetch = window.fetch ? window.fetch.bind(window) : null; | ||
| if (nativeFetch) { | ||
| window.fetch = (input, init) => { | ||
| if (typeof Request !== 'undefined' && input instanceof Request) { | ||
| const rewrittenURL = rewriteLoopbackURL(input.url); | ||
| if (rewrittenURL !== input.url) { | ||
| return nativeFetch(new Request(rewrittenURL, input), init); | ||
| } | ||
| return nativeFetch(input, init); | ||
| } | ||
| return nativeFetch(rewriteLoopbackURL(input), init); | ||
| }; | ||
| } | ||
|
|
||
| const nativeXHROpen = window.XMLHttpRequest && window.XMLHttpRequest.prototype.open; | ||
| if (nativeXHROpen) { | ||
| window.XMLHttpRequest.prototype.open = function(method, url, ...rest) { | ||
| return nativeXHROpen.call(this, method, rewriteLoopbackURL(url), ...rest); | ||
| }; | ||
| } | ||
|
|
||
| const NativeWebSocket = window.WebSocket; | ||
| if (typeof NativeWebSocket === 'function') { | ||
| const CmuxWebSocket = function(url, protocols) { | ||
| const rewrittenURL = rewriteLoopbackURL(url); | ||
| if (protocols === undefined) { | ||
| return new NativeWebSocket(rewrittenURL); | ||
| } | ||
| return new NativeWebSocket(rewrittenURL, protocols); | ||
| }; | ||
| CmuxWebSocket.prototype = NativeWebSocket.prototype; | ||
| Object.setPrototypeOf(CmuxWebSocket, NativeWebSocket); | ||
| window.WebSocket = CmuxWebSocket; | ||
| } | ||
|
|
||
| const NativeEventSource = window.EventSource; | ||
| if (typeof NativeEventSource === 'function') { | ||
| const CmuxEventSource = function(url, eventSourceInitDict) { | ||
| const rewrittenURL = rewriteLoopbackURL(url); | ||
| if (eventSourceInitDict === undefined) { | ||
| return new NativeEventSource(rewrittenURL); | ||
| } | ||
| return new NativeEventSource(rewrittenURL, eventSourceInitDict); | ||
| }; | ||
| CmuxEventSource.prototype = NativeEventSource.prototype; | ||
| Object.setPrototypeOf(CmuxEventSource, NativeEventSource); | ||
| window.EventSource = CmuxEventSource; | ||
| } | ||
|
|
||
| return true; | ||
| })(); | ||
| """ | ||
| }() | ||
|
|
||
| private static func javaScriptStringLiteral(_ value: String) -> String { | ||
| let escaped = value | ||
| .replacingOccurrences(of: "\\", with: "\\\\") | ||
| .replacingOccurrences(of: "'", with: "\\'") | ||
| .replacingOccurrences(of: "\n", with: "\\n") | ||
| .replacingOccurrences(of: "\r", with: "\\r") | ||
| return "'\(escaped)'" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.