fix: remove localhost bypasses from sync authentication and rate limiting, and add write authentication to package update API.

This commit is contained in:
Yusuf İpek
2025-11-24 09:29:36 +03:00
parent 219c8ab90f
commit cf2e62cf73
3 changed files with 39 additions and 57 deletions
+12 -3
View File
@@ -8,7 +8,7 @@ export async function GET(
) { ) {
try { try {
const packageData = await PackageService.getById(params.id) const packageData = await PackageService.getById(params.id)
if (!packageData) { if (!packageData) {
return NextResponse.json( return NextResponse.json(
{ error: 'Package not found' }, { error: 'Package not found' },
@@ -30,10 +30,19 @@ 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)
if (!packageData) { if (!packageData) {
return NextResponse.json( return NextResponse.json(
{ error: 'Package not found' }, { error: 'Package not found' },
@@ -66,7 +75,7 @@ export async function DELETE(
try { try {
const success = await PackageService.delete(params.id) const success = await PackageService.delete(params.id)
if (!success) { if (!success) {
return NextResponse.json( return NextResponse.json(
{ error: 'Package not found' }, { error: 'Package not found' },
+24 -44
View File
@@ -15,43 +15,30 @@ export class SyncAuth {
// In server-only mode, check for secret key in header // In server-only mode, check for secret key in header
const secretKey = request.headers.get('x-sync-secret') const secretKey = request.headers.get('x-sync-secret')
if (!this.SYNC_SECRET) { if (!this.SYNC_SECRET) {
return { return {
allowed: false, allowed: false,
reason: 'Sync secret key not configured on server' reason: 'Sync secret key not configured on server'
} }
} }
if (!secretKey) { if (!secretKey) {
return { return {
allowed: false, allowed: false,
reason: 'Sync secret key required in server-only mode' reason: 'Sync secret key required in server-only mode'
} }
} }
if (secretKey !== this.SYNC_SECRET) { if (secretKey !== this.SYNC_SECRET) {
return { return {
allowed: false, allowed: false,
reason: 'Invalid sync secret key' reason: 'Invalid sync secret key'
}
}
// Additional check: verify request is from localhost or same server
const clientIP = request.headers.get('x-forwarded-for') ||
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'
} }
} }
// If we reach here, the secret key is valid.
// In server-only mode, we strictly require the secret key, so no further checks are needed.
return { allowed: true } return { allowed: true }
} }
@@ -62,33 +49,26 @@ export class SyncAuth {
static async isWriteAllowed(request: NextRequest): Promise<{ allowed: boolean; reason?: string }> { static async isWriteAllowed(request: NextRequest): Promise<{ allowed: boolean; reason?: string }> {
// 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
const clientIP = request.headers.get('x-forwarded-for') ||
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) { // Check localhost - DISABLED FOR SECURITY
return { allowed: true } // We cannot trust X-Forwarded-For headers as they can be spoofed
} // const clientIP = request.headers.get('x-forwarded-for') || ...
// Always require secret key for write operations
if (!this.SYNC_SECRET) { if (!this.SYNC_SECRET) {
return { return {
allowed: false, allowed: false,
reason: 'Secret key not configured on server' reason: 'Secret key not configured on server'
} }
} }
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'
} }
} }
@@ -115,7 +95,7 @@ export class SyncAuth {
if (days === 0) { if (days === 0) {
return new Date(0) // Return epoch time if disabled return new Date(0) // Return epoch time if disabled
} }
const nextSync = new Date(lastSyncTime || new Date()) const nextSync = new Date(lastSyncTime || new Date())
nextSync.setDate(nextSync.getDate() + days) nextSync.setDate(nextSync.getDate() + days)
return nextSync return nextSync
+3 -10
View File
@@ -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' },