-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
If the csrf cookie is changed manually after it is set, the application crashes even with get request and the error does not go to the handler #56
Comments
Going to need more details for this one.
Changed how? Can you provide some code examples? Typically you shouldn't be manually changing the cookie in the first place. If you need to change some of the properties on the cookie, the feature to support doing that will likely be provided via fixing issue #46
Exactly which line of code are you seeing cause the crash? If you look at the middleware: const doubleCsrfProtection: doubleCsrfProtection = (req, res, next) => {
req.csrfToken = (overwrite?: boolean, validateOnReuse?: boolean) =>
generateToken(req, res, overwrite, validateOnReuse);
if (ignoredMethodsSet.has(req.method as RequestMethod)) {
next();
} else if (validateRequest(req)) {
next();
} else {
next(invalidCsrfTokenError);
}
}; You'll see that for a get request (by default), the middleware is entirely skipped, there is no access being made to the cookie. The only way the cookie is used on a get request is if you have overridden the
You've posted this error. How are you getting that error if it isn't going to the handler? Depending on the order of your middleware registration, it's possible the error is being passed to the default handler instead of your own. It's really difficult to have any insight here without additional details. Ideally a fully reproducible example |
Here is my config:
And here is my route:
after getting login page, if I change _csrf cookie manually, app immediately crashes even if I don't use doubleCsrf middleware just because of "generateToken". Its not prevented with error handling. |
I can't even console the error here |
1- Using like "generateToken(req, res, true)" fixes the issue since cookie will always be recreated. |
Edit: See below, since you're in a promise based context, the error not bubbling or being caught is the native behavior of express v4. It's documented in their error handling docs. Hey @Sertturk16 based on the example code you've provided so far, it's evident where the error is occurring. As part of the // If ovewrite is true, always generate a new token.
// If overwrite is false and there is no existing token, generate a new token.
// If overwrite is false and there is an existin token then validate the token and hash pair
// the existing cookie and reuse it if it is valid. If it isn't valid, then either throw or
// generate a new token based on validateOnReuse.
if (typeof csrfCookie === "string" && !overwrite) {
const [csrfToken, csrfTokenHash] = csrfCookie.split("|");
if (validateTokenAndHashPair(csrfToken, csrfTokenHash, possibleSecrets)) {
// If the pair is valid, reuse it
return { csrfToken, csrfTokenHash };
} else if (validateOnReuse) {
// If the pair is invalid, but we want to validate on generation, throw an error
// only if the option is set
throw invalidCsrfTokenError;
}
} So you should be able to do this: This part is expected behaviour. I asked the following questions, which remain unanswered:
So, the only issue I'm seeing here is that for some reason the error being thrown from Edit: Hey I just noticed your running async (req, res, next) => {
if (!req.user) {
return res.render('login', {
returnTo: req.session.returnTo,
csrfToken: generateToken(req, res)
})
}
next()
}, Why are you using async here? If you remove async, it will work, otherwise you need to handle the error yourself, as per the official express docs. You would have to do this: async (req, res, next) => {
try {
if (!req.user) {
return res.render('login', {
returnTo: req.session.returnTo,
csrfToken: generateToken(req, res)
})
}
next()
} catch (error) {
next(error);
}
}, This isn't a csrf-csrf bug. This is the intended / expected behavior of express. |
As there have been no replies and the issue seems to be the expected behavior, I'm closing this issue for now. |
ForbiddenError: invalid csrf token
at doubleCsrf (/Users/omersertturk/Desktop/repositories/id/node_modules/csrf-csrf/lib/cjs/index.cjs:17:61)
The text was updated successfully, but these errors were encountered: