Skip to content

Commit 6341eab

Browse files
fix(shared): reuse favicon privacy rules across clients
1 parent 23bfb2e commit 6341eab

11 files changed

Lines changed: 285 additions & 320 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
8686
import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
8787
import { downloadAndShareAttachment } from "../../lib/attachmentDownload";
8888
import { hasWideMarkdownBlock } from "../../lib/wideMarkdownBlocks";
89-
import { isPrivateHost } from "@t3tools/shared/privateHost";
89+
import { faviconUrlForOrigin } from "@t3tools/shared/favicon";
9090
import {
9191
hasNativeSelectableMarkdownText,
9292
SelectableMarkdownText,
@@ -597,11 +597,8 @@ const MarkdownExternalLink = memo(function MarkdownExternalLink(props: {
597597
readonly href: string;
598598
readonly onPress: (href: string) => void;
599599
}) {
600-
// A favicon service cannot resolve a private host. Skip the request so the
601-
// host name stays inside the network.
602-
const [failed, setFailed] = useState(
603-
() => isPrivateHost(props.host) || failedMarkdownFaviconHosts.has(props.host),
604-
);
600+
const [failedHost, setFailedHost] = useState<string | null>(null);
601+
const faviconUrl = faviconUrlForOrigin(`https://${props.host}`);
605602

606603
return (
607604
<NativeText
@@ -612,15 +609,17 @@ const MarkdownExternalLink = memo(function MarkdownExternalLink(props: {
612609
textDecorationLine: "none",
613610
}}
614611
>
615-
{!failed ? (
612+
{faviconUrl !== null &&
613+
failedHost !== props.host &&
614+
!failedMarkdownFaviconHosts.has(props.host) ? (
616615
<Image
617616
source={{
618-
uri: `https://www.google.com/s2/favicons?domain=${encodeURIComponent(props.host)}&sz=32`,
617+
uri: faviconUrl,
619618
}}
620619
style={[markdownLinkStyles.inlineIcon, markdownLinkStyles.favicon]}
621620
onError={() => {
622621
failedMarkdownFaviconHosts.add(props.host);
623-
setFailed(true);
622+
setFailedHost(props.host);
624623
}}
625624
/>
626625
) : (

‎apps/web/src/browser/browserTargetResolver.ts‎

Lines changed: 7 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -4,158 +4,16 @@ import type {
44
PreviewUrlResolution,
55
} from "@t3tools/contracts";
66
import { isLoopbackHost, normalizePreviewUrl } from "@t3tools/shared/preview";
7+
import { isLocalLoopbackHost, isPrivateNetworkHost } from "@t3tools/shared/hostClassification";
78

89
import { readPreparedConnection } from "~/state/session";
910

10-
export const normalizeHostname = (host: string): string =>
11-
host
12-
.toLowerCase()
13-
.replace(/^\[|\]$/g, "")
14-
.replace(/\.+$/u, "");
15-
16-
const parseIpv4Address = (host: string): readonly number[] | null => {
17-
const parts = normalizeHostname(host).split(".").map(Number);
18-
return parts.length === 4 &&
19-
parts.every((part) => Number.isInteger(part) && part >= 0 && part <= 255)
20-
? parts
21-
: null;
22-
};
23-
24-
const parseIpv4MappedIpv6Address = (host: string): readonly number[] | null => {
25-
const normalized = normalizeHostname(host);
26-
if (!normalized.startsWith("::ffff:")) return null;
27-
const suffix = normalized.slice("::ffff:".length);
28-
const dotted = parseIpv4Address(suffix);
29-
if (dotted) return dotted;
30-
const hextets = suffix.split(":");
31-
if (hextets.length !== 2 || hextets.some((part) => !/^[\da-f]{1,4}$/u.test(part))) return null;
32-
const high = Number.parseInt(hextets[0]!, 16);
33-
const low = Number.parseInt(hextets[1]!, 16);
34-
return [high >>> 8, high & 0xff, low >>> 8, low & 0xff];
35-
};
36-
37-
const parseIpv6Address = (host: string): readonly number[] | null => {
38-
const normalized = normalizeHostname(host);
39-
if (!normalized.includes(":")) return null;
40-
const halves = normalized.split("::");
41-
if (halves.length > 2) return null;
42-
const head = halves[0] ? halves[0].split(":") : [];
43-
const tail = halves[1] ? halves[1].split(":") : [];
44-
if ([...head, ...tail].some((part) => !/^[\da-f]{1,4}$/u.test(part))) return null;
45-
const missing = 8 - head.length - tail.length;
46-
if ((halves.length === 1 && missing !== 0) || (halves.length === 2 && missing < 1)) return null;
47-
return [...head, ...Array.from({ length: missing }, () => "0"), ...tail].map((part) =>
48-
Number.parseInt(part, 16),
49-
);
50-
};
51-
52-
const ipv6PrefixMatches = (
53-
address: readonly number[],
54-
prefix: readonly number[],
55-
prefixLength: number,
56-
): boolean => {
57-
const fullHextets = Math.floor(prefixLength / 16);
58-
if (address.slice(0, fullHextets).some((part, index) => part !== prefix[index])) return false;
59-
const remainingBits = prefixLength % 16;
60-
if (remainingBits === 0) return true;
61-
const mask = (0xffff << (16 - remainingBits)) & 0xffff;
62-
return (address[fullHextets]! & mask) === (prefix[fullHextets]! & mask);
63-
};
64-
65-
const isPrivateIpv4Address = (parts: readonly number[]): boolean =>
66-
parts[0] === 0 ||
67-
parts[0] === 10 ||
68-
parts[0] === 127 ||
69-
(parts[0] === 100 && parts[1]! >= 64 && parts[1]! <= 127) ||
70-
(parts[0] === 172 && parts[1]! >= 16 && parts[1]! <= 31) ||
71-
(parts[0] === 192 && parts[1] === 168) ||
72-
(parts[0] === 169 && parts[1] === 254) ||
73-
(parts[0] === 198 && parts[1]! >= 18 && parts[1]! <= 19);
74-
75-
const isSpecialPurposeIpv4Address = (parts: readonly number[]): boolean =>
76-
isPrivateIpv4Address(parts) ||
77-
parts[0]! >= 224 ||
78-
// Deliberately suppress the whole protocol-assignment block. IANA marks
79-
// .9 and .10 globally reachable, but privacy-safe false negatives are
80-
// preferable to disclosing another special-purpose address by mistake.
81-
(parts[0] === 192 && parts[1] === 0 && parts[2] === 0) ||
82-
(parts[0] === 192 && parts[1] === 0 && parts[2] === 2) ||
83-
(parts[0] === 192 && parts[1] === 88 && parts[2] === 99) ||
84-
(parts[0] === 198 && parts[1] === 51 && parts[2] === 100) ||
85-
(parts[0] === 203 && parts[1] === 0 && parts[2] === 113);
86-
87-
export const isLocalLoopbackHost = (host: string): boolean => {
88-
const normalized = normalizeHostname(host);
89-
if (normalized === "localhost" || normalized === "::1") return true;
90-
return parseIpv4Address(normalized)?.[0] === 127;
91-
};
92-
93-
export const isPrivateNetworkHost = (host: string): boolean => {
94-
const normalized = normalizeHostname(host);
95-
if (
96-
normalized === "::" ||
97-
isLocalLoopbackHost(normalized) ||
98-
normalized.endsWith(".localhost") ||
99-
normalized.endsWith(".local") ||
100-
normalized === "home.arpa" ||
101-
normalized.endsWith(".home.arpa") ||
102-
(!normalized.includes(".") && !normalized.includes(":"))
103-
) {
104-
return true;
105-
}
106-
if (normalized.endsWith(".ts.net")) return true;
107-
const parts = parseIpv4Address(normalized) ?? parseIpv4MappedIpv6Address(normalized);
108-
if (parts) return isPrivateIpv4Address(parts);
109-
const firstIpv6Token = normalized.split(":", 1)[0] ?? "";
110-
if (!normalized.includes(":") || !/^[\da-f]{1,4}$/u.test(firstIpv6Token)) return false;
111-
const firstIpv6Hextet = Number.parseInt(firstIpv6Token, 16);
112-
return (
113-
Number.isInteger(firstIpv6Hextet) &&
114-
((firstIpv6Hextet & 0xfe00) === 0xfc00 || (firstIpv6Hextet & 0xffc0) === 0xfe80)
115-
);
116-
};
117-
118-
/** Whether a hostname is eligible to be disclosed to a public favicon provider. */
119-
export const isPublicFaviconHost = (host: string): boolean => {
120-
// A single trailing dot is a valid absolute DNS name. Repeated trailing
121-
// dots are malformed and can conceal legacy numeric forms such as 127.1.
122-
if (host.endsWith("..")) return false;
123-
const normalized = normalizeHostname(host);
124-
if (isPrivateNetworkHost(normalized)) return false;
125-
if (
126-
[".alt", ".example", ".internal", ".invalid", ".onion", ".test"].some(
127-
(suffix) => normalized === suffix.slice(1) || normalized.endsWith(suffix),
128-
)
129-
) {
130-
return false;
131-
}
132-
const ipv4 = parseIpv4Address(normalized) ?? parseIpv4MappedIpv6Address(normalized);
133-
if (ipv4) return !isSpecialPurposeIpv4Address(ipv4);
134-
if (!normalized.includes(":")) return true;
135-
const ipv6 = parseIpv6Address(normalized);
136-
if (!ipv6) return false;
137-
if (ipv6PrefixMatches(ipv6, [0x0064, 0xff9b, 0, 0, 0, 0, 0, 0], 96)) {
138-
const embeddedIpv4 = [ipv6[6]! >>> 8, ipv6[6]! & 0xff, ipv6[7]! >>> 8, ipv6[7]! & 0xff];
139-
return !isSpecialPurposeIpv4Address(embeddedIpv4);
140-
}
141-
const first = ipv6[0]!;
142-
if ((first & 0xe000) !== 0x2000) return false;
143-
if (ipv6PrefixMatches(ipv6, [0x2001, 0, 0, 0, 0, 0, 0, 0], 23)) {
144-
const publicProtocolAssignment =
145-
(ipv6[1] === 1 &&
146-
ipv6.slice(2, 7).every((part) => part === 0) &&
147-
[1, 2, 3].includes(ipv6[7]!)) ||
148-
ipv6PrefixMatches(ipv6, [0x2001, 3, 0, 0, 0, 0, 0, 0], 32) ||
149-
ipv6PrefixMatches(ipv6, [0x2001, 4, 0x0112, 0, 0, 0, 0, 0], 48) ||
150-
ipv6PrefixMatches(ipv6, [0x2001, 0x20, 0, 0, 0, 0, 0, 0], 28) ||
151-
ipv6PrefixMatches(ipv6, [0x2001, 0x30, 0, 0, 0, 0, 0, 0], 28);
152-
return publicProtocolAssignment;
153-
}
154-
if (ipv6PrefixMatches(ipv6, [0x2001, 0x0db8, 0, 0, 0, 0, 0, 0], 32)) return false;
155-
if (ipv6PrefixMatches(ipv6, [0x2002, 0, 0, 0, 0, 0, 0, 0], 16)) return false;
156-
if (first === 0x3fff && (ipv6[1]! & 0xf000) === 0) return false;
157-
return true;
158-
};
11+
export {
12+
normalizeHostname,
13+
isLocalLoopbackHost,
14+
isPrivateNetworkHost,
15+
isPublicFaviconHost,
16+
} from "@t3tools/shared/hostClassification";
15917

16018
const readEnvironmentUrl = (environmentId: EnvironmentId): URL => {
16119
const connection = readPreparedConnection(environmentId);

‎apps/web/src/components/ChatMarkdown.test.tsx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,37 @@ function codeButton(renderer: ReactTestRenderer, label: string) {
7272
return button.props as ComponentProps<typeof Button>;
7373
}
7474

75+
describe("ChatMarkdown favicon privacy", () => {
76+
it("suppresses private link images while preserving public links across updates", async () => {
77+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
78+
let renderer: ReactTestRenderer | undefined;
79+
const markdown = (url: string) => <ChatMarkdown cwd="/tmp/project" text={`[Link](${url})`} />;
80+
try {
81+
await act(async () => {
82+
renderer = create(markdown("https://github.com"));
83+
});
84+
expect(renderer!.root.findAllByType("img").map((image) => image.props.src)).toEqual([
85+
"https://www.google.com/s2/favicons?domain=github.com&sz=32",
86+
]);
87+
for (const url of ["http://192.168.1.10:8080", "http://localhost:3000", "http://home.arpa"]) {
88+
await act(async () => {
89+
renderer!.update(markdown(url));
90+
});
91+
expect(renderer!.root.findAllByType("img")).toHaveLength(0);
92+
}
93+
await act(async () => {
94+
renderer!.update(markdown("https://github.com"));
95+
});
96+
expect(renderer!.root.findAllByType("img")).toHaveLength(1);
97+
} finally {
98+
await act(async () => {
99+
renderer?.unmount();
100+
});
101+
vi.unstubAllGlobals();
102+
}
103+
});
104+
});
105+
75106
describe("ChatMarkdown streaming", () => {
76107
it("preserves code controls and details without highlighting an unchanged fence again", async () => {
77108
const highlighter = await getSyntaxHighlighterPromise("text");

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type {
2828
ServerProviderSkill,
2929
ThreadLinkedPullRequest,
3030
} from "@t3tools/contracts";
31-
import { isPrivateHost } from "@t3tools/shared/privateHost";
31+
import { faviconUrlForOrigin } from "@t3tools/shared/favicon";
3232
import {
3333
isAtomCommandInterrupted,
3434
squashAtomCommandFailure,
@@ -1134,16 +1134,17 @@ const failedFaviconHosts = new Set<string>();
11341134

11351135
const MarkdownLinkFavicon = memo(function MarkdownLinkFavicon({ host }: { host: string }) {
11361136
const [failedHost, setFailedHost] = useState<string | null>(null);
1137+
const faviconUrl = faviconUrlForOrigin(`https://${host}`);
11371138
return (
11381139
<span
11391140
className="ms-[0.25em] me-[0.2em] inline-flex size-[14px] [vertical-align:-0.125em]"
11401141
aria-hidden
11411142
>
1142-
{isPrivateHost(host) || failedHost === host || failedFaviconHosts.has(host) ? (
1143+
{faviconUrl === null || failedHost === host || failedFaviconHosts.has(host) ? (
11431144
<GlobeIcon className={MARKDOWN_LINK_FAVICON_CLASS_NAME} />
11441145
) : (
11451146
<img
1146-
src={`https://www.google.com/s2/favicons?domain=${encodeURIComponent(host)}&sz=32`}
1147+
src={faviconUrl}
11471148
alt=""
11481149
loading="lazy"
11491150
draggable={false}

‎apps/web/src/lib/favicon.ts‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1 @@
1-
import { isPublicFaviconHost } from "~/browser/browserTargetResolver";
2-
3-
/**
4-
* Favicon helpers for the preview tab strip.
5-
*
6-
* Uses Google's s2 favicon endpoint (same approach as ami's tab strip).
7-
* Callers should always render a `<Globe />` fallback when the returned URL
8-
* fails to load via an `onError` handler.
9-
*/
10-
const FAVICON_PROVIDER = "https://www.google.com/s2/favicons";
11-
12-
export function faviconUrlForOrigin(rawUrl: string | null | undefined, size = 32): string | null {
13-
if (!rawUrl) return null;
14-
try {
15-
const url = new URL(rawUrl);
16-
if (!url.host) return null;
17-
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
18-
if (!isPublicFaviconHost(url.hostname)) return null;
19-
return `${FAVICON_PROVIDER}?domain=${encodeURIComponent(url.host)}&sz=${size}`;
20-
} catch {
21-
return null;
22-
}
23-
}
1+
export { faviconUrlForOrigin } from "@t3tools/shared/favicon";

‎packages/shared/package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@
267267
"types": "./src/nodeSqliteClient.ts",
268268
"import": "./src/nodeSqliteClient.ts"
269269
},
270-
"./privateHost": {
271-
"types": "./src/privateHost.ts",
272-
"import": "./src/privateHost.ts"
270+
"./hostClassification": {
271+
"types": "./src/hostClassification.ts",
272+
"import": "./src/hostClassification.ts"
273273
}
274274
},
275275
"scripts": {

‎packages/shared/src/favicon.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,50 @@
11
import { describe, expect, it } from "@effect/vitest";
22

3-
import { explicitFaviconUrl, faviconUrlForPage, toolActivityFaviconUrl } from "./favicon.ts";
3+
import {
4+
explicitFaviconUrl,
5+
faviconUrlForOrigin,
6+
faviconUrlForPage,
7+
toolActivityFaviconUrl,
8+
} from "./favicon.ts";
9+
10+
describe("faviconUrlForOrigin", () => {
11+
it.each([
12+
"http://192.168.1.10:8080",
13+
"http://localhost:3000",
14+
"http://home.arpa",
15+
"https://printer.local.",
16+
"https://api.internal",
17+
"https://box.tailnet.ts.net",
18+
"http://127.1",
19+
"http://0x7f000001",
20+
"http://[::]",
21+
"http://[::1]",
22+
"http://[::ffff:192.168.1.10]",
23+
"http://[fd00::1]",
24+
"http://[fe80::1]",
25+
"http://100.64.0.1",
26+
"http://198.51.100.1",
27+
"http://[2001:db8::1]",
28+
"http://service.test",
29+
"http://private.onion",
30+
"http://127.1..",
31+
])("does not disclose %s to the favicon provider", (origin) => {
32+
expect(faviconUrlForOrigin(origin)).toBeNull();
33+
});
34+
35+
it("keeps the public origin, port and requested size", () => {
36+
expect(faviconUrlForOrigin("https://github.com:8443/pingdotgg/t3code?private=query", 64)).toBe(
37+
"https://www.google.com/s2/favicons?domain=github.com%3A8443&sz=64",
38+
);
39+
});
40+
41+
it.each([null, undefined, "", "invalid URL", "file:///tmp/private", "data:text/plain,private"])(
42+
"rejects an invalid or unsupported origin %s",
43+
(origin) => {
44+
expect(faviconUrlForOrigin(origin)).toBeNull();
45+
},
46+
);
47+
});
448

549
describe("faviconUrlForPage", () => {
650
it("uses the page origin instead of a third-party favicon service", () => {

‎packages/shared/src/favicon.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isPublicFaviconHost } from "./hostClassification.ts";
2+
13
/**
24
* Mirrors Codex's generic Browser Use fallback: ask the page origin for its
35
* conventional favicon and let the image element fall back to a browser glyph.
@@ -79,3 +81,17 @@ export function toolActivityFaviconUrl(
7981
faviconUrlForPage(icon.pageUrl, size)
8082
);
8183
}
84+
85+
/** Return a public favicon URL without disclosing private or reserved hosts. */
86+
export function faviconUrlForOrigin(rawUrl: string | null | undefined, size = 32): string | null {
87+
if (!rawUrl) return null;
88+
try {
89+
const url = new URL(rawUrl);
90+
if (!url.host) return null;
91+
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
92+
if (!isPublicFaviconHost(url.hostname)) return null;
93+
return `https://www.google.com/s2/favicons?domain=${encodeURIComponent(url.host)}&sz=${size}`;
94+
} catch {
95+
return null;
96+
}
97+
}

0 commit comments

Comments
 (0)