Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
CSRF protection using the power of JWTs. Provides a number of stateless methods of csrf protection, if you don't want to keep a session.
Defaults to the double submit method of csrf protection, but supports a number of different strategies.
var express = require('express');
var app = express();
var jwtCSRF = require('jwt-csrf');
var jwtMiddleware = jwtCSRF.middleware(options); // This can be used like any other Express middleware
app.use(jwtMiddleware); // Executed on all requests
The middleware must be included before others to be effective.
On errors, jwt-csrf will call next(err)
with a jwtCSRF.CSRFError
. If you want to handle this specifically, you can do so in a middleware:
function(err, req, res, next) {
if (err instanceof jwtCSRF.CSRFError) {
explode();
}
}
options
is an Object with the following format:
DOUBLE_SUBMIT
.60
.x-csrf-jwt
.request.originalUrl
, then it will be tested against the url as a direct string match.AUTHED_TOKEN
and AUTHED_DOUBLE_SUBMIT
strategies. Must accept req
and return a user-specific token (like a user id) for a known user.req
and return a domain that the cookie will be scoped for (Ex: ".mysite.com"). Otherwise, defaults to the domain inside of the request.Persist two linked tokens on the client side, one via an http header, another via a cookie. On incoming requests, match the tokens.
Persist a token via an http header linked to the currently authenticated user. Validate against the user for incoming requests.
Requires getUserToken
to be set in options
A combination of DOUBLE_SUBMIT
and AUTHED_TOKEN
, either strategy passing will allow the request to go through.
Note that jwt-csrf only works for ajax calls, not full-page posts, since it relies on being able to set and read http headers.
Firstly, you will need to pass the token down in your initial page render. You can get the value as follows on the server-side, to insert into your initial html:
var jwtCsrf = require('jwt-csrf');
var token = jwtCsrf.getHeaderToken(req, res, { secret: mySecret });
You have two options for persisting the csrf token on the client side:
x-csrf-jwt
headerx-csrf-jwt
headerFor example:
var csrfJwt;
jQuery.ajax({
type: 'POST',
url: '/api/some/action',
headers: {
'x-csrf-jwt': csrfJwt
},
success: function(data, textStatus, request){
csrfJwt = request.getResponseHeader('x-csrf-jwt');
}
});
var jwtCsrf = require('jwt-csrf/client');
jwtCsrf.setToken(initialToken);
jwtCsrf.patchXhr();
This will hook into each request and response and automatically persist the token on the client side for you.
FAQs
A jwt middleware provider for hermes
We found that jwt-csrf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.