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

fix: Auth middleware params should not be mutated #261

Merged
merged 1 commit into from
Feb 22, 2024
Merged

Conversation

gkats
Copy link
Member

@gkats gkats commented Feb 22, 2024

Applying authentication options should happen inside the handler to avoid the authorization parameters from getting mutated on each handler execution.

This was caught by a test in an upcoming PR. Sadly, it's hard to write a test to catch the bug here.

Applying authentication options should happen inside the handler to
avoid the authorization parameters from getting mutated on each handler
execution.
@gkats gkats requested a review from a team as a code owner February 22, 2024 16:12
@gkats gkats merged commit 166afad into v2 Feb 22, 2024
4 checks passed
@gkats gkats deleted the fix-middleware-params branch February 22, 2024 16:17
if paramsErr != nil {
w.WriteHeader(http.StatusUnauthorized)
return
params := &AuthorizationParams{}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Should we maybe also avoid passing pointers to params fields such as in line 70?

claims, err := jwt.Verify(r.Context(), &params.VerifyParams)

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about it as well, but in this case at least I believe it's not "our" fault and the call chain won't modify something surprisingly?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed but on the other hand is there any point in passing a pointer? 🤔 Raising this now since we won't be able to change this later, it's a breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean changing all the SDK's methods to accept parameters as non-pointer types?

Copy link
Contributor

Choose a reason for hiding this comment

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

Unless the nil check is necessary or convenient, generally I don't see benefit in using pointers but I may be missing something. I was mostly referring to jwt.Verify though since it can encounter conditions where parameter objects are propagated through multiple layers. I can't imagine other methods having the same kind of flow, parameters will be probably constructed just near the call!

Copy link
Member Author

Choose a reason for hiding this comment

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

I see, thanks for the explanation. I understand the concern, but I'm not sure what's best from a design perspective.

So you're suggesting that all SDK methods accept pointers except jwt.Verify? What about jwt.Decode? Does it fall in the same category?

What's the general heuristic for deciding whether we should accept a pointer or non-pointer?

Is it ok to have all SDK methods accept pointers to structs (what we do now) except for one or two methods?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it ok to have all SDK methods accept pointers to structs (what we do now) except for one or two methods?

If you need to detect nil (no parameters) I think it's fine. jwt.Verify seems to receive options through this object and not request parameters. That might be a key difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants