PUT /api/cert_sync writes attacker-controlled content to any nginx conf-tree path, no validation, reloads without nginx -t
Description
The PUT /api/cert_sync endpoint writes attacker-chosen content to an attacker-named path anywhere under the Nginx configuration tree and then reloads Nginx, with no validation of the file content and no nginx -t syntax check before the reload. Any authenticated principal reaches it — including a default password-only (non-2FA) user and a write-scoped service token — because the route's secure-session middleware is a no-op for those principals and there is no admin role in the product.
Root Cause
SyncCertificate (api/certificate/certificate.go:319) binds the request into SyncCertificatePayload (internal/cert/sync.go:20-27), a struct that carries no binding tags, so the four fields (name, cert path, key path, cert/key content) are accepted verbatim. The handler constructs a cert.Content from those raw fields and calls content.WriteFile().
Content.WriteFile (internal/cert/write_file.go:40-106) performs exactly one check on each path field: helper.IsUnderDirectory(path, nginx.GetConfPath()). There is no extension check, no PEM/certificate parse, no directive validation (ValidateConfigFile/ValidateConfigDirectives are never called on this path), and no data-file check. The supplied bytes are staged to a temp file and atomically renamed onto the attacker-chosen path.
The handler then calls nginx.Reload() (certificate.go:370) unconditionally. The reload implementation issues nginx -s reload directly and never runs nginx -t first, so a syntactically broken or malicious configuration is loaded without validation.
This sink is distinct from the directive-denylist control: cert_sync never invokes the config validator at all, so hardening the denylist does not affect this path.
Impact
On any deployment (module-independent):
- Arbitrary create/overwrite of any file under the Nginx configuration tree — including
nginx.conf,conf.d/*.conf, site/stream config files, and SSL private keys. The attacker fully controls both the target filename (within the conf root) and the file bytes. - Integrity: overwriting or corrupting configuration/keys at an attacker-chosen path.
- Availability: writing a broken configuration and triggering a reload with no syntax check can wedge the reload and disrupt all served sites.
On a deployment whose Nginx image includes the perl, njs, or lua module, this escalates to remote code execution in the Nginx worker: the stock configuration includes include /etc/nginx/conf.d/*.conf;, so a planted conf.d/*.conf containing script/module directives is loaded on the triggered reload. This precondition (a script/module-enabled Nginx image) is the same class of RCE escalation seen in prior Nginx-UI configuration-write vulnerabilities.
Proof of Concept
As any authenticated default (non-2FA) user, or with a write-scoped service token:
curl -X PUT https://TARGET/api/cert_sync \
-H "Authorization: <token>" \
-H 'Content-Type: application/json' \
-d '{
"name": "evil",
"ssl_certificate_path": "/etc/nginx/conf.d/zz_evil.conf",
"ssl_certificate_key_path": "/etc/nginx/ssl/evil/evil.key",
"ssl_certificate": "# arbitrary nginx config directives here\n...",
"ssl_certificate_key": "x"
}'Content.WriteFile writes the verbatim ssl_certificate bytes to /etc/nginx/conf.d/zz_evil.conf (passes the only guard, IsUnderDirectory), then nginx.Reload() runs nginx -s reload with no nginx -t, loading the planted file. Both path fields must resolve under the conf root; any path there is accepted.
Attack Chain
- Entry:
PUT /api/cert_syncwith a JSON body naming a target path under/etc/nginxand arbitrary content.
- Guard:
AuthRequired()— a valid JWT or anui_pat_write-scoped token. - Bypass proof: route group at router/routers.go:119; no admin tier exists, so any authenticated principal has full management rights.
- Check:
RequireSecureSession()(internal/middleware/secure_session.go:73-128).
- Guard: interactive 2FA step-up.
- Bypass proof:
userNeedsSecureSession(secure_session.go:212-225) returns false for users without OTP/2FA/passkey, so the middleware callsc.Next()with no challenge (lines 97-105); write-scoped service tokens pass at lines 79-89.
- Bind:
cosy.BindAndValid(c, &json)intoSyncCertificatePayload.
- Guard: struct binding validation.
- Bypass proof:
SyncCertificatePayload(internal/cert/sync.go:20-27) declares no binding tags, so arbitrary path and content values are accepted.
- Short-circuit:
content.MatchesFiles()(api/certificate/certificate.go:356).
- Guard: skip when both files already hold exactly the submitted bytes.
- Bypass proof: returns false whenever the submitted content differs from on-disk (always true for an attack) → write proceeds.
- Sink:
content.WriteFile()(internal/cert/write_file.go:40-106).
- Guard:
helper.IsUnderDirectory(path, nginx.GetConfPath())on both path fields (lines 46-52) — the only guard. - Bypass proof:
/etc/nginx/conf.d/zz_evil.confresolves under the conf root → passes; no extension/PEM/directive check; verbatim bytes staged and atomically renamed onto the attacker path (lines 85-103).
- Reload:
nginx.Reload()(api/certificate/certificate.go:370).
- Guard:
nginx -tsyntax validation. - Bypass proof: the reload path issues
nginx -s reloaddirectly and never calls the config test; the plantedconf.d/*.confis loaded via the stockinclude /etc/nginx/conf.d/*.conf;.
- Impact: arbitrary conf-tree file overwrite/corruption plus an unvalidated reload; remote code execution in the worker on a perl/njs/lua-enabled image.
Bypass Evidence
internal/cert/write_file.gocontains no call toValidateConfigFile,ValidateConfigDirectives,IsDataFile, or any PEM/certificate parse — the only check isIsUnderDirectory.SyncCertificatePayload(internal/cert/sync.go:20-27) has nobinding:tags, so nothing in the request is rejected.nginx.Reload()at api/certificate/certificate.go:370 has no preceding config test; the reload implementation callsnginx -s reloaddirectly.- Verified present on the latest release tag v2.8.4 (code identical on tag and on HEAD d875dbd2).
Affected Versions
All versions up to and including 2.8.4 (github.com/0xJacky/Nginx-UI).
Suggested Fix
On the certificate sync write path, validate the written content the same way the config write paths are expected to be validated: require the cert and key fields to parse as a valid PEM certificate/private key before writing (reject arbitrary content), restrict the target filename to the certificate storage area rather than the whole conf tree, and run nginx -t before nginx -s reload, rolling back the written files if the test fails.
--- Reported by zx (Jace) — GitHub: @manus-use