Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Edge-CSRF is CSRF protection middleware for Next.js that runs in the edge runtime.
This library uses the cookie strategy from expressjs/csurf and the crypto logic from pillarjs/csrf except it only uses Next.js edge runtime dependencies so it can be used in Next.js middleware.
x-csrf-token
) or from request body field (csrf_token
)To use Edge-CSRF, first add it as a dependency to your app:
npm install edge-csrf
Next, create a middleware file (middleware.js
) for your project and add the Edge-CSRF middleware:
// middleware.js
import csrf from 'edge-csrf';
import { NextResponse } from 'next/server';
// initalize protection function
const csrfProtect = csrf();
export async function middleware(request) {
const response = NextResponse.next();
// csrf protection
const csrfError = await csrfProtect(request, response);
if (csrfError) {
const url = request.nextUrl.clone();
url.pathname = '/api/csrf-invalid';
return NextResponse.rewrite(url);
}
return response;
}
Next, create a handler to return CSRF error messages to the user:
// pages/api/csrf-invalid.js
export default function handler(req, res) {
res.status(403).send('invalid csrf token');
}
Now, all HTTP submission requests (e.g. POST, PUT, DELETE, PATCH) will be rejected if they do not include a valid CSRF token. To add the CSRF token to your forms, you can fetch it from the x-csrf-token
HTTP response header server-side or client-side. For example:
// pages/my-form.js
export function getServerSideProps({ res }) {
const csrfToken = res.getHeader('x-csrf-token') || '';
return {props: { csrfToken }};
}
export default function MyFormPage({ csrfToken }) {
return (
<form>
<input type="hidden" value={csrfToken}>
<input type="submit">
</form>
);
}
// default config
{
cookie: {
name: '_csrfSecret',
path: '/',
maxAge: 60 * 60 * 12,
domain: '',
secure: true,
httpOnly: true,
sameSite: 'String'
},
ignoreMethods: ['GET', 'HEAD', 'OPTIONS'],
saltByteLength: 8,
secretByteLength: 8,
token: {
responseHeader: 'x-csrf-token',
value: null
}
}
TODO:
FAQs
CSRF protection for Next.js middleware
The npm package edge-csrf receives a total of 4,851 weekly downloads. As such, edge-csrf popularity was classified as popular.
We found that edge-csrf demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.