# GHSA-534c-hcr7-67jg: Kimai has an XXE Leading to Local File Read

> An administrator can upload a malicious XLSX template to trigger XXE, allowing local file read or potential remote code execution.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** An administrator
- **Attackable:** Over the internet
- **Victim action needed:** A user has to click or open something
- **Fix available:** Yes
- **Published:** 17 Sep 2024

## What is GHSA-534c-hcr7-67jg?

GHSA-534c-hcr7-67jg is a vulnerability in kimai/kimai, published on 17 Sep 2024. An attacker needs administrator access to exploit it, and only if a user opens a file or follows a link.

### Official description

### Summary
Kimai uses [PHPSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) for importing and exporting invoices. Recently, a [CVE](https://github.com/PHPOffice/PhpSpreadsheet/security/advisories/GHSA-ghg6-32f9-2jp7) was identified in PHPSpreadsheet, which could lead to an XXE vulnerability.

### Details

Exploitation requires an Administrator account, allowing the upload of an `XLSX` template containing the payload. The vulnerability is triggered by the following code snippet:

```php
// https://github.com/kimai/kimai/blob/b1903ba18359be16dd32ea9c40377c486498f082/src/Invoice/Renderer/AbstractSpreadsheetRenderer.php#L41
public function render(InvoiceDocument $document, InvoiceModel $model): Response
{
    $spreadsheet = IOFactory::load($document->getFilename());
    $worksheet = $spreadsheet->getActiveSheet();
    $entries = $model->getCalculator()->getEntries();
    $sheetReplacer = $model->toArray();
    $invoiceItemCount = \count($entries);
    if ($invoiceItemCount > 1) {
        $this->addTemplateRows($worksheet, $invoiceItemCount);
    }
}
```

The `IOFactory::load` function utilizes `simplexml_load_string`, which has previously been demonstrated to be vulnerable to XXE attacks.

While this is not directly an XXE in Kimai, it does however impact the latest stable version.

 
### PoC

By uploading a malicious `XLSX` template, the payload will be triggered every time an invoice is generated.

```xml
<?xml version="1.0" encoding='UTF-7' standalone="yes"?>
+ADw-!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "php://filter/......." > %xxe;]>.....
```

For a better a visibility, I will upload both a:
- Malicious template sample for testing 
- An exported invoice, showing the contents of target file during the export. 

### Impact
Local File Read / RCE in edge cases where `phar://` can be utilized with [gadget chains](https://github.com/ambionics/phpggc) . 

[export.xlsx](https://github.com/user-attachments/files/16803913/export.xlsx)
[sample_template.xlsx](https://github.com/user-attachments/files/16803916/sample_template.xlsx)

## Which versions are affected by GHSA-534c-hcr7-67jg?

2 open source packages ship an affected version, including kimai/kimai.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `kimai/kimai` | Packagist | All versions | 2.21.0 |
| `kimai/kimai` | Packagist | All versions | 2.21.0 |

## How do I fix GHSA-534c-hcr7-67jg?

Upgrade kimai/kimai to 2.21.0 or later.

## Is GHSA-534c-hcr7-67jg being exploited?

There is no known exploitation of GHSA-534c-hcr7-67jg in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-534c-hcr7-67jg and fix it if it is affected.

What it is: GHSA-534c-hcr7-67jg is a vulnerability in kimai/kimai, published on 17 Sep 2024. An attacker needs administrator access to exploit it, and only if a user opens a file or follows a link.
Exploitation: There is no known exploitation of GHSA-534c-hcr7-67jg in the wild and no public exploit code.

Affected packages and fixed versions:
- kimai/kimai (Packagist): affected all versions; fixed in 2.21.0
- kimai/kimai (Packagist): affected all versions; fixed in 2.21.0

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-534c-hcr7-67jg/ (Markdown: https://insights.sbomify.com/GHSA-534c-hcr7-67jg/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-534c-hcr7-67jg

---
From sbomify Insights: https://insights.sbomify.com/GHSA-534c-hcr7-67jg/
Summaries are generated and may contain mistakes; the official description above is the record.
