fix(security): Phase 1 P0 hardening from cross-project audit
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:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user