forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage-proxy.ts
More file actions
117 lines (99 loc) · 3.17 KB
/
image-proxy.ts
File metadata and controls
117 lines (99 loc) · 3.17 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Image proxy utilities for privacy-safe README image rendering.
*
* Resolves: https://github.com/npmx-dev/npmx.dev/issues/1138
*/
import ipaddr from 'ipaddr.js'
/** Trusted image domains that don't need proxying (first-party or well-known CDNs) */
const TRUSTED_IMAGE_DOMAINS = [
// First-party
'npmx.dev',
// GitHub (already proxied by GitHub's own camo)
'raw.githubusercontent.com',
'github.com',
'user-images.githubusercontent.com',
'avatars.githubusercontent.com',
'repository-images.githubusercontent.com',
'github.githubassets.com',
'objects.githubusercontent.com',
// GitLab
'gitlab.com',
// CDNs commonly used in READMEs
'cdn.jsdelivr.net',
'unpkg.com',
// Well-known badge/shield services
'img.shields.io',
'shields.io',
'badge.fury.io',
'badgen.net',
'flat.badgen.net',
'codecov.io',
'coveralls.io',
'david-dm.org',
'snyk.io',
'app.fossa.com',
'api.codeclimate.com',
'bundlephobia.com',
'packagephobia.com',
]
/**
* Check if a URL points to a trusted domain that doesn't need proxying.
*/
export function isTrustedImageDomain(url: string): boolean {
const parsed = URL.parse(url)
if (!parsed) return false
const hostname = parsed.hostname.toLowerCase()
return TRUSTED_IMAGE_DOMAINS.some(
domain => hostname === domain || hostname.endsWith(`.${domain}`),
)
}
/**
* Validate that a URL is a valid HTTP(S) image URL suitable for proxying.
* Blocks private/reserved IPs (SSRF protection) using ipaddr.js for comprehensive
* IPv4, IPv6, and IPv4-mapped IPv6 range detection.
*/
export function isAllowedImageUrl(url: string): boolean {
const parsed = URL.parse(url)
if (!parsed) return false
// Only allow HTTP and HTTPS protocols
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
return false
}
const hostname = parsed.hostname.toLowerCase()
// Block non-IP hostnames that resolve to internal services
if (hostname === 'localhost' || hostname.endsWith('.local') || hostname.endsWith('.internal')) {
return false
}
// For IP addresses, use ipaddr.js to check against all reserved ranges
// (loopback, private RFC 1918, link-local 169.254, IPv6 ULA fc00::/7, etc.)
// ipaddr.process() also unwraps IPv4-mapped IPv6 (e.g. ::ffff:127.0.0.1 → 127.0.0.1)
const bare = hostname.startsWith('[') && hostname.endsWith(']') ? hostname.slice(1, -1) : hostname
if (ipaddr.isValid(bare)) {
const addr = ipaddr.process(bare)
if (addr.range() !== 'unicast') {
return false
}
}
return true
}
/**
* Convert an external image URL to a proxied URL.
* Trusted domains are returned as-is.
* Returns the original URL for non-HTTP(S) URLs.
*/
export function toProxiedImageUrl(url: string): string {
// Don't proxy data URIs, relative URLs, or anchor links
if (!url || url.startsWith('#') || url.startsWith('data:')) {
return url
}
const parsed = URL.parse(url)
if (!parsed || (parsed.protocol !== 'http:' && parsed.protocol !== 'https:')) {
return url
}
// Trusted domains don't need proxying
if (isTrustedImageDomain(url)) {
return url
}
// Proxy through our server endpoint
return `/api/registry/image-proxy?url=${encodeURIComponent(url)}`
}