Skip to content

Commit a3fcd57

Browse files
Merge pull request #52302 from nextcloud/backport/51491/stable31
2 parents 9bc3368 + d3fe082 commit a3fcd57

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

apps/dav/lib/Connector/Sabre/File.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ public function put($data) {
127127
$view = Filesystem::getView();
128128

129129
if ($needsPartFile) {
130+
$transferId = \rand();
130131
// mark file as partial while uploading (ignored by the scanner)
131-
$partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part';
132+
$partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . $transferId . '.part';
132133

133134
if (!$view->isCreatable($partFilePath) && $view->isUpdatable($this->path)) {
134135
$needsPartFile = false;
@@ -230,42 +231,36 @@ public function put($data) {
230231
fclose($target);
231232
}
232233

233-
if ($result === false) {
234-
$expected = -1;
235-
$lengthHeader = $this->request->getHeader('content-length');
236-
if ($lengthHeader) {
237-
$expected = (int)$lengthHeader;
238-
}
239-
if ($expected !== 0) {
240-
throw new Exception(
241-
$this->l10n->t(
242-
'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)',
243-
[
244-
$this->l10n->n('%n byte', '%n bytes', $count),
245-
$this->l10n->n('%n byte', '%n bytes', $expected),
246-
],
247-
)
248-
);
249-
}
234+
$lengthHeader = $this->request->getHeader('content-length');
235+
$expected = $lengthHeader !== '' ? (int)$lengthHeader : -1;
236+
if ($result === false && $expected >= 0) {
237+
throw new Exception(
238+
$this->l10n->t(
239+
'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)',
240+
[
241+
$this->l10n->n('%n byte', '%n bytes', $count),
242+
$this->l10n->n('%n byte', '%n bytes', $expected),
243+
],
244+
)
245+
);
250246
}
251247

252248
// if content length is sent by client:
253249
// double check if the file was fully received
254250
// compare expected and actual size
255-
$lengthHeader = $this->request->getHeader('content-length');
256-
if ($lengthHeader && $this->request->getMethod() === 'PUT') {
257-
$expected = (int)$lengthHeader;
258-
if ($count !== $expected) {
259-
throw new BadRequest(
260-
$this->l10n->t(
261-
'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.',
262-
[
263-
$this->l10n->n('%n byte', '%n bytes', $expected),
264-
$this->l10n->n('%n byte', '%n bytes', $count),
265-
],
266-
)
267-
);
268-
}
251+
if ($expected >= 0
252+
&& $expected !== $count
253+
&& $this->request->getMethod() === 'PUT'
254+
) {
255+
throw new BadRequest(
256+
$this->l10n->t(
257+
'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.',
258+
[
259+
$this->l10n->n('%n byte', '%n bytes', $expected),
260+
$this->l10n->n('%n byte', '%n bytes', $count),
261+
],
262+
)
263+
);
269264
}
270265
} catch (\Exception $e) {
271266
if ($e instanceof LockedException) {
@@ -381,9 +376,14 @@ public function put($data) {
381376
private function getPartFileBasePath($path) {
382377
$partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true);
383378
if ($partFileInStorage) {
384-
return $path;
379+
$filename = basename($path);
380+
// hash does not need to be secure but fast and semi unique
381+
$hashedFilename = hash('xxh128', $filename);
382+
return substr($path, 0, strlen($path) - strlen($filename)) . $hashedFilename;
385383
} else {
386-
return md5($path); // will place it in the root of the view with a unique name
384+
// will place the .part file in the users root directory
385+
// therefor we need to make the name (semi) unique - hash does not need to be secure but fast.
386+
return hash('xxh128', $path);
387387
}
388388
}
389389

build/integration/dav_features/dav-v2.feature

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ Feature: dav-v2
108108
When User "user0" uploads file "data/textfile.txt" to "/testquota/asdf.txt"
109109
Then the HTTP status code should be "201"
110110

111+
Scenario: Uploading a file with very long filename
112+
Given using new dav path
113+
And As an "admin"
114+
And user "user0" exists
115+
And user "user0" has a quota of "10 MB"
116+
And As an "user0"
117+
When User "user0" uploads file "data/textfile.txt" to "/long-filename-with-250-characters-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.txt"
118+
Then the HTTP status code should be "201"
119+
120+
Scenario: Uploading a file with a too long filename
121+
Given using new dav path
122+
And As an "admin"
123+
And user "user0" exists
124+
And user "user0" has a quota of "10 MB"
125+
And As an "user0"
126+
When User "user0" uploads file "data/textfile.txt" to "/long-filename-with-251-characters-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.txt"
127+
Then the HTTP status code should be "400"
128+
111129
Scenario: Create a search query on image
112130
Given using new dav path
113131
And As an "admin"
@@ -132,3 +150,14 @@ Feature: dav-v2
132150
Then Favorite search should work
133151
And the single response should contain a property "{http://owncloud.org/ns}favorite" with value "1"
134152

153+
Scenario: Create a search query on favorite
154+
Given using new dav path
155+
And As an "admin"
156+
And user "user0" exists
157+
And As an "user0"
158+
When User "user0" uploads file "data/green-square-256.png" to "/fav_image.png"
159+
Then Favorite search should work
160+
And the response should be empty
161+
When user "user0" favorites element "/fav_image.png"
162+
Then Favorite search should work
163+
And the single response should contain a property "{http://owncloud.org/ns}favorite" with value "1"

0 commit comments

Comments
 (0)