/nextjs-16:securing-data-access-layerfrom nextjs-16
Teach Data Access Layer pattern to prevent CVE-2025-29927 middleware authentication bypass. Use when implementing authentication, authorization, protecting routes, or working with server actions that need auth.
Ships in nextjs-16. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Next.js 16 has a critical authentication bypass vulnerability in middleware. Middleware NextResponse.redirect() and NextResponse.rewrite() DO NOT terminate execution, allowing unauthorized access to protected resources.
export function middleware(request: NextRequest) {
const session = request.cookies.get('session');
if (!session) {
return NextResponse.redirect(new URL('/login', request.url));
}
}
This middleware appears to protect routes, but code after the return statement still executes. Attackers can bypass authentication by manipulating requests.
Implement a multi-layer security strategy with authentication verification at every access point.
For comprehensive input validation patterns to use alongside authentication, use the sanitizing-user-inputs skill from the typescript plugin.
Create a centralized Data Access Layer that verifies authentication before ANY data access:
import 'server-only';
import { cookies } from 'next/headers';
import { decrypt } from '@/lib/session';
import { cache } from 'react';
export const verifySession = cache(async () => {
const cookie = (await cookies()).get('session')?.value;
const session = await decrypt(cookie);
if (!session?.userId) {
throw new Error('Unauthorized');
}
return { isAuth: true, userId: session.userId };
});
Key features:
cache() for request-level memoization (single verification per request)export default async function DashboardLayout({ children }) {
const session = await verifySession();
if (!session.isAuth) {
redirect('/login');
}
return <>{children}</>;
}
Verify session in layouts and pages to redirect unauthorized users. This is UX only, not security.
export async function getUser() {
const session = await verifySession();
const data = await db.query.users.findMany({
where: eq(users.id, session.userId),
});
return data;
}
Always verify session before database queries. This is your actual security boundary.
For type-safe database access patterns, use the ensuring-query-type-safety skill from prisma-6 to prevent type errors and runtime failures in your data access functions.
'use server';
export async function updateProfile(formData: FormData) {
const session = await verifySession();
const name = formData.get('name');
await db.update(users).set({ name }).where(eq(users.id, session.userId));
revalidatePath('/profile');
}
Verify session at the start of every server action that modifies data.
When working with authenticated features:
lib/dal.ts with verifySession() functionverifySession() in ALL data fetching functionsverifySession() in ALL server actionscache() for request-level memoizationexport async function verifyAdmin() {
const session = await verifySession();
const user = await db.query.users.findFirst({
where: eq(users.id, session.userId),
});
if (user?.role !== 'admin') {
throw new Error('Forbidden');
}
return { userId: session.userId, role: user.role };
}
export async function getPost(postId: string) {
const session = await verifySession();
const post = await db.query.posts.findFirst({
where: eq(posts.id, postId),
});
if (post.authorId !== session.userId) {
throw new Error('Forbidden');
}
return post;
}
If setting up PrismaClient with singleton pattern in Next.js, use the creating-client-singletons skill from prisma-6 for proper instantiation preventing connection pool exhaustion.
If implementing authenticated operations requiring atomicity (e.g., creating a post with tags, transferring ownership), use the using-interactive-transactions skill from prisma-6 for database-specific transaction patterns.
export async function getProfile(username: string) {
const session = await verifySession().catch(() => null);
const profile = await db.query.users.findFirst({
where: eq(users.username, username),
columns: {
username: true,
bio: true,
email: session ? true : false,
},
});
return profile;
}
See the references/ directory for:
dal-example.md - Complete working example with full implementationcve-2025-29927.md - Detailed vulnerability analysis and exploitation examplesApply this pattern when:
This is the most important security pattern in Next.js 16. Use it in every authenticated application.
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
9 skills in this plugin
/configuring-image-optimizationTeach image optimization configuration changes in Next.js 16. Use when configuring images, troubleshooting image loading, or migrating image settings./implementing-parallel-routesTeach parallel routes and slot patterns in Next.js 16. Use when implementing parallel routes, working with @slot syntax, or encountering missing default.tsx errors./migrating-async-request-apisTeach async request APIs in Next.js 16 - params, searchParams, cookies(), headers(), draftMode() are now async. Use when migrating from Next.js 15, fixing type errors, or working with request data./migrating-middleware-to-proxyTeach middleware.ts to proxy.ts migration in Next.js 16. Use when migrating middleware, encountering middleware errors, or implementing request proxying./reviewing-nextjs-16-patternsReview code for Next.js 16 compliance - security patterns, caching, breaking changes. Use when reviewing Next.js code, preparing for migration, or auditing for violations./securing-server-actionsTeach server action authentication and security patterns in Next.js 16. Use when implementing server actions, form handlers, or mutations that need authentication.npx claudepluginhub djankies/claude-configs --plugin nextjs-16