-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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: replace null character when serializing #49528
Merged
SebastianKrupinski
merged 1 commit into
master
from
fix/issue-47879-property-serialization
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
SebastianKrupinski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OC\Repair; | ||
|
||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\IRepairStep; | ||
|
||
class RemoveBrokenProperties implements IRepairStep { | ||
/** | ||
* RemoveBrokenProperties constructor. | ||
* | ||
* @param IDBConnection $db | ||
*/ | ||
public function __construct( | ||
private IDBConnection $db, | ||
) { | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getName() { | ||
return 'Remove broken DAV object properties'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function run(IOutput $output) { | ||
// retrieve all object properties | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->select('id', 'propertyvalue') | ||
->from('properties') | ||
->where($qb->expr()->eq('valuetype', $qb->createNamedParameter('3', IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); | ||
$result = $qb->executeQuery(); | ||
// find broken object properties | ||
$brokenIds = []; | ||
while ($entry = $result->fetch()) { | ||
if (!empty($entry['propertyvalue'])) { | ||
$object = @unserialize(str_replace('\x00', chr(0), $entry['propertyvalue'])); | ||
if ($object === false) { | ||
$brokenIds[] = $entry['id']; | ||
} | ||
} else { | ||
$brokenIds[] = $entry['id']; | ||
} | ||
} | ||
$result->closeCursor(); | ||
// delete broken object properties | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->delete('properties') | ||
->where($qb->expr()->in('id', $qb->createParameter('ids'), IQueryBuilder::PARAM_STR_ARRAY)); | ||
foreach (array_chunk($brokenIds, 1000) as $chunkIds) { | ||
$qb->setParameter('ids', $chunkIds, IQueryBuilder::PARAM_STR_ARRAY); | ||
$qb->executeStatement(); | ||
} | ||
$total = count($brokenIds); | ||
$output->info("$total broken object properties removed"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't this cause problems if the thing we're deserializing contains a backslash?
Imagine a nerd band that names themselves 'The \x00s'. When I make a calender entry '\x00s concert', it will get deserialized into '<NUL>s concert', most likely breaking something else along the way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is a the actual value, the pattern is as unique as possible, also this is only used for very specific types and not for everything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is that it is still possible to retroactively break stuff that used to work. I agree that the string '\x00' is unlikely to appear anywhere, but it's not impossible; so optimally there would be an escape sequence that makes in unambiguous; that would require a database migration, though.
But I agree it's better than the current situation; personally, I'd still prefer something more "unique"; if this is ever used for something that contains a windows path, for example, it might cause weird results (think
C:\files\x0000.txt
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree there is a very tiny chance this might happen, but there is no better solution for this. The is a chance this might happen no matter what we use as an replacement for null.
The properties don't contain file names, also in your example the file name would have to be
C:\files\\x0000.txt
with two slashes. This would then get escaped by serialize and we then replace null characters that serialize produces with\\x00
which is a common representation of null in serializationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, I see. I missed that we're replacing before deserializing. Guess it's not worth worrying about it that much.