# CVE-2023-35169: php-imap vulnerable to RCE through a directory traversal vulnerability

> Unsanitized attachment filenames in php-imap allow attackers to achieve remote code execution via directory traversal when saving emails.

- **Severity:** Critical 9.1 (`CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:C/C:H/I:H/A:H`)
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 23 Jun 2023
- **Updated:** 27 Nov 2024
- **Also known as:** GHSA-47p7-xfcc-4pv9, UBUNTU-CVE-2023-35169

## What is CVE-2023-35169?

CVE-2023-35169 is a critical path traversal vulnerability in Webklex php-imap, published on 23 Jun 2023. An attacker can exploit it over the internet without logging in, with no action from the victim.

### Official description

PHP-IMAP is a wrapper for common IMAP communication without the need to have the php-imap module installed / enabled. Prior to version 5.3.0, an unsanitized attachment filename allows any unauthenticated user to leverage a directory traversal vulnerability, which results in a remote code execution vulnerability. Every application that stores attachments with `Attachment::save()` without providing a `$filename` or passing unsanitized user input is affected by this attack.

An attacker can send an email with a malicious attachment to the inbox, which gets crawled with `webklex/php-imap` or `webklex/laravel-imap`. Prerequisite for the vulnerability is that the script stores the attachments without providing a `$filename`, or providing an unsanitized `$filename`, in `src/Attachment::save(string $path, string $filename = null)`. In this case, where no `$filename` gets passed into the `Attachment::save()` method, the package would use a series of unsanitized and insecure input values from the mail as fallback. Even if a developer passes a `$filename` into the `Attachment::save()` method, e.g. by passing the name or filename of the mail attachment itself (from email headers), the input values never get sanitized by the package. There is also no restriction about the file extension (e.g. ".php") or the contents of a file. This allows an attacker to upload malicious code of any type and content at any location where the underlying user has write permissions. The attacker can also overwrite existing files and inject malicious code into files that, e.g. get executed by the system via cron or requests.

Version 5.3.0 contains a patch for this issue.

Weakness: CWE-22 (Improper Limitation of a Pathname to a Restricted Directory)

## Which versions are affected by CVE-2023-35169?

4 open source packages ship an affected version, including webklex/php-imap and webklex/laravel-imap. It affects 1 product.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `webklex/php-imap` | Packagist | All versions | 5.3.0 |
| `webklex/php-imap` | Packagist | All versions | 5.3.0 |
| `webklex/laravel-imap` | Packagist | All versions | 5.3.0 |
| `webklex/laravel-imap` | Packagist | All versions | 5.3.0 |

Products: Webklex Php-Imap.

## How do I fix CVE-2023-35169?

Upgrade webklex/php-imap to 5.3.0 or later.

## Is CVE-2023-35169 being exploited?

It is not confirmed as exploited in the wild, but public exploit code for CVE-2023-35169 exists.

## Fix prompt for AI coding agents

```text
Check this codebase for CVE-2023-35169 and fix it if it is affected.

What it is: CVE-2023-35169 is a critical path traversal vulnerability in Webklex php-imap, published on 23 Jun 2023. An attacker can exploit it over the internet without logging in, with no action from the victim.
Exploitation: It is not confirmed as exploited in the wild, but public exploit code for CVE-2023-35169 exists.

Affected packages and fixed versions:
- webklex/php-imap (Packagist): affected all versions; fixed in 5.3.0
- webklex/php-imap (Packagist): affected all versions; fixed in 5.3.0
- webklex/laravel-imap (Packagist): affected all versions; fixed in 5.3.0
- webklex/laravel-imap (Packagist): affected all versions; fixed in 5.3.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/CVE-2023-35169/ (Markdown: https://insights.sbomify.com/CVE-2023-35169/index.md)
```

## Sources

- CVE record: https://www.cve.org/CVERecord?id=CVE-2023-35169
- NVD: https://nvd.nist.gov/vuln/detail/CVE-2023-35169
- OSV: https://osv.dev/vulnerability/CVE-2023-35169

---
From sbomify Insights: https://insights.sbomify.com/CVE-2023-35169/
Summaries are generated and may contain mistakes; the official description above is the record.
