Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
fix(auth): separate Google chooser hints from access (#252)
grim2bd82826019d
diff --git a/packages/auth/src/auth.ts b/packages/auth/src/auth.tsindex c59c9825..cb49764f 100644--- a/packages/auth/src/auth.ts+++ b/packages/auth/src/auth.ts@@ -24,6 +24,7 @@ import { rememberSlackInstall, replaceSlackConnection } from "./slack-grant";import { SLACK_REQUESTED_SCOPES, SLACK_USER_SCOPES } from "./slack-scopes";import { queueSlackInventorySync } from "./slack-sync";import {+ googleHostedDomainHint,hasSignInAllowList,isWorkspaceEmail,primaryWorkspaceDomain,@@ -45,7 +46,7 @@ if (env.google) {accessType: "offline",};- const hostedDomain = primaryWorkspaceDomain();+ const hostedDomain = googleHostedDomainHint();if (hostedDomain) google.hd = hostedDomain;socialProviders.google = google;diff --git a/packages/auth/src/workspace.ts b/packages/auth/src/workspace.tsindex d94bc1c2..c67ee85a 100644--- a/packages/auth/src/workspace.ts+++ b/packages/auth/src/workspace.ts@@ -10,6 +10,29 @@ const EMPTY: AllowList = { domains: [], addresses: [] };let cachedSource: string | undefined;let cached: AllowList = EMPTY;+function validHost(value: string): boolean {+ if (value.length > 253) return false;++ const labels = value.split(".");+ return labels.every(+ (label) =>+ label.length > 0 &&+ label.length <= 63 &&+ /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label),+ );+}++function parseAddress(value: string): { address: string; host: string } | null {+ const separator = value.indexOf("@");+ if (separator <= 0 || separator !== value.lastIndexOf("@")) return null;++ const local = value.slice(0, separator);+ const host = value.slice(separator + 1);+ if (/\s/.test(local) || !validHost(host)) return null;++ return { address: value, host };+}+function allowList(): AllowList {const source = process.env.ALLOWED_SIGN_IN ?? "";if (source === cachedSource) return cached;@@ -20,7 +43,13 @@ function allowList(): AllowList {for (const raw of source.split(",")) {const entry = raw.trim().toLowerCase().replace(/^@/, "");if (!entry) continue;- (entry.includes("@") ? addresses : domains).push(entry);++ if (entry.includes("@")) {+ const parsed = parseAddress(entry);+ if (parsed) addresses.push(parsed.address);+ } else if (validHost(entry)) {+ domains.push(entry);+ }}cachedSource = source;@@ -36,6 +65,16 @@ export function primaryWorkspaceDomain(): string | undefined {return allowList().domains[0];}+export function googleHostedDomainHint(): string | undefined {+ const { domains, addresses } = allowList();+ if (domains[0]) return domains[0];++ const hosts = new Set(+ addresses.map((address) => parseAddress(address)?.host).filter(Boolean),+ );+ return hosts.size === 1 ? [...hosts][0] : undefined;+}+export function hasSignInAllowList(): boolean {const { domains, addresses } = allowList();return domains.length > 0 || addresses.length > 0;@@ -45,17 +84,14 @@ export function isWorkspaceEmail(email: string | null | undefined): boolean {const value = email?.trim().toLowerCase();if (!value) return false;- const parts = value.split("@");- if (parts.length !== 2) return false;-- const [local, host] = parts;- if (!local || !host) return false;+ const parsed = parseAddress(value);+ if (!parsed) return false;const { domains, addresses } = allowList();if (addresses.includes(value)) return true;return domains.some(- (domain) => host === domain || host.endsWith(`.${domain}`),+ (domain) => parsed.host === domain || parsed.host.endsWith(`.${domain}`),);}diff --git a/packages/auth/test/workspace.spec.ts b/packages/auth/test/workspace.spec.tsnew file mode 100644index 00000000..067114ec--- /dev/null+++ b/packages/auth/test/workspace.spec.ts@@ -0,0 +1,60 @@+import { afterEach, describe, expect, it } from "bun:test";+import {+ googleHostedDomainHint,+ hasSignInAllowList,+ isWorkspaceEmail,+ primaryWorkspaceDomain,+} from "../src/workspace";++const originalAllowList = process.env.ALLOWED_SIGN_IN;++afterEach(() => {+ if (originalAllowList === undefined) delete process.env.ALLOWED_SIGN_IN;+ else process.env.ALLOWED_SIGN_IN = originalAllowList;+});++describe("Google hosted-domain hint", () => {+ it("uses the configured domain", () => {+ process.env.ALLOWED_SIGN_IN = "acme.com";+ expect(googleHostedDomainHint()).toBe("acme.com");+ });++ it("uses the common host from an address-only allow-list", () => {+ process.env.ALLOWED_SIGN_IN = "[email protected],[email protected]";+ expect(googleHostedDomainHint()).toBe("acme.com");+ });++ it("withholds the hint when allowed addresses use different hosts", () => {+ process.env.ALLOWED_SIGN_IN = "[email protected],[email protected]";+ expect(googleHostedDomainHint()).toBeUndefined();+ });++ it("does not widen access for an address-only allow-list", () => {+ process.env.ALLOWED_SIGN_IN = "[email protected]";+ expect(googleHostedDomainHint()).toBe("acme.com");+ expect(primaryWorkspaceDomain()).toBeUndefined();+ expect(isWorkspaceEmail("[email protected]")).toBe(false);+ });+});++describe("malformed allow-list hosts", () => {+ it.each([+ "https://acme.com",+ "-acme.com",+ "acme..com",+ "[email protected]",+ "owner@@acme.com",+ ])("rejects %s", (entry) => {+ process.env.ALLOWED_SIGN_IN = entry;+ expect(hasSignInAllowList()).toBe(false);+ expect(googleHostedDomainHint()).toBeUndefined();+ });++ it.each(["[email protected]", "[email protected]", "owner @acme.com"])(+ "rejects the sign-in address %s",+ (email) => {+ process.env.ALLOWED_SIGN_IN = email;+ expect(isWorkspaceEmail(email)).toBe(false);+ },+ );+});