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

nullcheck for $this->email #15

Merged
merged 3 commits into from
Feb 26, 2024
Merged

Conversation

StrykeSlammerII
Copy link
Contributor

Without the nullcheck, a permissions test against !is_master(self.id) results in the error trim(): Argument #1 ($string) must be of type string, null given here in getAvatarAttribute().

With the nullcheck, there is no error and userfrosting.log gives

[2024-01-31T18:47:36.554796-05:00] auth.DEBUG: Evaluating access condition '!is_master(self.id)' with parameters: {
    "user": {
        "UserFrosting\\Sprinkle\\Account\\Database\\Models\\User": {
            "full_name": " ",
            "avatar": "https://www.gravatar.com/avatar/?d=mm"
        }
    },
    "self": {
[etc]

While $this->email appears elsewhere to be expected as type string rather than ?string, the auth.DEBUG output demonstrates that it is somehow fed a blank $user from somewhere.

This PR is a bandaid fix. Searching for the source of the blank $user and resolving that would be a better fix.

@lcharette
Copy link
Member

lcharette commented Feb 25, 2024

I was able to reproduce locally by passing an empty user to AccessConditionEvaluator::evaluate directly, however I'm not sure how you encountered this bug in normal code?

The only way a user doesn't have an email is if, well, he doesn't have one defined. And email is (Should?) be mandatory in the UI... Other way is for the "not logged in" user to be used, which shouldn't happen either.

Actually, empty string is fine... email needs to be null, which should only happen when User is not saved:

$user = new User();
$user->toArray(); //<-- Exception

Can you share more how to replicate this bug?

@lcharette lcharette self-assigned this Feb 25, 2024
@StrykeSlammerII
Copy link
Contributor Author

I ran into it when accidentally injecting an empty user.
(chat reference: https://chat.userfrosting.com/channel/support?msg=NZvHNcLrHemfW3zNW)

That may not be normal code 😁 but hitting an error in getAvatarAttribute() made tracing the injection failure more difficult.
I don't have a copy of the error handy, but IIRC Whoops error handler fails if getAvatarAttribute() fails.
$this->logger->debug($user) also fails--again, it just makes debugging more difficult.

If we want getAvatarAttribute() to throw an error in the unexpected case where $user->email is null, I'd like to add a comment instead regarding injecting an empty user.
Let me know if that's the preferred solution and I'll change this PR.

Copy link

codecov bot commented Feb 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (4715f7a) to head (ed8568e).
Report is 4 commits behind head on 5.0.

Additional details and impacted files
@@             Coverage Diff             @@
##                 5.0       #15   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
+ Complexity       552       551    -1     
===========================================
  Files             93        93           
  Lines           2104      2108    +4     
===========================================
+ Hits            2104      2108    +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lcharette
Copy link
Member

Alright, just to be sure it wasn't another underlying issue.

I added a test and simplify the syntax, should be good to go.

@lcharette lcharette merged commit dc333fe into userfrosting:5.0 Feb 26, 2024
26 checks passed
@StrykeSlammerII StrykeSlammerII deleted the patch-1 branch February 26, 2024 06:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants