Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix(cli): normalize bare hosts with numeric ports (#1443)

Captain107e0f2a24f1
diff --git a/bin/utils/url.ts b/bin/utils/url.ts
index 0bfca9dc..1b0a1904 100644
--- a/bin/utils/url.ts
+++ b/bin/utils/url.ts
@@ -20,6 +20,15 @@ export function getDomain(inputUrl: string): string | null {
// Appends 'https://' protocol to the URL if not present.
export function appendProtocol(inputUrl: string): string {
+ // URL accepts a bare host:port as an opaque scheme. Recognize dotted
+ // hosts and localhost without reinterpreting other schemes like tel:.
+ if (
+ /^(?:localhost|[a-z0-9-]+(?:\.[a-z0-9-]+)+\.?):\d+(?:[/?#]|$)/i.test(
+ inputUrl,
+ )
+ ) {
+ return `https://${inputUrl}`;
+ }
try {
new URL(inputUrl);
return inputUrl;
diff --git a/dist/cli.js b/dist/cli.js
index 96fd78f7..b5d2f796 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -954,6 +954,11 @@ function getDomain(inputUrl) {
}
// Appends 'https://' protocol to the URL if not present.
function appendProtocol(inputUrl) {
+ // URL accepts a bare host:port as an opaque scheme. Recognize dotted
+ // hosts and localhost without reinterpreting other schemes like tel:.
+ if (/^(?:localhost|[a-z0-9-]+(?:\.[a-z0-9-]+)+\.?):\d+(?:[/?#]|$)/i.test(inputUrl)) {
+ return `https://${inputUrl}`;
+ }
try {
new URL(inputUrl);
return inputUrl;
diff --git a/tests/unit/url.test.ts b/tests/unit/url.test.ts
index ac716d45..29b57d45 100644
--- a/tests/unit/url.test.ts
+++ b/tests/unit/url.test.ts
@@ -38,17 +38,28 @@ describe('getDomain', () => {
describe('appendProtocol', () => {
it('leaves an already-schemed URL unchanged', () => {
expect(appendProtocol('https://github.com')).toBe('https://github.com');
- expect(appendProtocol('http://localhost')).toBe('http://localhost');
+ expect(appendProtocol('http://localhost:3000')).toBe(
+ 'http://localhost:3000',
+ );
+ expect(appendProtocol('example.com://8443')).toBe('example.com://8443');
+ expect(appendProtocol('tel:1234')).toBe('tel:1234');
+ expect(appendProtocol('file:///tmp/index.html')).toBe(
+ 'file:///tmp/index.html',
+ );
});
it('prepends https:// to a bare host', () => {
expect(appendProtocol('github.com')).toBe('https://github.com');
});
- it('leaves a bare host:port unchanged (WHATWG parses "localhost:" as a scheme)', () => {
- // Documents a known parser quirk: new URL('localhost:3000') succeeds with
- // scheme "localhost:", so the input is treated as already-schemed.
- expect(appendProtocol('localhost:3000')).toBe('localhost:3000');
+ it.each([
+ 'localhost:3000',
+ 'example.com:8443/dashboard',
+ 'example.com.:8443/dashboard',
+ 'localhost:3000?view=summary',
+ 'example.com:8443#section',
+ ])('prepends https:// to a bare host with a port: %s', (input) => {
+ expect(appendProtocol(input)).toBe(`https://${input}`);
});
});
@@ -65,4 +76,8 @@ describe('normalizeUrl', () => {
// appendProtocol('') -> 'https://', which new URL() rejects.
expect(() => normalizeUrl('')).toThrow('is invalid');
});
+
+ it('rejects an out-of-range port on a bare host', () => {
+ expect(() => normalizeUrl('example.com:65536')).toThrow('is invalid');
+ });
});