fix(security): Phase 1 P0 hardening from cross-project audit
All checks were successful
CI / Lint, Unit Tests & Build (push) Successful in 5m42s
CI / Deploy production (on server) (push) Successful in 33s

Close open uploads, image-proxy SSRF, fail-open AI quotas in production,
auth gaps on app routes, and MCP tenant isolation issues.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Antigravity
2026-06-20 16:53:19 +00:00
parent 40f30155c2
commit 4d96605144
16 changed files with 264 additions and 95 deletions

View File

@@ -20,12 +20,20 @@ export interface EntitlementResult {
remaining: number;
limit: number;
tier: SubscriptionTier;
reason?: 'QUOTA_EXCEEDED' | 'TIER_LIMITED' | 'FEATURE_NOT_AVAILABLE';
reason?: 'QUOTA_EXCEEDED' | 'TIER_LIMITED' | 'FEATURE_NOT_AVAILABLE' | 'SERVICE_UNAVAILABLE';
message?: string;
upgradeTier?: 'PRO' | 'BUSINESS';
byokConfigured?: boolean;
}
export class QuotaServiceUnavailableError extends Error {
code = 'QUOTA_SERVICE_UNAVAILABLE';
constructor(message = 'Quota service temporarily unavailable') {
super(message);
}
}
export class QuotaExceededError extends Error {
code = 'QUOTA_EXCEEDED';
upgradeTier: 'PRO' | 'BUSINESS';
@@ -77,6 +85,10 @@ export class QuotaExceededError extends Error {
const TTL_SECONDS = 90 * 24 * 60 * 60;
function shouldFailClosedOnRedisError(): boolean {
return process.env.NODE_ENV === 'production';
}
const INCREMENT_BY_LUA = `
local count = tonumber(ARGV[1]) or 1
local ttl = tonumber(ARGV[2])
@@ -194,7 +206,17 @@ export async function canUseFeature(
byokConfigured: await hasAnyActiveByok(userId),
};
} catch (err) {
console.error('[entitlements] Redis unavailable, allowing request (fail-open):', err);
console.error('[entitlements] Redis unavailable:', err);
if (shouldFailClosedOnRedisError()) {
return {
allowed: false,
remaining: 0,
limit,
tier,
reason: 'SERVICE_UNAVAILABLE',
message: 'Quota service temporarily unavailable. Please try again later.',
};
}
return { allowed: true, remaining: limit, limit, tier };
}
}
@@ -264,7 +286,10 @@ export async function reserveUsageOrThrow(
}
} catch (err) {
if (err instanceof QuotaExceededError) throw err;
console.error('[entitlements] Redis unavailable, allowing request (fail-open):', err);
console.error('[entitlements] Redis unavailable:', err);
if (shouldFailClosedOnRedisError()) {
throw new QuotaServiceUnavailableError();
}
}
}