Repository navigation
Expand file tree
/
Copy pathurl.ts
More file actions
85 lines (79 loc) · 2.93 KB
/
Copy pathurl.ts
File metadata and controls
85 lines (79 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Shared URL helpers. Consolidates logic that was previously copy-pasted across
// feed/content/profile components (ensureHttps, getFaviconUrl, getHostname) and
// adds a scheme guard for user-supplied href values.
/**
* Returns `url` only if it is an absolute http(s) URL, otherwise `undefined`.
*
* Use this for `href` attributes built from user-supplied URLs (profile
* website, job application link, link-post external URL). React does not treat
* `href` as HTML, so escaping is not the concern here — the danger is a
* `javascript:` / `data:` scheme executing on click. `z.string().url()` does
* NOT reject those schemes, so the guard must live at the render boundary too.
* `http://` is upgraded to `https://`.
*/
export function safeExternalHref(
url: string | null | undefined,
): string | undefined {
if (!url) return undefined;
let parsed: URL;
try {
parsed = new URL(url.trim());
} catch {
return undefined;
}
if (parsed.protocol === "http:") {
parsed.protocol = "https:";
return parsed.toString();
}
return parsed.protocol === "https:" ? url.trim() : undefined;
}
// `https://host[:port]/<absolute-url>` — an upstream feed prefixed its own
// origin onto an already-absolute URL. Anchored so the embedded scheme must sit
// immediately after the host's first slash: this is the precise shape of the
// bug and avoids mangling a scheme that legitimately appears deeper in the path
// (path-based image proxies, a slug containing `http://`) or in a query string.
const DOUBLED_ORIGIN = /^https?:\/\/[^/]+\/(https?:\/\/.+)$/i;
/**
* Unwraps a URL that an upstream feed prefixed with its own origin, leaving an
* already-absolute URL doubled up — e.g. HackerNoon's `media:thumbnail` serves
* `https://hackernoon.com/https://cdn.hackernoon.com/x.png`, which 404s. Returns
* the inner URL; a normal URL is returned unchanged.
*/
export function unwrapDoubledUrl(
url: string | null | undefined,
): string | null {
if (!url) return null;
return DOUBLED_ORIGIN.exec(url)?.[1] ?? url;
}
/** Upgrades an `http://` URL to `https://`. Returns null for empty input. */
export function ensureHttps(url: string | null | undefined): string | null {
if (!url) return null;
if (url.startsWith("http://")) {
return url.replace("http://", "https://");
}
return url;
}
/** Builds a Google s2 favicon URL for the given site, or null if unparseable. */
export function getFaviconUrl(
websiteUrl: string | null | undefined,
size = 32,
): string | null {
if (!websiteUrl) return null;
try {
const { hostname } = new URL(websiteUrl);
return `https://www.google.com/s2/favicons?domain=${hostname}&sz=${size}`;
} catch {
return null;
}
}
/** Extracts the hostname from a URL string, or null if unparseable. */
export function getHostname(
urlString: string | null | undefined,
): string | null {
if (!urlString) return null;
try {
return new URL(urlString).hostname;
} catch {
return null;
}
}