-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
215 lines (186 loc) · 6.89 KB
/
vite.config.ts
File metadata and controls
215 lines (186 loc) · 6.89 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import type { IncomingMessage, ServerResponse, RequestOptions } from 'http';
import https from 'https';
import http from 'http';
const MAX_REDIRECTS = 5;
// Rate limiting for dev proxy
const RATE_LIMIT_REQUESTS = 10;
const RATE_LIMIT_WINDOW_MS = 60_000;
const rateLimitMap = new Map<string, { count: number; resetAt: number }>();
// SSRF protection - block private/local IPs (IPv4 + IPv6)
const BLOCKED_PATTERN = /^(localhost|127\.|10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|169\.254\.|0\.0\.0\.0|\[::1\]|\[fe80:|\[fc[0-9a-f]{2}:|\[fd[0-9a-f]{2}:)/i;
function checkRateLimit(ip: string): { allowed: boolean; retryAfter: number } {
const now = Date.now();
let entry = rateLimitMap.get(ip);
if (!entry || now > entry.resetAt) {
entry = { count: 0, resetAt: now + RATE_LIMIT_WINDOW_MS };
rateLimitMap.set(ip, entry);
}
entry.count++;
if (entry.count > RATE_LIMIT_REQUESTS) {
return { allowed: false, retryAfter: Math.ceil((entry.resetAt - now) / 1000) };
}
return { allowed: true, retryAfter: 0 };
}
function proxyRequest(
targetUrl: URL,
res: ServerResponse,
redirectCount: number
): void {
if (redirectCount > MAX_REDIRECTS) {
res.statusCode = 508;
res.setHeader('Content-Type', 'text/plain');
res.end('Too many redirects — the URL redirects more than 5 times.');
return;
}
const client = targetUrl.protocol === 'https:' ? https : http;
const options: RequestOptions = {
hostname: targetUrl.hostname,
port: targetUrl.port || (targetUrl.protocol === 'https:' ? 443 : 80),
path: targetUrl.pathname + targetUrl.search,
method: 'GET',
headers: {
'Host': targetUrl.host,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7',
'Accept-Encoding': 'gzip, deflate, br',
'Cache-Control': 'no-cache',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
},
timeout: 25000, // 25 second timeout
};
const proxyReq = client.request(options, (proxyRes) => {
const status = proxyRes.statusCode || 200;
// Handle redirects
if (status >= 300 && status < 400 && proxyRes.headers.location) {
const location = proxyRes.headers.location;
let nextUrl: URL;
try {
nextUrl = new URL(location, targetUrl);
} catch {
res.statusCode = 502;
res.setHeader('Content-Type', 'text/plain');
res.end('Invalid redirect URL received from server.');
return;
}
// Re-validate redirect target to prevent SSRF via open redirect
if (!['http:', 'https:'].includes(nextUrl.protocol)) {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Only HTTP(S) allowed');
return;
}
if (BLOCKED_PATTERN.test(nextUrl.hostname)) {
res.statusCode = 403;
res.setHeader('Content-Type', 'text/plain');
res.end('Private addresses not allowed');
return;
}
proxyRequest(nextUrl, res, redirectCount + 1);
return;
}
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', proxyRes.headers['content-type'] || 'text/html');
res.statusCode = status;
proxyRes.pipe(res);
});
proxyReq.on('timeout', () => {
proxyReq.destroy();
res.statusCode = 504;
res.setHeader('Content-Type', 'text/plain');
res.end('Gateway timeout — the target server took too long to respond.');
});
proxyReq.on('error', (err) => {
const errMsg = err.message.toLowerCase();
if (errMsg.includes('enotfound') || errMsg.includes('getaddrinfo')) {
res.statusCode = 502;
res.setHeader('Content-Type', 'text/plain');
res.end('Domain not found — the website does not exist or DNS lookup failed.');
return;
}
if (errMsg.includes('econnrefused')) {
res.statusCode = 502;
res.setHeader('Content-Type', 'text/plain');
res.end('Connection refused — the server is not accepting connections.');
return;
}
if (errMsg.includes('econnreset')) {
res.statusCode = 502;
res.setHeader('Content-Type', 'text/plain');
res.end('Connection reset — the server closed the connection unexpectedly.');
return;
}
if (errMsg.includes('etimedout') || errMsg.includes('timeout')) {
res.statusCode = 504;
res.setHeader('Content-Type', 'text/plain');
res.end('Connection timed out — the server is not responding.');
return;
}
if (errMsg.includes('cert') || errMsg.includes('ssl') || errMsg.includes('tls')) {
res.statusCode = 502;
res.setHeader('Content-Type', 'text/plain');
res.end('SSL/TLS error — the website has certificate issues.');
return;
}
console.error(`[Proxy] Unhandled error from ${targetUrl.hostname}:`, err.message);
res.statusCode = 502;
res.setHeader('Content-Type', 'text/plain');
res.end('Proxy error — unable to retrieve the requested page.');
});
proxyReq.end();
}
export default defineConfig(({ command }) => ({
base: '/',
plugins: [
react(),
{
name: 'cors-proxy',
configureServer(server) {
server.middlewares.use('/proxy', (req: IncomingMessage, res: ServerResponse) => {
// Rate limiting (use localhost for dev)
const clientIp = req.socket.remoteAddress || '127.0.0.1';
const rateCheck = checkRateLimit(clientIp);
if (!rateCheck.allowed) {
res.statusCode = 429;
res.setHeader('Retry-After', String(rateCheck.retryAfter));
res.end(`Rate limit exceeded. Try again in ${rateCheck.retryAfter} seconds.`);
return;
}
const urlParam = new URL(req.url!, 'http://localhost').searchParams.get('url');
if (!urlParam) {
res.statusCode = 400;
res.end('Missing url parameter');
return;
}
let targetUrl: URL;
try {
targetUrl = new URL(urlParam);
} catch {
res.statusCode = 400;
res.end('Invalid URL');
return;
}
// Protocol check
if (!['http:', 'https:'].includes(targetUrl.protocol)) {
res.statusCode = 400;
res.end('Only HTTP(S) allowed');
return;
}
// SSRF protection
if (BLOCKED_PATTERN.test(targetUrl.hostname)) {
res.statusCode = 403;
res.end('Private addresses not allowed');
return;
}
proxyRequest(targetUrl, res, 0);
});
},
},
],
}));