question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow accessibleBy to throw ForbiddenError when the query denied

See original GitHub issue

Is your feature request related to a problem? Please describe.

First thanks for this amazing library, it’s really helpful in many ways.

By using @casl/mongoose and for example ExpressJS I’m trying to find a way to distinguish between
“document not found” and “forbidden” errors.

For example here:

app.get("/:todoId", async (req, res, next) => {
  try {
    const todo = await ToDo.findById(req.params.todoId).accessibleBy(req.ability);
    // problem: if todo is null - was it not found or forbidden?
    return res.json(todo);
  } catch (err) {  
    return next(err);
  }
}); 

In both cases when either the document is not found or the user is not allowed to access it null is returned in todo variable.

Describe the solution you’d like

By giving the option (maybe by adding an argument to .accessibleBy or even better: plugin configuration) to throw ForbiddenError automatically in case of authorization failure we can return 403 status to any caught ForbiddenError thrown by the endpoints.

For example:

function errorHandler (err, req, res, next) {
  ...
  if (err.name === 'ForbiddenError') {
     res.status(403)
  } 
  ...
  res.render('error', { error: err })
}

Describe alternatives you’ve considered (optional)

Using mongoose’s .orFail() method, but instead integrating this ability in @casl/mongoose can really ease the usage in most of the cases instead of patching every endpoint.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
stalniycommented, Feb 12, 2021

🎉 This issue has been resolved in version 4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

1reaction
stalniycommented, Jan 11, 2021

will be released in the next version

Read more comments on GitHub >

github_iconTop Results From Across the Web

403 Forbidden Error: What It Is and How to Fix It - Airbrake Blog
This occurs when attempting to access an invalid or forbidden URL that the web server software has restricted. For example, Apache servers ...
Read more >
HTTP Error 403 Forbidden: What It Means and How to Fix It
Simply put: the server has determined that you are not allowed access to the thing you've requested. According to RFC 7231: The 403 ......
Read more >
HTTP 403 Forbidden | What is 403 Forbidden Error and How ...
HTTP 403 presents a unique error case in the sense that it is returned when the client is denied access to resources it...
Read more >
How to Fix a 403 Forbidden Error on Your WordPress Site
The 403 Forbidden error indicates that the server understood the request but refuses to authorize it. Find out more about the causes and ......
Read more >
Resolve Access Denied errors from a CloudFront distribution ...
Your bucket policy must not have a deny statement that blocks public read access to the s3:GetObject action. If you have an explicit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found