open
https://gitlab.synchro.net/main/sbbs/-/work_items/1383
When a FastCGI backend's response doesn't start with valid CGI headers, `do_cgi_stuff()` in `src/sbbs3/websrvr.cpp` falls back to "send it all as plain text". That path has three bugs, and php-fpm reaches it routinely: any "Primary script unknown" reply begins with an `FCGI_STDERR` record.
Reproduced on wiki.synchro.net with `GET
http://wiki.synchro.net/index.php HTTP/1.1` (absolute form; see the companion issue for why php-fpm can't find the script):
```
HTTP/1.1 200 OK
Connection: Keep-Alive
Location:
http://wiki.synchro.net/index.php
Content-Type: text/plain
x.phpx.phpContent-type: text/html; charset=UTF-8
File not found.
```
and the client then waits until its timeout. Debug log: `Executing FastCGI: .../index.php`, `Received invalid CGI headers, sending result as plain-text`, `Result: HTTP/1.1 200 OK`.
## 1. Uninitialized stack memory sent to the client
`fbuf[1026]` in `do_cgi_stuff()` is never initialized. The header loop pushes `fbuf` into `tmpbuf` for each line, and the plain-text fallback writes every `tmpbuf` entry, then `fbuf` itself, to the client. Only the Linux pipe CGI reader (`pipereadline()`) fills `fbuf`:
- `fastcgi_readln_out()` takes `fbuf`/`fbufsz` and never writes them.
- The Win32 `cgi_readln_out()` passes `NULL, 0` to `pipereadline()`.
So on the FastCGI path (any OS), and the CGI path on Windows, whatever was on the stack goes out in the response body. `x.phpx.php` above is two copies of such leftovers. With other stack contents this discloses server memory to any client that can trigger a malformed FastCGI/CGI reply.
## 2. `FCGI_STDERR` data is treated as response output
`fastcgi_read_wait_timeout()` maps `FCGI_STDERR` to `CGI_OUTPUT_READY` (the first `switch` returns `CGI_ERROR_READY`, but a freshly read stderr header sets `CGI_OUTPUT_READY`). The stderr text is then read by `readln_out()` as if it were a stdout header line. "Primary script unknown" has no `:`, so parsing ends as "invalid headers". Separately, the `Status: 404 Not Found` line read in the next iteration is consumed by the branch that sends the fallback headers and never reaches the client, so the reply goes out as `200 OK`. Stderr records should go to the log, not into the response, and should not affect header parsing.
## 3. Fallback reply is unframed on a keep-alive connection
The fallback calls `send_headers(session, cgi_status, false)`: not chunked, no `Content-Length`, while `session->req.keep_alive` still allows `Connection: Keep-Alive`. The client cannot find the end of the body and waits until the server's inactivity timeout or its own. Either chunk it, as the valid-headers path does for HTTP/1.1, or clear `keep_alive` before sending the headers so the close delimits the body.
— *Authored by Claude (Claude Code), on behalf of @rswindell*
--- SBBSecho 3.38-Linux
* Origin: Vertrauen - [vert/cvs/bbs].synchro.net (1:103/705)