Branchwell

AI Resource Pro / website / SECURITY_UPDATES.md

Show the source

Blog API Security Updates

Overview

This document outlines the security improvements implemented and needed for the blog API endpoints. All write operations (POST, PUT, DELETE) must be secured with both admin authentication and API key validation.

Security Requirements

All write operations on blog endpoints must implement:

  1. Admin authentication via Supabase (validateRequest() with isAdmin check)
  2. API key validation (validateApiKey())

Ideally, use the validateAdminApiRequest() middleware which implements both checks.

Already Updated

  • ✅ /src/app/api/blog/route.ts - Main blog API (POST, PUT, DELETE)
  • ✅ /src/app/api/admin/blog/create/route.ts - Admin blog creation

Remaining Routes to Update

The following routes need to be updated to use the validateAdminApiRequest() middleware:

  1. /src/app/api/admin/blog/update/route.ts
  2. /src/app/api/admin/blog/delete/route.ts
  3. /src/app/api/admin/blog/upload-image/route.ts
  4. /src/app/api/admin/blog/route.ts
  5. /src/app/api/admin/blog/[id]/route.ts
  6. /src/app/api/admin/blog/categories/route.ts

Implementation Pattern

Replace the existing authentication check:

const { user } = await validateRequest();
    
if (!user || !user.isAdmin) {
  return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}

With the secure middleware:

// Validate security requirements for admin API endpoints
const securityError = await validateAdminApiRequest(request);
if (securityError) {
  return securityError;
}

Don't forget to import the middleware:

import { validateAdminApiRequest } from '@/middleware/protected-routes';

And ensure the request parameter is properly typed as NextRequest:

export async function POST(request: NextRequest) {
  // ...
}

Testing

After implementing these changes, test all endpoints with:

  1. Valid admin authentication + valid API key (should succeed)
  2. Valid admin authentication + invalid API key (should fail)
  3. Invalid admin authentication + valid API key (should fail)
  4. Invalid admin authentication + invalid API key (should fail)