Skip to content

Commit e8492bc

Browse files
nickvergessenAndyScherzinger
authored andcommitted
fix(dav): Improve handling and logging of bulk upload failures
Signed-off-by: Joas Schilling <[email protected]>
1 parent d9abfa0 commit e8492bc

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

apps/dav/lib/BulkUpload/BulkUploadPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
6565
return true;
6666
}
6767

68-
$multiPartParser = new MultipartRequestParser($request);
68+
$multiPartParser = new MultipartRequestParser($request, $this->logger);
6969
$writtenFiles = [];
7070

7171
while (!$multiPartParser->isAtLastBoundary()) {

apps/dav/lib/BulkUpload/MultipartRequestParser.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace OCA\DAV\BulkUpload;
2424

2525
use OCP\AppFramework\Http;
26+
use Psr\Log\LoggerInterface;
2627
use Sabre\DAV\Exception;
2728
use Sabre\DAV\Exception\BadRequest;
2829
use Sabre\DAV\Exception\LengthRequired;
@@ -42,7 +43,10 @@ class MultipartRequestParser {
4243
/**
4344
* @throws BadRequest
4445
*/
45-
public function __construct(RequestInterface $request) {
46+
public function __construct(
47+
RequestInterface $request,
48+
protected LoggerInterface $logger,
49+
) {
4650
$stream = $request->getBody();
4751
$contentType = $request->getHeader('Content-Type');
4852

@@ -78,7 +82,7 @@ private function parseBoundaryFromHeaders(string $contentType): string {
7882
$boundaryValue = trim($boundaryValue);
7983

8084
// Remove potential quotes around boundary value.
81-
if (substr($boundaryValue, 0, 1) == '"' && substr($boundaryValue, -1) == '"') {
85+
if (substr($boundaryValue, 0, 1) === '"' && substr($boundaryValue, -1) === '"') {
8286
$boundaryValue = substr($boundaryValue, 1, -1);
8387
}
8488

@@ -179,6 +183,11 @@ private function readPartHeaders(): array {
179183
throw new Exception('An error occurred while reading headers of a part');
180184
}
181185

186+
if (!str_contains($line, ':')) {
187+
$this->logger->error('Header missing ":" on bulk request: ' . json_encode($line));
188+
throw new Exception('An error occurred while reading headers of a part', Http::STATUS_BAD_REQUEST);
189+
}
190+
182191
try {
183192
[$key, $value] = explode(':', $line, 2);
184193
$headers[strtolower(trim($key))] = trim($value);

apps/dav/tests/unit/Files/MultipartRequestParserTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@
2323
namespace OCA\DAV\Tests\unit\DAV;
2424

2525
use \OCA\DAV\BulkUpload\MultipartRequestParser;
26+
use Psr\Log\LoggerInterface;
2627
use Test\TestCase;
2728

2829
class MultipartRequestParserTest extends TestCase {
30+
31+
protected LoggerInterface $logger;
32+
33+
protected function setUp(): void {
34+
$this->logger = $this->createMock(LoggerInterface::class);
35+
}
36+
2937
private function getValidBodyObject() {
3038
return [
3139
[
@@ -73,7 +81,7 @@ private function getMultipartParser(array $parts, array $headers = [], string $b
7381
->method('getBody')
7482
->willReturn($stream);
7583

76-
return new MultipartRequestParser($request);
84+
return new MultipartRequestParser($request, $this->logger);
7785
}
7886

7987

@@ -90,7 +98,7 @@ public function testBodyTypeValidation(): void {
9098
->willReturn($bodyStream);
9199

92100
$this->expectExceptionMessage('Body should be of type resource');
93-
new MultipartRequestParser($request);
101+
new MultipartRequestParser($request, $this->logger);
94102
}
95103

96104
/**

0 commit comments

Comments
 (0)