mirror of
https://github.com/yusufipk/RepoHub.git
synced 2026-09-11 18:46:07 +00:00
fix: remove localhost bypasses from sync authentication and rate limiting, and add write authentication to package update API.
This commit is contained in:
@@ -30,6 +30,15 @@ export async function PUT(
|
|||||||
request: NextRequest,
|
request: NextRequest,
|
||||||
{ params }: { params: { id: string } }
|
{ params }: { params: { id: string } }
|
||||||
) {
|
) {
|
||||||
|
// Check auth
|
||||||
|
const auth = await SyncAuth.isWriteAllowed(request)
|
||||||
|
if (!auth.allowed) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: auth.reason || 'Unauthorized' },
|
||||||
|
{ status: 403 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const body = await request.json()
|
const body = await request.json()
|
||||||
const packageData = await PackageService.update(params.id, body)
|
const packageData = await PackageService.update(params.id, body)
|
||||||
|
|||||||
+9
-29
@@ -37,21 +37,8 @@ export class SyncAuth {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Additional check: verify request is from localhost or same server
|
// If we reach here, the secret key is valid.
|
||||||
const clientIP = request.headers.get('x-forwarded-for') ||
|
// In server-only mode, we strictly require the secret key, so no further checks are needed.
|
||||||
request.headers.get('x-real-ip') ||
|
|
||||||
'unknown'
|
|
||||||
|
|
||||||
const allowedIPs = ['127.0.0.1', 'localhost', '::1']
|
|
||||||
const isLocalRequest = allowedIPs.includes(clientIP.split(',')[0].trim())
|
|
||||||
|
|
||||||
if (!isLocalRequest && secretKey !== this.SYNC_SECRET) {
|
|
||||||
return {
|
|
||||||
allowed: false,
|
|
||||||
reason: 'Sync operations only allowed from server in server-only mode'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { allowed: true }
|
return { allowed: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,18 +50,11 @@ export class SyncAuth {
|
|||||||
// Always enforce auth for writes
|
// Always enforce auth for writes
|
||||||
const secretKey = request.headers.get('x-sync-secret')
|
const secretKey = request.headers.get('x-sync-secret')
|
||||||
|
|
||||||
// Check localhost
|
// Check localhost - DISABLED FOR SECURITY
|
||||||
const clientIP = request.headers.get('x-forwarded-for') ||
|
// We cannot trust X-Forwarded-For headers as they can be spoofed
|
||||||
request.headers.get('x-real-ip') ||
|
// const clientIP = request.headers.get('x-forwarded-for') || ...
|
||||||
'unknown'
|
|
||||||
|
|
||||||
const allowedIPs = ['127.0.0.1', 'localhost', '::1']
|
|
||||||
const isLocalRequest = allowedIPs.includes(clientIP.split(',')[0].trim())
|
|
||||||
|
|
||||||
if (isLocalRequest) {
|
|
||||||
return { allowed: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Always require secret key for write operations
|
||||||
if (!this.SYNC_SECRET) {
|
if (!this.SYNC_SECRET) {
|
||||||
return {
|
return {
|
||||||
allowed: false,
|
allowed: false,
|
||||||
@@ -83,12 +63,12 @@ export class SyncAuth {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (secretKey === this.SYNC_SECRET) {
|
if (secretKey === this.SYNC_SECRET) {
|
||||||
return { allowed: true }
|
return { allowed: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
allowed: false,
|
allowed: false,
|
||||||
reason: 'Write operations require authentication'
|
reason: 'Write operations require authentication'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-10
@@ -16,16 +16,9 @@ export async function middleware(request: NextRequest) {
|
|||||||
request.ip ||
|
request.ip ||
|
||||||
'CACHE_TOKEN'
|
'CACHE_TOKEN'
|
||||||
|
|
||||||
// Exempt localhost from rate limiting
|
// Rate limit everyone, including localhost, to prevent spoofing attacks
|
||||||
const isLocalhost = ip === '127.0.0.1' ||
|
// 50 requests per minute per IP
|
||||||
ip === '::1' ||
|
await limiter.check(null, 50, ip)
|
||||||
ip === 'localhost' ||
|
|
||||||
ip === 'CACHE_TOKEN'
|
|
||||||
|
|
||||||
if (!isLocalhost) {
|
|
||||||
// 50 requests per minute per IP
|
|
||||||
await limiter.check(null, 50, ip)
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Too Many Requests' },
|
{ error: 'Too Many Requests' },
|
||||||
|
|||||||
Reference in New Issue
Block a user