-
Notifications
You must be signed in to change notification settings - Fork 107
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
[BUG] return an Error will not stop middleware chain.Still call route handler! #322
Comments
Could you provide a reproducible example? I believe the example snippets already have tests for them which would fail If they were not behaving properly? |
|
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as outdated.
This comment was marked as outdated.
I´ve now tested all the version between, its from 6.16.4 -> 6.17.2 This bug originates from this commit: e521785 I don´t know if im the only one who returns a error in the middleware if a session is rejected, but if thats commen this might cause security issues for projects using hyper-express. On my application it gives everyone full admin permissions. Code i used for testing: const HyperExpress = require('hyper-express');
const app = new HyperExpress.Server();
const port = process.env.PORT || 80;
const bindip = process.env.BINDIP || '0.0.0.0'
const verifyRequest = () => {
return async (req, res) => {
try {
console.log("Yes")
throw new Error("No")
} catch (error) {
return error; // This will trigger global error handler as we are returning an Error
}
};
};
app.get('/', verifyRequest(), async (req, res) => {
res.status(200);
res.json({
message: `Secret`
});
});
app.set_error_handler((req, res, error) => {
res.status(500);
res.json({
message: error.message
});
});
app.listen(port, bindip)
.then((socket) => console.log(`Listening on port: ${port}`))
.catch((error) => console.error(`Failed to start webserver on: ${port}\nError: ${error}`)); |
This isn't actually a bug in the code but rather a problem in the documentation. The problem is using a return statement doesn't actually pass control to the error handler, and I don't believe it should either from a design philosophy. If you change your
EDIT: To clarify the current documentation states "Throwing or iterating |
Thanks for your quick response pointing this out @zeta-squared. I've tryed it like you said and it works in the new version again. throw new Error("No")
^
Error: No
at Object.handler (C:\Users\1515\Desktop\hypertest\index.js:10:15)
at Route.handle (C:\Users\1515\Desktop\hypertest\node_modules\hyper-express\src\components\router\Route.js:112:37)
at Server._handle_uws_request (C:\Users\1515\Desktop\hypertest\node_modules\hyper-express\src\components\Server.js:527:19) In gerneral there should be a test to catch when any behaviour with error handling changes like this. At least i do not expect such significant change from such a smal version jump. |
That's strange I copied your snippet from above and tested with my suggestions and they work. I've included it below. const HyperExpress = require('hyper-express');
const app = new HyperExpress.Server();
const port = process.env.PORT || 5000;
const bindip = process.env.BINDIP || '0.0.0.0'
const verifyRequest = () => {
return async (req, res, next) => {
/**
* Optionally replace the try-catch block with:
* console.log("Yes")
* throw new Error("No")
*/
try {
console.log("Yes")
throw new Error("No")
} catch (error) {
next(error); // This will trigger global error handler as we are returning an Error
}
};
};
app.get('/', verifyRequest(), async (req, res) => {
res.status(200);
res.json({
message: `Secret`
});
});
app.set_error_handler((req, res, error) => {
res.status(500);
res.json({
message: error.message
});
});
app.listen(port, bindip)
.then((socket) => console.log(`Listening on port: ${port}`))
.catch((error) => console.error(`Failed to start webserver on: ${port}\nError: ${error}`)); |
I´ve looked at @zeta-squared #328 and your solution there with const verifyRequest = () => {
return async (req, res, next) => {
try {
console.log("Yes")
throw new Error("No")
} catch (error) {
next(error);
};
};
}; |
I have left the throw in my fork because that works in the current (6.17.3) version of hyper-express so it should be included. Just to help me understand the situation a bit more, are you trying to transition to the current version of hyper-express or are you wanting to stay on 6.16.4 and add additional features to your application but are running into issues because the current documentation is not relevant for 6.16.4? |
I work on this application for over a year and from time to time i update my packages with Spoiler: My spesific story, not relevant to this issue at all just for your questionNow yesterday i wrote a API route to allow the user to view and remove all of his current sessions, when testing this i got the error that the user was not defind when the UI updated after deletion was completed. My `verifyRequest` middleware validates the session and then adds users permissions and the user itself to `req.user` which i use in my route to save a database transaction. At first glance, this makes sense because the session got deleted by the previus request. However later i realized that it should not even go into the route because the session was invalid but because "bug" (or whatever you wanna call it) the returned error did not matter anymore. Most routes perform actions by session context so `db.user.updateName(req.user.id)` so a user can only ever update his own data, but admin routes use `db.user.updateName(param.id)` (param refering to `/editname/:id`. So on 6.17.3 every user can just send any token, or even just not include a token at all and can edit any user with those routes because the middleware won´t stop it anymore.Edit: If you wanna look at the code, its there: |
@BolverBlitz Ok I understand now. Going back to:
Are you saying that just using throw instead of a try-catch in 6.17.3 does not work either? |
@BolverBlitz is your application written in JavaScript or TypeScript? |
@zeta-squared Pure JS, i did link to it there in the spoiler.
|
@BolverBlitz I'm sorry I couldn't be anymore help. I have looked at the code you linked and there are many try-catch blocks which will catch then return the error. It's possible that these are causing the conflict. At least for now you have a working solution. When you have had a chance to write some tests and reproduce the bug can you please let me know. I need to make sure that if this is indeed an unintended bug that I account for it as well with the router specific error handlers in my fork - and of course if they're accepted into the main repo. |
Use async middleware, retuen an Error. Still call route handler.
But in middleware docs said it will trigger global error handler as we are returning an Error,Docs
The text was updated successfully, but these errors were encountered: