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

[stable28] fix(dav): Improve handling and logging of bulk upload failures #42087

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion apps/dav/lib/BulkUpload/BulkUploadPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
return true;
}

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

while (!$multiPartParser->isAtLastBoundary()) {
Expand Down
13 changes: 11 additions & 2 deletions apps/dav/lib/BulkUpload/MultipartRequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCA\DAV\BulkUpload;

use OCP\AppFramework\Http;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Exception\LengthRequired;
Expand All @@ -42,7 +43,10 @@ class MultipartRequestParser {
/**
* @throws BadRequest
*/
public function __construct(RequestInterface $request) {
public function __construct(
RequestInterface $request,
protected LoggerInterface $logger,
) {
$stream = $request->getBody();
$contentType = $request->getHeader('Content-Type');

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

// Remove potential quotes around boundary value.
if (substr($boundaryValue, 0, 1) == '"' && substr($boundaryValue, -1) == '"') {
if (substr($boundaryValue, 0, 1) === '"' && substr($boundaryValue, -1) === '"') {
$boundaryValue = substr($boundaryValue, 1, -1);
}

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

if (!str_contains($line, ':')) {
$this->logger->error('Header missing ":" on bulk request: ' . json_encode($line));
throw new Exception('An error occurred while reading headers of a part', Http::STATUS_BAD_REQUEST);
}

try {
[$key, $value] = explode(':', $line, 2);
$headers[strtolower(trim($key))] = trim($value);
Expand Down
12 changes: 10 additions & 2 deletions apps/dav/tests/unit/Files/MultipartRequestParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@
namespace OCA\DAV\Tests\unit\DAV;

use \OCA\DAV\BulkUpload\MultipartRequestParser;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class MultipartRequestParserTest extends TestCase {

protected LoggerInterface $logger;

protected function setUp(): void {
$this->logger = $this->createMock(LoggerInterface::class);
}

private function getValidBodyObject() {
return [
[
Expand Down Expand Up @@ -73,7 +81,7 @@ private function getMultipartParser(array $parts, array $headers = [], string $b
->method('getBody')
->willReturn($stream);

return new MultipartRequestParser($request);
return new MultipartRequestParser($request, $this->logger);
}


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

$this->expectExceptionMessage('Body should be of type resource');
new MultipartRequestParser($request);
new MultipartRequestParser($request, $this->logger);
}

/**
Expand Down
Loading