-
Notifications
You must be signed in to change notification settings - Fork 84
Fix SSL write data loss and flush-before-read on non-blocking writes #347
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
jsvd
wants to merge
1
commit into
jruby:master
Choose a base branch
from
jsvd:fix-ssl-write-flush-before-read
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # frozen_string_literal: false | ||
| require File.expand_path('test_helper', File.dirname(__FILE__)) | ||
|
|
||
| class TestSSLWriteFlush < TestCase | ||
|
|
||
| include SSLTestHelper | ||
|
|
||
| # write_nonblock a large payload then read the server's response. | ||
| # | ||
| # This exercises the write -> read transition used by net/http for POST | ||
| # requests. Two bugs in SSLSocket caused data loss here: | ||
| # 1. write() called netWriteData.clear() after a partial non-blocking | ||
| # flush, discarding encrypted bytes not yet sent to the socket. | ||
| # 2. sysreadImpl() did not flush pending netWriteData before reading. | ||
| # | ||
| # Without the fix the TLS stream is corrupted and the connection breaks | ||
| # with EPIPE. | ||
| def test_write_nonblock_then_read | ||
| data_size = 256 * 1024 | ||
| data = "X" * data_size | ||
|
|
||
| server_proc = proc { |ctx, ssl| | ||
| begin | ||
| received = "" | ||
| begin | ||
| while received.bytesize < data_size | ||
| received << ssl.readpartial(8192) | ||
| end | ||
| rescue EOFError | ||
| end | ||
| ssl.write("GOT:#{received.bytesize}") | ||
| ensure | ||
| ssl.close rescue nil | ||
| end | ||
| } | ||
|
|
||
| start_server0(PORT, OpenSSL::SSL::VERIFY_NONE, true, | ||
| server_proc: server_proc) do |server, port| | ||
| sock = TCPSocket.new("127.0.0.1", port) | ||
| sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, 2048) | ||
| ssl = OpenSSL::SSL::SSLSocket.new(sock) | ||
| ssl.connect | ||
| ssl.sync_close = true | ||
|
|
||
| remaining = data | ||
| while remaining.bytesize > 0 | ||
| begin | ||
| written = ssl.write_nonblock(remaining) | ||
| remaining = remaining.byteslice(written..-1) | ||
| rescue IO::WaitWritable | ||
| IO.select(nil, [ssl]) | ||
| retry | ||
| end | ||
| end | ||
|
|
||
| response = "" | ||
| deadline = Time.now + 30 | ||
| loop do | ||
| remaining_time = deadline - Time.now | ||
| break if remaining_time <= 0 | ||
| if IO.select([ssl], nil, nil, [remaining_time, 1].min) | ||
| begin | ||
| chunk = ssl.read_nonblock(16384, exception: false) | ||
| case chunk | ||
| when :wait_readable then next | ||
| when nil then break | ||
| else response << chunk | ||
| end | ||
| rescue EOFError | ||
| break | ||
| end | ||
| end | ||
| end | ||
|
|
||
| assert_match(/^GOT:#{data_size}$/, response, | ||
| "Server should receive all #{data_size} bytes") | ||
| ssl.close | ||
| end | ||
| end | ||
|
|
||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw. was this actually relevant in context of #242?
also any thoughts about forcing a blocking
flushDataor the comment above in general (doing a write in context of user requesting a non-blocking read)?