Skip to content

feat: add max_header_len & validate_handshake options #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tjasko
Copy link

@tjasko tjasko commented Mar 15, 2025

Introducing new functionality to add two new options to client:new(). Changes have been implemented in a way to retain backward compatibility.

  • Added max_header_len option to limit the maximum allowed header size during the WebSocket upgrade process.
  • Added validate_handshake option to enforce that the WebSocket handshake response must return HTTP 101.
  • Improved HTTP response parsing by checking the status line and extracting response headers properly.
  • Added new test cases

Not all existing tests are passing, but that's unrelated to this improvement. I had to re-generate a new certificate to get Nginx happy, this likely should be fixed in another pull request & add the below to the Makefile:

$ openssl req -new -key test.key -out test.csr -subj "/C=US/ST=California/L=San Francisco/O=OpenResty/OU=OpenResty/CN=test.com/emailAddress=agentzh@gmail.com"
$ openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt

Thanks for considering this change!

@tjasko tjasko force-pushed the feat/ws-upgrade-validation branch from 884133b to 9710364 Compare March 15, 2025 02:42
- Added `max_header_len` option to limit the maximum allowed header
  size during the WebSocket upgrade process.
- Added `validate_handshake` option to enforce that the WebSocket
  handshake response must return HTTP 101.
- Improved HTTP response parsing by checking the status line and
  extracting response headers properly.
- Added new test cases
@tjasko tjasko force-pushed the feat/ws-upgrade-validation branch from 9710364 to 2db11ae Compare March 15, 2025 03:10
m, err = re_match(header, [[^\s*HTTP/1\.1\s+]], "jo")
if not m then
-- Validate HTTP status line.
local status_line_end = header:find("\r?\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

use nginx.re to get the status code directly.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review. Are you stating that you wish to revert to the previous re_match() behavior & not validate the HTTP status line per HTTP spec?

Copy link
Contributor

Choose a reason for hiding this comment

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

The original implementation is more efficient and already meets the requirements, so we can keep it as is.

^error: "response headers too large \(limit: 1024 bytes\)"
--- no_error_log
[error]
[warn]
Copy link
Contributor

Choose a reason for hiding this comment

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

require a newline at the end of file.

Copy link
Author

Choose a reason for hiding this comment

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

Will update, thanks!

@tjasko tjasko requested a review from zhuizhuhaomeng June 27, 2025 01:07
m, err = re_match(header, [[^\s*HTTP/1\.1\s+]], "jo")
if not m then
-- Validate HTTP status line.
local status_line_end = header:find("\r?\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

The original implementation is more efficient and already meets the requirements, so we can keep it as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants