-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"PoC: [GHSA-rqgh-gxv4-6657](https://github.com/gotenberg/gotenberg/security/advisories/GHSA-rqgh-gxv4-6657) (CVE-2026-42589)\n\n## `/forms/pdfengines/metadata/write` parses a JSON object. The keys become arguments to a Perl process.\n\nGotenberg is a Docker-packaged stateless PDF API written in Go. CVE-2026-42589 lives in its `pdfengines` module, which exposes a route called `writeMetadata`:\n\n```go\n// pkg/modules/pdfengines/routes.go\nfunc writeMetadataRoute(...) api.Route {\n return api.Route{\n Method: http.MethodPost,\n Path: \"/forms/pdfengines/metadata/write\",\n Handler: ...\n }\n}\n```\n\nThe handler reads multipart form data, parses a `metadata` field as JSON, and hands the resulting map to the engine:\n\n```go\n// pkg/modules/api/multipart.go (FormDataPdfMetadata excerpt)\nvar metadata map[string]any\nif err := json.Unmarshal([]byte(value), &metadata); err != nil {\n return fmt.Errorf(\"unmarshal metadata: %w\", err)\n}\n```\n\nA `map[string]any` from `encoding/json` accepts any UTF-8 string as a key. There is no schema. The keys travel into the engine untouched.\n\nThe engine is the `exiftool` module:\n\n```go\n// pkg/modules/exiftool/exiftool.go (v8.29.1, pre-patch)\nfunc (engine *ExifTool) WriteMetadata(\n ctx context.Context, logger *zap.Logger,\n inputPath string, metadata map[string]interface{},\n) error {\n exifToolBinary, err := exifTool.NewExiftool(...)\n if err != nil {\n return fmt.Errorf(\"create new exiftool binary: %w\", err)\n }\n defer exifToolBinary.Close()\n\n fileMetadata := exifToolBinary.ExtractMetadata(inputPath)\n if fileMetadata[0].Err != nil {\n return fmt.Errorf(\"extract metadata: %w\", fileMetadata[0].Err)\n }\n\n for key, value := range metadata {\n if isDangerousTag(key) {\n continue\n }\n // serialize value to string\n fileMetadata[0].SetString(key, val)\n }\n\n return exifToolBinary.WriteMetadata(fileMetadata)\n}\n```\n\n`exifTool.NewExiftool` is the constructor for `github.com/barasher/go-exiftool`, a third-party wrapper around Phil Harvey's ExifTool. It is the bridge.\n\n```go\n// github.com/barasher/go-exiftool v1.10.0, exiftool.go\nvar initArgs = []string{\"-stay_open\", \"True\", \"-@\", \"-\"}\n\nfunc NewExiftool(opts ...func(*Exiftool) error) (*Exiftool, error) {\n e := &Exiftool{}\n // ...\n e.cmd = exec.Command(e.binPath, initArgs...)\n e.stdin, _ = e.cmd.StdinPipe()\n e.stdMergedOut, _ = e.cmd.StdoutPipe()\n // ...\n}\n```\n\n`-stay_open True -@ -` is ExifTool's persistent mode. Phil Harvey designed it for batch tools that want to reuse a single Perl interpreter across many invocations. ExifTool reads one argument per line from the `-@ -` file (stdin), and treats a literal line containing `-execute` as the end of an argv batch. Everything between batches behaves exactly like an argv to the `exiftool` command.\n\n`go-exiftool` exposes this through `WriteMetadata`. Inside the wrapper the loop writes each key=value pair to the persistent stdin:\n\n```go\n// barasher/go-exiftool exiftool.go, WriteMetadata\nfor k, v := range origin.Fields {\n // serialize v -> str\n fmt.Fprintln(e.stdin, \"-\"+k+\"=\"+str)\n}\nfmt.Fprintln(e.stdin, \"-overwrite_original\")\nfmt.Fprintln(e.stdin, origin.File)\nfmt.Fprintln(e.stdin, \"-execute\")\n```\n\n`fmt.Fprintln` writes the formatted string and appends `\\n`. The destination is stdin of a Perl process whose protocol uses `\\n` as the argv separator. If `k` contains `\\n`, the wrapper does not see two keys, the wrapper sees one key. ExifTool sees two arguments.\n\n## The PoC is one curl.\n\nThe advisory publishes the proof:\n\n```bash\ncurl -X POST http://target:3000/forms/pdfengines/metadata/write \\\n -F \"files=@input.pdf\" \\\n -F 'metadata={\"Title\\n-if\\n(system(\\\"id\\\"))\": \"x\"}'\n```\n\nThe metadata field is a JSON object with a single key. The key contains two embedded newlines. After `json.Unmarshal`, Go has a `map[string]any` with one entry whose key is the literal string `Title\\n-if\\n(system(\"id\"))` and whose value is `x`. The dangerous-tag filter, which compares against the exact strings `FileName` and `Directory`, does not match. The loop calls `SetString`. The wrapper builds an argv line.\n\nWhen `fmt.Fprintln(e.stdin, \"-\"+k+\"=\"+str)` runs, the bytes that hit the Perl process are:\n\n```\n-Title\n-if\n(system(\"id\"))=x\n-overwrite_original\ninput.pdf\n-execute\n```\n\nExifTool sees `-Title` (a valid tag write request with no value), then `-if`, then `(system(\"id\"))=x` as the argument to `-if`. ExifTool's `-if` flag exists. It is documented. It takes a Perl expression that gets passed to `eval` and used to decide whether the current file matches a condition. The expression evaluates side effects. `system(\"id\")` runs `/usr/bin/id` with the privileges of the Gotenberg process, which in the Docker image is root.\n\nThe argv parse continues with `-overwrite_original`, the file path, and `-execute`. The PDF gets written. The HTTP response returns 200. The container has run an attacker's command, and the only thing in the request body that looks unusual is a `\\n` in a JSON string. JSON strings are allowed to contain `\\n`.\n\n## The patch sequence is the testimony.\n\nThe Gotenberg repository's git log between the CVE-2026-42589 advisory date and the next minor release contains four commits that touch the exiftool module. The progression is the auto-confession.\n\n| Commit | Date | What it adds |\n| --- | --- | --- |\n| `405f106` | Apr 6 | `safeKeyPattern` regex on keys |\n| `55d1952` | Apr 11 | `validateMetadataValue` rejecting `\\n\\r\\x00` in values |\n| `46e1909` | Apr 13 | Group-prefix strip in `isDangerousTag` |\n| `68e0f88` | Apr 24 | Delete go-exiftool. Rewrite with `exec.CommandContext`. |\n\nThe first commit lands the regex:\n\n```go\nvar safeKeyPattern = regexp.MustCompile(`^[a-zA-Z0-9\\-_.:]+$`)\n```\n\nThis is the obvious fix. Keys are ExifTool tag names. Tag names are alphanumerics, colons (for group prefixes), and a few punctuation characters. Anything else is rejected. The commit message describes the fix as \"validate metadata keys\" and stops there.\n\nFive days later the second commit lands. It adds a function nobody promised:\n\n```go\nfunc validateMetadataValue(v interface{}) error {\n // reject \\n, \\r, \\x00 in serialized form\n ...\n}\n```\n\nThe comment in the diff names the gap the first patch left:\n\n> // metadata values are also passed through go-exiftool's stdin pipe; a value\n> // containing a newline can terminate the current argument and inject a new\n> // flag the same way a key can.\n\nThe first patch shipped without anyone running the test that would have caught this. The second patch documents that the value vector exists and was missed.\n\nTwo days after that, the third commit lands. The dangerous-tag filter was checking exact string equality against `FileName` and `Directory`. ExifTool accepts group-prefixed tags. `System:FileName` is the same tag as `FileName` for write purposes. The filter saw two different strings. The third commit strips the group prefix before comparing:\n\n```go\nfunc isDangerousTag(k string) bool {\n if i := strings.LastIndex(k, \":\"); i != -1 {\n k = k[i+1:]\n }\n // ... compare against allowlist\n}\n```\n\nThree commits, three different ways the wrapper around `go-exiftool` failed to be safe. The advisory is published with these three commits as the named fix. The release is tagged `v8.31.0` on April 16. GHSA-rqgh-gxv4-6657 lists `< 8.31.0` as the affected range for CVE-2026-42589.\n\nEight days after `v8.31.0`, the maintainer pushes commit `68e0f88`. The diff is large. The summary line is \"refactor exiftool integration.\" The substance is the deletion of the `barasher/go-exiftool` import, the removal of the persistent-mode wrapper, and the replacement of `WriteMetadata` with this:\n\n```go\n// pkg/modules/exiftool/exiftool.go (post-rewrite)\nfunc (engine *ExifTool) WriteMetadata(\n ctx context.Context, logger *zap.Logger,\n inputPath string, metadata map[string]interface{},\n) error {\n args := []string{\"-overwrite_original\"}\n for k, v := range metadata {\n if !safeKeyPattern.MatchString(k) {\n continue\n }\n if err := validateMetadataValue(v); err != nil {\n continue\n }\n args = append(args, fmt.Sprintf(\"-%s=%v\", k, v))\n }\n args = append(args, inputPath)\n\n cmd := exec.CommandContext(ctx, engine.binPath, args...)\n return cmd.Run()\n}\n```\n\nThe validations stay. The persistent-mode pipe is gone. ExifTool is now invoked the way every shell script in the world invokes it: an argv. A `\\n` inside `args[i]` is no longer a separator, because there is no separator. The kernel's `execve` reads the argv as a fixed-length array of byte strings.\n\nThe comment the maintainer left next to the new function reads, verbatim:\n\n> // We invoke exiftool via argv rather than via stay_open mode. Argv is not\n> // subject to the line-delimited injection that the previous backend permitted\n> // even after we validated keys and values, because validation is harder than\n> // not parsing.\n\n\"Validation is harder than not parsing\" is the kind of sentence that gets engraved on the door of a vault after the vault has been emptied once.\n\n## `v8.31.0` is the validation. `v8.32.0` is the architecture.\n\nThe release timeline lays it out:\n\n| Version | Date | Contents |\n| --- | --- | --- |\n| `v8.29.1` | Mar 30 | Last vulnerable release |\n| `v8.31.0` | Apr 16 | Three validation commits. Advisory names this. |\n| `v8.32.0` | Apr 30 | Library deletion. Argv rewrite. No advisory mention. |\n\nThe CVE-2026-42589 advisory says fixed in `v8.31.0`. The advisory is correct in the narrow sense that the published PoC, including the group-prefix bypass that produced the third commit, no longer works against `v8.31.0`. The advisory does not say that two weeks after `v8.31.0` shipped, the maintainer concluded that key and value validation on top of a line-delimited stdin protocol was not a sound abstraction and ripped the substrate out.\n\nAnyone who reads the advisory and pins `>= 8.31.0` is pinned to the version where Gotenberg is trying to make `go-exiftool` safe. Anyone who reads the changelog past the advisory date is on the version where Gotenberg has decided `go-exiftool` is not a thing it is using anymore.\n\nThis is the second auto-confession. The first is in the comment on the second patch (\"a value containing a newline can terminate the current argument\"). The second is in the version skew: an advisory naming the validation release, a follow-up release naming nothing, and a deleted dependency.\n\nThe deleted dependency is still on the Go module proxy. `barasher/go-exiftool` v1.10.0 has not been yanked. It is still the top hit when a Go developer searches for \"exiftool go library.\" Its README does not mention that one of its known consumers removed it for being unsafe in the presence of attacker-controlled keys.\n\n## `go-exiftool` is the substrate. Gotenberg is one of its consumers.\n\nThe mechanism class here is [content-is-command](/patterns/content-is-command). User data is supposed to be data. The library decides that an efficient way to talk to ExifTool is to keep one Perl process alive and feed it newline-delimited arguments. The boundary between \"what the application asked for\" and \"what ExifTool will execute\" is a `\\n` byte inside a string that already came from a JSON parser that does not treat `\\n` as special. The application reads a map. The map's keys are strings. The library writes strings into a pipe. The pipe is a control channel that uses string contents as commands.\n\nThe chain is an [emergent-primitive](/patterns/emergent-primitive): `encoding/json` accepting any UTF-8 in a map key, `go-exiftool`'s `Fprintln(e.stdin, ...)` interpolation, ExifTool's `-stay_open True -@ -` line-delimited argv protocol, and ExifTool's `-if` flag accepting Perl expressions. Each of these components is documented. Each makes sense in isolation. The primitive is not in any of them. The primitive is in their composition.\n\nGotenberg is not the only place this happens. The Node.js library `exiftool-vendored` shipped [CVE-2026-43893](/posts/exiftool-vendored-cve-2026-43893-keys-were-raw) with the same shape three weeks earlier: user-supplied keys reach the same `-stay_open` Perl stdin without value-side encoding. Two language ecosystems, two wrappers, three weeks apart, both shipping the same class of mistake on top of Phil Harvey's persistent-mode protocol. The protocol is not the bug. The wrappers' decision to interpolate user input into the protocol is the bug. Both wrappers were popular for years before anyone filed.\n\nThe same class of \"the parser does what you wrote, not what you meant\" runs through every recent post on this site. [Handlebars trusted its own AST](/posts/handlebars-cve-2026-33937-trusts-its-own-ast) on the assumption that the compiler is the only thing that produces ASTs. [graphql-ruby compiled the schema](/posts/graphql-ruby-cve-2025-27407-loaded-the-schema-by-compiling-it) on the assumption that the schema came from the application. Gotenberg passed JSON keys through `go-exiftool`'s stdin pipe on the assumption that the keys came from a trusted JSON object. The assumption is the bug. The bug class is the assumption.\n\nThe Apr 11 comment said it. The Apr 24 comment said it twice. \"Validation is harder than not parsing\" is not a slogan, it is the postmortem.","closing_line":"`v8.31.0` fixed the bug. `v8.32.0` fixed the library.","hook_md":"Gotenberg's fix for CVE-2026-42589 starts as three validation patches and ends as a library deletion. The validation patches add a regex on user-supplied metadata keys, a control-character check on metadata values, and a group-prefix strip for the pseudo-tag filter. Eleven days after the third patch, the maintainer removes `go-exiftool` from `go.mod` and rewrites the integration in fifty lines of `exec.CommandContext`. The comment in the new code is more honest than the commit message: argv invocation is not susceptible to stdin-protocol injection the way the previous go-exiftool backend was, and the previous backend is still on the Go module proxy.","post_id":349,"slug":"gotenberg-cve-2026-42589-fixed-it-twice","title":"CVE-2026-42589: Gotenberg Fixed It Twice. The Advisory Names The First Fix.","type":"initial","unreadable_sentence":"`v8.31.0` fixed the bug. `v8.32.0` fixed the library."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCajhI/wAKCRDeZjl4jgkQ Ji+vAQDxaa3pGZzxDK/rqON7VU/bWdaicfYzQZzP9B1g0I0cOQD9FWycrJ8sAcY3 yTU+WvdKHdGS/+QHkLLYOmMu9uLArg8= =64x3 -----END PGP SIGNATURE-----