AI Resource Pro / website / API_SECURITY.md
API Security Requirements
Overview
This document outlines the security requirements for all API endpoints in the AI Resource Pro application.
Core Security Principles
- Dual Authentication for All Write Operations
- All POST, PUT, DELETE, and PATCH requests must be secured with:
- Admin authentication (Supabase auth + is_admin check)
- Valid API key (NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY)
- All POST, PUT, DELETE, and PATCH requests must be secured with:
- No Security Bypasses
- No endpoints should create security vulnerabilities
- No endpoints to bypass authentication
- No endpoints to set admin status without proper validation
- Admin User Management
- User management should be done via Supabase Dashboard
- Admin rights assignment should be done via database administration tools
- No API endpoints should ever create or modify admin status
Authentication Methods
1. Admin Authentication
- Based on Supabase authentication
- User must be authenticated with Supabase
- User must have
is_admin: truein the database
2. API Key Validation
- All secured endpoints must validate the
x-api-keyheader - API key must match the
AI_RESOURCE_PRO_API_KEYenvironment variable - API key validation is implemented in
src/middleware/auth.ts
Implementation
Security Middleware
We use a standardized middleware to enforce API security:
// src/middleware/protected-routes.ts
export async function validateAdminApiRequest(request: NextRequest) {
// 1. Validate admin authentication
const { user } = await validateRequest();
if (!user || !user.isAdmin) {
return NextResponse.json(
{ error: 'Unauthorized - Admin authentication required' },
{ status: 401 }
);
}
// 2. Validate API key
const apiKeyError = validateApiKey(request);
if (apiKeyError) {
return apiKeyError;
}
// Both checks passed
return null;
}
Using the Security Middleware
All protected API endpoints should use the security middleware:
export async function POST(request: NextRequest) {
// Validate security requirements
const securityError = await validateAdminApiRequest(request);
if (securityError) {
return securityError;
}
// Proceed with the endpoint logic
// ...
}
Security Testing
Before creating any new API endpoint, ensure that:
- All POST, PUT, DELETE, PATCH operations are secured
- No endpoints bypass authentication mechanisms
- No endpoints modify admin status via API
- API security middleware is applied correctly
Common Issues
Missing API Key
If you receive a 401 Unauthorized error with "Missing API key header", make sure you're:
- Including the
x-api-keyheader in your request - Setting the header value to match the
NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEYenvironment variable
Authentication Failed
If you receive a 401 Unauthorized error with "Admin authentication required", ensure:
- You're properly authenticated (logged in as admin)
- Your user has
is_admin: truein the database - The admin_session_active cookie is set
Development Notes
- In development, use the API key from your
.env.localfile for testing - Never create endpoints that bypass security mechanisms
- Never set
BYPASS_AUTH_IN_DEV=truein production environments