feat: Enhance security metrics handling with new thresholds and improved messaging
This commit is contained in:
@@ -15,7 +15,7 @@ import {AUTO_PROJECT_DISABLE, DASHBOARD_URI, LANDING_URI} from '../app/constants
|
|||||||
* These limits protect AWS SES reputation and prevent account suspension
|
* These limits protect AWS SES reputation and prevent account suspension
|
||||||
*/
|
*/
|
||||||
const SECURITY_THRESHOLDS = {
|
const SECURITY_THRESHOLDS = {
|
||||||
// Minimum emails required before enforcing limits (prevents false positives)
|
// Minimum emails required before enforcing rate-based limits (prevents false positives)
|
||||||
MIN_EMAILS_FOR_ENFORCEMENT: 100,
|
MIN_EMAILS_FOR_ENFORCEMENT: 100,
|
||||||
|
|
||||||
// Bounce rate thresholds (hard bounces only)
|
// Bounce rate thresholds (hard bounces only)
|
||||||
@@ -31,11 +31,37 @@ const SECURITY_THRESHOLDS = {
|
|||||||
COMPLAINT_ALLTIME_CRITICAL: 0.12,
|
COMPLAINT_ALLTIME_CRITICAL: 0.12,
|
||||||
|
|
||||||
// Minimum absolute counts (prevents small sample size false positives)
|
// Minimum absolute counts (prevents small sample size false positives)
|
||||||
// Both percentage AND absolute count must be exceeded to trigger
|
// Both percentage AND absolute count must be exceeded to trigger rate-based checks
|
||||||
MIN_BOUNCES_FOR_CRITICAL: 10,
|
MIN_BOUNCES_FOR_CRITICAL: 10,
|
||||||
MIN_BOUNCES_FOR_WARNING: 5,
|
MIN_BOUNCES_FOR_WARNING: 5,
|
||||||
MIN_COMPLAINTS_FOR_CRITICAL: 5,
|
MIN_COMPLAINTS_FOR_CRITICAL: 5,
|
||||||
MIN_COMPLAINTS_FOR_WARNING: 3,
|
MIN_COMPLAINTS_FOR_WARNING: 3,
|
||||||
|
|
||||||
|
// === Absolute count ceilings ===
|
||||||
|
// These trigger regardless of rate — catches high-volume spammers who dilute their bounce rate
|
||||||
|
// 24-hour absolute ceilings
|
||||||
|
BOUNCE_24H_CEILING_WARNING: 50,
|
||||||
|
BOUNCE_24H_CEILING_CRITICAL: 100,
|
||||||
|
COMPLAINT_24H_CEILING_WARNING: 10,
|
||||||
|
COMPLAINT_24H_CEILING_CRITICAL: 25,
|
||||||
|
|
||||||
|
// 7-day absolute ceilings
|
||||||
|
BOUNCE_7DAY_CEILING_WARNING: 200,
|
||||||
|
BOUNCE_7DAY_CEILING_CRITICAL: 500,
|
||||||
|
COMPLAINT_7DAY_CEILING_WARNING: 30,
|
||||||
|
COMPLAINT_7DAY_CEILING_CRITICAL: 75,
|
||||||
|
|
||||||
|
// === New project thresholds (projects < 30 days old) ===
|
||||||
|
// Legitimate senders ramp up gradually; spammers blast immediately
|
||||||
|
NEW_PROJECT_AGE_DAYS: 30,
|
||||||
|
NEW_PROJECT_BOUNCE_24H_CEILING_WARNING: 20,
|
||||||
|
NEW_PROJECT_BOUNCE_24H_CEILING_CRITICAL: 50,
|
||||||
|
NEW_PROJECT_BOUNCE_7DAY_CEILING_WARNING: 75,
|
||||||
|
NEW_PROJECT_BOUNCE_7DAY_CEILING_CRITICAL: 150,
|
||||||
|
NEW_PROJECT_COMPLAINT_24H_CEILING_WARNING: 5,
|
||||||
|
NEW_PROJECT_COMPLAINT_24H_CEILING_CRITICAL: 10,
|
||||||
|
NEW_PROJECT_COMPLAINT_7DAY_CEILING_WARNING: 15,
|
||||||
|
NEW_PROJECT_COMPLAINT_7DAY_CEILING_CRITICAL: 30,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
interface RateData {
|
interface RateData {
|
||||||
@@ -50,8 +76,10 @@ interface SecurityStatus {
|
|||||||
projectId: string;
|
projectId: string;
|
||||||
isHealthy: boolean;
|
isHealthy: boolean;
|
||||||
shouldDisable: boolean;
|
shouldDisable: boolean;
|
||||||
|
twentyFourHour: RateData;
|
||||||
sevenDay: RateData;
|
sevenDay: RateData;
|
||||||
allTime: RateData;
|
allTime: RateData;
|
||||||
|
isNewProject: boolean;
|
||||||
violations: string[];
|
violations: string[];
|
||||||
warnings: string[];
|
warnings: string[];
|
||||||
}
|
}
|
||||||
@@ -86,6 +114,13 @@ export class SecurityService {
|
|||||||
projectId,
|
projectId,
|
||||||
isHealthy: true,
|
isHealthy: true,
|
||||||
shouldDisable: false,
|
shouldDisable: false,
|
||||||
|
twentyFourHour: {
|
||||||
|
total: 0,
|
||||||
|
bounces: 0,
|
||||||
|
complaints: 0,
|
||||||
|
bounceRate: 0,
|
||||||
|
complaintRate: 0,
|
||||||
|
},
|
||||||
sevenDay: {
|
sevenDay: {
|
||||||
total: 0,
|
total: 0,
|
||||||
bounces: 0,
|
bounces: 0,
|
||||||
@@ -100,6 +135,7 @@ export class SecurityService {
|
|||||||
bounceRate: 0,
|
bounceRate: 0,
|
||||||
complaintRate: 0,
|
complaintRate: 0,
|
||||||
},
|
},
|
||||||
|
isNewProject: false,
|
||||||
violations: [],
|
violations: [],
|
||||||
warnings: [],
|
warnings: [],
|
||||||
};
|
};
|
||||||
@@ -133,12 +169,18 @@ export class SecurityService {
|
|||||||
`[SECURITY] Project ${projectId} (${project.name}) has CRITICAL security violations but auto-disable is turned off:`,
|
`[SECURITY] Project ${projectId} (${project.name}) has CRITICAL security violations but auto-disable is turned off:`,
|
||||||
status.violations,
|
status.violations,
|
||||||
);
|
);
|
||||||
|
signale.info(
|
||||||
|
`[SECURITY] 24-hour stats: ${status.twentyFourHour.bounces} bounces, ${status.twentyFourHour.complaints} complaints out of ${status.twentyFourHour.total} emails`,
|
||||||
|
);
|
||||||
signale.info(
|
signale.info(
|
||||||
`[SECURITY] 7-day stats: ${status.sevenDay.bounces} bounces, ${status.sevenDay.complaints} complaints out of ${status.sevenDay.total} emails`,
|
`[SECURITY] 7-day stats: ${status.sevenDay.bounces} bounces, ${status.sevenDay.complaints} complaints out of ${status.sevenDay.total} emails`,
|
||||||
);
|
);
|
||||||
signale.info(
|
signale.info(
|
||||||
`[SECURITY] All-time stats: ${status.allTime.bounces} bounces, ${status.allTime.complaints} complaints out of ${status.allTime.total} emails`,
|
`[SECURITY] All-time stats: ${status.allTime.bounces} bounces, ${status.allTime.complaints} complaints out of ${status.allTime.total} emails`,
|
||||||
);
|
);
|
||||||
|
if (status.isNewProject) {
|
||||||
|
signale.info(`[SECURITY] Project is under ${SECURITY_THRESHOLDS.NEW_PROJECT_AGE_DAYS} days old — stricter ceilings apply`);
|
||||||
|
}
|
||||||
|
|
||||||
// Send notification about critical security violations
|
// Send notification about critical security violations
|
||||||
await NtfyService.notifySecurityWarning(project.name, projectId, status.violations);
|
await NtfyService.notifySecurityWarning(project.name, projectId, status.violations);
|
||||||
@@ -201,11 +243,17 @@ export class SecurityService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a project's security metrics (for admin/dashboard display)
|
* Get a project's security metrics (for dashboard display)
|
||||||
|
* Does NOT expose internal thresholds — only computed health levels
|
||||||
*/
|
*/
|
||||||
public static async getProjectSecurityMetrics(projectId: string): Promise<{
|
public static async getProjectSecurityMetrics(projectId: string): Promise<{
|
||||||
status: SecurityStatus;
|
status: SecurityStatus;
|
||||||
thresholds: typeof SECURITY_THRESHOLDS;
|
levels: {
|
||||||
|
bounce7Day: 'healthy' | 'warning' | 'critical';
|
||||||
|
bounceAllTime: 'healthy' | 'warning' | 'critical';
|
||||||
|
complaint7Day: 'healthy' | 'warning' | 'critical';
|
||||||
|
complaintAllTime: 'healthy' | 'warning' | 'critical';
|
||||||
|
};
|
||||||
isDisabled: boolean;
|
isDisabled: boolean;
|
||||||
}> {
|
}> {
|
||||||
const [status, project] = await Promise.all([
|
const [status, project] = await Promise.all([
|
||||||
@@ -216,13 +264,55 @@ export class SecurityService {
|
|||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Strip internal details from the client-facing response:
|
||||||
|
// - Replace detailed violation/warning messages (they contain exact thresholds)
|
||||||
|
// - Remove 24-hour data and new project flag (reveals enforcement windows)
|
||||||
|
const sanitizedStatus: SecurityStatus = {
|
||||||
|
...status,
|
||||||
|
twentyFourHour: {total: 0, bounces: 0, complaints: 0, bounceRate: 0, complaintRate: 0},
|
||||||
|
isNewProject: false,
|
||||||
|
violations: status.violations.map(() => 'Security threshold exceeded'),
|
||||||
|
warnings: status.warnings.map(() => 'Approaching security threshold'),
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status,
|
status: sanitizedStatus,
|
||||||
thresholds: SECURITY_THRESHOLDS,
|
levels: {
|
||||||
|
bounce7Day: this.computeLevel(
|
||||||
|
status.sevenDay.bounceRate,
|
||||||
|
SECURITY_THRESHOLDS.BOUNCE_7DAY_WARNING,
|
||||||
|
SECURITY_THRESHOLDS.BOUNCE_7DAY_CRITICAL,
|
||||||
|
),
|
||||||
|
bounceAllTime: this.computeLevel(
|
||||||
|
status.allTime.bounceRate,
|
||||||
|
SECURITY_THRESHOLDS.BOUNCE_ALLTIME_WARNING,
|
||||||
|
SECURITY_THRESHOLDS.BOUNCE_ALLTIME_CRITICAL,
|
||||||
|
),
|
||||||
|
complaint7Day: this.computeLevel(
|
||||||
|
status.sevenDay.complaintRate,
|
||||||
|
SECURITY_THRESHOLDS.COMPLAINT_7DAY_WARNING,
|
||||||
|
SECURITY_THRESHOLDS.COMPLAINT_7DAY_CRITICAL,
|
||||||
|
),
|
||||||
|
complaintAllTime: this.computeLevel(
|
||||||
|
status.allTime.complaintRate,
|
||||||
|
SECURITY_THRESHOLDS.COMPLAINT_ALLTIME_WARNING,
|
||||||
|
SECURITY_THRESHOLDS.COMPLAINT_ALLTIME_CRITICAL,
|
||||||
|
),
|
||||||
|
},
|
||||||
isDisabled: project?.disabled ?? false,
|
isDisabled: project?.disabled ?? false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static computeLevel(
|
||||||
|
value: number,
|
||||||
|
warningThreshold: number,
|
||||||
|
criticalThreshold: number,
|
||||||
|
): 'healthy' | 'warning' | 'critical' {
|
||||||
|
if (value >= criticalThreshold) return 'critical';
|
||||||
|
if (value >= warningThreshold) return 'warning';
|
||||||
|
return 'healthy';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate bounce and complaint rates for a project
|
* Calculate bounce and complaint rates for a project
|
||||||
*/
|
*/
|
||||||
@@ -270,10 +360,23 @@ export class SecurityService {
|
|||||||
*/
|
*/
|
||||||
private static async calculateSecurityStatus(projectId: string): Promise<SecurityStatus> {
|
private static async calculateSecurityStatus(projectId: string): Promise<SecurityStatus> {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
||||||
const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
||||||
|
|
||||||
// Get 7-day and all-time rates in parallel
|
// Get project age to determine if stricter new-project thresholds apply
|
||||||
const [sevenDay, allTime] = await Promise.all([
|
const project = await prisma.project.findUnique({
|
||||||
|
where: {id: projectId},
|
||||||
|
select: {createdAt: true},
|
||||||
|
});
|
||||||
|
|
||||||
|
const projectAgeDays = project
|
||||||
|
? (now.getTime() - project.createdAt.getTime()) / (1000 * 60 * 60 * 24)
|
||||||
|
: Infinity;
|
||||||
|
const isNewProject = projectAgeDays < SECURITY_THRESHOLDS.NEW_PROJECT_AGE_DAYS;
|
||||||
|
|
||||||
|
// Get 24-hour, 7-day and all-time rates in parallel
|
||||||
|
const [twentyFourHour, sevenDay, allTime] = await Promise.all([
|
||||||
|
this.calculateRates(projectId, oneDayAgo),
|
||||||
this.calculateRates(projectId, sevenDaysAgo),
|
this.calculateRates(projectId, sevenDaysAgo),
|
||||||
this.calculateRates(projectId),
|
this.calculateRates(projectId),
|
||||||
]);
|
]);
|
||||||
@@ -281,13 +384,91 @@ export class SecurityService {
|
|||||||
const violations: string[] = [];
|
const violations: string[] = [];
|
||||||
const warnings: string[] = [];
|
const warnings: string[] = [];
|
||||||
|
|
||||||
|
// Pick absolute count ceilings based on project age
|
||||||
|
const bounceCeilings = isNewProject
|
||||||
|
? {
|
||||||
|
ceiling24hWarning: SECURITY_THRESHOLDS.NEW_PROJECT_BOUNCE_24H_CEILING_WARNING,
|
||||||
|
ceiling24hCritical: SECURITY_THRESHOLDS.NEW_PROJECT_BOUNCE_24H_CEILING_CRITICAL,
|
||||||
|
ceiling7dWarning: SECURITY_THRESHOLDS.NEW_PROJECT_BOUNCE_7DAY_CEILING_WARNING,
|
||||||
|
ceiling7dCritical: SECURITY_THRESHOLDS.NEW_PROJECT_BOUNCE_7DAY_CEILING_CRITICAL,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
ceiling24hWarning: SECURITY_THRESHOLDS.BOUNCE_24H_CEILING_WARNING,
|
||||||
|
ceiling24hCritical: SECURITY_THRESHOLDS.BOUNCE_24H_CEILING_CRITICAL,
|
||||||
|
ceiling7dWarning: SECURITY_THRESHOLDS.BOUNCE_7DAY_CEILING_WARNING,
|
||||||
|
ceiling7dCritical: SECURITY_THRESHOLDS.BOUNCE_7DAY_CEILING_CRITICAL,
|
||||||
|
};
|
||||||
|
|
||||||
|
const complaintCeilings = isNewProject
|
||||||
|
? {
|
||||||
|
ceiling24hWarning: SECURITY_THRESHOLDS.NEW_PROJECT_COMPLAINT_24H_CEILING_WARNING,
|
||||||
|
ceiling24hCritical: SECURITY_THRESHOLDS.NEW_PROJECT_COMPLAINT_24H_CEILING_CRITICAL,
|
||||||
|
ceiling7dWarning: SECURITY_THRESHOLDS.NEW_PROJECT_COMPLAINT_7DAY_CEILING_WARNING,
|
||||||
|
ceiling7dCritical: SECURITY_THRESHOLDS.NEW_PROJECT_COMPLAINT_7DAY_CEILING_CRITICAL,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
ceiling24hWarning: SECURITY_THRESHOLDS.COMPLAINT_24H_CEILING_WARNING,
|
||||||
|
ceiling24hCritical: SECURITY_THRESHOLDS.COMPLAINT_24H_CEILING_CRITICAL,
|
||||||
|
ceiling7dWarning: SECURITY_THRESHOLDS.COMPLAINT_7DAY_CEILING_WARNING,
|
||||||
|
ceiling7dCritical: SECURITY_THRESHOLDS.COMPLAINT_7DAY_CEILING_CRITICAL,
|
||||||
|
};
|
||||||
|
|
||||||
|
const projectLabel = isNewProject ? ' (new project)' : '';
|
||||||
|
|
||||||
|
// === Absolute count ceiling checks (rate-independent) ===
|
||||||
|
// These catch high-volume spammers who dilute their bounce rate by blasting emails
|
||||||
|
|
||||||
|
// 24-hour bounce ceilings
|
||||||
|
if (twentyFourHour.bounces >= bounceCeilings.ceiling24hCritical) {
|
||||||
|
violations.push(
|
||||||
|
`24-hour bounce count${projectLabel} (${twentyFourHour.bounces} bounces) exceeds critical ceiling (${bounceCeilings.ceiling24hCritical})`,
|
||||||
|
);
|
||||||
|
} else if (twentyFourHour.bounces >= bounceCeilings.ceiling24hWarning) {
|
||||||
|
warnings.push(
|
||||||
|
`24-hour bounce count${projectLabel} (${twentyFourHour.bounces} bounces) exceeds warning ceiling (${bounceCeilings.ceiling24hWarning})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7-day bounce ceilings
|
||||||
|
if (sevenDay.bounces >= bounceCeilings.ceiling7dCritical) {
|
||||||
|
violations.push(
|
||||||
|
`7-day bounce count${projectLabel} (${sevenDay.bounces} bounces) exceeds critical ceiling (${bounceCeilings.ceiling7dCritical})`,
|
||||||
|
);
|
||||||
|
} else if (sevenDay.bounces >= bounceCeilings.ceiling7dWarning) {
|
||||||
|
warnings.push(
|
||||||
|
`7-day bounce count${projectLabel} (${sevenDay.bounces} bounces) exceeds warning ceiling (${bounceCeilings.ceiling7dWarning})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 24-hour complaint ceilings
|
||||||
|
if (twentyFourHour.complaints >= complaintCeilings.ceiling24hCritical) {
|
||||||
|
violations.push(
|
||||||
|
`24-hour complaint count${projectLabel} (${twentyFourHour.complaints} complaints) exceeds critical ceiling (${complaintCeilings.ceiling24hCritical})`,
|
||||||
|
);
|
||||||
|
} else if (twentyFourHour.complaints >= complaintCeilings.ceiling24hWarning) {
|
||||||
|
warnings.push(
|
||||||
|
`24-hour complaint count${projectLabel} (${twentyFourHour.complaints} complaints) exceeds warning ceiling (${complaintCeilings.ceiling24hWarning})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7-day complaint ceilings
|
||||||
|
if (sevenDay.complaints >= complaintCeilings.ceiling7dCritical) {
|
||||||
|
violations.push(
|
||||||
|
`7-day complaint count${projectLabel} (${sevenDay.complaints} complaints) exceeds critical ceiling (${complaintCeilings.ceiling7dCritical})`,
|
||||||
|
);
|
||||||
|
} else if (sevenDay.complaints >= complaintCeilings.ceiling7dWarning) {
|
||||||
|
warnings.push(
|
||||||
|
`7-day complaint count${projectLabel} (${sevenDay.complaints} complaints) exceeds warning ceiling (${complaintCeilings.ceiling7dWarning})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Rate-based checks (existing logic) ===
|
||||||
// Only enforce if minimum emails threshold is met
|
// Only enforce if minimum emails threshold is met
|
||||||
const hasMinimumVolumeAllTime = allTime.total >= SECURITY_THRESHOLDS.MIN_EMAILS_FOR_ENFORCEMENT;
|
const hasMinimumVolumeAllTime = allTime.total >= SECURITY_THRESHOLDS.MIN_EMAILS_FOR_ENFORCEMENT;
|
||||||
const hasMinimumVolume7Day = sevenDay.total >= SECURITY_THRESHOLDS.MIN_EMAILS_FOR_ENFORCEMENT;
|
const hasMinimumVolume7Day = sevenDay.total >= SECURITY_THRESHOLDS.MIN_EMAILS_FOR_ENFORCEMENT;
|
||||||
|
|
||||||
// Check 7-day bounce rate (only if 7-day volume is sufficient)
|
// Check 7-day bounce rate (only if 7-day volume is sufficient)
|
||||||
if (hasMinimumVolume7Day) {
|
if (hasMinimumVolume7Day) {
|
||||||
// Critical: requires BOTH rate AND absolute count thresholds
|
|
||||||
if (
|
if (
|
||||||
sevenDay.bounceRate >= SECURITY_THRESHOLDS.BOUNCE_7DAY_CRITICAL &&
|
sevenDay.bounceRate >= SECURITY_THRESHOLDS.BOUNCE_7DAY_CRITICAL &&
|
||||||
sevenDay.bounces >= SECURITY_THRESHOLDS.MIN_BOUNCES_FOR_CRITICAL
|
sevenDay.bounces >= SECURITY_THRESHOLDS.MIN_BOUNCES_FOR_CRITICAL
|
||||||
@@ -307,7 +488,6 @@ export class SecurityService {
|
|||||||
|
|
||||||
// Check 7-day complaint rate (only if 7-day volume is sufficient)
|
// Check 7-day complaint rate (only if 7-day volume is sufficient)
|
||||||
if (hasMinimumVolume7Day) {
|
if (hasMinimumVolume7Day) {
|
||||||
// Critical: requires BOTH rate AND absolute count thresholds
|
|
||||||
if (
|
if (
|
||||||
sevenDay.complaintRate >= SECURITY_THRESHOLDS.COMPLAINT_7DAY_CRITICAL &&
|
sevenDay.complaintRate >= SECURITY_THRESHOLDS.COMPLAINT_7DAY_CRITICAL &&
|
||||||
sevenDay.complaints >= SECURITY_THRESHOLDS.MIN_COMPLAINTS_FOR_CRITICAL
|
sevenDay.complaints >= SECURITY_THRESHOLDS.MIN_COMPLAINTS_FOR_CRITICAL
|
||||||
@@ -327,7 +507,6 @@ export class SecurityService {
|
|||||||
|
|
||||||
// Check all-time rates (only if all-time volume is sufficient)
|
// Check all-time rates (only if all-time volume is sufficient)
|
||||||
if (hasMinimumVolumeAllTime) {
|
if (hasMinimumVolumeAllTime) {
|
||||||
// Check all-time bounce rate - requires BOTH rate AND absolute count
|
|
||||||
if (
|
if (
|
||||||
allTime.bounceRate >= SECURITY_THRESHOLDS.BOUNCE_ALLTIME_CRITICAL &&
|
allTime.bounceRate >= SECURITY_THRESHOLDS.BOUNCE_ALLTIME_CRITICAL &&
|
||||||
allTime.bounces >= SECURITY_THRESHOLDS.MIN_BOUNCES_FOR_CRITICAL
|
allTime.bounces >= SECURITY_THRESHOLDS.MIN_BOUNCES_FOR_CRITICAL
|
||||||
@@ -344,7 +523,6 @@ export class SecurityService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check all-time complaint rate - requires BOTH rate AND absolute count
|
|
||||||
if (
|
if (
|
||||||
allTime.complaintRate >= SECURITY_THRESHOLDS.COMPLAINT_ALLTIME_CRITICAL &&
|
allTime.complaintRate >= SECURITY_THRESHOLDS.COMPLAINT_ALLTIME_CRITICAL &&
|
||||||
allTime.complaints >= SECURITY_THRESHOLDS.MIN_COMPLAINTS_FOR_CRITICAL
|
allTime.complaints >= SECURITY_THRESHOLDS.MIN_COMPLAINTS_FOR_CRITICAL
|
||||||
@@ -366,8 +544,10 @@ export class SecurityService {
|
|||||||
projectId,
|
projectId,
|
||||||
isHealthy: violations.length === 0,
|
isHealthy: violations.length === 0,
|
||||||
shouldDisable: violations.length > 0,
|
shouldDisable: violations.length > 0,
|
||||||
|
twentyFourHour,
|
||||||
sevenDay,
|
sevenDay,
|
||||||
allTime,
|
allTime,
|
||||||
|
isNewProject,
|
||||||
violations,
|
violations,
|
||||||
warnings,
|
warnings,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,265 @@
|
|||||||
|
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
||||||
|
import {EmailStatus, EmailSourceType} from '@plunk/db';
|
||||||
|
import {SecurityService} from '../SecurityService';
|
||||||
|
import {factories, getPrismaClient} from '../../../../../test/helpers';
|
||||||
|
import {redis} from '../../database/redis';
|
||||||
|
|
||||||
|
vi.mock('../../app/constants.js', async () => {
|
||||||
|
const actual = await vi.importActual('../../app/constants.js');
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
AUTO_PROJECT_DISABLE: true,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mock NtfyService to prevent actual notifications
|
||||||
|
vi.mock('../NtfyService.js', () => ({
|
||||||
|
NtfyService: {
|
||||||
|
notifySecurityWarning: vi.fn(),
|
||||||
|
notifyProjectDisabledForSecurity: vi.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Mock email sending for project disabled notifications
|
||||||
|
vi.mock('@plunk/email', () => ({
|
||||||
|
ProjectDisabledEmail: vi.fn(),
|
||||||
|
sendPlatformEmail: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('SecurityService', () => {
|
||||||
|
let projectId: string;
|
||||||
|
let contactId: string;
|
||||||
|
const prisma = getPrismaClient();
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const {project} = await factories.createUserWithProject();
|
||||||
|
projectId = project.id;
|
||||||
|
|
||||||
|
const contact = await factories.createContact({projectId});
|
||||||
|
contactId = contact.id;
|
||||||
|
|
||||||
|
// Clear redis cache
|
||||||
|
await redis.flushdb();
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper to create N emails, some of which are bounced
|
||||||
|
*/
|
||||||
|
async function createEmails(count: number, opts?: {bouncedCount?: number; complainedCount?: number; createdAt?: Date}) {
|
||||||
|
const bouncedCount = opts?.bouncedCount ?? 0;
|
||||||
|
const complainedCount = opts?.complainedCount ?? 0;
|
||||||
|
const createdAt = opts?.createdAt ?? new Date();
|
||||||
|
|
||||||
|
const emails = [];
|
||||||
|
for (let i = 0; i < count; i++) {
|
||||||
|
emails.push(
|
||||||
|
prisma.email.create({
|
||||||
|
data: {
|
||||||
|
projectId,
|
||||||
|
contactId,
|
||||||
|
subject: `Test ${i}`,
|
||||||
|
body: '<p>test</p>',
|
||||||
|
from: '[email protected]',
|
||||||
|
status: EmailStatus.SENT,
|
||||||
|
sourceType: EmailSourceType.TRANSACTIONAL,
|
||||||
|
sentAt: createdAt,
|
||||||
|
createdAt,
|
||||||
|
bouncedAt: i < bouncedCount ? createdAt : null,
|
||||||
|
complainedAt: i >= bouncedCount && i < bouncedCount + complainedCount ? createdAt : null,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await Promise.all(emails);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Rate-based checks (existing behavior)', () => {
|
||||||
|
it('should report healthy when bounce rate is below warning', async () => {
|
||||||
|
// 200 emails, 5 bounces = 2.5% (below 5% warning)
|
||||||
|
await createEmails(200, {bouncedCount: 5});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.isHealthy).toBe(true);
|
||||||
|
expect(status.shouldDisable).toBe(false);
|
||||||
|
expect(status.violations).toHaveLength(0);
|
||||||
|
expect(status.warnings).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should trigger warning when 7-day bounce rate exceeds warning threshold', async () => {
|
||||||
|
// 100 emails, 6 bounces = 6% (above 5% warning, below 10% critical)
|
||||||
|
await createEmails(100, {bouncedCount: 6});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.isHealthy).toBe(true);
|
||||||
|
expect(status.warnings.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should trigger violation when 7-day bounce rate exceeds critical threshold', async () => {
|
||||||
|
// 100 emails, 11 bounces = 11% (above 10% critical)
|
||||||
|
await createEmails(100, {bouncedCount: 11});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.isHealthy).toBe(false);
|
||||||
|
expect(status.shouldDisable).toBe(true);
|
||||||
|
expect(status.violations.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not enforce rate checks when below minimum volume', async () => {
|
||||||
|
// 50 emails (below 100 minimum), 10 bounces = 20% (would exceed critical)
|
||||||
|
await createEmails(50, {bouncedCount: 10});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
// Rate-based check doesn't trigger, but absolute count ceiling might
|
||||||
|
// With 10 bounces in 24h, this is below the 50-bounce ceiling for established projects
|
||||||
|
expect(status.violations).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Absolute count ceilings', () => {
|
||||||
|
it('should trigger critical when 24-hour bounce count exceeds ceiling', async () => {
|
||||||
|
// 20,000 emails, 101 bounces = 0.5% rate (well below rate threshold)
|
||||||
|
// But 101 bounces > 100 (24h critical ceiling for established projects)
|
||||||
|
await createEmails(20000, {bouncedCount: 101});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.shouldDisable).toBe(true);
|
||||||
|
expect(status.violations.some(v => v.includes('24-hour bounce count'))).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should trigger warning when 24-hour bounce count exceeds warning ceiling', async () => {
|
||||||
|
// 10,000 emails, 51 bounces = 0.51% (below rate threshold)
|
||||||
|
// But 51 > 50 (24h warning ceiling)
|
||||||
|
await createEmails(10000, {bouncedCount: 51});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.isHealthy).toBe(true); // warnings don't make it unhealthy
|
||||||
|
expect(status.warnings.some(w => w.includes('24-hour bounce count'))).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should trigger critical when 24-hour complaint count exceeds ceiling', async () => {
|
||||||
|
// 20,000 emails, 26 complaints = 0.13% (below complaint rate critical of 0.15%)
|
||||||
|
// But 26 > 25 (24h complaint critical ceiling)
|
||||||
|
await createEmails(20000, {complainedCount: 26});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.shouldDisable).toBe(true);
|
||||||
|
expect(status.violations.some(v => v.includes('24-hour complaint count'))).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should NOT trigger ceiling when bounce count is below ceiling', async () => {
|
||||||
|
// 20,000 emails, 40 bounces = well below 100 critical and below 50 warning ceiling
|
||||||
|
await createEmails(20000, {bouncedCount: 40});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.isHealthy).toBe(true);
|
||||||
|
expect(status.violations).toHaveLength(0);
|
||||||
|
expect(status.warnings).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('New project stricter thresholds', () => {
|
||||||
|
it('should apply stricter ceilings for projects under 30 days old', async () => {
|
||||||
|
// Default project is created "now", so it's a new project
|
||||||
|
// 10,000 emails, 51 bounces (above 50 new project 24h critical ceiling)
|
||||||
|
await createEmails(10000, {bouncedCount: 51});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.isNewProject).toBe(true);
|
||||||
|
expect(status.shouldDisable).toBe(true);
|
||||||
|
expect(status.violations.some(v => v.includes('new project'))).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should apply standard ceilings for projects over 30 days old', async () => {
|
||||||
|
// Age the project to 31 days
|
||||||
|
const oldDate = new Date(Date.now() - 31 * 24 * 60 * 60 * 1000);
|
||||||
|
await prisma.project.update({
|
||||||
|
where: {id: projectId},
|
||||||
|
data: {createdAt: oldDate},
|
||||||
|
});
|
||||||
|
|
||||||
|
// 10,000 emails, 51 bounces (above 50 new project ceiling, below 100 standard ceiling)
|
||||||
|
await createEmails(10000, {bouncedCount: 51});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.isNewProject).toBe(false);
|
||||||
|
// 51 is above the 50-bounce 24h warning ceiling for established projects
|
||||||
|
expect(status.warnings.some(w => w.includes('24-hour bounce count'))).toBe(true);
|
||||||
|
// But below the 100-bounce 24h critical ceiling
|
||||||
|
expect(status.violations.some(v => v.includes('24-hour bounce count'))).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should catch new project blasting emails with delayed bounces', async () => {
|
||||||
|
// Simulate the spammer scenario: new project sends 20K emails,
|
||||||
|
// only 55 bounces have come back so far (rate is tiny: 0.275%)
|
||||||
|
await createEmails(20000, {bouncedCount: 55});
|
||||||
|
|
||||||
|
const status = await SecurityService.getSecurityStatus(projectId);
|
||||||
|
expect(status.isNewProject).toBe(true);
|
||||||
|
expect(status.shouldDisable).toBe(true);
|
||||||
|
// 55 > 50 new project 24h critical ceiling
|
||||||
|
expect(status.violations.some(v => v.includes('24-hour bounce count'))).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('checkAndEnforceSecurityLimits', () => {
|
||||||
|
it('should disable project when critical thresholds are exceeded', async () => {
|
||||||
|
// Create enough bounces to trigger critical
|
||||||
|
await createEmails(20000, {bouncedCount: 101});
|
||||||
|
|
||||||
|
await SecurityService.checkAndEnforceSecurityLimits(projectId);
|
||||||
|
|
||||||
|
const project = await prisma.project.findUnique({
|
||||||
|
where: {id: projectId},
|
||||||
|
select: {disabled: true},
|
||||||
|
});
|
||||||
|
expect(project?.disabled).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should NOT disable project when only warnings exist', async () => {
|
||||||
|
// 10,000 emails, 51 bounces (above warning but below critical for established project)
|
||||||
|
const oldDate = new Date(Date.now() - 31 * 24 * 60 * 60 * 1000);
|
||||||
|
await prisma.project.update({
|
||||||
|
where: {id: projectId},
|
||||||
|
data: {createdAt: oldDate},
|
||||||
|
});
|
||||||
|
await createEmails(10000, {bouncedCount: 51});
|
||||||
|
|
||||||
|
await SecurityService.checkAndEnforceSecurityLimits(projectId);
|
||||||
|
|
||||||
|
const project = await prisma.project.findUnique({
|
||||||
|
where: {id: projectId},
|
||||||
|
select: {disabled: true},
|
||||||
|
});
|
||||||
|
expect(project?.disabled).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getProjectSecurityMetrics (client-facing)', () => {
|
||||||
|
it('should NOT expose internal thresholds or detailed violation messages', async () => {
|
||||||
|
// Create a violation scenario
|
||||||
|
await createEmails(100, {bouncedCount: 15});
|
||||||
|
|
||||||
|
const metrics = await SecurityService.getProjectSecurityMetrics(projectId);
|
||||||
|
|
||||||
|
// Should have levels, not thresholds
|
||||||
|
expect(metrics.levels).toBeDefined();
|
||||||
|
expect((metrics as Record<string, unknown>).thresholds).toBeUndefined();
|
||||||
|
|
||||||
|
// Violation messages should be generic
|
||||||
|
if (metrics.status.violations.length > 0) {
|
||||||
|
for (const v of metrics.status.violations) {
|
||||||
|
expect(v).toBe('Security threshold exceeded');
|
||||||
|
expect(v).not.toMatch(/\d+%/); // No percentages
|
||||||
|
expect(v).not.toMatch(/\d+ minimum/); // No absolute numbers
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 24-hour data should be zeroed out
|
||||||
|
expect(metrics.status.twentyFourHour.total).toBe(0);
|
||||||
|
expect(metrics.status.twentyFourHour.bounces).toBe(0);
|
||||||
|
|
||||||
|
// New project flag should be hidden
|
||||||
|
expect(metrics.status.isNewProject).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,22 +1,24 @@
|
|||||||
import {
|
import {Alert, AlertDescription, AlertTitle, Card, CardContent, CardDescription, CardHeader, CardTitle} from '@plunk/ui';
|
||||||
Alert,
|
|
||||||
AlertDescription,
|
|
||||||
AlertTitle,
|
|
||||||
Card,
|
|
||||||
CardContent,
|
|
||||||
CardDescription,
|
|
||||||
CardHeader,
|
|
||||||
CardTitle,
|
|
||||||
Progress,
|
|
||||||
} from '@plunk/ui';
|
|
||||||
import {AlertCircle, AlertTriangle, CheckCircle, Shield} from 'lucide-react';
|
import {AlertCircle, AlertTriangle, CheckCircle, Shield} from 'lucide-react';
|
||||||
import type {ProjectSecurityMetrics} from '@plunk/types';
|
import type {ProjectSecurityMetrics, SecurityLevel} from '@plunk/types';
|
||||||
|
|
||||||
interface SecuritySettingsProps {
|
interface SecuritySettingsProps {
|
||||||
metrics: ProjectSecurityMetrics;
|
metrics: ProjectSecurityMetrics;
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const STATUS_CONFIG: Record<SecurityLevel, {color: string; icon: typeof CheckCircle; bg: string; label: string}> = {
|
||||||
|
healthy: {color: 'text-green-600', icon: CheckCircle, bg: 'bg-green-100', label: 'Healthy'},
|
||||||
|
warning: {color: 'text-orange-600', icon: AlertTriangle, bg: 'bg-orange-100', label: 'Warning'},
|
||||||
|
critical: {color: 'text-red-600', icon: AlertCircle, bg: 'bg-red-100', label: 'Critical'},
|
||||||
|
};
|
||||||
|
|
||||||
|
function getOverallLevel(status: ProjectSecurityMetrics['status']): SecurityLevel {
|
||||||
|
if (status.violations.length > 0) return 'critical';
|
||||||
|
if (status.warnings.length > 0) return 'warning';
|
||||||
|
return 'healthy';
|
||||||
|
}
|
||||||
|
|
||||||
export function SecuritySettings({metrics, isLoading}: SecuritySettingsProps) {
|
export function SecuritySettings({metrics, isLoading}: SecuritySettingsProps) {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
@@ -32,42 +34,9 @@ export function SecuritySettings({metrics, isLoading}: SecuritySettingsProps) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const {status, thresholds, isDisabled} = metrics;
|
const {status, levels, isDisabled} = metrics;
|
||||||
|
const overallLevel = getOverallLevel(status);
|
||||||
// Helper to get status color and icon
|
const overallConfig = STATUS_CONFIG[overallLevel];
|
||||||
const getStatusIndicator = (rate: number, warningThreshold: number, criticalThreshold: number) => {
|
|
||||||
if (rate >= criticalThreshold) {
|
|
||||||
return {color: 'text-red-600', icon: AlertCircle, bg: 'bg-red-600', label: 'Critical'};
|
|
||||||
}
|
|
||||||
if (rate >= warningThreshold) {
|
|
||||||
return {color: 'text-orange-600', icon: AlertTriangle, bg: 'bg-orange-500', label: 'Warning'};
|
|
||||||
}
|
|
||||||
return {color: 'text-green-600', icon: CheckCircle, bg: 'bg-green-600', label: 'Healthy'};
|
|
||||||
};
|
|
||||||
|
|
||||||
const sevenDayBounceStatus = getStatusIndicator(
|
|
||||||
status.sevenDay.bounceRate,
|
|
||||||
thresholds.BOUNCE_7DAY_WARNING,
|
|
||||||
thresholds.BOUNCE_7DAY_CRITICAL,
|
|
||||||
);
|
|
||||||
|
|
||||||
const allTimeBounceStatus = getStatusIndicator(
|
|
||||||
status.allTime.bounceRate,
|
|
||||||
thresholds.BOUNCE_ALLTIME_WARNING,
|
|
||||||
thresholds.BOUNCE_ALLTIME_CRITICAL,
|
|
||||||
);
|
|
||||||
|
|
||||||
const sevenDayComplaintStatus = getStatusIndicator(
|
|
||||||
status.sevenDay.complaintRate,
|
|
||||||
thresholds.COMPLAINT_7DAY_WARNING,
|
|
||||||
thresholds.COMPLAINT_7DAY_CRITICAL,
|
|
||||||
);
|
|
||||||
|
|
||||||
const allTimeComplaintStatus = getStatusIndicator(
|
|
||||||
status.allTime.complaintRate,
|
|
||||||
thresholds.COMPLAINT_ALLTIME_WARNING,
|
|
||||||
thresholds.COMPLAINT_ALLTIME_CRITICAL,
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
@@ -75,131 +44,99 @@ export function SecuritySettings({metrics, isLoading}: SecuritySettingsProps) {
|
|||||||
<Card>
|
<Card>
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<div className={`p-2 rounded-lg ${status.isHealthy ? 'bg-green-100' : 'bg-red-100'}`}>
|
<div className={`p-2 rounded-lg ${overallConfig.bg}`}>
|
||||||
<Shield className={`h-5 w-5 ${status.isHealthy ? 'text-green-600' : 'text-red-600'}`} />
|
<Shield className={`h-5 w-5 ${overallConfig.color}`} />
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<CardTitle>Security Overview</CardTitle>
|
<CardTitle>Security Overview</CardTitle>
|
||||||
<CardDescription>
|
<CardDescription>
|
||||||
{status.isHealthy ? 'Your project is in good standing' : 'Action required to maintain project health'}
|
{overallLevel === 'healthy' && 'Your project is in good standing'}
|
||||||
|
{overallLevel === 'warning' && 'Your email health needs attention'}
|
||||||
|
{overallLevel === 'critical' && 'Action required to maintain project health'}
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
{/* Project Disabled Alert */}
|
|
||||||
{isDisabled && (
|
{isDisabled && (
|
||||||
<Alert variant="destructive" className="mb-4">
|
<Alert variant="destructive" className="mb-4">
|
||||||
<AlertCircle className="h-4 w-4" />
|
<AlertCircle className="h-4 w-4" />
|
||||||
<AlertTitle>Project Disabled</AlertTitle>
|
<AlertTitle>Project Disabled</AlertTitle>
|
||||||
<AlertDescription>
|
<AlertDescription>
|
||||||
This project has been disabled due to critical security violations. Contact support to resolve.
|
This project has been disabled due to security violations. Contact support to resolve.
|
||||||
</AlertDescription>
|
</AlertDescription>
|
||||||
</Alert>
|
</Alert>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Violations */}
|
{overallLevel === 'critical' && !isDisabled && (
|
||||||
{status.violations.length > 0 && !isDisabled && (
|
|
||||||
<Alert variant="destructive" className="mb-4">
|
<Alert variant="destructive" className="mb-4">
|
||||||
<AlertCircle className="h-4 w-4" />
|
<AlertCircle className="h-4 w-4" />
|
||||||
<AlertTitle>Critical Violations ({status.violations.length})</AlertTitle>
|
<AlertTitle>Critical</AlertTitle>
|
||||||
<AlertDescription>
|
<AlertDescription>
|
||||||
<ul className="list-disc list-inside space-y-1 text-sm mt-2">
|
Your bounce or complaint rates have exceeded acceptable levels. Review your contact lists and sending
|
||||||
{status.violations.map((violation, idx) => (
|
practices to avoid project suspension.
|
||||||
<li key={idx}>{violation}</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</AlertDescription>
|
</AlertDescription>
|
||||||
</Alert>
|
</Alert>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Warnings */}
|
{overallLevel === 'warning' && (
|
||||||
{status.warnings.length > 0 && (
|
|
||||||
<Alert variant="warning" className="mb-4">
|
<Alert variant="warning" className="mb-4">
|
||||||
<AlertTriangle className="h-4 w-4" />
|
<AlertTriangle className="h-4 w-4" />
|
||||||
<AlertTitle>Security Warnings ({status.warnings.length})</AlertTitle>
|
<AlertTitle>Warning</AlertTitle>
|
||||||
<AlertDescription>
|
<AlertDescription>
|
||||||
<ul className="list-disc list-inside space-y-1 text-sm mt-2">
|
Your bounce or complaint rates are approaching limits. Review your contact lists and remove invalid
|
||||||
{status.warnings.map((warning, idx) => (
|
addresses to maintain good standing.
|
||||||
<li key={idx}>{warning}</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</AlertDescription>
|
</AlertDescription>
|
||||||
</Alert>
|
</Alert>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Healthy Status */}
|
{overallLevel === 'healthy' && !isDisabled && (
|
||||||
{status.isHealthy && !isDisabled && (
|
|
||||||
<Alert>
|
<Alert>
|
||||||
<CheckCircle className="h-4 w-4 text-green-600" />
|
<CheckCircle className="h-4 w-4 text-green-600" />
|
||||||
<AlertDescription>
|
<AlertDescription>
|
||||||
All security metrics are within acceptable thresholds. Keep up the good work!
|
All security metrics are within acceptable levels. Keep up the good work!
|
||||||
</AlertDescription>
|
</AlertDescription>
|
||||||
</Alert>
|
</Alert>
|
||||||
)}
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* Bounce Rate Metrics */}
|
{/* Bounce Metrics */}
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle>Bounce Rate Metrics</CardTitle>
|
<CardTitle>Bounce Rate</CardTitle>
|
||||||
<CardDescription>Hard bounces indicate invalid or non-existent email addresses</CardDescription>
|
<CardDescription>Hard bounces indicate invalid or non-existent email addresses</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent className="space-y-4">
|
<CardContent className="space-y-4">
|
||||||
{/* 7-Day Bounce Rate */}
|
<HealthMetric
|
||||||
<MetricDisplay
|
label="Last 7 Days"
|
||||||
label="7-Day Bounce Rate"
|
level={levels.bounce7Day}
|
||||||
rate={status.sevenDay.bounceRate}
|
detail={`${status.sevenDay.bounceRate.toFixed(2)}% bounce rate (${status.sevenDay.bounces.toLocaleString()} of ${status.sevenDay.total.toLocaleString()} emails)`}
|
||||||
count={status.sevenDay.bounces}
|
|
||||||
total={status.sevenDay.total}
|
|
||||||
warningThreshold={thresholds.BOUNCE_7DAY_WARNING}
|
|
||||||
criticalThreshold={thresholds.BOUNCE_7DAY_CRITICAL}
|
|
||||||
status={sevenDayBounceStatus}
|
|
||||||
/>
|
/>
|
||||||
|
<HealthMetric
|
||||||
{/* All-Time Bounce Rate */}
|
label="All Time"
|
||||||
<MetricDisplay
|
level={levels.bounceAllTime}
|
||||||
label="All-Time Bounce Rate"
|
detail={`${status.allTime.bounceRate.toFixed(2)}% bounce rate (${status.allTime.bounces.toLocaleString()} of ${status.allTime.total.toLocaleString()} emails)`}
|
||||||
rate={status.allTime.bounceRate}
|
|
||||||
count={status.allTime.bounces}
|
|
||||||
total={status.allTime.total}
|
|
||||||
warningThreshold={thresholds.BOUNCE_ALLTIME_WARNING}
|
|
||||||
criticalThreshold={thresholds.BOUNCE_ALLTIME_CRITICAL}
|
|
||||||
status={allTimeBounceStatus}
|
|
||||||
/>
|
/>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* Complaint Rate Metrics */}
|
{/* Complaint Metrics */}
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle>Complaint Rate Metrics</CardTitle>
|
<CardTitle>Complaint Rate</CardTitle>
|
||||||
<CardDescription>Complaints occur when recipients mark emails as spam</CardDescription>
|
<CardDescription>Complaints occur when recipients mark emails as spam</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent className="space-y-4">
|
<CardContent className="space-y-4">
|
||||||
{/* 7-Day Complaint Rate */}
|
<HealthMetric
|
||||||
<MetricDisplay
|
label="Last 7 Days"
|
||||||
label="7-Day Complaint Rate"
|
level={levels.complaint7Day}
|
||||||
rate={status.sevenDay.complaintRate}
|
detail={`${status.sevenDay.complaintRate.toFixed(3)}% complaint rate (${status.sevenDay.complaints.toLocaleString()} of ${status.sevenDay.total.toLocaleString()} emails)`}
|
||||||
count={status.sevenDay.complaints}
|
|
||||||
total={status.sevenDay.total}
|
|
||||||
warningThreshold={thresholds.COMPLAINT_7DAY_WARNING}
|
|
||||||
criticalThreshold={thresholds.COMPLAINT_7DAY_CRITICAL}
|
|
||||||
status={sevenDayComplaintStatus}
|
|
||||||
isComplaintRate
|
|
||||||
/>
|
/>
|
||||||
|
<HealthMetric
|
||||||
{/* All-Time Complaint Rate */}
|
label="All Time"
|
||||||
<MetricDisplay
|
level={levels.complaintAllTime}
|
||||||
label="All-Time Complaint Rate"
|
detail={`${status.allTime.complaintRate.toFixed(3)}% complaint rate (${status.allTime.complaints.toLocaleString()} of ${status.allTime.total.toLocaleString()} emails)`}
|
||||||
rate={status.allTime.complaintRate}
|
|
||||||
count={status.allTime.complaints}
|
|
||||||
total={status.allTime.total}
|
|
||||||
warningThreshold={thresholds.COMPLAINT_ALLTIME_WARNING}
|
|
||||||
criticalThreshold={thresholds.COMPLAINT_ALLTIME_CRITICAL}
|
|
||||||
status={allTimeComplaintStatus}
|
|
||||||
isComplaintRate
|
|
||||||
/>
|
/>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
@@ -207,60 +144,25 @@ export function SecuritySettings({metrics, isLoading}: SecuritySettingsProps) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MetricDisplayProps {
|
interface HealthMetricProps {
|
||||||
label: string;
|
label: string;
|
||||||
rate: number;
|
level: SecurityLevel;
|
||||||
count: number;
|
detail: string;
|
||||||
total: number;
|
|
||||||
warningThreshold: number;
|
|
||||||
criticalThreshold: number;
|
|
||||||
status: {
|
|
||||||
color: string;
|
|
||||||
icon: React.ComponentType<{className?: string}>;
|
|
||||||
bg: string;
|
|
||||||
label: string;
|
|
||||||
};
|
|
||||||
isComplaintRate?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function MetricDisplay({
|
function HealthMetric({label, level, detail}: HealthMetricProps) {
|
||||||
label,
|
const config = STATUS_CONFIG[level];
|
||||||
rate,
|
const Icon = config.icon;
|
||||||
count,
|
|
||||||
total,
|
|
||||||
warningThreshold,
|
|
||||||
criticalThreshold,
|
|
||||||
status,
|
|
||||||
isComplaintRate = false,
|
|
||||||
}: MetricDisplayProps) {
|
|
||||||
const Icon = status.icon;
|
|
||||||
const progressValue = Math.min((rate / criticalThreshold) * 100, 100);
|
|
||||||
const decimals = isComplaintRate ? 3 : 2;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="border border-neutral-200 rounded-lg p-4">
|
<div className="flex items-center justify-between border border-neutral-200 rounded-lg p-4">
|
||||||
<div className="flex items-center justify-between mb-3">
|
<div>
|
||||||
<div>
|
<h3 className="font-medium text-neutral-900">{label}</h3>
|
||||||
<h3 className="font-medium text-neutral-900">{label}</h3>
|
<p className="text-sm text-neutral-600 mt-1">{detail}</p>
|
||||||
<p className="text-sm text-neutral-600 mt-1">
|
|
||||||
{count.toLocaleString()} / {total.toLocaleString()} emails
|
|
||||||
<span className="text-neutral-400 mx-2">•</span>
|
|
||||||
<strong>{rate.toFixed(decimals)}%</strong>
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div className={`flex items-center gap-2 ${status.color}`}>
|
|
||||||
<Icon className="h-4 w-4" />
|
|
||||||
<span className="text-sm font-medium">{status.label}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
<div className={`flex items-center gap-2 ${config.color}`}>
|
||||||
<div className="space-y-2">
|
<Icon className="h-4 w-4" />
|
||||||
<Progress value={progressValue} className="h-2" indicatorClassName={status.bg} />
|
<span className="text-sm font-medium">{config.label}</span>
|
||||||
<div className="flex justify-between text-xs text-neutral-500">
|
|
||||||
<span>0%</span>
|
|
||||||
<span>Warning: {warningThreshold}%</span>
|
|
||||||
<span>Critical: {criticalThreshold}%</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ interface SecurityWarningBannerProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function SecurityWarningBanner({status}: SecurityWarningBannerProps) {
|
export function SecurityWarningBanner({status}: SecurityWarningBannerProps) {
|
||||||
// Don't show if no warnings or violations
|
|
||||||
const hasCriticalViolations = status.violations.length > 0;
|
const hasCriticalViolations = status.violations.length > 0;
|
||||||
const hasWarnings = status.warnings.length > 0;
|
const hasWarnings = status.warnings.length > 0;
|
||||||
|
|
||||||
@@ -16,12 +15,10 @@ export function SecurityWarningBanner({status}: SecurityWarningBannerProps) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine severity - critical violations take precedence
|
|
||||||
const variant = hasCriticalViolations ? 'destructive' : 'warning';
|
const variant = hasCriticalViolations ? 'destructive' : 'warning';
|
||||||
const title = hasCriticalViolations
|
const title = hasCriticalViolations
|
||||||
? 'Critical Security Violations - Immediate Action Required'
|
? 'Critical - Immediate Action Required'
|
||||||
: 'Security Warning - Action Required';
|
: 'Warning - Action Recommended';
|
||||||
const issues = hasCriticalViolations ? status.violations : status.warnings;
|
|
||||||
const messageColor = hasCriticalViolations ? 'text-red-800' : 'text-amber-800';
|
const messageColor = hasCriticalViolations ? 'text-red-800' : 'text-amber-800';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -30,16 +27,10 @@ export function SecurityWarningBanner({status}: SecurityWarningBannerProps) {
|
|||||||
<AlertTitle>{title}</AlertTitle>
|
<AlertTitle>{title}</AlertTitle>
|
||||||
<AlertDescription className="flex flex-col sm:flex-row sm:items-start sm:justify-between gap-3">
|
<AlertDescription className="flex flex-col sm:flex-row sm:items-start sm:justify-between gap-3">
|
||||||
<div className="space-y-2 flex-1">
|
<div className="space-y-2 flex-1">
|
||||||
<p className="text-sm font-medium">Your project has exceeded the following security thresholds:</p>
|
<p className={`text-sm ${messageColor}`}>
|
||||||
<ul className={`list-disc list-inside space-y-1 text-sm ${messageColor}`}>
|
|
||||||
{issues.map((issue, idx) => (
|
|
||||||
<li key={idx}>{issue}</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
<p className={`text-xs ${messageColor} mt-2`}>
|
|
||||||
{hasCriticalViolations
|
{hasCriticalViolations
|
||||||
? 'Your project may be suspended soon. Please review the detailed metrics immediately and take action to improve your email quality.'
|
? 'Your bounce or complaint rates have exceeded acceptable levels. Review your contact lists and sending practices to avoid project suspension.'
|
||||||
: 'High bounce or complaint rates can lead to project suspension. Review the detailed metrics and take action to improve your email quality.'}
|
: 'Your bounce or complaint rates are approaching limits. Review your contact lists and remove invalid addresses to maintain good standing.'}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<Link href="/settings?tab=security">
|
<Link href="/settings?tab=security">
|
||||||
|
|||||||
@@ -14,26 +14,25 @@ export interface SecurityStatus {
|
|||||||
projectId: string;
|
projectId: string;
|
||||||
isHealthy: boolean;
|
isHealthy: boolean;
|
||||||
shouldDisable: boolean;
|
shouldDisable: boolean;
|
||||||
|
twentyFourHour: SecurityRateData;
|
||||||
sevenDay: SecurityRateData;
|
sevenDay: SecurityRateData;
|
||||||
allTime: SecurityRateData;
|
allTime: SecurityRateData;
|
||||||
|
isNewProject: boolean;
|
||||||
violations: string[];
|
violations: string[];
|
||||||
warnings: string[];
|
warnings: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SecurityThresholds {
|
export type SecurityLevel = 'healthy' | 'warning' | 'critical';
|
||||||
MIN_EMAILS_FOR_ENFORCEMENT: number;
|
|
||||||
BOUNCE_7DAY_WARNING: number;
|
export interface SecurityMetricLevels {
|
||||||
BOUNCE_7DAY_CRITICAL: number;
|
bounce7Day: SecurityLevel;
|
||||||
BOUNCE_ALLTIME_WARNING: number;
|
bounceAllTime: SecurityLevel;
|
||||||
BOUNCE_ALLTIME_CRITICAL: number;
|
complaint7Day: SecurityLevel;
|
||||||
COMPLAINT_7DAY_WARNING: number;
|
complaintAllTime: SecurityLevel;
|
||||||
COMPLAINT_7DAY_CRITICAL: number;
|
|
||||||
COMPLAINT_ALLTIME_WARNING: number;
|
|
||||||
COMPLAINT_ALLTIME_CRITICAL: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ProjectSecurityMetrics {
|
export interface ProjectSecurityMetrics {
|
||||||
status: SecurityStatus;
|
status: SecurityStatus;
|
||||||
thresholds: SecurityThresholds;
|
levels: SecurityMetricLevels;
|
||||||
isDisabled: boolean;
|
isDisabled: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user