Skip to content

Commit 81fbb8d

Browse files
bigcat88christian-byrne
authored andcommitted
feat(auth): Allow SSO login only for whitelisted addresses (localhost) (#5815)
## Summary Hide Google/GitHub SSO login options when the UI is accessed from **non‑local** addresses. This PR also adds a **static whitelist** (editable in code) so we can allow additional hosts if needed. Default whitelisted addresses: 1. `localhost` and any subdomain: `*.localhost` 2. IPv4 loopback `127.0.0.0/8` (e.g., `127.x.y.z`) 4. IPv6 loopback `::1` (including equivalent textual forms such as `::0001`) ## Changes - **What**: * Add `src/utils/hostWhitelist.ts` with `normalizeHost` and `isHostWhitelisted` helpers. * Update `SignInContent.vue` to **hide** SSO options when `isHostWhitelisted(normalizeHost(window.location.hostname))` returns `false`. - **Breaking**: * Users accessing from Runpod or other previously allowed **non‑local** hosts will **lose** SSO login options. If we need to keep SSO there, we should add those hosts to the whitelist in `hostWhitelist.ts`. ## Review Focus 1. Verify that logging in from local addresses (`localhost`, `*.localhost`, `127.0.0.1`, `::1`) **does not change** the current behavior: SSO is visible. 2. Verify that from a **non‑local** address, SSO options are **not** displayed. ## Screenshots (if applicable) UI opened from `192.168.2.109` address: <img width="500" height="990" alt="Screenshot From 2025-09-27 13-22-15" src="https://github.com/user-attachments/assets/c97b10a1-b069-43e4-a26b-a71eeb228a51" /> UI opened from default `127.0.0.1` address(nothing changed): <img width="462" height="955" alt="Screenshot From 2025-09-27 13-35-27" src="https://github.com/user-attachments/assets/bb2bf21c-dc8d-49cb-b48e-8fc6e408023c" /> ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-5815-feat-auth-Allow-SSO-login-only-for-whitelisted-addresses-localhost-27b6d73d365081ccbe84c034cf8e416d) by [Unito](https://www.unito.io)
1 parent 3abb35e commit 81fbb8d

File tree

3 files changed

+248
-30
lines changed

3 files changed

+248
-30
lines changed

src/components/dialog/content/SignInContent.vue

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,39 @@
4545
<span class="text-muted">{{ t('auth.login.orContinueWith') }}</span>
4646
</Divider>
4747

48-
<!-- Social Login Buttons -->
48+
<!-- Social Login Buttons (hidden if host not whitelisted) -->
4949
<div class="flex flex-col gap-6">
50-
<Button
51-
type="button"
52-
class="h-10"
53-
severity="secondary"
54-
outlined
55-
@click="signInWithGoogle"
56-
>
57-
<i class="pi pi-google mr-2"></i>
58-
{{
59-
isSignIn
60-
? t('auth.login.loginWithGoogle')
61-
: t('auth.signup.signUpWithGoogle')
62-
}}
63-
</Button>
64-
65-
<Button
66-
type="button"
67-
class="h-10"
68-
severity="secondary"
69-
outlined
70-
@click="signInWithGithub"
71-
>
72-
<i class="pi pi-github mr-2"></i>
73-
{{
74-
isSignIn
75-
? t('auth.login.loginWithGithub')
76-
: t('auth.signup.signUpWithGithub')
77-
}}
78-
</Button>
50+
<template v-if="ssoAllowed">
51+
<Button
52+
type="button"
53+
class="h-10"
54+
severity="secondary"
55+
outlined
56+
@click="signInWithGoogle"
57+
>
58+
<i class="pi pi-google mr-2"></i>
59+
{{
60+
isSignIn
61+
? t('auth.login.loginWithGoogle')
62+
: t('auth.signup.signUpWithGoogle')
63+
}}
64+
</Button>
65+
66+
<Button
67+
type="button"
68+
class="h-10"
69+
severity="secondary"
70+
outlined
71+
@click="signInWithGithub"
72+
>
73+
<i class="pi pi-github mr-2"></i>
74+
{{
75+
isSignIn
76+
? t('auth.login.loginWithGithub')
77+
: t('auth.signup.signUpWithGithub')
78+
}}
79+
</Button>
80+
</template>
7981

8082
<Button
8183
type="button"
@@ -149,6 +151,7 @@ import { useI18n } from 'vue-i18n'
149151
import { useFirebaseAuthActions } from '@/composables/auth/useFirebaseAuthActions'
150152
import { COMFY_PLATFORM_BASE_URL } from '@/config/comfyApi'
151153
import type { SignInData, SignUpData } from '@/schemas/signInSchema'
154+
import { isHostWhitelisted, normalizeHost } from '@/utils/hostWhitelist'
152155
import { isInChina } from '@/utils/networkUtil'
153156
154157
import ApiKeyForm from './signin/ApiKeyForm.vue'
@@ -164,6 +167,7 @@ const authActions = useFirebaseAuthActions()
164167
const isSecureContext = window.isSecureContext
165168
const isSignIn = ref(true)
166169
const showApiKeyForm = ref(false)
170+
const ssoAllowed = isHostWhitelisted(normalizeHost(window.location.hostname))
167171
168172
const toggleState = () => {
169173
isSignIn.value = !isSignIn.value

src/utils/hostWhitelist.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
const HOST_WHITELIST: string[] = ['localhost']
14+
15+
/** Normalize for comparison: lowercase, strip port/brackets, trim trailing dot. */
16+
export function normalizeHost(input: string): string {
17+
let h = (input || '').trim().toLowerCase()
18+
19+
// Trim a trailing dot: 'localhost.' -> 'localhost'
20+
h = h.replace(/\.$/, '')
21+
22+
// Remove ':port' safely.
23+
// Case 1: [IPv6]:port
24+
const mBracket = h.match(/^\[([^\]]+)\]:(\d+)$/)
25+
if (mBracket) {
26+
h = mBracket[1] // keep only the host inside the brackets
27+
} else {
28+
// Case 2: hostname/IPv4:port (exactly one ':')
29+
const mPort = h.match(/^([^:]+):(\d+)$/)
30+
if (mPort) h = mPort[1]
31+
}
32+
33+
// Strip any remaining brackets (e.g., '[::1]' -> '::1')
34+
h = h.replace(/^\[|\]$/g, '')
35+
36+
return h
37+
}
38+
39+
/** Public check used by the UI. */
40+
export function isHostWhitelisted(rawHost: string): boolean {
41+
const host = normalizeHost(rawHost)
42+
if (isLocalhostLabel(host)) return true
43+
if (isIPv4Loopback(host)) return true
44+
if (isIPv6Loopback(host)) return true
45+
const normalizedList = HOST_WHITELIST.map(normalizeHost)
46+
return normalizedList.includes(host)
47+
}
48+
49+
/* -------------------- Helpers -------------------- */
50+
51+
function isLocalhostLabel(h: string): boolean {
52+
// 'localhost' and any subdomain (e.g., 'app.localhost')
53+
return h === 'localhost' || h.endsWith('.localhost')
54+
}
55+
56+
const IPV4_OCTET = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|0?\\d?\\d)'
57+
const V4_LOOPBACK_RE = new RegExp(
58+
'^127\\.' + IPV4_OCTET + '\\.' + IPV4_OCTET + '\\.' + IPV4_OCTET + '$'
59+
)
60+
61+
function isIPv4Loopback(h: string): boolean {
62+
// 127/8 with strict 0–255 octets (leading zeros allowed, e.g., 127.000.000.001)
63+
return V4_LOOPBACK_RE.test(h)
64+
}
65+
66+
// Fully expanded IPv6 loopback: 0:0:0:0:0:0:0:1 (allow leading zeros up to 4 chars)
67+
const V6_FULL_LOOPBACK_RE = /^(?:0{1,4}:){7}0{0,3}1$/i
68+
69+
// Compressed IPv6 loopback forms around '::' with only zero groups before the final :1
70+
// - Left side: zero groups separated by ':' (no trailing colon required)
71+
// - Right side: zero groups each followed by ':' (so the final ':1' is provided by the pattern)
72+
// The final group is exactly value 1, with up to 3 leading zeros (e.g., '0001').
73+
const V6_COMPRESSED_LOOPBACK_RE =
74+
/^((?:0{1,4}(?::0{1,4}){0,6})?)::((?:0{1,4}:){0,6})0{0,3}1$/i
75+
76+
function isIPv6Loopback(h: string): boolean {
77+
// Exact full form: 0:0:0:0:0:0:0:1 (with up to 3 leading zeros on the final "1" group)
78+
if (V6_FULL_LOOPBACK_RE.test(h)) return true
79+
80+
// Compressed forms that still equal ::1 (e.g., ::1, ::0001, 0:0::1, ::0:1, etc.)
81+
const m = h.match(V6_COMPRESSED_LOOPBACK_RE)
82+
if (!m) return false
83+
84+
// Count explicit zero groups on each side of '::' to ensure at least one group is compressed.
85+
// (leftCount + rightCount) must be ≤ 6 so that the total expanded groups = 8.
86+
const leftCount = m[1] ? m[1].match(/0{1,4}:/gi)?.length ?? 0 : 0
87+
const rightCount = m[2] ? m[2].match(/0{1,4}:/gi)?.length ?? 0 : 0
88+
89+
// Require that at least one group was actually compressed: i.e., leftCount + rightCount ≤ 6.
90+
return leftCount + rightCount <= 6
91+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { isHostWhitelisted, normalizeHost } from '@/utils/hostWhitelist'
4+
5+
describe('hostWhitelist utils', () => {
6+
describe('normalizeHost', () => {
7+
it.each([
8+
['LOCALHOST', 'localhost'],
9+
['localhost.', 'localhost'], // trims trailing dot
10+
['localhost:5173', 'localhost'], // strips :port
11+
['127.0.0.1:5173', '127.0.0.1'], // strips :port
12+
['[::1]:5173', '::1'], // strips brackets + :port
13+
['[::1]', '::1'], // strips brackets
14+
['::1', '::1'], // leaves plain IPv6
15+
[' [::1] ', '::1'], // trims whitespace
16+
['APP.LOCALHOST', 'app.localhost'], // lowercases
17+
['example.com.', 'example.com'], // trims trailing dot
18+
['[2001:db8::1]:8443', '2001:db8::1'], // IPv6 with brackets+port
19+
['2001:db8::1', '2001:db8::1'] // plain IPv6 stays
20+
])('normalizeHost(%o) -> %o', (input, expected) => {
21+
expect(normalizeHost(input)).toBe(expected)
22+
})
23+
24+
it('does not strip non-numeric suffixes (not a port pattern)', () => {
25+
expect(normalizeHost('example.com:abc')).toBe('example.com:abc')
26+
expect(normalizeHost('127.0.0.1:abc')).toBe('127.0.0.1:abc')
27+
})
28+
})
29+
30+
describe('isHostWhitelisted', () => {
31+
describe('localhost label', () => {
32+
it.each([
33+
'localhost',
34+
'LOCALHOST',
35+
'localhost.',
36+
'localhost:5173',
37+
'foo.localhost',
38+
'Foo.Localhost',
39+
'sub.foo.localhost',
40+
'foo.localhost:5173'
41+
])('should allow %o', (input) => {
42+
expect(isHostWhitelisted(input)).toBe(true)
43+
})
44+
45+
it.each([
46+
'localhost.com',
47+
'evil-localhost',
48+
'notlocalhost',
49+
'foo.localhost.evil'
50+
])('should NOT allow %o', (input) => {
51+
expect(isHostWhitelisted(input)).toBe(false)
52+
})
53+
})
54+
55+
describe('IPv4 127/8 loopback', () => {
56+
it.each([
57+
'127.0.0.1',
58+
'127.1.2.3',
59+
'127.255.255.255',
60+
'127.0.0.1:3000',
61+
'127.000.000.001', // leading zeros are still digits 0-255
62+
'127.0.0.1.' // trailing dot should be tolerated
63+
])('should allow %o', (input) => {
64+
expect(isHostWhitelisted(input)).toBe(true)
65+
})
66+
67+
it.each([
68+
'126.0.0.1',
69+
'127.256.0.1',
70+
'127.-1.0.1',
71+
'127.0.0.1:abc',
72+
'128.0.0.1',
73+
'192.168.1.10',
74+
'10.0.0.2',
75+
'0.0.0.0',
76+
'255.255.255.255',
77+
'127.0.0', // malformed
78+
'127.0.0.1.5' // malformed
79+
])('should NOT allow %o', (input) => {
80+
expect(isHostWhitelisted(input)).toBe(false)
81+
})
82+
})
83+
84+
describe('IPv6 loopback ::1 (all textual forms)', () => {
85+
it.each([
86+
'::1',
87+
'[::1]',
88+
'[::1]:5173',
89+
'::0001',
90+
'0:0:0:0:0:0:0:1',
91+
'0000:0000:0000:0000:0000:0000:0000:0001',
92+
// Compressed equivalents of ::1 (with zeros compressed)
93+
'0:0::1',
94+
'0:0:0:0:0:0::1',
95+
'::0:1' // compressing the initial zeros (still ::1 when expanded)
96+
])('should allow %o', (input) => {
97+
expect(isHostWhitelisted(input)).toBe(true)
98+
})
99+
100+
it.each([
101+
'::2',
102+
'::',
103+
'::0',
104+
'0:0:0:0:0:0:0:2',
105+
'fe80::1', // link-local, not loopback
106+
'2001:db8::1',
107+
'::1:5173', // bracketless "port-like" suffix must not pass
108+
':::1', // invalid (triple colon)
109+
'0:0:0:0:0:0:::1', // invalid compression
110+
'[::1%25lo0]',
111+
'[::1%25lo0]:5173',
112+
'::1%25lo0'
113+
])('should NOT allow %o', (input) => {
114+
expect(isHostWhitelisted(input)).toBe(false)
115+
})
116+
117+
it('should reject empty/whitespace-only input', () => {
118+
expect(isHostWhitelisted('')).toBe(false)
119+
expect(isHostWhitelisted(' ')).toBe(false)
120+
})
121+
})
122+
})
123+
})

0 commit comments

Comments
 (0)