GitHub Advisoryadvisory · Packagist

GHSA-w9xv-qf98-ccq4 PhpSpreadsheet allows absolute path traversal and Server-Side Request Forgery in HTML writer when embedding images is enabled

MEDIUM Published 2024-10-07 Modified 2025-03-06 Official page ↗
Aliases
CVE-2024-45291
Ecosystem
Packagist
Packages
phpoffice/phpspreadsheet, phpoffice/phpexcel
Severity
Moderate
CVSS vector
CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:C/C:H/I:N/A:N
Weaknesses
CWE-22, CWE-36, CWE-918
Reviewed
2024-10-07

Affected packages

• Packagist phpoffice/phpspreadsheet: >= 2.2.0, < 2.3.0 → fixed in 2.3.0 • Packagist phpoffice/phpspreadsheet: < 1.29.2 → fixed in 1.29.2 • Packagist phpoffice/phpspreadsheet: >= 2.0.0, < 2.1.1 → fixed in 2.1.1 • Packagist phpoffice/phpexcel: <= 1.8.2 (no fix yet)

Description

Summary It's possible for an attacker to construct an XLSX file that links images from arbitrary paths. When embedding images has been enabled in HTML writer with `$writer->setEmbedImages(true);` those files will be included in the output as `data:` URLs, regardless of the file's type. Also URLs can be used for embedding, resulting in a Server-Side Request Forgery vulnerability. Details XLSX files allow embedding or linking media. When In `xl/drawings/drawing1.xml` an attacker can do e.g.: ```xml ``` And then, in `xl/drawings/_rels/drawing1.xml.rels` they can set the path to anything, such as: ```xml ``` or ```xml ``` When the HTML writer is outputting the image, it does not check the path in any way. Also the `getimagesize()` call does not mitigate this, because when `getimagesize()` returns false, an empty mime type is used. ```php if ($this->embedImages || str_starts_with($imageData, 'zip://')) { $picture = @file_get_contents($filename); if ($picture !== false) { $imageDetails = getimagesize($filename) ?: ['mime' => '']; // base64 encode the binary data $base64 = base64_encode($picture); $imageData = 'data:' . $imageDetails['mime'] . ';base64,' . $base64; } } $html .= 'getOffsetX() . 'px; top: ' . $drawing->getOffsetY() . 'px; width: ' . $drawing->getWidth() . 'px; height: ' . $drawing->getHeight() . 'px;" src="' . $imageData . '" alt="' . $filedesc . '" />'; ``` PoC ```php load(__DIR__ . '/book.xlsx'); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet); $writer->setEmbedImages(true); $output = $writer->generateHTMLAll(); // The below is just for demo purposes $pattern = '/data:;base64,(?[^"]+)/i'; preg_match_all($pattern, $output, $matches); print("* /etc/passwd content: *\n"); print(base64_decode($matches['data'][0])); print("* HTTP response content: *\n"); print(base64_decode($matches['data'][1])); ``` Add this file in the same directory: book.xlsx Run with: `php index.php` Impact When embedding images has been enabled, an attacker can read arbitrary files on the server and perform arbitrary HTTP GET requests, potentially e.g. revealing secrets. Note that any PHP protocol wrappers can be used, meaning that if for example the `expect://` wrapper is enabled, also remote code execution is possible.

Related

Alias · CVE (1)

  • CVE-2024-45291 PHPSpreadsheet is a pure PHP library for reading and writing spreadsheet files. vulnerability

Weakness · CWE (3)

  • CWE-22 Improper Limitation of a Pathname to a Restricted Directory weakness
  • CWE-36 Absolute Path Traversal weakness
  • CWE-918 Server-Side Request Forgery weakness

References