Line data Source code
1 : /**
2 : * Whitelisting helper for enabling SSO on safe, local-only hosts.
3 : *
4 : * Built-ins (always allowed):
5 : * • 'localhost' and any subdomain of '.localhost' (e.g., app.localhost)
6 : * • IPv4 loopback 127.0.0.0/8 (e.g., 127.0.0.1, 127.1.2.3)
7 : * • IPv6 loopback ::1 (supports compressed/expanded textual forms)
8 : *
9 : * No environment variables are used. To add more exact hostnames,
10 : * edit HOST_WHITELIST below.
11 : */
12 :
13 2356 : const HOST_WHITELIST: string[] = ['localhost']
14 :
15 : /** Normalize for comparison: lowercase, strip port/brackets, trim trailing dot. */
16 26 : export function normalizeHost(input: string): string {
17 26 : let h = (input || '').trim().toLowerCase()
18 :
19 : // Trim a trailing dot: 'localhost.' -> 'localhost'
20 26 : h = h.replace(/\.$/, '')
21 :
22 : // Remove ':port' safely.
23 : // Case 1: [IPv6]:port
24 26 : const mBracket = h.match(/^\[([^\]]+)\]:(\d+)$/)
25 26 : if (mBracket) {
26 0 : h = mBracket[1] // keep only the host inside the brackets
27 : } else {
28 : // Case 2: hostname/IPv4:port (exactly one ':')
29 26 : const mPort = h.match(/^([^:]+):(\d+)$/)
30 26 : if (mPort) h = mPort[1]
31 : }
32 :
33 : // Strip any remaining brackets (e.g., '[::1]' -> '::1')
34 26 : h = h.replace(/^\[|\]$/g, '')
35 :
36 26 : return h
37 : }
38 :
39 : /** Whether the host can only name the machine the URL was written on. */
40 0 : export function isLoopbackHost(rawHost: string): boolean {
41 0 : const host = normalizeHost(rawHost)
42 0 : return (
43 : isLocalhostLabel(host) ||
44 : isIPv4Loopback(host) ||
45 : isIPv6Loopback(host) ||
46 : isUnspecifiedAddress(host) ||
47 : isIPv4MappedLoopback(host)
48 : )
49 : }
50 :
51 : /** Public check used by the UI. */
52 13 : export function isHostWhitelisted(rawHost: string): boolean {
53 13 : const host = normalizeHost(rawHost)
54 13 : if (isLocalhostLabel(host)) return true
55 0 : if (isIPv4Loopback(host)) return true
56 0 : if (isIPv6Loopback(host)) return true
57 0 : if (isComfyOrgHost(host)) return true
58 : const normalizedList = HOST_WHITELIST.map(normalizeHost)
59 0 : return normalizedList.includes(host)
60 : }
61 :
62 : /* -------------------- Helpers -------------------- */
63 :
64 13 : function isLocalhostLabel(h: string): boolean {
65 : // 'localhost' and any subdomain (e.g., 'app.localhost')
66 13 : return h === 'localhost' || h.endsWith('.localhost')
67 : }
68 :
69 2356 : const IPV4_OCTET = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|0?\\d?\\d)'
70 2356 : const V4_LOOPBACK_RE = new RegExp(
71 : '^127\\.' + IPV4_OCTET + '\\.' + IPV4_OCTET + '\\.' + IPV4_OCTET + '$'
72 : )
73 :
74 0 : function isIPv4Loopback(h: string): boolean {
75 : // 127/8 with strict 0–255 octets (leading zeros allowed, e.g., 127.000.000.001)
76 0 : return V4_LOOPBACK_RE.test(h)
77 : }
78 :
79 : // Fully expanded IPv6 loopback: 0:0:0:0:0:0:0:1 (allow leading zeros up to 4 chars)
80 2356 : const V6_FULL_LOOPBACK_RE = /^(?:0{1,4}:){7}0{0,3}1$/i
81 :
82 : // Compressed IPv6 loopback forms around '::' with only zero groups before the final :1
83 : // - Left side: zero groups separated by ':' (no trailing colon required)
84 : // - Right side: zero groups each followed by ':' (so the final ':1' is provided by the pattern)
85 : // The final group is exactly value 1, with up to 3 leading zeros (e.g., '0001').
86 2356 : const V6_COMPRESSED_LOOPBACK_RE =
87 : /^((?:0{1,4}(?::0{1,4}){0,6})?)::((?:0{1,4}:){0,6})0{0,3}1$/i
88 :
89 0 : function isIPv6Loopback(h: string): boolean {
90 : // Exact full form: 0:0:0:0:0:0:0:1 (with up to 3 leading zeros on the final "1" group)
91 0 : if (V6_FULL_LOOPBACK_RE.test(h)) return true
92 :
93 : // Compressed forms that still equal ::1 (e.g., ::1, ::0001, 0:0::1, ::0:1, etc.)
94 0 : const m = h.match(V6_COMPRESSED_LOOPBACK_RE)
95 0 : if (!m) return false
96 :
97 : // Count explicit zero groups on each side of '::' to ensure at least one group is compressed.
98 : // (leftCount + rightCount) must be ≤ 6 so that the total expanded groups = 8.
99 : const leftCount = m[1] ? (m[1].match(/0{1,4}:/gi)?.length ?? 0) : 0
100 : const rightCount = m[2] ? (m[2].match(/0{1,4}:/gi)?.length ?? 0) : 0
101 :
102 : // Require that at least one group was actually compressed: i.e., leftCount + rightCount ≤ 6.
103 0 : return leftCount + rightCount <= 6
104 : }
105 :
106 0 : function isUnspecifiedAddress(h: string): boolean {
107 0 : return h === '0.0.0.0' || h === '::'
108 : }
109 :
110 2356 : const V6_MAPPED_HEX_RE = /^::ffff:([0-9a-f]{1,4}):[0-9a-f]{1,4}$/i
111 2356 : const V6_MAPPED_DOTTED_RE = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/i
112 :
113 0 : function isIPv4MappedLoopback(h: string): boolean {
114 0 : const hex = V6_MAPPED_HEX_RE.exec(h)
115 0 : if (hex) {
116 : const firstOctet = parseInt(hex[1], 16) >> 8
117 0 : return firstOctet === 127
118 : }
119 0 : const dotted = V6_MAPPED_DOTTED_RE.exec(h)
120 0 : return dotted ? isIPv4Loopback(dotted[1]) : false
121 : }
122 :
123 2356 : const COMFY_ORG_HOST = /\.comfy\.org$/
124 :
125 0 : function isComfyOrgHost(h: string): boolean {
126 0 : return COMFY_ORG_HOST.test(h)
127 : }
|