Skip to content

A route handler loses its guard ​

A Next.js App Router endpoint that creates orders. The change reads like a tidy-up of POST, but it deletes the admin check in front of the write and adds a second write to an audit log, reached through a new helper in another file.

The report puts both on POST: the guard under removed rules, and the audit write under added effects, propagated from recordAudit through the call graph. The session lookup the guard depended on disappears from POST's effects for the same reason.

Configuration ​

json
{
  "$schema": "https://aburi.dev/schema/aburi.config.v1.json",
  "languages": ["@aburi/lang-typescript"],
  "frameworks": ["@aburi/framework-next"],
  "effects": ["@aburi/effects-prisma"]
}

The change ​

diff
diff --git a/app/api/orders/route.ts b/app/api/orders/route.ts
index 273d5b0..0e50027 100644
--- a/app/api/orders/route.ts
+++ b/app/api/orders/route.ts
@@ -1,16 +1,12 @@
 import { NextResponse } from "next/server"
-import { auth } from "../../../lib/auth"
-import { createOrder, recentOrders } from "../../../lib/db"
+import { createOrder, recentOrders, recordAudit } from "../../../lib/db"
 
 export async function GET() {
   return NextResponse.json(await recentOrders())
 }
 
 export async function POST(req: Request) {
-  const session = await auth()
-  if (session?.user.role !== "admin") {
-    return NextResponse.json({ error: "forbidden" }, { status: 403 })
-  }
   const order = await createOrder(await req.json())
+  await recordAudit("order.create", order.id)
   return NextResponse.json(order, { status: 201 })
 }
diff --git a/lib/db.ts b/lib/db.ts
index 8962f3f..c1dd4cd 100644
--- a/lib/db.ts
+++ b/lib/db.ts
@@ -7,7 +7,11 @@ export function recentOrders() {
 }
 
 export function createOrder(data: { customerId: string; totalCents: number }) {
-  return prisma.order.create({ data })
+  return prisma.order.create({ data: { ...data, source: "api" } })
+}
+
+export function recordAudit(action: string, orderId: string) {
+  return prisma.auditLog.create({ data: { action, orderId } })
 }
 
 export function findSession(token: string) {

What aburi diff reports ​

Aburi diff: HEAD~1..HEAD ​

Summary: +1 added · -0 removed · ~2 changed · 0 moved · 0 moved+changed

🔧 Logic changes ​

POST (function) ​

File: app/api/orders/route.ts:8

  • rules removed:
    • guard: session?.user.role !== "admin" (L11)
  • effects added:
    • db.write: prisma.auditLog.create [propagated from ts:lib/db.ts#recordAudit] [effects-prisma]
  • effects removed:
    • db.read: prisma.session.findUnique [propagated from ts:lib/auth.ts#auth] [effects-prisma]
  • calls added:
    • recordAudit (L10)
  • calls removed:
    • auth (L10)
    • NextResponse.json (L15)

🧵 Slice View ​

From POST (3 symbols) ​

Files: app/api/orders/route.ts, lib/db.ts

  • POST — logic and syntax changed · route.ts:8
    • createOrder — syntax only · db.ts:9
    • recordAudit — new function with db.write · db.ts:13

➕ Added ​

recordAudit (function) ​

File: lib/db.ts:13Signature: (action: string, orderId: string)Effects:

  • db.write: prisma.auditLog.create (L14) [effects-prisma]

🔗 Dependency changes ​

Symbol-level added ​
  • POST in app/api/orders/route.ts → recordAudit in lib/db.ts (via call)
Symbol-level removed ​
  • POST in app/api/orders/route.ts → auth in lib/auth.ts (via call)

🎨 Syntax-only changes ​

1 symbol (implementation refactor only, no semantic change)
  • createOrder (lib/db.ts:9)

Released under the Apache License 2.0.