⚠️ This issue respects the following points: ⚠️
Bug description
Bug description
occ files:scan <user> (and, more generally, any code path that walks a user's mounted
external/federated storages, e.g. background jobs) crashes with an unhandled fatal PHP
error when it encounters a federated share whose source share has been deleted on the
remote instance. Instead of skipping that single dead share, the crash aborts the entire
scan for that user — even if only one out of hundreds of shares is affected.
Steps to reproduce
- On instance A, user
alice shares a folder with user bob on instance B via
federated sharing (share_type 6, or 9 for a group share). bob accepts it.
- On instance A, the share is removed (e.g.
alice unshares it, or her account is
deleted) — the corresponding row in oc_share on instance A is gone, but the
matching row in oc_share_external on instance B is not cleaned up (this can happen
for several reasons and is arguably a separate issue, but is easy to reproduce by
just deleting the oc_share row directly for testing).
- On instance B, run
occ files:scan bob (or occ files:scan bob -v).
Expected behavior
The scan should skip the dead external share (optionally logging a warning) and
continue scanning the rest of the user's files, finishing with the dead share simply
absent or marked unavailable.
Actual behavior
The scan aborts entirely with an unhandled fatal error. No further files are scanned
for that user in that run.
Root cause
In apps/files_sharing/lib/External/Manager.php, the Manager::$user property is
populated from $userSession->getUser() in the constructor. Under a CLI invocation
(occ), there is no active user session, so $user stays null.
When checkStorageAvailability() determines that a federated share is unreachable
(because the remote share no longer exists) and calls removeShare(), that method
eventually calls stripPath(), which dereferences $this->user->getUID() — on a
null object. This throws an unhandled Error, which is not caught anywhere in the
call chain, so it propagates all the way up and aborts the whole occ files:scan
process for that user instead of just failing to remove the one dead share.
Suggested fix
A few options, roughly in order of how surgical they are:
- In
Manager::stripPath() (or wherever $this->user->getUID() is called), guard
against $this->user being null and fall back to resolving the UID some other way
(e.g. from the share row itself, since the owning user is already known in that
context) instead of dereferencing a null object.
- Wrap the
removeShare() call inside checkStorageAvailability() in a try/catch so a
failure to remove one dead share logs an error and moves on, rather than bubbling up
and aborting the whole scan.
- More generally: CLI commands that walk external storages should ensure
IUserSession::setUser() is set to the scanned user before touching code paths that
assume an active session, so Manager::$user is never null in this context.
Happy to test a patch against our instances if one is proposed — we hit this
repeatedly across a small Nextcloud cluster (multiple nodes federating shares with
each other) and currently work around it by pre-emptively finding and removing dead
oc_share_external rows via direct DB queries before running occ files:scan.
Environment
- Nextcloud version: 33.0.4.1
- PHP version: 8.3.14
- Database: MySQL 8.0.29-21,
- Installation method: manual
- Affected apps:
files_sharing (federated / external shares)
Steps to reproduce
- On instance A, user
alice shares a folder with user bob on instance B via
federated sharing (share_type 6, or 9 for a group share). bob accepts it.
- On instance A, the share is removed (e.g.
alice unshares it, or her account is
deleted) — the corresponding row in oc_share on instance A is gone, but the
matching row in oc_share_external on instance B is not cleaned up (this can happen
for several reasons and is arguably a separate issue, but is easy to reproduce by
just deleting the oc_share row directly for testing).
- On instance B, run
occ files:scan bob (or occ files:scan bob -v).
Expected behavior
The scan should skip the dead external share (optionally logging a warning) and
continue scanning the rest of the user's files, finishing with the dead share simply
absent or marked unavailable.
Nextcloud Server version
33
Operating system
Debian/Ubuntu
PHP engine version
PHP 8.3
Web server
Nginx
Database engine version
MySQL
Is this bug present after an update or on a fresh install?
None
Are you using the Nextcloud Server Encryption module?
None
What user-backends are you using?
Configuration report
List of activated Apps
Nextcloud Signing status
Nextcloud Logs
Additional info
No response
Bug description
Bug description
occ files:scan <user>(and, more generally, any code path that walks a user's mountedexternal/federated storages, e.g. background jobs) crashes with an unhandled fatal PHP
error when it encounters a federated share whose source share has been deleted on the
remote instance. Instead of skipping that single dead share, the crash aborts the entire
scan for that user — even if only one out of hundreds of shares is affected.
Steps to reproduce
aliceshares a folder with userbobon instance B viafederated sharing (
share_type6, or 9 for a group share).bobaccepts it.aliceunshares it, or her account isdeleted) — the corresponding row in
oc_shareon instance A is gone, but thematching row in
oc_share_externalon instance B is not cleaned up (this can happenfor several reasons and is arguably a separate issue, but is easy to reproduce by
just deleting the
oc_sharerow directly for testing).occ files:scan bob(orocc files:scan bob -v).Expected behavior
The scan should skip the dead external share (optionally logging a warning) and
continue scanning the rest of the user's files, finishing with the dead share simply
absent or marked unavailable.
Actual behavior
The scan aborts entirely with an unhandled fatal error. No further files are scanned
for that user in that run.
Root cause
In
apps/files_sharing/lib/External/Manager.php, theManager::$userproperty ispopulated from
$userSession->getUser()in the constructor. Under a CLI invocation(
occ), there is no active user session, so$userstaysnull.When
checkStorageAvailability()determines that a federated share is unreachable(because the remote share no longer exists) and calls
removeShare(), that methodeventually calls
stripPath(), which dereferences$this->user->getUID()— on anullobject. This throws an unhandledError, which is not caught anywhere in thecall chain, so it propagates all the way up and aborts the whole
occ files:scanprocess for that user instead of just failing to remove the one dead share.
Suggested fix
A few options, roughly in order of how surgical they are:
Manager::stripPath()(or wherever$this->user->getUID()is called), guardagainst
$this->userbeingnulland fall back to resolving the UID some other way(e.g. from the share row itself, since the owning user is already known in that
context) instead of dereferencing a null object.
removeShare()call insidecheckStorageAvailability()in a try/catch so afailure to remove one dead share logs an error and moves on, rather than bubbling up
and aborting the whole scan.
IUserSession::setUser()is set to the scanned user before touching code paths thatassume an active session, so
Manager::$useris never null in this context.Happy to test a patch against our instances if one is proposed — we hit this
repeatedly across a small Nextcloud cluster (multiple nodes federating shares with
each other) and currently work around it by pre-emptively finding and removing dead
oc_share_externalrows via direct DB queries before runningocc files:scan.Environment
files_sharing(federated / external shares)Steps to reproduce
aliceshares a folder with userbobon instance B viafederated sharing (
share_type6, or 9 for a group share).bobaccepts it.aliceunshares it, or her account isdeleted) — the corresponding row in
oc_shareon instance A is gone, but thematching row in
oc_share_externalon instance B is not cleaned up (this can happenfor several reasons and is arguably a separate issue, but is easy to reproduce by
just deleting the
oc_sharerow directly for testing).occ files:scan bob(orocc files:scan bob -v).Expected behavior
The scan should skip the dead external share (optionally logging a warning) and
continue scanning the rest of the user's files, finishing with the dead share simply
absent or marked unavailable.
Nextcloud Server version
33
Operating system
Debian/Ubuntu
PHP engine version
PHP 8.3
Web server
Nginx
Database engine version
MySQL
Is this bug present after an update or on a fresh install?
None
Are you using the Nextcloud Server Encryption module?
None
What user-backends are you using?
Configuration report
List of activated Apps
Nextcloud Signing status
Nextcloud Logs
Additional info
No response