-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathproxy.ts
More file actions
447 lines (380 loc) · 14.9 KB
/
Copy pathproxy.ts
File metadata and controls
447 lines (380 loc) · 14.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import {NextResponse} from 'next/server'
import type {NextRequest} from 'next/server'
import {verifyAccessToken} from '@/lib/auth/tokens'
import {getAppDomain} from '@/lib/custom-domains/utils'
import {db} from '@/lib/db'
// ─── IP Whitelist ────────────────────────────────────────────────────────────
interface IpConfig { enabled: boolean; whitelist: string[] }
let cachedIpConfig: IpConfig = { enabled: false, whitelist: [] }
let ipCacheExpiry = 0
const IP_CACHE_TTL_MS = 30_000
async function getIpConfig(baseUrl: string): Promise<IpConfig> {
const now = Date.now()
if (now < ipCacheExpiry) return cachedIpConfig
const secret = process.env.INTERNAL_API_SECRET
if (!secret) return { enabled: false, whitelist: [] }
try {
const res = await fetch(`${baseUrl}/api/internal/ip-config`, {
headers: { 'x-internal-secret': secret },
signal: AbortSignal.timeout(3000),
})
if (res.ok) {
cachedIpConfig = await res.json() as IpConfig
ipCacheExpiry = now + IP_CACHE_TTL_MS
}
} catch { /* fail open */ }
return cachedIpConfig
}
function getClientIp(req: NextRequest): string {
return req.headers.get('x-forwarded-for')?.split(',')[0].trim()
?? req.headers.get('x-real-ip')
?? '127.0.0.1'
}
function ipMatchesCidr(ip: string, cidr: string): boolean {
const entry = cidr.trim()
if (!entry) return false
if (!entry.includes('/')) return ip === entry
if (!ip.includes('.')) return false
const [network, prefixStr] = entry.split('/')
const prefix = parseInt(prefixStr, 10)
if (isNaN(prefix) || prefix < 0 || prefix > 32) return false
const ipToU32 = (s: string) => s.split('.').reduce((a, p) => (a * 256 + parseInt(p, 10)), 0) >>> 0
const mask = prefix === 0 ? 0 : (~0 << (32 - prefix)) >>> 0
return (ipToU32(ip) & mask) === (ipToU32(network) & mask)
}
const IP_WHITELIST_PUBLIC_PREFIXES = [
'/api/internal/', '/api/health', '/api/check-setup', '/api/system/version',
'/api/config/', '/api/setup/', '/api/auth/', '/api/changelog/',
'/api/integrations/widget/', '/api/avatar/', '/api/analytics/track',
'/_next/', '/favicon.ico',
]
function isIpProtectedPath(pathname: string): boolean {
for (const p of IP_WHITELIST_PUBLIC_PREFIXES) {
if (pathname.startsWith(p)) return false
}
return pathname.startsWith('/dashboard') || pathname.startsWith('/api/')
}
const ALWAYS_PUBLIC_PATHS = [
'/_next/',
'/favicon.ico',
'/public/',
'/static/',
'/_chrverify/',
'/changerawr-domain-verification/',
'/widget.css',
'/widget-bundle.js'
]
// Server action honeypot tracking
const attemptTracker = new Map<string, number>()
const FUCK_OFF_THRESHOLD = 5 // I love this variable, sorry professionalists!
// Default allowed external domains for CDN resources
const DEFAULT_ALLOWED_EXTERNAL_DOMAINS = [
'cloudflareinsights.com',
'static.cloudflareinsights.com',
'cdnjs.cloudflare.com',
'unpkg.com',
'cdn.jsdelivr.net',
'fonts.googleapis.com',
'fonts.gstatic.com'
]
// Get additional allowed domains from environment variable
function getAllowedExternalDomains(): string[] {
const envDomains = process.env.ALLOWED_EXTERNAL_DOMAINS || ''
const additionalDomains = envDomains.split(',').map(d => d.trim()).filter(Boolean)
return [...DEFAULT_ALLOWED_EXTERNAL_DOMAINS, ...additionalDomains]
}
// Domain security config cache (60 second TTL)
interface DomainSecurityConfig {
forceHttps: boolean
fetchedAt: number
}
const domainSecurityCache = new Map<string, DomainSecurityConfig>()
const CACHE_TTL_MS = 60_000 // 60 seconds
async function getDomainSecurityConfig(hostname: string): Promise<DomainSecurityConfig | null> {
// Check cache first
const cached = domainSecurityCache.get(hostname)
if (cached && Date.now() - cached.fetchedAt < CACHE_TTL_MS) {
return cached
}
try {
const domain = await db.customDomain.findUnique({
where: { domain: hostname },
select: {
forceHttps: true,
},
})
if (!domain) {
return null
}
const config: DomainSecurityConfig = {
forceHttps: domain.forceHttps,
fetchedAt: Date.now(),
}
domainSecurityCache.set(hostname, config)
return config
} catch (error) {
console.error('[proxy] Error fetching domain security config:', error)
return null
}
}
const PUBLIC_API_PATHS = [
'/api/auth/',
'/api/setup/',
'/api/check-setup',
'/api/auth/oauth/',
]
const AUTH_ROUTES = ['/login', '/register', '/setup']
const PUBLIC_CONTENT_PATHS = [
'/reset-password/',
'/changelog/',
'/unsubscribed',
'/experiments/',
'/forgot-password',
'/two-factor',
'/cli/auth',
]
function isAlwaysPublicPath(pathname: string): boolean {
return ALWAYS_PUBLIC_PATHS.some(path => pathname.startsWith(path)) ||
pathname.includes('.')
}
function isPublicApiPath(pathname: string): boolean {
return PUBLIC_API_PATHS.some(path => pathname.startsWith(path))
}
function isPublicContentPath(pathname: string): boolean {
return PUBLIC_CONTENT_PATHS.some(path => pathname.startsWith(path))
}
function isAllowedExternalDomain(hostname: string): boolean {
const allowedDomains = getAllowedExternalDomains()
return allowedDomains.some(domain =>
hostname === domain || hostname.endsWith(`.${domain}`)
)
}
function isCustomDomain(hostname: string): boolean {
try {
const appDomain = normalizeHostname(getAppDomain())
if (hostname.includes('localhost') || hostname.includes('127.0.0.1')) {
return false
}
return hostname !== appDomain && !hostname.endsWith(`.${appDomain}`)
} catch {
return false
}
}
function normalizeHostname(rawHost: string): string {
const firstHost = rawHost.split(',')[0]?.trim().toLowerCase() || ''
return firstHost.replace(/:\d+$/, '')
}
function handleCustomDomain(request: NextRequest, hostname: string, pathname: string): NextResponse {
const url = request.nextUrl.clone()
url.pathname = `/changelog/custom-domain/${encodeURIComponent(hostname)}${pathname === '/' ? '' : pathname}`
return NextResponse.rewrite(url)
}
async function isSetupComplete(): Promise<boolean> {
// Check environment variable first to avoid excessive API calls
// Set SETUP_COMPLETE=true in your .env after initial setup is done
if (process.env.SETUP_COMPLETE === 'true') {
return true
}
const headers = new Headers({'x-middleware-check': 'true'})
try {
const baseUrl = process.env.NEXT_PUBLIC_APP_URL
// Support both HTTP and HTTPS URLs
if (!baseUrl) return false
const response = await fetch(`${baseUrl}/api/check-setup`, {headers})
if (!response.ok) return false
const data = await response.json()
return !!data.isComplete
} catch {
return false
}
}
export async function proxy(request: NextRequest) {
const {pathname} = request.nextUrl
const rawHost = request.headers.get('x-forwarded-host') || request.headers.get('host') || ''
const hostname = normalizeHostname(rawHost)
// 🍯 HONEYPOT: Catch server action exploit attempts
const nextAction = request.headers.get('next-action')
if (nextAction) {
const ip = request.headers.get('x-forwarded-for') ||
request.headers.get('x-real-ip') ||
'unknown'
const userAgent = request.headers.get('user-agent') || 'unknown'
// Track attempts per IP
const currentAttempts = (attemptTracker.get(ip) || 0) + 1
attemptTracker.set(ip, currentAttempts)
// Log the attempt
console.warn('🔒 [SECURITY] Server action exploit attempt:', {
action: nextAction,
ip,
userAgent,
attempts: currentAttempts,
path: pathname,
timestamp: new Date().toISOString(),
})
// If they've tried too many times, tell them to fuck off
if (currentAttempts >= FUCK_OFF_THRESHOLD) {
console.error(`🚫 [SECURITY] IP ${ip} exceeded attempt threshold (${currentAttempts} attempts)`)
return new NextResponse(
JSON.stringify({ error: 'Access denied. Stop trying to exploit this server, fuck off.' }),
{ status: 403, headers: { 'content-type': 'application/json' } }
)
}
// Return a realistic unauthorized error
return new NextResponse(
JSON.stringify({ error: 'Unauthorized: Insufficient permissions' }),
{ status: 401, headers: { 'content-type': 'application/json' } }
)
}
// ACME HTTP-01 challenge passthrough — MUST be first, before ANY redirects or auth checks.
// Let's Encrypt validates challenges over both HTTP and HTTPS.
if (pathname.startsWith('/.well-known/acme-challenge/')) {
console.log(`[proxy] 🔐 ACME challenge request: ${hostname}${pathname}`)
console.log(`[proxy] Protocol: ${request.headers.get('x-forwarded-proto') || 'unknown'}`)
return NextResponse.next()
}
// IP whitelist check — only applies to dashboard + API paths, never custom domains
if (!isCustomDomain(hostname) && isIpProtectedPath(pathname)) {
const baseUrl = process.env.NEXT_PUBLIC_APP_URL ?? request.nextUrl.origin
const ipConfig = await getIpConfig(baseUrl)
if (ipConfig.enabled) {
const clientIp = getClientIp(request)
const allowed = ipConfig.whitelist.length === 0
|| ipConfig.whitelist.some(entry => ipMatchesCidr(clientIp, entry))
if (!allowed) {
return new NextResponse('Access denied', {
status: 403,
headers: { 'Content-Type': 'text/plain' },
})
}
}
}
// Force HTTPS redirect for custom domains (production only)
if (isCustomDomain(hostname) && process.env.NODE_ENV === 'production') {
const securityConfig = await getDomainSecurityConfig(hostname)
if (securityConfig?.forceHttps) {
const proto = request.headers.get('x-forwarded-proto')
// Redirect HTTP to HTTPS with 308 (preserves POST method)
if (proto === 'http') {
const url = request.nextUrl.clone()
url.protocol = 'https:'
// Ensure we redirect to the external custom hostname without internal app ports.
url.host = hostname
url.port = ''
return NextResponse.redirect(url, 308)
}
}
}
// Allow requests to allowed external domains (CDNs, Cloudflare, etc.)
// This fixes CORS issues with external scripts and resources
if (isAllowedExternalDomain(hostname)) {
const response = NextResponse.next()
response.headers.set('Access-Control-Allow-Origin', '*')
response.headers.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization')
return response
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const isApiRequest = pathname.startsWith('/api/')
if (isCustomDomain(hostname) && pathname === '/rss.xml') {
const url = request.nextUrl.clone()
url.pathname = `/changelog/custom-domain/${encodeURIComponent(hostname)}/rss.xml`
return NextResponse.rewrite(url)
}
if (isAlwaysPublicPath(pathname)) {
return NextResponse.next()
}
if (isCustomDomain(hostname)) {
const encodedHostname = encodeURIComponent(hostname)
// Prevent rewrite loops when middleware sees an already-rewritten internal pathname.
if (pathname.startsWith(`/changelog/custom-domain/${encodedHostname}`)) {
return NextResponse.next()
}
// ACME challenges must pass through for ALL domains (already handled above, but double-check)
if (pathname.startsWith('/.well-known/acme-challenge/')) {
return NextResponse.next()
}
if (pathname.startsWith('/api/')) {
if (pathname.startsWith('/api/changelog/')) {
return NextResponse.next()
}
return new NextResponse(null, {status: 404})
}
if (pathname === '/rss.xml') {
const url = request.nextUrl.clone()
url.pathname = `/changelog/custom-domain/${encodedHostname}/rss.xml`
return NextResponse.rewrite(url)
}
return handleCustomDomain(request, hostname, pathname)
}
if (pathname === '/api/check-setup') {
return NextResponse.next()
}
if (isPublicApiPath(pathname)) {
return NextResponse.next()
}
if (pathname.startsWith('/api/')) {
return NextResponse.next()
}
if (isPublicContentPath(pathname)) {
return NextResponse.next()
}
if (pathname === '/setup') {
const setupComplete = await isSetupComplete()
if (setupComplete) {
return NextResponse.redirect(new URL('/login', request.url))
}
return NextResponse.next()
}
const setupComplete = await isSetupComplete()
if (!setupComplete) {
return NextResponse.redirect(new URL('/setup', request.url))
}
const accessToken = request.cookies.get('accessToken')?.value
const refreshToken = request.cookies.get('refreshToken')?.value
if (AUTH_ROUTES.some(route => pathname.startsWith(route))) {
if (accessToken) {
try {
const userId = await verifyAccessToken(accessToken)
if (userId) {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
} catch {
}
}
return NextResponse.next()
}
if (!accessToken) {
if (refreshToken) {
return NextResponse.next()
}
const url = new URL('/login', request.url)
url.searchParams.set('from', pathname)
return NextResponse.redirect(url)
}
try {
const userId = await verifyAccessToken(accessToken)
if (!userId) {
if (refreshToken) {
return NextResponse.next()
}
const url = new URL('/login', request.url)
url.searchParams.set('from', pathname)
return NextResponse.redirect(url)
}
const response = NextResponse.next()
response.headers.set('x-user-id', userId)
return response
} catch {
if (refreshToken) {
return NextResponse.next()
}
const url = new URL('/login', request.url)
url.searchParams.set('from', pathname)
return NextResponse.redirect(url)
}
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico).*)',
],
}