-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(auth): dynamic signup/login ban lists via AWS AppConfig #4911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheodoreSpeaks
wants to merge
3
commits into
staging
Choose a base branch
from
feat/faster-ban-user
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+574
−100
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { AccessControlConfig } from '@/lib/auth/access-control' | ||
|
|
||
| const { mockFetch, envRef, flagRef } = vi.hoisted(() => ({ | ||
| mockFetch: vi.fn(), | ||
| envRef: { | ||
| APPCONFIG_APPLICATION: 'sim-staging' as string | undefined, | ||
| APPCONFIG_ENVIRONMENT: 'staging' as string | undefined, | ||
| BLOCKED_SIGNUP_DOMAINS: undefined as string | undefined, | ||
| ALLOWED_LOGIN_EMAILS: undefined as string | undefined, | ||
| ALLOWED_LOGIN_DOMAINS: undefined as string | undefined, | ||
| BLOCKED_EMAIL_MX_HOSTS: undefined as string | undefined, | ||
| }, | ||
| flagRef: { isAppConfigEnabled: false }, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/config/appconfig', () => ({ | ||
| fetchAppConfigProfile: mockFetch, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/config/env', () => ({ | ||
| get env() { | ||
| return envRef | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/config/feature-flags', () => ({ | ||
| get isAppConfigEnabled() { | ||
| return flagRef.isAppConfigEnabled | ||
| }, | ||
| })) | ||
|
|
||
| import { getAccessControlConfig } from '@/lib/auth/access-control' | ||
|
|
||
| const empty: AccessControlConfig = { | ||
| blockedSignupDomains: [], | ||
| allowedLoginEmails: [], | ||
| allowedLoginDomains: [], | ||
| blockedEmailMxHosts: [], | ||
| } | ||
|
|
||
| describe('getAccessControlConfig', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| flagRef.isAppConfigEnabled = false | ||
| Object.assign(envRef, { | ||
| BLOCKED_SIGNUP_DOMAINS: undefined, | ||
| ALLOWED_LOGIN_EMAILS: undefined, | ||
| ALLOWED_LOGIN_DOMAINS: undefined, | ||
| BLOCKED_EMAIL_MX_HOSTS: undefined, | ||
| }) | ||
| }) | ||
|
|
||
| describe('env fallback (AppConfig disabled)', () => { | ||
| it('returns empty lists when nothing is set', async () => { | ||
| expect(await getAccessControlConfig()).toEqual(empty) | ||
| expect(mockFetch).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('parses, trims, lowercases, and dedupes csv env vars', async () => { | ||
| envRef.BLOCKED_SIGNUP_DOMAINS = 'Gmail.com, yahoo.com ,gmail.com,' | ||
| envRef.ALLOWED_LOGIN_DOMAINS = 'Sim.ai' | ||
| const result = await getAccessControlConfig() | ||
| expect(result.blockedSignupDomains).toEqual(['gmail.com', 'yahoo.com']) | ||
| expect(result.allowedLoginDomains).toEqual(['sim.ai']) | ||
| expect(mockFetch).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| describe('AppConfig source (enabled)', () => { | ||
| beforeEach(() => { | ||
| flagRef.isAppConfigEnabled = true | ||
| }) | ||
|
|
||
| it('reads the access-control profile and normalizes the payload', async () => { | ||
| mockFetch.mockImplementation((_ids, parse) => | ||
| Promise.resolve( | ||
| parse({ | ||
| blockedSignupDomains: ['X.com'], | ||
| allowedLoginDomains: ['sim.ai'], | ||
| blockedEmailMxHosts: 'not-an-array', | ||
| }) | ||
| ) | ||
| ) | ||
|
|
||
| const result = await getAccessControlConfig() | ||
| expect(result.blockedSignupDomains).toEqual(['x.com']) | ||
| expect(result.allowedLoginDomains).toEqual(['sim.ai']) | ||
| expect(result.blockedEmailMxHosts).toEqual([]) | ||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| { application: 'sim-staging', environment: 'staging', profile: 'access-control' }, | ||
| expect.any(Function) | ||
| ) | ||
| }) | ||
|
|
||
| it('falls back to env vars when the fetch yields null', async () => { | ||
| envRef.BLOCKED_SIGNUP_DOMAINS = 'spam.example' | ||
| mockFetch.mockResolvedValue(null) | ||
| const result = await getAccessControlConfig() | ||
| expect(result.blockedSignupDomains).toEqual(['spam.example']) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { fetchAppConfigProfile } from '@/lib/core/config/appconfig' | ||
| import { env } from '@/lib/core/config/env' | ||
| import { isAppConfigEnabled } from '@/lib/core/config/feature-flags' | ||
|
|
||
| /** | ||
| * Name of the AppConfig configuration profile holding the signup/login gating | ||
| * lists. This is a cross-repo contract: it must match the `CfnConfigurationProfile` | ||
| * name created by the infra stack. | ||
| */ | ||
| const ACCESS_CONTROL_PROFILE = 'access-control' | ||
|
|
||
| /** | ||
| * Normalized signup/login gating lists. All entries are trimmed, lowercased, and | ||
| * de-duplicated. Domains are bare hostnames; MX hosts are substrings matched | ||
| * against resolved MX exchanges; emails are full addresses. | ||
| */ | ||
| export interface AccessControlConfig { | ||
| blockedSignupDomains: string[] | ||
| allowedLoginEmails: string[] | ||
| allowedLoginDomains: string[] | ||
| blockedEmailMxHosts: string[] | ||
| } | ||
|
|
||
| function normalizeList(values: unknown): string[] { | ||
| if (!Array.isArray(values)) return [] | ||
| return Array.from(new Set(values.map((v) => String(v).trim().toLowerCase()).filter(Boolean))) | ||
| } | ||
|
|
||
| function parseCsv(value: string | undefined): string[] { | ||
| return normalizeList(value?.split(',')) | ||
| } | ||
|
|
||
| /** | ||
| * Fallback source for self-hosted/OSS/local deployments that have no AppConfig. | ||
| * Reads the same env vars the app used before AppConfig. | ||
| */ | ||
| function fromEnv(): AccessControlConfig { | ||
| return { | ||
| blockedSignupDomains: parseCsv(env.BLOCKED_SIGNUP_DOMAINS), | ||
| allowedLoginEmails: parseCsv(env.ALLOWED_LOGIN_EMAILS), | ||
| allowedLoginDomains: parseCsv(env.ALLOWED_LOGIN_DOMAINS), | ||
| blockedEmailMxHosts: parseCsv(env.BLOCKED_EMAIL_MX_HOSTS), | ||
| } | ||
| } | ||
|
|
||
| function parseConfig(json: unknown): AccessControlConfig { | ||
| const obj = (json && typeof json === 'object' ? json : {}) as Record<string, unknown> | ||
| return { | ||
| blockedSignupDomains: normalizeList(obj.blockedSignupDomains), | ||
| allowedLoginEmails: normalizeList(obj.allowedLoginEmails), | ||
| allowedLoginDomains: normalizeList(obj.allowedLoginDomains), | ||
| blockedEmailMxHosts: normalizeList(obj.blockedEmailMxHosts), | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the current signup/login gating lists. Reads from AWS AppConfig on | ||
| * hosted deployments (cached, ~30s TTL, never blocks after the first fetch), | ||
| * otherwise falls back to env vars so self-hosted/OSS works with no AWS. | ||
| */ | ||
| export async function getAccessControlConfig(): Promise<AccessControlConfig> { | ||
| if (!isAppConfigEnabled) return fromEnv() | ||
|
|
||
| const value = await fetchAppConfigProfile( | ||
| { | ||
| application: env.APPCONFIG_APPLICATION as string, | ||
| environment: env.APPCONFIG_ENVIRONMENT as string, | ||
| profile: ACCESS_CONTROL_PROFILE, | ||
| }, | ||
| parseConfig | ||
| ) | ||
|
|
||
| return value ?? fromEnv() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bannedEmailsdenylist from PR description is absentThe PR description explicitly promises "Add a new
bannedEmailsdenylist that blocks a specific address at both sign-in and sign-up." TheAccessControlConfiginterface has nobannedEmailsfield,parseConfigdoes not extract it,fromEnvdoes not read it, andauth.tshas no corresponding check. AnybannedEmailsarray seeded into the AppConfig profile by the paired infra PR (simstudioai/infra#201) will be silently dropped — operators who add a specific address to that list expecting it to be blocked at sign-in will get no enforcement.