Auto-detect scheme for AUTHGUARD_URL instead of always assuming https
A bare host:port (e.g. a LAN AuthGuard reachable only over plain HTTP) was always defaulting to https, so a TLS handshake against a plain-HTTP AuthGuard failed at the transport level. Now a loopback, RFC1918/RFC4193 private, or link-local host (or a .local name) defaults to http instead; an explicit http:// or https:// is still always honored as-is. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
730bf0463f
commit
449bb6fd6a
8 files changed
+241
-27
No files matched your search
@@ -58,9 +58,11 @@ export class AuthClient {
|
||||
/**
|
||||
* @param {string} baseUrl - AuthGuard's base URL (e.g. from AUTHGUARD_URL), no trailing slash required.
|
||||
* May be given without a scheme (e.g. "auth.example.com" or "192.168.1.50:8080") -
|
||||
* "https://" is assumed in that case, so a deployment only needs to name the
|
||||
* host, not repeat the scheme; an explicit "http://" (for a plain-HTTP
|
||||
* local/LAN AuthGuard) is left as-is.
|
||||
* the scheme is then inferred from the host: a loopback, RFC1918/RFC4193 private,
|
||||
* or link-local address, or a ".local" name, defaults to "http://" (a plain-HTTP
|
||||
* AuthGuard on a local network is the common case for those), anything else
|
||||
* defaults to "https://". An explicit "http://" or "https://" is always left
|
||||
* as-is, overriding the inference.
|
||||
* @param {{cookieName?: string, timeoutMs?: number, fetchImpl?: typeof fetch}} [options]
|
||||
*/
|
||||
constructor(baseUrl, options = {}) {
|
||||
@@ -73,7 +75,47 @@ export class AuthClient {
|
||||
static _normalizeBaseUrl(baseUrl) {
|
||||
const trimmed = String(baseUrl ?? '').trim().replace(/\/+$/, '');
|
||||
if (!trimmed) return trimmed;
|
||||
return /^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`;
|
||||
if (/^https?:\/\//i.test(trimmed)) return trimmed;
|
||||
return AuthClient._isPrivateHost(trimmed) ? `http://${trimmed}` : `https://${trimmed}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports whether hostport (a bare "host" or "host:port", no scheme) names
|
||||
* a loopback, RFC1918/RFC4193 private, or link-local address, or a
|
||||
* ".local" mDNS name - the kind of host a plain-HTTP AuthGuard is
|
||||
* typically reachable at on a local network, as opposed to a public
|
||||
* hostname expected to terminate TLS itself. Never performs DNS
|
||||
* resolution: only literal IPs and the "localhost"/".local" names are
|
||||
* recognized, so a real public hostname that merely happens to resolve to
|
||||
* a private address still defaults to https.
|
||||
*/
|
||||
static _isPrivateHost(hostport) {
|
||||
let host = hostport;
|
||||
if (host.startsWith('[')) {
|
||||
const end = host.indexOf(']');
|
||||
if (end !== -1) host = host.slice(1, end);
|
||||
} else {
|
||||
const parts = host.split(':');
|
||||
if (parts.length === 2 && /^\d+$/.test(parts[1])) host = parts[0];
|
||||
}
|
||||
host = host.toLowerCase();
|
||||
if (host === 'localhost' || host.endsWith('.local')) return true;
|
||||
const ipv4 = host.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
|
||||
if (ipv4) {
|
||||
const a = Number(ipv4[1]);
|
||||
const b = Number(ipv4[2]);
|
||||
return (
|
||||
a === 127 ||
|
||||
a === 10 ||
|
||||
(a === 172 && b >= 16 && b <= 31) ||
|
||||
(a === 192 && b === 168) ||
|
||||
(a === 169 && b === 254)
|
||||
);
|
||||
}
|
||||
if (host === '::1') return true;
|
||||
if (/^fe80:/.test(host)) return true;
|
||||
if (/^f[cd][0-9a-f]{2}:/.test(host)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// --- session validation (the original, minimal surface) -----------------
|
||||
|
||||
@@ -31,15 +31,32 @@ function recordingFetch(responses) {
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
test('constructor assumes https for a bare host', () => {
|
||||
test('constructor assumes https for a bare public host', () => {
|
||||
assert.equal(new AuthClient('auth.example.com').baseUrl, 'https://auth.example.com');
|
||||
assert.equal(new AuthClient('192.168.1.50:8080').baseUrl, 'https://192.168.1.50:8080');
|
||||
assert.equal(new AuthClient('auth.example.com:8080').baseUrl, 'https://auth.example.com:8080');
|
||||
});
|
||||
|
||||
test('constructor assumes http for a bare private/loopback/link-local host', () => {
|
||||
assert.equal(new AuthClient('192.168.1.50:8080').baseUrl, 'http://192.168.1.50:8080');
|
||||
assert.equal(new AuthClient('10.1.2.3:8080').baseUrl, 'http://10.1.2.3:8080');
|
||||
assert.equal(new AuthClient('172.20.0.5:8080').baseUrl, 'http://172.20.0.5:8080');
|
||||
assert.equal(new AuthClient('127.0.0.1:8080').baseUrl, 'http://127.0.0.1:8080');
|
||||
assert.equal(new AuthClient('169.254.1.1:8080').baseUrl, 'http://169.254.1.1:8080');
|
||||
assert.equal(new AuthClient('localhost:8080').baseUrl, 'http://localhost:8080');
|
||||
assert.equal(new AuthClient('authguard.local:8080').baseUrl, 'http://authguard.local:8080');
|
||||
assert.equal(new AuthClient('[::1]:8080').baseUrl, 'http://[::1]:8080');
|
||||
assert.equal(new AuthClient('[fd00::1]:8080').baseUrl, 'http://[fd00::1]:8080');
|
||||
});
|
||||
|
||||
test('constructor assumes https for a 172.x address outside the 16-31 private range', () => {
|
||||
assert.equal(new AuthClient('172.32.0.5:8080').baseUrl, 'https://172.32.0.5:8080');
|
||||
});
|
||||
|
||||
test('constructor leaves an explicit scheme alone', () => {
|
||||
assert.equal(new AuthClient('http://auth.example.com').baseUrl, 'http://auth.example.com');
|
||||
assert.equal(new AuthClient('https://auth.example.com').baseUrl, 'https://auth.example.com');
|
||||
assert.equal(new AuthClient('HTTP://auth.example.com').baseUrl, 'HTTP://auth.example.com');
|
||||
assert.equal(new AuthClient('https://192.168.1.50:8080').baseUrl, 'https://192.168.1.50:8080');
|
||||
});
|
||||
|
||||
test('constructor strips a trailing slash before checking the scheme', () => {
|
||||
|
||||
Reference in new issue
Block a user