Skip to content

fix(appstore): Ensure returned apps from AppStore are valid - #47854

Merged
solracsf merged 2 commits into
masterfrom
validAppsFromAppStore
Sep 13, 2024
Merged

fix(appstore): Ensure returned apps from AppStore are valid#47854
solracsf merged 2 commits into
masterfrom
validAppsFromAppStore

Conversation

@solracsf

@solracsfsolracsf commented Sep 9, 2024

Copy link
Copy Markdown
Member

Checklist

@solracsf
solracsfforce-pushed the validAppsFromAppStore branch from b1246a9 to ae17f47CompareSeptember 9, 2024 17:56
@solracsfsolracsf added this to the Nextcloud 31 milestone Sep 9, 2024

@come-nccome-nc left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
MemberAuthor

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

provokateurin commented Sep 10, 2024

Copy link
Copy Markdown
Member

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.

@provokateurinprovokateurin left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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

Copy link
Copy Markdown
MemberAuthor

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

@provokateurin

Copy link
Copy Markdown
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
Copy Markdown
MemberAuthor

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
Copy Markdown
Member

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

@solracsf

Copy link
Copy Markdown
MemberAuthor

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

@provokateurin

Copy link
Copy Markdown
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

Copy link
Copy Markdown
MemberAuthor

Done ✅

@provokateurin

Copy link
Copy Markdown
Member

/backport to stable30

@provokateurin

Copy link
Copy Markdown
Member

/backport to stable29

@provokateurin

Copy link
Copy Markdown
Member

/backport to stable28

Comment threadlib/private/App/AppStore/Fetcher/AppFetcher.php Outdated
Co-authored-by: Côme Chilliet <91878298+come-nc@users.noreply.github.com>
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

@solracsf@provokateurin@come-nc