Skip to content
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

enhancement: make policy error public #19374

Merged
merged 6 commits into from Feb 9, 2024

Conversation

innerdvations
Copy link
Contributor

What does it do?

PolicyError messages will be public to the API as originally intended

Why is it needed?

PolicyError was created to allow policies to throw errors with messages visible to the user. Unfortunately, it was broken for all (or nearly all) of v4, so it was considered a security risk to suddenly make them visible at that point.

In v5, we will add the originally itended behavior to allow policies to throw a custom error message.

How to test it?

Create a policy that throws a custom error message and you should see it
Any other type of ForbiddenError should still be caught and made generic (ie, the message should not be included in the api response)

Related issue(s)/PR(s)

Let us know if this is related to any issue/pull request

@innerdvations innerdvations added flag: 💥 Breaking change This PR contains breaking changes and should not be merged source: core:strapi Source is core/strapi package pr: fix This PR is fixing a bug flag: documentation This PR requires a documentation update labels Jan 30, 2024
@innerdvations innerdvations added this to the 5.0.0 milestone Jan 30, 2024
@innerdvations innerdvations self-assigned this Jan 30, 2024
@derrickmehaffy
Copy link
Member

The docs on this state that technically any error can be used in a policy and that error type should be used 🤔

https://docs.strapi.io/dev-docs/error-handling#policies

At least for all of our built in error classes. (I'm thinking things like ratelimit errors)

@innerdvations
Copy link
Contributor Author

innerdvations commented Jan 30, 2024

The docs on this state that technically any error can be used in a policy and that error type should be used 🤔

docs.strapi.io/dev-docs/error-handling#policies

At least for all of our built in error classes. (I'm thinking things like ratelimit errors)

Hmm. It does seem to say that, but I don't think that has ever worked that way, has it? We might need to update the v4 docs again to clarify.

I think this might be the best we get for v5 RC, but I would LOVE to improve on it and somehow catch anything coming from a policy and make it public. I was thinking an alternative (that could extend beyond policies and into all of Strapi) is that an error could simply have a public property and if it does, we let it through to the content API. But that can come later in v5 as a new feature and not break anything.

EDIT: Ohhhh, but reading what you said again, yeah, this brings it in line with all the other "special" error types being public. RateLimit, etc. The only ones that are converted to generic are ForbiddenError and ApplicationError I believe.

@derrickmehaffy
Copy link
Member

It did originally yeah as I did a whole best practices talk about this a while back.

@derrickmehaffy
Copy link
Member

Yeah those extra classes just build on the generic application error usually.

@derrickmehaffy
Copy link
Member

Here is the talk I did: https://youtu.be/dGNVYTEJNY8?si=2bArhx8_ncghQidJ

@derrickmehaffy
Copy link
Member

Also FYI I'm the one who wrote those docs and created that policy error if you look at the git blame 🙈

@derrickmehaffy
Copy link
Member

#12080

#15938

@innerdvations
Copy link
Contributor Author

innerdvations commented Jan 31, 2024

Yeah, so to be clear, I think this is actually all we need to do, right? Everything except forbiddenError and unauthorizedError are thrown to the content API. And PolicyError is only "special" because it's the only one that inherits from ForbiddenError so we have to watch out for it explicitly.

I suppose a better option would be to start with the list of "allowed" ones and find/throw them first, I might update to do it that way so it's more clear about what we're doing and it can't get messed up again in the future.

(Edit) Let's discuss this today before I do anything, we might be able to simplify all of this and go with what was originally intended when you wrote the feature

@innerdvations innerdvations changed the title enh: make policy error public enhancement: make policy error public Feb 9, 2024
Copy link
Member

@christiancp100 christiancp100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QAed it and works great! 🍝

@innerdvations innerdvations merged commit 7fdffa1 into v5/main Feb 9, 2024
96 checks passed
@innerdvations innerdvations deleted the fix/make-policy-error-public branch February 9, 2024 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: 💥 Breaking change This PR contains breaking changes and should not be merged flag: documentation This PR requires a documentation update pr: fix This PR is fixing a bug source: core:strapi Source is core/strapi package
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants