Skip to content

http_server: accept empty HTTP header values - #12175

Open
kimonus wants to merge 1 commit into
fluent:masterfrom
kimonus:fix-http-empty-header
Open

http_server: accept empty HTTP header values#12175
kimonus wants to merge 1 commit into
fluent:masterfrom
kimonus:fix-http-empty-header

Conversation

@kimonus

@kimonus kimonus commented Jul 30, 2026

Copy link
Copy Markdown

Fixes #12174.

Summary

The Monkey HTTP/1 parser rejected syntactically valid generic headers with an
empty field value. The HTTP/1 server then discarded the parser error, leaving
keep-alive clients without a response.

This change accepts empty generic field values, including values containing
only optional whitespace. Semantic validation for headers such as Host and
Content-Length remains unchanged.

Testing

  • Monkey parser and HTTP server libraries build successfully.
  • tests/runtime/in_http.c passes compiler syntax checking.
  • Added a regression request covering both Header:\r\n and
    Header: \r\n forms.
  • The complete flb-rt-in_http target could not be built locally because this
    checkout's unrelated in_tail build requires unavailable Flex/Bison-generated
    multiline APIs.

No packaging changes are included.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed HTTP request handling for headers with empty or whitespace-only values.
    • Requests containing empty header values are now parsed and processed successfully instead of being rejected.
  • Tests

    • Added coverage for HTTP POST requests containing empty and whitespace-only headers.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The HTTP parser now accepts empty and whitespace-only header values, finalizes them normally, and continues header processing. The runtime HTTP test sends requests containing both forms and asserts that the server returns HTTP 201.

Changes

Empty header values

Layer / File(s) Summary
Accept empty header values
lib/monkey/mk_server/mk_http_parser.c
MK_ST_HEADER_VALUE finalizes zero-length values on \r and trims both spaces and tabs before non-empty values.
Exercise empty header requests
tests/runtime/in_http.c
The runtime test sends empty and whitespace-only headers and asserts a successful HTTP 201 response.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant HTTPClient
  participant HTTPServer
  participant mk_http_parser
  HTTPClient->>HTTPServer: Send request with empty header values
  HTTPServer->>mk_http_parser: Parse header value
  mk_http_parser-->>HTTPServer: Finalize empty value and continue
  HTTPServer-->>HTTPClient: Return HTTP 201 response
Loading

Suggested reviewers: cosmo0920

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the main change: accepting empty HTTP header values.
Linked Issues check ✅ Passed The parser change and regression test address #12174 by accepting empty or whitespace-only generic headers and confirming a 201 response for both forms.
Out of Scope Changes check ✅ Passed The changes are limited to the parser and its regression test, with no unrelated code paths or features introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/monkey/mk_server/mk_http_parser.c`:
- Around line 999-1008: Update the MK_HEADER_HOST handling in header_lookup to
reject zero-length Host values, including values empty after optional
whitespace, while preserving acceptance of non-empty Host headers and generic
empty-header behavior.

In `@tests/runtime/in_http.c`:
- Line 454: Update the test around send_empty_header_request() so the request
uses a distinguishable payload, then assert that the corresponding output or
callback is observed directly. Ensure the later num > 0 check cannot be
satisfied by the preceding ordinary POST alone.
- Around line 379-395: Update the request/response logic around send(),
setsockopt(), and recv() to handle partial TCP writes and reads: loop until all
request bytes are sent, validate setsockopt() success, and accumulate response
data across recv() calls until the header terminator "\r\n" is found or the
buffer is full. Preserve null termination and reject send, socket-option,
receive, and incomplete/invalid response failures.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2d4fe72f-846a-4a04-9793-54b1aeb8d981

📥 Commits

Reviewing files that changed from the base of the PR and between 117a727 and 56fc47e.

📒 Files selected for processing (2)
  • lib/monkey/mk_server/mk_http_parser.c
  • tests/runtime/in_http.c

Comment thread lib/monkey/mk_server/mk_http_parser.c
Comment thread tests/runtime/in_http.c Outdated
Comment thread tests/runtime/in_http.c

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 56fc47e6b3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +999 to +1003
if (buffer[p->i] == '\r') {
p->header_val = p->i;
mark_end();

ret = header_lookup(p, buffer);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid semantic scans from empty header values

When an empty known header reaches header_lookup, header->val.len becomes 0; for Transfer-Encoding: that length is passed to mk_string_search_n, whose len <= 0 path falls back to scanning the NUL-terminated remainder of the request. In plugins/in_http/http_conn.c the buffer is NUL-terminated before parsing, so a request like Transfer-Encoding:\r\nContent-Length: ...\r\n\r\n{"msg":"chunked"} is misclassified as chunked and mk_http_parser_ok returns pending instead of processing the Content-Length body. Please either reject empty semantic headers such as Transfer-Encoding or bypass their value parsing when the value length is zero.

Useful? React with 👍 / 👎.

@kimonus kimonus Jul 30, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correction: commit 2d920dd now bypasses zero-length Transfer-Encoding semantic scanning, preventing mk_string_search_n from reading beyond the field. Empty Host and Content-Length remain rejected.

@kimonus
kimonus force-pushed the fix-http-empty-header branch 3 times, most recently from 2a9298a to 2d920dd Compare July 30, 2026 10:49
@dmytro-tkachenko2

Copy link
Copy Markdown

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Accept empty generic HTTP/1.1 field values, including values containing only
optional whitespace. The parser previously returned an error before request
completion, while the HTTP/1 server discarded that error and left keep-alive
clients without a response.

Add an in_http runtime regression request covering both empty-value wire forms.

Reject empty semantic Upgrade values, avoid scanning beyond empty Connection
and Transfer-Encoding fields, and close HTTP/1 sessions on parser errors.
The runtime regression also verifies that an invalid empty Upgrade header closes
the connection.

Fixes fluent#12174.

The focused Monkey parser and HTTP server libraries build successfully.
The complete runtime test target is blocked in this environment by unrelated
in_tail Flex/Bison-generated multiline dependencies.

Signed-off-by: kimonus <kimonus@users.noreply.github.com>
@kimonus
kimonus force-pushed the fix-http-empty-header branch from 2d920dd to 1feac14 Compare July 30, 2026 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

in_http: HTTP/1.1 empty header value can leave keep-alive request without response

2 participants