
Containerised PoC and lab for CVE-2026-126147, an unauthenticated path traversal in the FortiMail management API that enables arbitrary file write and root code execution.
| CVE | CVE-2026-126147 |
| Component | Fortinet FortiMail — web management interface API |
| Advisory | Fortinet PSIRT FG-IR-26-199, published 2026-11-10 |
| Affected | FortiMail 8.0.0 – 8.0.1, 7.6.0 – 7.6.6, 7.4.0 – 7.4.8, 7.2 all versions |
| Fixed in | FortiMail 8.0.2, 7.6.7, 7.4.9; migrate off 7.2 |
| CWE | CWE-22 — Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') |
| Impact | Unauthenticated remote code execution as the appliance service account (root on the appliance) |
| Vector | Network, unauthenticated — a crafted request to the management interface API |
| Trigger | The caller-supplied path component of a file write or file read request |
| Credit | Fortinet PSIRT |
Fortinet's advisory (FG-IR-26-199) describes an improper limitation of a pathname to a restricted directory in FortiMail and states that the flaw "may allow an unauthenticated attacker to execute unauthorized code or commands via crafted requests". The component is the FortiMail API, and the vector is a request that carries a path the appliance is not supposed to leave its restricted data directory.
That is exactly the shape of the defect reproduced here. A management-interface request
carries a caller-supplied path. The appliance joins it to a restricted root directory
and hands the result to the file system. Nothing constrains the joined path to the
root, so ../ leaves it. The file lands wherever the attacker chose, and on FortiMail
that includes the directories the appliance reads and executes — which turns a
directory traversal into "execute unauthorized code or commands" without any
authentication, any prior access and any user interaction.
This repository contains a containerised laboratory that reproduces the flaw end to end, a differential against the patched behaviour, a payload and endpoint sweep against the patched line, and the result of testing the mitigation in the two forms it can take.
FortiMail is proprietary software. None of its code, binaries or configuration are redistributed here, and Fortinet does not publish the management interface's endpoint list or its path handling. What is reproducible here is therefore:
Everything this repository asserts about the vendor product is sourced from the advisory. Everything it asserts about the model is labelled as such. The model is the only thing that runs in the laboratory, and it is built so that the vulnerable and the patched behaviour differ in the path handling alone.
The model lives in lab/fortimail_web.py and is selected with FML_MODE:
FML_MODE | behaviour | version reported |
|---|---|---|
vuln | the caller-supplied path is joined to the restricted root with no normalisation and no containment check — the traversal escapes | 8.0.1 |
fixed | the joined path is resolved and the resolved path must stay inside the restricted root; every write and read that leaves it is refused | 8.0.2 |
fixed-partial | a remediation shape that compares the un-normalised joined path against the root with a string prefix test | 8.0.2 |
fixed-partial is not a claim about the vendor's patch. It is the shape a hurried
fix takes, and it exists here so that the sweep has a target that is neither the
vulnerable build nor a correct one.
Three properties of the model are worth stating explicitly, because each of them changes what an attacker can do and each of them is a property of the appliance rather than of the exploit:
shared-link)
that points at the appliance script directory.The whole defect is the path handling. In the model it is three functions, and the vulnerable and the patched build differ in one of them.
The vulnerable build joins the caller-supplied name to the restricted root and returns
the result, with no normalisation and no containment check —
lab/fortimail_web.py:141:
if MODE == "vuln":
# FortiMail 8.0.1 model: the caller-supplied name is appended to the
# restricted root and handed to the file system as it is.
try:
return os.path.join(ROOT, name), None
except (OSError, ValueError) as exc:
return None, "invalid path: %s" % exc
os.path.join() is a string operation. It does not resolve .., it does not notice
that the second component is absolute, and it does not care that the result leaves the
directory it was supposed to stay in. For the name ../exec/pwn.sh and the root
/opt/lab/store it returns:
/opt/lab/store/../exec/pwn.sh
which the file system resolves to:
/opt/lab/exec/pwn.sh
The patched build resolves the path first and then requires containment —
lab/fortimail_web.py:156:
# FortiMail 8.0.2 model: resolve, then require containment.
try:
target = os.path.realpath(os.path.join(ROOT, name))
except (OSError, ValueError) as exc:
return None, "invalid path: %s" % exc
if not _inside(target):
return None, "path outside the restricted directory"
return target, None
with the containment test at lab/fortimail_web.py:122:
def _inside(path):
try:
root = os.path.realpath(ROOT)
real = os.path.realpath(path)
except (OSError, ValueError):
return False
return real == root or real.startswith(root + os.sep)
realpath() is what makes the check meaningful. It collapses .., it resolves
symlinks, and it makes the comparison against the root a comparison of two absolute,
fully-resolved paths. A path that merely starts with the root is not inside the
root, and the patched build refuses it with HTTP 403.
The write itself carries no authorization. The management interface in the model
serves the API without an authentication gate, because the advisory rates the flaw
AV:N/AC:L/PR:N/UI:N — the request is processed before any authentication decision.
lab/fortimail_web.py:371:
target, perr = resolve_write(name)
record_write(route, name, target, perr)
if perr:
self._json(403, {"error": perr, "path": name})
return
werr = write_file(target, data)
The traversal becomes code execution because the appliance executes what lands in its
script directory. lab/fortimail_web.py:219: