Skip to content
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 UniqueConstraintViolationException while insert into oc_filecache #12411

Merged
merged 1 commit into from
Nov 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

namespace OC\Files\Cache;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Driver\Statement;
use OCP\Files\Cache\ICache;
Expand Down Expand Up @@ -238,6 +239,8 @@ public function put($file, array $data) {
*
* @return int file id
* @throws \RuntimeException
*
* @suppress SqlInjectionChecker
*/
public function insert($file, array $data) {
// normalize file
Expand Down Expand Up @@ -268,20 +271,28 @@ public function insert($file, array $data) {
return trim($item, "`");
}, $queryParts);
$values = array_combine($queryParts, $params);
if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
'storage',
'path_hash',
])
) {
return (int)$this->connection->lastInsertId('*PREFIX*filecache');

try {
$builder = $this->connection->getQueryBuilder();
$builder->insert('filecache');

foreach ($values as $column => $value) {
$builder->setValue($column, $builder->createNamedParameter($value));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also said, that there is an sql injection problem (this came from our plugin to Phan). I double checked and the column as well as the argument are both properly escaped.

}

if ($builder->execute()) {
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
}
} catch(UniqueConstraintViolationException $e) {
// entry exists already
}

// The file was created in the mean time
MorrisJobke marked this conversation as resolved.
Show resolved Hide resolved
if (($id = $this->getId($file)) > -1) {
$this->update($id, $data);
return $id;
} else {
throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.');
throw new \RuntimeException('File entry could not be inserted but could also not be selected with getId() in order to perform an update. Please try again.');
}
}

Expand Down