-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The IsLibrary guard covers two branches. The file write is the third.\n\nPre-patch `DalLog`, the print/write tail of the function:\n\n```go\n// internal/printing/logger.go, v2.12.0\n// Printing\nmutex.Lock()\nif options.IsLibrary {\n options.ScanResult.Logs = append(options.ScanResult.Logs, text)\n} else {\n if level == \"PRINT\" {\n StopSpinner(options)\n if options.Format == \"json\" || options.Format == \"jsonl\" {\n ftext = text\n fmt.Println(text)\n } else {\n ftext = \"[POC]\" + text\n // colored stdout print\n }\n RestartSpinner(options)\n } else {\n if !options.Silence {\n if text != \"HIDDENMESSAGE!!\" {\n text = \"\\r\" + text\n fmt.Fprintln(os.Stderr, text)\n }\n }\n }\n}\n\nif options.OutputFile != \"\" {\n var fdtext string\n if ftext != \"\" {\n fdtext = ftext\n f, err := os.OpenFile(options.OutputFile,\n os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)\n if err != nil {\n fmt.Fprintln(os.Stderr, \"output file error (file)\")\n }\n defer f.Close()\n if _, err := f.WriteString(fdtext + \"\\n\"); err != nil {\n fmt.Fprintln(os.Stderr, \"output file error (write)\")\n }\n }\n}\nmutex.Unlock()\n```\n\nThe `if options.IsLibrary` block exists because library-mode dalfox has a contract: the caller is the consumer of the log lines, the dalfox process is not allowed to write to the operator's terminal, the lines accumulate in `ScanResult.Logs` for the calling program to read. Both observable outputs the function's author considered, the append to the slice and the print to stdout, are inside the if/else. The library caller gets the append. The CLI caller gets the print.\n\nThe file-write block is below the closing brace. It has its own guard, `if options.OutputFile != \"\"`, but it does not have the library-mode guard. When `IsLibrary` is true and `OutputFile` is non-empty, the library caller gets the append AND the process opens the file and writes the line to disk.\n\n`ftext` is set by the level switch above the if/else, but only when `allWrite` is true, which is true when `options.OutputAll` is true OR `options.Debug` is true. The advisory names exactly those three fields. The first names the path. The other two say \"write at all.\" Either of them flips the third branch from no-op to live.\n\n## Initialize hardcodes the library flag and copies the attacker's path\n\n`pkg/server/scan.go`'s `ScanFromAPI` calls `dalfox.Initialize(target, target.Options)`. Initialize starts with hardcoded library-mode defaults:\n\n```go\n// lib/func.go, v2.12.0\nnewOptions := model.Options{\n IsLibrary: true,\n OutputFile: \"\",\n Debug: false,\n OutputAll: false,\n Silence: true,\n NoColor: true,\n NoSpinner: true,\n // ...\n}\n```\n\nFive separate fields are set to \"library mode\" values. The contract is written into the defaults: a library caller has chosen embedding, not invocation, and embedding does not write to the operator's filesystem.\n\nInitialize's override tables then copy specific fields back from the caller's options:\n\n```go\n// lib/func.go: string-options table\n\"OutputFile\": {&newOptions.OutputFile, options.OutputFile},\n// ...\nfor _, opt := range stringOptions {\n if opt.source != \"\" {\n *opt.value = opt.source\n }\n}\n\n// lib/func.go: bool-options table\n\"OutputAll\": {&newOptions.OutputAll, options.OutputAll},\n\"Debug\": {&newOptions.Debug, options.Debug},\n// ...\nfor _, opt := range boolOptions {\n if opt.source {\n *opt.target = true\n }\n}\n```\n\n`OutputFile` is copied if non-empty. `OutputAll` and `Debug` are copied if true. `IsLibrary` is not in the bool-options table. The library-mode default at the top of the function wins; the attacker's JSON cannot override it.\n\nAfter Initialize, `newOptions.IsLibrary == true` and `newOptions.OutputFile`, `newOptions.OutputAll`, `newOptions.Debug` are whatever the attacker's JSON said. Every `DalLog` call in the scan goroutine takes the library branch (no terminal output) and writes the same line to the attacker's chosen path.\n\n## The first log line fires before the scan sends a payload\n\n`pkg/scanning/scan.go:187`, near the top of the scan goroutine:\n\n```go\nparsedURL, err := url.Parse(target)\nif err != nil {\n printing.DalLog(\"SYSTEM\", \"Unable to parse URL: \"+target+\". Please ensure it is a valid URL.\", options)\n return scanResult, err\n}\n```\n\nA request body whose URL fails `url.Parse` reaches this line within microseconds of the goroutine starting. The level is `SYSTEM`. The switch sets `ftext = \"[*] \" + text` when `allWrite` is true. The if/else takes the library branch. The file-write block runs. The attacker's path gets `[*] Unable to parse URL: ://. Please ensure it is a valid URL.\\n` appended, or, if the path did not exist, created with mode `0644` and that one line written to it.\n\nThe CLI operator running `dalfox server` does not see this. The library branch was taken; nothing went to stderr. Echo's access log records `POST /scan 200`. The file exists on disk. Nothing on the operator's terminal indicates that anything happened.\n\nThis is the structural difference between this CVE and the sibling [CVE-2026-45087, the RCE via `--found-action`](/posts/dalfox-cve-2026-45087-rest-api-was-the-cli). The RCE requires the scanner to find a reflection: `foundAction` fires from `pkg/scanning/sendReq.go`, which runs after `scan.Scan` has tested at least one payload against the target and observed it echoed in a response body. The file write fires from `DalLog`, which the scan goroutine calls from its first line of bookkeeping. There is no \"reflection found\" precondition. There is no \"the target was vulnerable to XSS\" precondition. There is `url.Parse` returning an error on the request body's URL field, or a successful target fetch that hits the next `SYSTEM` line at `scan.go:212`, or any of fifty-nine other call sites the scan goroutine hits as it runs.\n\n## The chain is one curl against a default-config server\n\nThe default `--port` is `6664`. The default `--host` is `0.0.0.0`. The default `--api-key` is empty. The auth middleware registers only when the key is non-empty. The server starts, prints `Listen 0.0.0.0:6664`, and serves.\n\n```bash\n$ curl -X POST http://target:6664/scan \\\n -H 'Content-Type: application/json' \\\n -d '{\n \"url\": \"http://attacker.example/\",\n \"options\": {\n \"output\": \"/home/dalfox/.ssh/authorized_keys\",\n \"output-all\": true\n }\n }'\n{\"code\":200,\"msg\":\"\"}\n```\n\n`postScanHandler` accepts the body, generates a scan id, spawns the goroutine. `ScanFromAPI` calls `dalfox.Initialize`, which copies the attacker's `OutputFile` and `OutputAll` across. `scan.Scan` runs. The first `DalLog(\"SYSTEM\", \"Valid target [ code:200 / size:N ]\", options)` call at `scan.go:212` hits the moment the target URL is fetched. The level is `SYSTEM`. `allWrite` is true because `OutputAll` is true. `ftext` is set to `[*] Valid target [ code:200 / size:N ]`. The if/else takes the library branch. The file-write block opens `/home/dalfox/.ssh/authorized_keys` with `O_APPEND|O_CREATE` and mode `0644`. Whatever was at that path is appended to. Whatever was not at that path is created.\n\n`0644` on `authorized_keys` is wrong for OpenSSH's strict-modes check, and `[*] Valid target` is not a public key. The example is the primitive's ceiling, not its most artful exploitation. The primitive is \"create a 0644-mode file at any path the dalfox process user can write to.\" `/var/spool/cron/crontabs/`, `/etc/cron.d/`, `~/.bash_logout`, a webroot CGI directory if dalfox is colocated with one, a sentinel file another service polls for, a serialized-session path the application server reads back, any of these is a path where \"create the file and own it\" matters more than \"the content is valid for that file's syntax.\"\n\nThe primitive does not stop on the first call. Subsequent scan iterations hit `DalLog` dozens of times each. The file is appended to on every line. The attacker chose the path; they also chose how many lines to add, by choosing the URL and the depth of the scan options. A target that fails fast adds a handful of lines. A target that passes discovery and runs the full payload corpus adds hundreds.\n\n## The fix is a denylist at the API boundary. DalLog is byte-identical.\n\nCommit `02ad18f`, May 7, 2026, the patch that closes all three GHSAs that share this root cause:\n\n```go\n// pkg/server/server.go, added in v2.13.0\n// sanitizeAPIScanOptions strips fields that would let an API caller drive\n// host-side filesystem or shell execution. These options are only safe for\n// local CLI users; in REST/MCP mode the caller is the request body, so we\n// refuse to honor them regardless of authentication state.\n//\n// See GHSA-v25v-m36w-jp4h, GHSA-8hf9-3q64-q2qf, GHSA-35wr-x7v6-9fv2.\nfunc sanitizeAPIScanOptions(o *model.Options) {\n o.FoundAction = \"\"\n o.FoundActionShell = \"\"\n o.OutputFile = \"\"\n o.OutputAll = false\n o.CustomPayloadFile = \"\"\n o.CustomBlindXSSPayloadFile = \"\"\n o.HarFilePath = \"\"\n}\n\nfunc postScanHandler(c echo.Context, scans *[]string, options *model.Options) error {\n rq := new(Req)\n if err := c.Bind(rq); err != nil {\n // ...\n }\n sanitizeAPIScanOptions(&rq.Options)\n // ...\n go ScanFromAPI(rq.URL, rq.Options, *options, sid)\n // ...\n}\n```\n\n`OutputFile = \"\"` and `OutputAll = false` are forced into the API-bound options before they reach `dalfox.Initialize`. The third field the advisory names, `Debug`, is not in the sanitizer. It does not need to be; the file-write block short-circuits when `OutputFile == \"\"` regardless of what `Debug` is. The sanitizer closes the chain at the path field, not at the trigger fields.\n\n`internal/printing/logger.go` between v2.12.0 and v2.13.0 differs by exactly one line:\n\n```diff\n@@ -192,6 +192,7 @@ func DalLog(level, text string, options model.Options) {\n \t\t}\n \n \tcase \"YELLOW\":\n+\t\tftext = text\n \t\tif options.AuroraObject != nil {\n```\n\nThe line adds the `YELLOW` log level to the set that writes to `OutputFile` when the file-write block is reached. The patch that closes CVE-2026-45089 widens the file-write surface inside the same function it leaves structurally untouched. The `if options.IsLibrary { ... } else { ... }` block in v2.13.0 is in the same position, with the same scope, as it was in v2.12.0. The file-write block is still three lines below the closing brace. The library-mode contract is still violated on every call where `OutputFile` is set.\n\nThe patch decided the trust boundary is the API surface. The patch did not decide the trust boundary is also library-mode versus host-modification mode. Both decisions were available. One block of code, three lines, moved into the existing `if options.IsLibrary` block's `else` branch, would have closed the primitive at the logger. The patch made the other decision: a denylist of seven specific field names at the only call site that the GHSA reporters demonstrated. The library-mode guard is preserved as it was. The next caller that hands `DalLog` a non-empty `OutputFile` runs the third branch the same way the REST API server did.\n\n## The contract was \"library mode produces no side effects on the host\"\n\nThe if/else in `DalLog` is not decorative. The function's author wrote it to discriminate between the two callers dalfox supports as first-class: an operator at a terminal running `dalfox url https://target.example/`, and a Go program that imports `github.com/hahwul/dalfox/v2/lib` and calls `lib.NewScan(target)`. Library callers expect to be the only consumer of the log output. Their calling program is responsible for surface: it decides whether to log to a file, whether to print, whether to discard. The dalfox process is not supposed to make those decisions on the caller's behalf.\n\nThe five hardcoded defaults at the top of Initialize are the contract's text. `IsLibrary: true`, `OutputFile: \"\"`, `Silence: true`, `NoColor: true`, `NoSpinner: true`. Each disables a default-CLI side effect. The library contract reads \"no terminal output, no file output, no spinner, no color, no banners.\"\n\nThe same Initialize function then copies `OutputFile` from the caller's options if non-empty. That is the contract's loophole. A library caller who explicitly asks for a file output gets one. The loophole exists because a CLI run also goes through Initialize: `dalfox url https://target.example/ -o output.txt` populates `target.Options.OutputFile`, Initialize copies it across, the CLI's expected file output works.\n\nThe REST API caller is a third class of caller. They are nominally library-mode (`IsLibrary` is hardcoded true; the if/else is taken on the library side), so the operator sees no terminal output. They are CLI-mode in the only field that matters, the one that controls the file write. The contract's text says \"library mode produces no side effects on the host.\" The runtime behavior is: every scan log line writes to the attacker's chosen path. The operator's terminal stays clean for the duration.\n\nThis is the [security-tool-as-primitive](/patterns/security-tool-as-primitive) shape applied to the scanner's logging facility. The CLI feature that lets an operator pipe scan output to `findings.txt` is the privileged action. The privilege exists because operators do this. The same privilege, reachable through a struct field that the REST handler binds from JSON, is a remote `open(path, O_CREAT|O_APPEND, 0644)` against whatever the dalfox process can write. The denylist in `sanitizeAPIScanOptions` is the boundary that holds the primitive off the wire today. The contract `IsLibrary` was supposed to enforce is not.\n\nThe sibling post on [CVE-2026-45087](/posts/dalfox-cve-2026-45087-rest-api-was-the-cli) names the broader structural cause and the [design-debt-driver](/patterns/design-debt-driver) shape behind it: `model.Options` doing double duty as flag struct and request body, with seventy-two JSON tags each becoming a network-controlled field, fixed by a denylist of eight. That post is the better treatment of the boundary failure. This CVE's contribution is the second-order observation: the function at the end of the propagation chain, `DalLog`, was itself written by an author who understood library mode well enough to gate the stdout branch on it, and the same author left the file-write block outside the gate. The struct was wrong at the boundary. The function was wrong at the receiver. The patch closed the boundary side. The receiver was not changed.\n\nThis is [the-detector-is-the-target](/patterns/the-detector-is-the-target) in its quietest form. The dalfox process is a vulnerability scanner. Its job description is \"send arbitrary requests to URLs the operator chose, parse the responses, log what was found.\" The REST API moves the choice of URL from the operator to the network. The scanner's logging, which is a feature, becomes a remote file-touch on the host the scanner runs on. The operator sees nothing the entire time. The line was always written to a slice for the library caller to read; in v2.12.0 server mode, it was also written to a path the network caller specified.\n\nPoC: [GHSA-8hf9-3q64-q2qf](https://github.com/advisories/GHSA-8hf9-3q64-q2qf)","closing_line":"The library-mode guard is unchanged in v2.13.0. Any future caller that hands `DalLog` an attacker-controlled `OutputFile` reopens the third branch. The sanitizer in `pkg/server` is the only thing keeping the file path off the wire.","hook_md":"`internal/printing/logger.go` is the file dalfox writes every scan log line through. The file's only exported function, `DalLog`, opens with eight lines of level-prefix bookkeeping and then enters an `if options.IsLibrary { ... } else { ... }` block. The if branch appends the formatted line to `options.ScanResult.Logs` so a Go-library caller can collect it. The else branch prints to stdout for the CLI operator. Those are the two output modes the function's author considered.\n\nThree lines below the brace that closes the if/else, the function reaches a second block: `if options.OutputFile != \"\" { os.OpenFile(options.OutputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); ... }`. That block runs whichever of the two branches above was taken. CVE-2026-45089 is what happens when `options.OutputFile` is a string the network caller chose.","post_id":534,"slug":"dalfox-cve-2026-45089-file-write-outside-the-islibrary-guard","title":"CVE-2026-45089: Dalfox's File-Write Block Was Outside the IsLibrary Guard","type":"initial","unreadable_sentence":"The patch lives in pkg/server. The bug lives in internal/printing. The same release changed both files; the change in internal/printing widened what writes to OutputFile."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCapcwwAAKCRDeZjl4jgkQ JiQKAP975IDrHqJMNcoF1X2IvOQRGLarhM0bFrbTeKB4f6TeLAEAqGqhH51OHJaF cIY3cmBsstiMKY+bDT/8Q0eh2VayCQo= =5UDV -----END PGP SIGNATURE-----