Skip to content

Security: add session guard to /api/chat and audit per-user scoping across API routes #85

Description

@shreekardittakavi

Spec: SDR 4.6 Security and Privacy — "The system shall protect user-uploaded academic content and personal schedule information" and "shall restrict access to each user's private notes, study materials, and calendar data." Also AGENTS.md gotcha #3.

The problem

app/api/chat/route.ts is eleven lines and has no session check. Anyone who can reach the deployment can POST to it and run inference on the project's Gemini key:

export async function POST(req: Request) {
  const { messages } = await req.json();
  return createAgentUIStreamResponse({ agent: baseAgent, uiMessages: messages });
}

Today the blast radius is quota abuse, since the agent has no tools. The moment AskHamiz gains tools over notes, classes, and the calendar, this becomes unauthenticated access to every user's data. It has to be fixed before that work lands.

Scope

  • Add the standard guard to /api/chat:
    const session = await auth.api.getSession({ headers: await headers() });
    if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  • Audit every route under app/api/ for two things: a session guard, and a userId filter on every DB read and write. Ownership must be enforced server-side, not inferred from a client-supplied id.
  • Add per-user rate limiting on AI generation routes (chat, quiz generate, flashcards, note upload) so one account cannot exhaust the shared model quota.
  • Confirm error responses follow AGENTS.md §5 — short user-facing string, real error to console.error, never error.message from a catch.

Acceptance criteria

  • /api/chat returns 401 without a session
  • Every API route is either guarded or documented as deliberately public
  • No route trusts a client-supplied user id for ownership
  • AI routes are rate-limited per user

Metadata

Metadata

Assignees

No one assigned

    Labels

    BackendAPI, DB, and server-side workSecurityAuth, access control, privacybugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions