Skip to content

Commit b96304e

Browse files
fix(web): link favicons never send ports or internal hostnames to Google
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent a33b41f commit b96304e

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ describe("faviconUrlForOrigin", () => {
2323
"http://service.test",
2424
"http://private.onion",
2525
"http://127.1..",
26+
"https://grafana.corp",
27+
"https://build.lan",
28+
"https://wiki.intranet",
29+
"https://grafana.internal.acme-corp.com",
30+
"https://jira.corp.acme-corp.com:8443",
2631
])("does not disclose %s to the favicon provider", (origin) => {
2732
expect(faviconUrlForOrigin(origin)).toBeNull();
2833
});
2934

30-
it("keeps the public origin, port and requested size", () => {
35+
it("sends only the public hostname and requested size, never the port", () => {
3136
expect(faviconUrlForOrigin("https://github.com:8443/pingdotgg/t3code?private=query", 64)).toBe(
32-
"https://www.google.com/s2/favicons?domain=github.com%3A8443&sz=64",
37+
"https://www.google.com/s2/favicons?domain=github.com&sz=64",
3338
);
3439
});
3540

‎packages/shared/src/favicon.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@ export function toolActivityFaviconUrl(
8282
);
8383
}
8484

85-
/** Return a public favicon URL without disclosing private or reserved hosts. */
85+
/**
86+
* Return a public favicon URL that discloses only a public hostname, never the
87+
* port, path, or a private or internal-looking host. Callers show a generic
88+
* icon when this returns null.
89+
*/
8690
export function faviconUrlForOrigin(rawUrl: string | null | undefined, size = 32): string | null {
8791
if (!rawUrl) return null;
8892
try {
8993
const url = new URL(rawUrl);
90-
if (!url.host) return null;
94+
if (!url.hostname) return null;
9195
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
9296
if (!isPublicFaviconHost(url.hostname)) return null;
93-
return `https://www.google.com/s2/favicons?domain=${encodeURIComponent(url.host)}&sz=${size}`;
97+
return `https://www.google.com/s2/favicons?domain=${encodeURIComponent(url.hostname)}&sz=${size}`;
9498
} catch {
9599
return null;
96100
}

‎packages/shared/src/hostClassification.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ export const isPrivateNetworkHost = (host: string): boolean => {
119119
);
120120
};
121121

122+
// Reserved and special-use names, plus the private TLDs RFC 6762 Appendix G
123+
// records as common on internal networks.
124+
const PRIVATE_FAVICON_TLDS = [
125+
".alt",
126+
".corp",
127+
".example",
128+
".home",
129+
".internal",
130+
".intranet",
131+
".invalid",
132+
".lan",
133+
".onion",
134+
".private",
135+
".test",
136+
];
137+
const INTERNAL_HOST_LABELS: ReadonlySet<string> = new Set(["corp", "internal", "intranet"]);
138+
122139
/** Whether a hostname is eligible to be disclosed to a public favicon provider. */
123140
export const isPublicFaviconHost = (host: string): boolean => {
124141
// A single trailing dot is a valid absolute DNS name. Repeated trailing
@@ -127,12 +144,15 @@ export const isPublicFaviconHost = (host: string): boolean => {
127144
const normalized = normalizeHostname(host);
128145
if (isPrivateNetworkHost(normalized)) return false;
129146
if (
130-
[".alt", ".example", ".internal", ".invalid", ".onion", ".test"].some(
147+
PRIVATE_FAVICON_TLDS.some(
131148
(suffix) => normalized === suffix.slice(1) || normalized.endsWith(suffix),
132149
)
133150
) {
134151
return false;
135152
}
153+
// Company networks often nest internal services under a public domain
154+
// (`grafana.internal.acme.com`); never send those names to a third party.
155+
if (normalized.split(".").some((label) => INTERNAL_HOST_LABELS.has(label))) return false;
136156
const ipv4 = parseIpv4Address(normalized) ?? parseIpv4MappedIpv6Address(normalized);
137157
if (ipv4) return !isSpecialPurposeIpv4Address(ipv4);
138158
if (!normalized.includes(":")) return true;

0 commit comments

Comments
 (0)