auth0 / node-oauth2-jwt-bearer

Monorepo for libraries that protect Node APIs with OAuth2 Bearer JWTs
MIT License
90 stars 30 forks source link

Crashed my express server. #136

Open DavidWalley opened 7 months ago

DavidWalley commented 7 months ago

Checklist

Description

I made a mistake in my client's authentication code, and it crashed my server.

Reproduction

I have been running a development express server without any problem for some time. I made an error in the client, which I have fixed. But, I was able to crash the express server. I don't want this, or any error, to crash the server, I just want an error message, or rejected request. I don't know where to start with this error: InvalidTokenError: Invalid Compact JWS at /home/dave/Desktop/AAA/PrizeKick/App/api3/node_modules/express-oauth2-jwt-bearer/dist/index.js:300:19 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async /home/dave/Desktop/AAA/PrizeKick/App/api3/node_modules/express-oauth2-jwt-bearer/dist/index.js:403:24

Obviously I am doing something wrong but the server crashed and I don't want someone else to crash the server.

Additional context

No response

express-oauth2-jwt-bearer version

1.6.0

Node.js version

v20.10.0

carluis321 commented 6 months ago

@DavidWalley did you create the middleware for catching errors?

ehaynes99 commented 1 month ago

There's nothing particularly unique about this middleware vs others. You should always have a global "catch all" error handler attached to your app AFTER all other routes and middleware. Note that express looks at the number of arguments to determine if a handler is an error handler or not, so even if unused, it has to have 4.

const errorHandler: ErrorRequestHandler = (err, req, res, _next) => {
  console.error('Unhandled error during request', err)
  res.set('Content-Type', 'application/json').status(500).json({ error: err.message })
}

// very last `use`
app.use(errorHandler)