Skip to content

Commit 3c9abb0

Browse files
authored
Merge pull request #47777 from nextcloud/backport/47769/stable29
[stable29] fix: Use sha256 to hash arguments of background jobs
2 parents 3bc5ea4 + 0b6ec1b commit 3c9abb0

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OC\Core\Migrations;
11+
12+
use Closure;
13+
use OCP\DB\ISchemaWrapper;
14+
use OCP\DB\QueryBuilder\IQueryBuilder;
15+
use OCP\IDBConnection;
16+
use OCP\Migration\IOutput;
17+
use OCP\Migration\SimpleMigrationStep;
18+
19+
/**
20+
* Migrate the argument_hash column of oc_jobs to use sha256 instead of md5.
21+
*/
22+
class Version28000Date20240828142927 extends SimpleMigrationStep {
23+
public function __construct(
24+
protected IDBConnection $connection,
25+
) {
26+
}
27+
28+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
29+
/** @var ISchemaWrapper $schema */
30+
$schema = $schemaClosure();
31+
32+
// Increase the column size from 32 to 64
33+
$table = $schema->getTable('jobs');
34+
$table->modifyColumn('argument_hash', [
35+
'notnull' => false,
36+
'length' => 64,
37+
]);
38+
39+
return $schema;
40+
}
41+
42+
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
43+
$chunkSize = 1000;
44+
$offset = 0;
45+
$nullHash = hash('sha256', 'null');
46+
47+
$selectQuery = $this->connection->getQueryBuilder()
48+
->select('*')
49+
->from('jobs')
50+
->setMaxResults($chunkSize);
51+
52+
$insertQuery = $this->connection->getQueryBuilder();
53+
$insertQuery->update('jobs')
54+
->set('argument_hash', $insertQuery->createParameter('argument_hash'))
55+
->where($insertQuery->expr()->eq('id', $insertQuery->createParameter('id')));
56+
57+
do {
58+
$result = $selectQuery
59+
->setFirstResult($offset)
60+
->executeQuery();
61+
62+
$jobs = $result->fetchAll();
63+
$count = count($jobs);
64+
65+
foreach ($jobs as $jobRow) {
66+
if ($jobRow['argument'] === 'null') {
67+
$hash = $nullHash;
68+
} else {
69+
$hash = hash('sha256', $jobRow['argument']);
70+
}
71+
$insertQuery->setParameter('id', (string)$jobRow['id'], IQueryBuilder::PARAM_INT);
72+
$insertQuery->setParameter('argument_hash', $hash);
73+
$insertQuery->executeStatement();
74+
}
75+
76+
$offset += $chunkSize;
77+
} while ($count === $chunkSize);
78+
}
79+
}

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,7 @@
12741274
'OC\\Core\\Migrations\\Version28000Date20231004103301' => $baseDir . '/core/Migrations/Version28000Date20231004103301.php',
12751275
'OC\\Core\\Migrations\\Version28000Date20231103104802' => $baseDir . '/core/Migrations/Version28000Date20231103104802.php',
12761276
'OC\\Core\\Migrations\\Version28000Date20231126110901' => $baseDir . '/core/Migrations/Version28000Date20231126110901.php',
1277+
'OC\\Core\\Migrations\\Version28000Date20240828142927' => $baseDir . '/core/Migrations/Version28000Date20240828142927.php',
12771278
'OC\\Core\\Migrations\\Version29000Date20231126110901' => $baseDir . '/core/Migrations/Version29000Date20231126110901.php',
12781279
'OC\\Core\\Migrations\\Version29000Date20231213104850' => $baseDir . '/core/Migrations/Version29000Date20231213104850.php',
12791280
'OC\\Core\\Migrations\\Version29000Date20240124132201' => $baseDir . '/core/Migrations/Version29000Date20240124132201.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
13071307
'OC\\Core\\Migrations\\Version28000Date20231004103301' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20231004103301.php',
13081308
'OC\\Core\\Migrations\\Version28000Date20231103104802' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20231103104802.php',
13091309
'OC\\Core\\Migrations\\Version28000Date20231126110901' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20231126110901.php',
1310+
'OC\\Core\\Migrations\\Version28000Date20240828142927' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20240828142927.php',
13101311
'OC\\Core\\Migrations\\Version29000Date20231126110901' => __DIR__ . '/../../..' . '/core/Migrations/Version29000Date20231126110901.php',
13111312
'OC\\Core\\Migrations\\Version29000Date20231213104850' => __DIR__ . '/../../..' . '/core/Migrations/Version29000Date20231213104850.php',
13121313
'OC\\Core\\Migrations\\Version29000Date20240124132201' => __DIR__ . '/../../..' . '/core/Migrations/Version29000Date20240124132201.php',

lib/private/BackgroundJob/JobList.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
use Psr\Log\LoggerInterface;
4444
use function get_class;
4545
use function json_encode;
46-
use function md5;
4746
use function strlen;
4847

4948
class JobList implements IJobList {
@@ -73,7 +72,7 @@ public function add($job, $argument = null, ?int $firstCheck = null): void {
7372
->values([
7473
'class' => $query->createNamedParameter($class),
7574
'argument' => $query->createNamedParameter($argumentJson),
76-
'argument_hash' => $query->createNamedParameter(md5($argumentJson)),
75+
'argument_hash' => $query->createNamedParameter(hash('sha256', $argumentJson)),
7776
'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
7877
'last_checked' => $query->createNamedParameter($firstCheck, IQueryBuilder::PARAM_INT),
7978
]);
@@ -83,7 +82,7 @@ public function add($job, $argument = null, ?int $firstCheck = null): void {
8382
->set('last_checked', $query->createNamedParameter($firstCheck, IQueryBuilder::PARAM_INT))
8483
->set('last_run', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT))
8584
->where($query->expr()->eq('class', $query->createNamedParameter($class)))
86-
->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(md5($argumentJson))));
85+
->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(hash('sha256', $argumentJson))));
8786
}
8887
$query->executeStatement();
8988
}
@@ -104,7 +103,7 @@ public function remove($job, $argument = null): void {
104103
->where($query->expr()->eq('class', $query->createNamedParameter($class)));
105104
if (!is_null($argument)) {
106105
$argumentJson = json_encode($argument);
107-
$query->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(md5($argumentJson))));
106+
$query->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(hash('sha256', $argumentJson))));
108107
}
109108

110109
// Add galera safe delete chunking if using mysql
@@ -145,7 +144,7 @@ public function has($job, $argument): bool {
145144
$query->select('id')
146145
->from('jobs')
147146
->where($query->expr()->eq('class', $query->createNamedParameter($class)))
148-
->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(md5($argument))))
147+
->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(hash('sha256', $argument))))
149148
->setMaxResults(1);
150149

151150
$result = $query->executeQuery();

0 commit comments

Comments
 (0)