Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ async function readAuthFromStore(
}
}

function isLoopbackHostname(hostname: string): boolean {
const normalized = hostname.replace(/^\[|\]$/g, '').toLowerCase();
if (normalized === 'localhost' || normalized === '::1') return true;
const octets = normalized.split('.').map(Number);
return octets.length === 4 && octets.every(Number.isInteger) && octets[0] === 127;
}

export function getBaseUrl(
options?: Record<string, unknown>,
authBaseUrl?: string,
Expand All @@ -266,6 +273,10 @@ export function getBaseUrl(
warn(`Ignoring unsupported baseURL protocol: ${sanitizeForLog(parsed.protocol)}`);
continue;
}
if (parsed.protocol === 'http:' && !isLoopbackHostname(parsed.hostname)) {
warn(`Ignoring insecure remote HTTP baseURL: ${sanitizeForLog(trimmed)}`);
continue;
}
return trimmed.replace(/\/+$/, '');
} catch {
warn(`Ignoring invalid baseURL: ${sanitizeForLog(trimmed)}`);
Expand Down
10 changes: 9 additions & 1 deletion test/plugin.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,12 @@ test('authorize stores baseURL and optional apiKey as JSON', async () => {
const parsed = JSON.parse(result.key);
assert.equal(parsed.baseURL, 'http://127.0.0.1:8317/v1');
assert.equal(parsed.apiKey, '');
});
});
test('getBaseUrl rejects plaintext HTTP for remote hosts but allows loopback', async () => {
const { getBaseUrl } = await import('../dist/src/plugin.js');
assert.equal(getBaseUrl({ baseURL: 'http://localhost:8317/v1' }), 'http://localhost:8317/v1');
assert.equal(getBaseUrl({ baseURL: 'http://127.42.0.1:8317/v1' }), 'http://127.42.0.1:8317/v1');
assert.equal(getBaseUrl({ baseURL: 'http://[::1]:8317/v1' }), 'http://[::1]:8317/v1');
assert.equal(getBaseUrl({ baseURL: 'http://proxy.example/v1' }), 'http://localhost:8317/v1');
assert.equal(getBaseUrl({ baseURL: 'http://192.168.1.10:8317/v1' }), 'http://localhost:8317/v1');
});