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
118 lines (107 loc) · 2.9 KB
/
image-proxy.ts
File metadata and controls
118 lines (107 loc) · 2.9 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
118
/**
* Image proxy utilities for privacy-safe README image rendering.
*
* Resolves: https://github.com/npmx-dev/npmx.dev/issues/1138
*/
/** 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 {
try {
const parsed = new URL(url)
const hostname = parsed.hostname.toLowerCase()
return TRUSTED_IMAGE_DOMAINS.some(
domain => hostname === domain || hostname.endsWith(`.${domain}`),
)
} catch {
return false
}
}
/**
* Validate that a URL is a valid HTTP(S) image URL suitable for proxying.
*/
export function isAllowedImageUrl(url: string): boolean {
try {
const parsed = new URL(url)
// Only allow HTTP and HTTPS protocols
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
return false
}
// Block localhost / private IPs to prevent SSRF
const hostname = parsed.hostname.toLowerCase()
if (
hostname === 'localhost' ||
hostname === '127.0.0.1' ||
hostname === '::1' ||
hostname === '0.0.0.0' ||
hostname.startsWith('10.') ||
hostname.startsWith('192.168.') ||
hostname.startsWith('172.') ||
hostname.endsWith('.local') ||
hostname.endsWith('.internal')
) {
return false
}
return true
} catch {
return false
}
}
/**
* 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
}
try {
const parsed = new URL(url)
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
return url
}
} catch {
// Not an absolute URL, return as-is (relative URLs are fine)
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)}`
}