deckhearth/pages/api/collections/[identifier]/permissions.js
Randall Stillwell a84373642e fix(security): drop wildcard CORS + redundant OPTIONS from 24 API routes (P0 #5)
Closes P0 #5 (CORS) from PARTIAL → RESOLVED. fix-auth-bypass
Brief 4 (commit 297afca) cleaned login + register; this brief
sweeps the remaining 24 pages/api/** handlers that carried the
identical scaffolded wildcard-CORS + redundant-OPTIONS pattern,
plus adds a blocking forbidden-cors-headers CI job to lock in
the cleanup against future regression.

Per architect Decision 1 — Option B (sweep all 24 in one PR)
chosen over Option A (narrow verify.js-only + queue separate
sweep). Pattern-drift audit (14 of 24 files spot-checked across
parent + architect) found zero drift; mechanical safety
confirmed.

Per Decision 2 — OPTIONS handler deleted entirely (matches
Brief 4 precedent). Same-origin Vercel deployment doesn't
preflight; method check at top of handler returns 405 if any
client ever sends OPTIONS again.

Per Decision 3 — verify.js's overly-permissive Allow-Methods:
'GET, POST, PUT, DELETE, OPTIONS' is moot (deleted under D2);
the handler's existing `if (req.method !== 'GET') return 405`
guard at line 17 (now line ~5) is the remaining gate.

Per Decision 4 — no new per-route tests this convoy. None of
the 24 routes have vitest coverage today; adding handler-level
tests is the queued fill-vitest-handler-coverage convoy.

Per Decision 5 — new `forbidden-cors-headers` CI job added,
modeled verbatim on `forbidden-endpoints`. Blocking (no
`|| true`, no `continue-on-error`). Greps pages/api/ for any
`Access-Control-Allow-(Origin|Methods|Headers)` reappearance
and exits 1 on hit.

Verification:
  - npm run lint: 128 problems (baseline match)
  - npm run test:run: 21/21 vitest pass (no regression)
  - git grep -nE "Access-Control-Allow-..." -- 'pages/api/**':
    zero matches
  - git grep -nE "OPTIONS" -- 'pages/api/**': zero matches
    (post-sweep)
  - new forbidden-cors-headers grep exits 0 against swept tree

No code paths in lib/**, components/**, scripts/**, or test/**
touched. No package.json / lockfile churn. No workflow YAML
beyond the single ci.yml job addition. No AGENTS.md edits
(doc-writer pass at convoy close).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-24 20:24:47 -05:00

199 lines
No EOL
5.9 KiB
JavaScript

import { sql } from '@vercel/postgres';
import { getUserFromRequest } from '../../../../lib/permission-middleware';
import { isValidSlug } from '../../../../lib/slug-utils';
export default async function handler(req, res) {
try {
// Get authenticated user
const user = await getUserFromRequest(req);
if (!user) {
return res.status(401).json({ error: 'Authentication required' });
}
const { identifier } = req.query;
if (!identifier) {
return res.status(400).json({ error: 'Collection identifier is required' });
}
// Determine if identifier is a slug or numeric ID
const isSlug = isValidSlug(identifier) || isNaN(parseInt(identifier));
// Get collection and verify ownership
let collectionResult;
if (isSlug) {
collectionResult = await sql`
SELECT * FROM collections
WHERE slug = ${identifier} AND user_id = ${user.userId}
`;
} else {
const numericId = parseInt(identifier);
collectionResult = await sql`
SELECT * FROM collections
WHERE id = ${numericId} AND user_id = ${user.userId}
`;
}
if (collectionResult.rows.length === 0) {
return res.status(404).json({ error: 'Collection not found or you do not have permission to manage permissions' });
}
const collection = collectionResult.rows[0];
if (req.method === 'GET') {
// Get all permissions for this collection
const permissionsResult = await sql`
SELECT
cp.*,
u.email,
u.first_name,
u.last_name
FROM collection_permissions cp
JOIN users u ON cp.user_id = u.id
WHERE cp.collection_id = ${collection.id}
ORDER BY cp.created_at DESC
`;
const permissions = permissionsResult.rows.map(perm => ({
id: perm.id,
userId: perm.user_id,
email: perm.email,
firstName: perm.first_name,
lastName: perm.last_name,
role: perm.role,
status: perm.status,
createdAt: perm.created_at,
updatedAt: perm.updated_at
}));
res.status(200).json({ permissions });
} else if (req.method === 'POST') {
// Add new permission
const { email, role = 'viewer' } = req.body;
if (!email) {
return res.status(400).json({ error: 'Email is required' });
}
if (!['viewer', 'editor', 'owner'].includes(role)) {
return res.status(400).json({ error: 'Invalid role. Must be viewer, editor, or owner' });
}
// Find user by email
const userResult = await sql`
SELECT id FROM users WHERE email = ${email}
`;
if (userResult.length === 0) {
return res.status(404).json({ error: 'User not found' });
}
const targetUserId = userResult[0].id;
// Check if permission already exists
const existingResult = await sql`
SELECT id FROM collection_permissions
WHERE collection_id = ${collection.id} AND user_id = ${targetUserId}
`;
if (existingResult.length > 0) {
return res.status(400).json({ error: 'User already has permissions for this collection' });
}
// Create new permission
const result = await sql`
INSERT INTO collection_permissions (collection_id, user_id, role, status)
VALUES (${collection.id}, ${targetUserId}, ${role}, 'active')
RETURNING *
`;
res.status(201).json({
message: 'Permission added successfully',
permission: result[0]
});
} else if (req.method === 'PUT') {
// Update existing permission
const { permissionId, role, status } = req.body;
if (!permissionId) {
return res.status(400).json({ error: 'Permission ID is required' });
}
const updateFields = [];
const updateValues = [];
let paramIndex = 1;
if (role !== undefined) {
if (!['viewer', 'editor', 'owner'].includes(role)) {
return res.status(400).json({ error: 'Invalid role' });
}
updateFields.push(`role = $${paramIndex}`);
updateValues.push(role);
paramIndex++;
}
if (status !== undefined) {
if (!['active', 'pending', 'revoked'].includes(status)) {
return res.status(400).json({ error: 'Invalid status' });
}
updateFields.push(`status = $${paramIndex}`);
updateValues.push(status);
paramIndex++;
}
if (updateFields.length === 0) {
return res.status(400).json({ error: 'No fields to update' });
}
updateFields.push('updated_at = CURRENT_TIMESTAMP');
updateValues.push(permissionId, collection.id);
const updateQuery = `
UPDATE collection_permissions
SET ${updateFields.join(', ')}
WHERE id = $${paramIndex} AND collection_id = $${paramIndex + 1}
RETURNING *
`;
const result = await sql.query(updateQuery, updateValues);
if (result.length === 0) {
return res.status(404).json({ error: 'Permission not found' });
}
res.status(200).json({
message: 'Permission updated successfully',
permission: result[0]
});
} else if (req.method === 'DELETE') {
// Remove permission
const { permissionId } = req.body;
if (!permissionId) {
return res.status(400).json({ error: 'Permission ID is required' });
}
const result = await sql`
DELETE FROM collection_permissions
WHERE id = ${permissionId} AND collection_id = ${collection.id}
RETURNING *
`;
if (result.length === 0) {
return res.status(404).json({ error: 'Permission not found' });
}
res.status(200).json({ message: 'Permission removed successfully' });
} else {
res.status(405).json({ error: 'Method not allowed' });
}
} catch (error) {
console.error('Collection permissions API error:', error);
res.status(500).json({ error: 'Internal server error' });
}
}