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(appstore): Ensure returned apps from AppStore are valid #47854

Merged
merged 2 commits into from
Sep 13, 2024

Conversation

solracsf
Copy link
Member

@solracsf solracsf commented Sep 9, 2024

Checklist

@solracsf solracsf force-pushed the validAppsFromAppStore branch from b1246a9 to ae17f47 Compare September 9, 2024 17:56
@solracsf solracsf added this to the Nextcloud 31 milestone Sep 9, 2024
Copy link
Contributor

@come-nc come-nc left a comment

Choose a reason for hiding this comment

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

This does not fix the linked TypeError from what I understand.
The problem of the TypeError is more that $response['data'] was not set I suppose?
Any idea what the response looked like? Because if it was empty it would return early line 57 already.

@solracsf
Copy link
Member Author

My analysis was that some elements of $response['data'] were null or not properly initialized. Specifically, when some apps didn’t have matching releases (or, in this case, were not available), $response['data'][$dataKey] = [] is set. However, there may be cases where $response['data'] elements are null or invalid, causing array_filter() to throw an error.

By adding the is_array() check, we should prevent non-array or null values from being passed to array_filter(), which avoids the TypeError. But maybe i'm not seeing it the right way... 👀

@provokateurin
Copy link
Member

provokateurin commented Sep 10, 2024

No @come-nc's analysis is right:

<?php
array_filter(null);

will give you

PHP Fatal error:  Uncaught TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in /home/jld3103/.config/JetBrains/IntelliJIdea2024.2/scratches/scratch_4.php:2
Stack trace:
#0 /home/jld3103/.config/JetBrains/IntelliJIdea2024.2/scratches/scratch_4.php(2): array_filter()
#1 {main}
  thrown in /home/jld3103/.config/JetBrains/IntelliJIdea2024.2/scratches/scratch_4.php on line 2

Process finished with exit code 255

Which is exactly the same error message. The error message clearly mentions the method and the argument.

Running

<?php
array_filter([null]);

results in no error.

Copy link
Member

@provokateurin provokateurin left a comment

Choose a reason for hiding this comment

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

The if (empty($response)) { check further up should be extended to be if (empty($response) || $response['data'] === null) {.

@provokateurin
Copy link
Member

However, there may be cases where $response['data'] elements are null or invalid, causing array_filter() to throw an error.

If no callback is given to array_filter it will remove all falsy values which includes null, so null is not an invalid element in the given array.

@solracsf solracsf force-pushed the validAppsFromAppStore branch from d4f8e65 to f02902c Compare September 10, 2024 16:43
@solracsf
Copy link
Member Author

Thanks for your inputs, six eyes see better than two 🤣
PR amended.

@provokateurin
Copy link
Member

I wonder if we should have a warning log for the return case to inform the admin that the appstore returned a faulty response? Otherwise it is just silent and they can't know why.

@solracsf
Copy link
Member Author

Agree.

Maybe something like:

$this->logger->warning('Response from appstore is invalid, apps could not be retrieved. Try again later.', ['app' => 'appstoreFetcher']);

This should inform admin that response was not valid, but also this could be temporary and it can be retried (in case appstore is down for maintenance, overloaded...).

@provokateurin
Copy link
Member

Sounds good, but I would either omit the app parameter or set it to core.

@solracsf
Copy link
Member Author

All the other warnings have it, that's why I've included it. 🤔

@provokateurin
Copy link
Member

Ah ok, then it's good. Can you add it to the PR?

Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>

fix: lint

chore: remove space

Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>

fix: check if response array is null

Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>

chore: Add log
@solracsf solracsf force-pushed the validAppsFromAppStore branch from f02902c to 8585b05 Compare September 11, 2024 09:13
@solracsf
Copy link
Member Author

Done ✅

@solracsf solracsf requested review from susnux September 11, 2024 09:15
@provokateurin
Copy link
Member

/backport to stable30

@provokateurin
Copy link
Member

/backport to stable29

@provokateurin
Copy link
Member

/backport to stable28

@solracsf solracsf enabled auto-merge September 11, 2024 09:55
Co-authored-by: Côme Chilliet <91878298+come-nc@users.noreply.github.com>
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
@solracsf solracsf requested a review from come-nc September 13, 2024 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Upgrade from 29.0.4 to 29.0.6 fails with TypeError in AppFetcher.php
3 participants