security: widen API-key auth to content/AI routes (v0.27.0)

Convert requireSession -> requireSessionOrApiKey across recipes,
collections, meal-plans, shopping-lists, pantry, feed, and ai/*
(52 routes) so API keys work end-to-end, not just for the handful of
endpoints that supported them before. Scope was explicitly confirmed
per-resource-family with the user before any file was touched.

Left session-cookie-only, deliberately: users/me*, ai-keys/*,
webhooks/*, conversations/*, notifications/*, push/subscribe, admin/*
— account/credential-adjacent surface that shouldn't widen without a
separate, explicit decision.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Arnaud
2026-07-14 11:20:26 +02:00
parent 6c4fc93aab
commit 3e71bd29a2
56 changed files with 171 additions and 159 deletions
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { db, mealPlanEntries, recipes, eq, and, or, ne, inArray } from "@epicure/db";
import { requireSession } from "@/lib/api-auth";
import { requireSessionOrApiKey } from "@/lib/api-auth";
import { getMealPlanAccessById, canWriteMealPlan } from "@/lib/meal-plan-access";
import { dispatchWebhook } from "@/lib/webhooks";
@@ -10,8 +10,8 @@ type Params = { params: Promise<{ mealPlanId: string }> };
// Polled by SharedMealPlanView so every collaborator sees each other's
// changes, and the plan owner's own tab (a different component/URL, per
// meal-plans/[weekStart]/entries) picks up edits made from here too.
export async function GET(_req: NextRequest, { params }: Params) {
const { session, response } = await requireSession();
export async function GET(req: NextRequest, { params }: Params) {
const { session, response } = await requireSessionOrApiKey(req);
if (response) return response;
const { mealPlanId } = await params;
@@ -43,7 +43,7 @@ const Schema = z.object({
});
export async function POST(req: NextRequest, { params }: Params) {
const { session, response } = await requireSession();
const { session, response } = await requireSessionOrApiKey(req);
if (response) return response;
const { mealPlanId } = await params;
@@ -89,7 +89,7 @@ export async function POST(req: NextRequest, { params }: Params) {
}
export async function DELETE(req: NextRequest, { params }: Params) {
const { session, response } = await requireSession();
const { session, response } = await requireSessionOrApiKey(req);
if (response) return response;
const { mealPlanId } = await params;
@@ -1,12 +1,12 @@
import { NextRequest, NextResponse } from "next/server";
import { db, mealPlans, users, eq } from "@epicure/db";
import { requireSession } from "@/lib/api-auth";
import { requireSessionOrApiKey } from "@/lib/api-auth";
import { getMealPlanAccessById } from "@/lib/meal-plan-access";
type Params = { params: Promise<{ mealPlanId: string }> };
export async function GET(_req: NextRequest, { params }: Params) {
const { session, response } = await requireSession();
export async function GET(req: NextRequest, { params }: Params) {
const { session, response } = await requireSessionOrApiKey(req);
if (response) return response;
const { mealPlanId } = await params;