|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { DocumentDBConnectionString } from '../utils/DocumentDBConnectionString'; |
| 7 | + |
| 8 | +// Safe default when we cannot positively identify an Azure endpoint. This is the |
| 9 | +// historical behavior and covers the public cloud (including private endpoints, |
| 10 | +// which still resolve under *.azure.com). |
| 11 | +const DEFAULT_ALLOWED_HOSTS = ['*.azure.com']; |
| 12 | + |
| 13 | +/** |
| 14 | + * Resolve the OIDC ALLOWED_HOSTS list from a connection string. |
| 15 | + * |
| 16 | + * ALLOWED_HOSTS is a security control: the driver only sends the OIDC token to a |
| 17 | + * server whose hostname matches one of these patterns. It must therefore stay a |
| 18 | + * curated allowlist. We intentionally do NOT echo the raw connection-string host |
| 19 | + * back into the allowlist: doing so would let an attacker-supplied host (e.g. |
| 20 | + * `evil.com`) widen its own allowlist to `*.evil.com`, defeating the control. |
| 21 | + * |
| 22 | + * Instead we recognize the Azure-family registrable suffix (`azure.<tld>`) and |
| 23 | + * allow only `*.azure.<tld>`. This keeps the public-cloud posture identical to |
| 24 | + * the previous hardcoded `*.azure.com` while transparently extending it to |
| 25 | + * sovereign clouds (`azure.us`, `azure.cn`, ...). Anything we cannot positively |
| 26 | + * classify as Azure falls back to the safe public-cloud default. |
| 27 | + * |
| 28 | + * Note: sovereign clouds also use a different Entra token endpoint; wiring that |
| 29 | + * up is tracked separately. This change only widens the host allowlist. |
| 30 | + */ |
| 31 | +export function getOidcAllowedHosts(connectionString: string): string[] { |
| 32 | + try { |
| 33 | + const parsed = new DocumentDBConnectionString(connectionString); |
| 34 | + |
| 35 | + // Deduplicate so a replica-set connection string with several hosts in |
| 36 | + // the same cloud yields a single `*.azure.<tld>` entry. |
| 37 | + const suffixes = new Set<string>(); |
| 38 | + for (const host of parsed.hosts ?? []) { |
| 39 | + const suffix = getAzureHostSuffix(host); |
| 40 | + if (suffix) { |
| 41 | + suffixes.add(suffix); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (suffixes.size > 0) { |
| 46 | + return [...suffixes].map((suffix) => `*.${suffix}`); |
| 47 | + } |
| 48 | + } catch { |
| 49 | + // Connection string could not be parsed; fall back to the safe default. |
| 50 | + } |
| 51 | + |
| 52 | + return DEFAULT_ALLOWED_HOSTS; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Returns the Azure-family registrable suffix (`azure.<tld>`) for a host, or |
| 57 | + * `undefined` if the host is not an Azure endpoint. |
| 58 | + * |
| 59 | + * The `azure` label must be the registrable second level (immediately before the |
| 60 | + * top-level label) so that lookalikes such as `azure.com.evil.com` are rejected. |
| 61 | + */ |
| 62 | +function getAzureHostSuffix(host: string): string | undefined { |
| 63 | + // `hosts` entries may carry a port (e.g. `cluster.azure.com:10255`) and an |
| 64 | + // IPv6 literal is wrapped in brackets; neither is part of the suffix. |
| 65 | + const hostname = host |
| 66 | + .replace(/^\[.*\]/, '') // drop bracketed IPv6 literals (never Azure FQDNs) |
| 67 | + .split(':')[0] |
| 68 | + ?.trim() |
| 69 | + .toLowerCase(); |
| 70 | + |
| 71 | + if (!hostname) { |
| 72 | + return undefined; |
| 73 | + } |
| 74 | + |
| 75 | + const labels = hostname.split('.'); |
| 76 | + if (labels.length < 2) { |
| 77 | + return undefined; |
| 78 | + } |
| 79 | + |
| 80 | + const topLevel = labels[labels.length - 1]; |
| 81 | + const secondLevel = labels[labels.length - 2]; |
| 82 | + |
| 83 | + if (secondLevel === 'azure' && topLevel.length > 0) { |
| 84 | + return `azure.${topLevel}`; |
| 85 | + } |
| 86 | + |
| 87 | + return undefined; |
| 88 | +} |
0 commit comments