Normalize protocol-relative remote host input as https (#3971)
This commit is contained in:
1 parent
4804a9824f
commit
c49d424e3a
2 files changed
+68
-4
No files matched your search
@@ -59,6 +59,70 @@ describe("remote", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("treats a protocol-relative host as https", () => {
|
||||
expect(
|
||||
resolveRemotePairingTarget({
|
||||
host: "//remote.example.com",
|
||||
pairingCode: "pairing-token",
|
||||
}),
|
||||
).toEqual({
|
||||
credential: "pairing-token",
|
||||
httpBaseUrl: "https://remote.example.com/",
|
||||
wsBaseUrl: "wss://remote.example.com/",
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves the port when normalizing a protocol-relative host", () => {
|
||||
expect(
|
||||
resolveRemotePairingTarget({
|
||||
host: "//remote.example.com:3000",
|
||||
pairingCode: "pairing-token",
|
||||
}),
|
||||
).toEqual({
|
||||
credential: "pairing-token",
|
||||
httpBaseUrl: "https://remote.example.com:3000/",
|
||||
wsBaseUrl: "wss://remote.example.com:3000/",
|
||||
});
|
||||
});
|
||||
|
||||
it("normalizes a protocol-relative host from a hosted pairing link", () => {
|
||||
expect(
|
||||
resolveRemotePairingTarget({
|
||||
pairingUrl: "https://app.t3.codes/pair?host=%2F%2Fremote.example.com#token=pairing-token",
|
||||
}),
|
||||
).toEqual({
|
||||
credential: "pairing-token",
|
||||
httpBaseUrl: "https://remote.example.com/",
|
||||
wsBaseUrl: "wss://remote.example.com/",
|
||||
});
|
||||
});
|
||||
|
||||
it("collapses extra leading slashes instead of producing an empty host", () => {
|
||||
expect(
|
||||
resolveRemotePairingTarget({
|
||||
host: "///example.com",
|
||||
pairingCode: "pairing-token",
|
||||
}),
|
||||
).toEqual({
|
||||
credential: "pairing-token",
|
||||
httpBaseUrl: "https://example.com/",
|
||||
wsBaseUrl: "wss://example.com/",
|
||||
});
|
||||
});
|
||||
|
||||
it("does not double-prepend https when the host already carries a scheme", () => {
|
||||
expect(
|
||||
resolveRemotePairingTarget({
|
||||
host: "//https://example.com",
|
||||
pairingCode: "pairing-token",
|
||||
}),
|
||||
).toEqual({
|
||||
credential: "pairing-token",
|
||||
httpBaseUrl: "https://example.com/",
|
||||
wsBaseUrl: "wss://example.com/",
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves host ports when normalizing a bare host input", () => {
|
||||
expect(
|
||||
resolveRemotePairingTarget({
|
||||
|
||||
@@ -81,10 +81,10 @@ const normalizeRemoteBaseUrl = (
|
||||
throw new RemoteBackendUrlMissingError();
|
||||
}
|
||||
|
||||
const normalizedInput =
|
||||
/^[a-zA-Z][a-zA-Z\d+-]*:\/\//.test(trimmed) || trimmed.startsWith("//")
|
||||
? trimmed
|
||||
: `https://${trimmed}`;
|
||||
const withoutLeadingSlashes = trimmed.replace(/^\/+/, "");
|
||||
const normalizedInput = /^[a-zA-Z][a-zA-Z\d+-]*:\/\//.test(withoutLeadingSlashes)
|
||||
? withoutLeadingSlashes
|
||||
: `https://${withoutLeadingSlashes}`;
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(normalizedInput);
|
||||
|
||||
Reference in new issue
Block a user