Skip to content

Commit

Permalink
Merge pull request #47698 from nextcloud/backport/47627/stable27
Browse files Browse the repository at this point in the history
[stable27] [LinkReferenceProvider] Better size check
  • Loading branch information
AndyScherzinger authored Sep 4, 2024
2 parents 479a0a9 + e00aef6 commit 97135ee
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/private/Collaboration/Reference/LinkReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function fetchReference(Reference $reference): void {
}
$linkContentLength = $headResponse->getHeader('Content-Length');
if (is_numeric($linkContentLength) && (int) $linkContentLength > 5 * 1024 * 1024) {
$this->logger->debug('Skip resolving links pointing to content length > 5 MB');
$this->logger->debug('[Head] Skip resolving links pointing to content length > 5 MiB');
return;
}
$linkContentType = $headResponse->getHeader('Content-Type');
Expand All @@ -126,18 +126,28 @@ private function fetchReference(Reference $reference): void {
return;
}
try {
$response = $client->get($reference->getId(), [ 'timeout' => 10 ]);
$response = $client->get($reference->getId(), [ 'timeout' => 10, 'stream' => true ]);
} catch (\Exception $e) {
$this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]);
return;
}

$responseBody = (string)$response->getBody();
$body = $response->getBody();
if (is_resource($body)) {
$responseContent = fread($body, 5 * 1024 * 1024);
if (!feof($body)) {
$this->logger->debug('[Get] Skip resolving links pointing to content length > 5 MiB');
return;
}
} else {
$this->logger->error('[Get] Impossible to check content length');
return;
}

// OpenGraph handling
$consumer = new Consumer();
$consumer->useFallbackMode = true;
$object = $consumer->loadHtml($responseBody);
$object = $consumer->loadHtml($responseContent);

$reference->setUrl($reference->getId());

Expand Down

0 comments on commit 97135ee

Please sign in to comment.