Skip to content

armgit / crm

Public Imported from GitHub
0

Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.

TypeScript

fix: address review on the favicon resolver and design pass

grim0c5753c8be47
diff --git a/apps/api/src/companies/companies.service.ts b/apps/api/src/companies/companies.service.ts
index 18fec888..9f4efef7 100644
--- a/apps/api/src/companies/companies.service.ts
+++ b/apps/api/src/companies/companies.service.ts
@@ -378,7 +378,14 @@ export class CompaniesService {
// and it is the one a rep can see. Cleared so the row shows a
// placeholder until the new domain answers, rather than confidently
// showing the old company's mark.
+ //
+ // All three variants, not just `iconUrl`: the agent's `brandToUpdate`
+ // only fills fields that are null, so a stale `iconDarkUrl` would
+ // survive re-enrichment and keep showing the previous company's mark
+ // to anyone in dark mode.
data.iconUrl = null;
+ data.iconDarkUrl = null;
+ data.iconTone = null;
}
}
diff --git a/apps/api/src/companies/favicon.service.ts b/apps/api/src/companies/favicon.service.ts
index 51f14566..a373fdb5 100644
--- a/apps/api/src/companies/favicon.service.ts
+++ b/apps/api/src/companies/favicon.service.ts
@@ -33,10 +33,16 @@ export class FaviconService {
const iconUrl = await resolveFavicon(domain);
if (!iconUrl) return false;
- // `iconUrl: null` in the filter, so an icon the agent found while we
+ // Filtered on the domain as well as `iconUrl`. The lookup is slow and
+ // nothing awaits it, so two quick domain edits leave two in flight: the
+ // older one can return last and stamp the previous company's icon onto
+ // the new domain. Matching the domain we actually resolved makes a
+ // late arrival a no-op instead.
+ //
+ // `iconUrl: null` for the other race — an icon the agent found while we
// were fetching is never overwritten by ours.
const { count } = await this.db.company.updateMany({
- where: { id: companyId, iconUrl: null },
+ where: { id: companyId, iconUrl: null, domain },
data: { iconUrl },
});
diff --git a/apps/app/app/(app)/settings/google-connection.tsx b/apps/app/app/(app)/settings/google-connection.tsx
index e82fb52d..e960aaa0 100644
--- a/apps/app/app/(app)/settings/google-connection.tsx
+++ b/apps/app/app/(app)/settings/google-connection.tsx
@@ -38,10 +38,42 @@ const SOURCES = {
},
} as const;
+/**
+ * Where a "Resolve" link is allowed to point.
+ *
+ * The URL is scraped out of a vendor error string with a regex, and the string
+ * reaches us through a sync that talks to a third party. Rendering whatever it
+ * contains as a link a rep will click is a phishing hop waiting to happen, so
+ * the destination has to be somewhere the fix could actually live.
+ */
+const RESOLVE_HOSTS = [
+ "console.cloud.google.com",
+ "console.developers.google.com",
+ "support.google.com",
+ "myaccount.google.com",
+];
+
+function resolveLink(error: string): string | undefined {
+ const found = error.match(/https?:\/\/[^\s)]+/)?.[0];
+ if (!found) return undefined;
+
+ try {
+ const url = new URL(found);
+ const allowed =
+ url.protocol === "https:" &&
+ RESOLVE_HOSTS.some(
+ (host) => url.hostname === host || url.hostname.endsWith(`.${host}`),
+ );
+ return allowed ? url.toString() : undefined;
+ } catch {
+ return undefined;
+ }
+}
+
function explain(error: string) {
return {
summary: error.split(/(?<=\.)\s/)[0] ?? error,
- url: error.match(/https?:\/\/[^\s)]+/)?.[0],
+ url: resolveLink(error),
};
}
diff --git a/packages/db/src/favicon.ts b/packages/db/src/favicon.ts
index 08e9ade6..bc1889f2 100644
--- a/packages/db/src/favicon.ts
+++ b/packages/db/src/favicon.ts
@@ -1,6 +1,87 @@
+import dns from "node:dns/promises";
+import net from "node:net";
+
const TIMEOUT_MS = 5_000;
const MAX_HTML_BYTES = 512_000;
+/**
+ * A page can advertise as many `<link rel="icon">` tags as it likes, and each
+ * one costs a HEAD request. Ten is more than any real site publishes and caps
+ * a hostile one at eleven requests including the `/favicon.ico` fallback.
+ */
+const MAX_ICON_CANDIDATES = 10;
+
+/** Enough for the usual apex-to-www or http-to-https hop, and no further. */
+const MAX_REDIRECTS = 3;
+
+/**
+ * Addresses that must never be fetched.
+ *
+ * The domain on a company is attacker-supplied — anyone who can create a
+ * company picks it — and this code runs inside the API, which can reach things
+ * the internet cannot: cloud metadata on 169.254.169.254, Postgres on the
+ * private network, anything bound to loopback. A `<link rel="icon">` pointing
+ * at one of those turns "resolve a favicon" into a request forgery primitive,
+ * so every hop is checked rather than only the domain we started from.
+ */
+function isBlockedAddress(ip: string): boolean {
+ const v4 = ip.startsWith("::ffff:") ? ip.slice(7) : ip;
+
+ if (net.isIPv4(v4)) {
+ const [a = 0, b = 0] = v4.split(".").map(Number);
+ return (
+ a === 0 || // this network
+ a === 10 || // private
+ a === 127 || // loopback
+ (a === 169 && b === 254) || // link-local, incl. cloud metadata
+ (a === 172 && b >= 16 && b <= 31) || // private
+ (a === 192 && b === 168) || // private
+ (a === 100 && b >= 64 && b <= 127) || // carrier-grade NAT
+ (a === 198 && (b === 18 || b === 19)) || // benchmarking
+ a >= 224 // multicast and reserved
+ );
+ }
+
+ const v6 = ip.toLowerCase();
+ return (
+ v6 === "::" ||
+ v6 === "::1" || // loopback
+ v6.startsWith("fc") ||
+ v6.startsWith("fd") || // unique local
+ v6.startsWith("fe8") ||
+ v6.startsWith("fe9") ||
+ v6.startsWith("fea") ||
+ v6.startsWith("feb") // link-local
+ );
+}
+
+/**
+ * Whether every address a hostname resolves to is publicly routable.
+ *
+ * Checked before each request rather than once up front, because a redirect
+ * can send us somewhere else entirely.
+ *
+ * This does not close the DNS-rebinding window: the name is resolved here and
+ * again by `fetch`, and a hostile server can answer differently the second
+ * time. Closing it properly means pinning the connection to the address we
+ * validated, which needs a custom agent. This blocks the whole practical
+ * attack — an icon link pointing straight at an internal host — and the
+ * remaining hole needs an attacker who controls authoritative DNS.
+ */
+async function resolvesToPublicHost(hostname: string): Promise<boolean> {
+ if (net.isIP(hostname)) return !isBlockedAddress(hostname);
+
+ try {
+ const addresses = await dns.lookup(hostname, { all: true });
+ return (
+ addresses.length > 0 &&
+ addresses.every((address) => !isBlockedAddress(address.address))
+ );
+ } catch {
+ return false;
+ }
+}
+
const ICON_REL = /^(shortcut )?icon$|^apple-touch-icon(-precomposed)?$/i;
function iconsFromHtml(html: string, base: URL): string[] {
@@ -31,31 +112,100 @@ function iconsFromHtml(html: string, base: URL): string[] {
return found.sort((a, b) => b.size - a.size).map((icon) => icon.href);
}
-async function request(url: string, method: "GET" | "HEAD" = "GET") {
+/**
+ * A fetch that refuses to leave the public internet.
+ *
+ * Redirects are followed by hand — `redirect: "follow"` would chase a `Location`
+ * into the private network without ever showing it to us, which is exactly the
+ * hop that needs checking.
+ */
+async function request(
+ url: string,
+ method: "GET" | "HEAD" = "GET",
+): Promise<{ response: Response; url: URL } | null> {
+ let target: URL;
try {
- return await fetch(url, {
- method,
- signal: AbortSignal.timeout(TIMEOUT_MS),
- redirect: "follow",
- // Some origins serve a different page, or none, to an unknown agent.
- headers: { "user-agent": "Mozilla/5.0 (compatible; CRM/1.0)" },
- });
+ target = new URL(url);
} catch {
return null;
}
+
+ for (let hop = 0; hop <= MAX_REDIRECTS; hop += 1) {
+ // Anything that is not plain HTTP can address things a browser would not:
+ // file://, and the gopher/dict tricks that turn a fetcher into a client
+ // for whatever is listening on an internal port.
+ if (target.protocol !== "https:" && target.protocol !== "http:")
+ return null;
+ if (!(await resolvesToPublicHost(target.hostname))) return null;
+
+ let response: Response;
+ try {
+ response = await fetch(target, {
+ method,
+ signal: AbortSignal.timeout(TIMEOUT_MS),
+ redirect: "manual",
+ // Some origins serve a different page, or none, to an unknown agent.
+ headers: { "user-agent": "Mozilla/5.0 (compatible; CRM/1.0)" },
+ });
+ } catch {
+ return null;
+ }
+
+ const location = response.headers.get("location");
+ if (response.status >= 300 && response.status < 400 && location) {
+ await response.body?.cancel();
+ try {
+ target = new URL(location, target);
+ } catch {
+ return null;
+ }
+ continue;
+ }
+
+ return { response, url: target };
+ }
+
+ return null;
}
-/** The page's HTML and the URL it ended up at, after redirects. */
+/**
+ * The page's HTML and the URL it ended up at, after redirects.
+ *
+ * Read a chunk at a time and cancelled at the cap, rather than `await
+ * response.text()` and then slicing: slicing throws the excess away only after
+ * the whole body is already in memory, so a hostile origin streaming gigabytes
+ * would exhaust the API before the limit was ever applied.
+ */
async function readPage(url: URL): Promise<{ body: string; url: URL } | null> {
- const response = await request(url.toString());
- if (!response?.ok) return null;
+ const result = await request(url.toString());
+ if (!result?.response.ok) return null;
+ const { response } = result;
if (!(response.headers.get("content-type") ?? "").includes("html")) {
+ await response.body?.cancel();
+ return null;
+ }
+ if (!response.body) return null;
+
+ const reader = response.body.getReader();
+ const decoder = new TextDecoder();
+ let body = "";
+ let bytes = 0;
+
+ try {
+ while (bytes < MAX_HTML_BYTES) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ bytes += value.byteLength;
+ body += decoder.decode(value, { stream: true });
+ }
+ } catch {
return null;
+ } finally {
+ await reader.cancel().catch(() => {});
}
- const body = (await response.text()).slice(0, MAX_HTML_BYTES);
- return { body, url: new URL(response.url || url.toString()) };
+ return { body, url: result.url };
}
/**
@@ -66,10 +216,13 @@ async function readPage(url: URL): Promise<{ body: string; url: URL } | null> {
* — the one outcome worse than the placeholder it replaced.
*/
async function servesImage(url: string): Promise<boolean> {
- const response = await request(url, "HEAD");
- if (!response?.ok) return false;
+ const result = await request(url, "HEAD");
+ if (!result?.response.ok) return false;
- return (response.headers.get("content-type") ?? "").startsWith("image/");
+ await result.response.body?.cancel();
+ return (result.response.headers.get("content-type") ?? "").startsWith(
+ "image/",
+ );
}
/**
@@ -87,7 +240,11 @@ export async function resolveFavicon(
const base = new URL(`https://${domain}`);
const page = await readPage(base);
- const candidates = page ? iconsFromHtml(page.body, page.url) : [];
+ // Capped before the fallback is appended, so a page listing a thousand
+ // icons cannot turn one company lookup into a thousand HEAD requests.
+ const candidates = page
+ ? iconsFromHtml(page.body, page.url).slice(0, MAX_ICON_CANDIDATES)
+ : [];
// Every site is entitled to this path whether it advertises it or not,
// so it is the last candidate rather than a separate branch.
candidates.push(new URL("/favicon.ico", page?.url ?? base).toString());
diff --git a/packages/ui/src/components/dropdown-menu.tsx b/packages/ui/src/components/dropdown-menu.tsx
index 0721efa9..8936b287 100644
--- a/packages/ui/src/components/dropdown-menu.tsx
+++ b/packages/ui/src/components/dropdown-menu.tsx
@@ -43,7 +43,7 @@ function DropdownMenuContent({
sideOffset={sideOffset}
align={align}
className={cn(
- "z-50 max-h-(--radix-dropdown-menu-content-available-height) w-(--radix-dropdown-menu-trigger-width) min-w-32 origin-(--radix-dropdown-menu-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-none bg-popover text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:overflow-hidden data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
+ "z-50 max-h-(--radix-dropdown-menu-content-available-height) w-(--radix-dropdown-menu-trigger-width) min-w-32 origin-(--radix-dropdown-menu-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-lg bg-popover text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:overflow-hidden data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
className,
)}
{...props}
@@ -75,7 +75,7 @@ function DropdownMenuItem({
data-inset={inset}
data-variant={variant}
className={cn(
- "group/dropdown-menu-item relative flex cursor-default items-center gap-2 rounded-none px-2 py-2 text-xs outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-inset:pl-7 data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 data-[variant=destructive]:focus:text-destructive dark:data-[variant=destructive]:focus:bg-destructive/20 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 data-[variant=destructive]:*:[svg]:text-destructive",
+ "group/dropdown-menu-item relative flex cursor-default items-center gap-2 rounded-lg px-2 py-2 text-xs outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-inset:pl-7 data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 data-[variant=destructive]:focus:text-destructive dark:data-[variant=destructive]:focus:bg-destructive/20 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 data-[variant=destructive]:*:[svg]:text-destructive",
className,
)}
{...props}
diff --git a/packages/ui/src/styles/globals.css b/packages/ui/src/styles/globals.css
index a6fb8a75..dddcf8e6 100644
--- a/packages/ui/src/styles/globals.css
+++ b/packages/ui/src/styles/globals.css
@@ -159,7 +159,13 @@
/* Lighter than the background: on near-black there is nothing darker to
* draw a line with, so a border sits above its surface rather than below. */
--border: #2a2a2a;
- --input: #3d3d3d;
+ /*
+ * Lighter than `--border` on purpose, and lighter than it first was. A
+ * control's edge is what tells you it can be typed into, so it has to clear
+ * 3:1 against the surface behind it — #3d3d3d managed about 1.5:1 on a
+ * #171717 card, which is a border you can only find if you know it is there.
+ */
+ --input: #6b6b6b;
/* Much stronger than light: the page it covers is already dark. */
--overlay: rgb(0 0 0 / 0.55);
/*