Skip to content

Commit 332bb16

Browse files
enriquepablomickenordin
authored andcommitted
fix: backwards compatibility for shares for instances before upgrading
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
1 parent 1b8505a commit 332bb16

1 file changed

Lines changed: 19 additions & 39 deletions

File tree

apps/federatedfilesharing/lib/Migration/Version1012Date20260306120000.php

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use OCP\IUserManager;
2020
use OCP\Migration\IOutput;
2121
use OCP\Migration\SimpleMigrationStep;
22-
use OCP\Security\ISecureRandom;
2322
use OCP\Server;
2423
use OCP\Share\IShare;
2524

@@ -28,9 +27,10 @@
2827
* as permanent tokens, which is required for the OCM token exchange flow.
2928
*
3029
* Shares created before this fork used TokenHandler (15-char tokens) and never
31-
* registered in oc_authtoken. Those tokens are replaced with new 32-char tokens.
32-
* Note: the remote's copy of a replaced token becomes stale; affected shares will
33-
* need to be re-created.
30+
* registered in oc_authtoken. Those legacy short tokens are left untouched so
31+
* that the receiving instance can continue to authenticate via Basic auth with
32+
* the original token. They will never participate in the token exchange flow,
33+
* but they will keep working until the share is re-created with a new token.
3434
*
3535
* Shares created by this fork (32-char tokens) that are somehow missing from
3636
* oc_authtoken are silently repaired.
@@ -43,7 +43,6 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
4343
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
4444
$db = Server::get(IDBConnection::class);
4545
$tokenProvider = Server::get(PublicKeyTokenProvider::class);
46-
$random = Server::get(ISecureRandom::class);
4746
$userManager = Server::get(IUserManager::class);
4847

4948
$qb = $db->getQueryBuilder();
@@ -58,7 +57,6 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array
5857
))
5958
->executeQuery();
6059

61-
$replaced = 0;
6260
$registered = 0;
6361
$skipped = 0;
6462

@@ -68,30 +66,21 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array
6866
$uid = (string)$row['uid_initiator'];
6967

7068
if (strlen($token) < PublicKeyTokenProvider::TOKEN_MIN_LENGTH) {
71-
// Old short token from TokenHandler — cannot register in oc_authtoken.
72-
// Generate a new 32-char token and update oc_share.
73-
$newToken = $random->generate(
74-
32,
75-
ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS
76-
);
77-
78-
$updateQb = $db->getQueryBuilder();
79-
$updateQb->update('share')
80-
->set('token', $updateQb->createNamedParameter($newToken))
81-
->where($updateQb->expr()->eq('id', $updateQb->createNamedParameter($shareId, IQueryBuilder::PARAM_INT)));
82-
$updateQb->executeStatement();
69+
// Old short token from TokenHandler — leave it as-is.
70+
// Replacing it would invalidate the token stored on the receiving instance,
71+
// breaking Basic-auth access to those shares. These shares keep working via
72+
// Basic auth and are simply not eligible for the OCM token exchange flow.
73+
$skipped++;
74+
continue;
75+
}
8376

84-
$token = $newToken;
85-
$replaced++;
86-
} else {
87-
// Long token — check if it's already in oc_authtoken.
88-
try {
89-
$tokenProvider->getToken($token);
90-
$skipped++;
91-
continue;
92-
} catch (InvalidTokenException) {
93-
// Not registered yet — fall through to create it.
94-
}
77+
// Long token — check if it's already in oc_authtoken.
78+
try {
79+
$tokenProvider->getToken($token);
80+
$skipped++;
81+
continue;
82+
} catch (InvalidTokenException) {
83+
// Not registered yet — fall through to create it.
9584
}
9685

9786
$user = $userManager->get($uid);
@@ -120,18 +109,9 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array
120109
$result->closeCursor();
121110

122111
$output->info(sprintf(
123-
'Federated share token migration: %d replaced (short tokens), %d registered, %d already up-to-date.',
124-
$replaced,
112+
'Federated share token migration: %d registered, %d skipped (already up-to-date or legacy short token).',
125113
$registered,
126114
$skipped
127115
));
128-
129-
if ($replaced > 0) {
130-
$output->warning(sprintf(
131-
'%d federated share(s) had their token replaced. The remote side\'s copy of the '
132-
. 'old token is now stale — those shares will need to be re-created.',
133-
$replaced
134-
));
135-
}
136116
}
137117
}

0 commit comments

Comments
 (0)