jira-importer opened a new issue, #919:
URL: https://github.com/apache/httpcomponents-core/issues/919

   **[Christian 
Klauser](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=chklauser)**
 opened 
**[HTTPCORE-442](https://issues.apache.org/jira/browse/HTTPCORE-442?redirect=false)**
 and commented
   
   I'm running into a rather esoteric problem when reading the response to an 
HTTP request using `httpcore-nio` (used via CXF). On a high level we get a 
MalfromedChunkCodingException because the ChunkDecoder doesn't receive the last 
chunk ( `"0\r\n\r\n"` is missing  ) We see the following situation:
   
   * The plain text content fed into the SOAP/JAXB deserialization machinery 
cuts off mid-way through
   * **because** the `ChunkDecoder` threw a `MalformedChunkCodingException`'
   * **because** `this.buffer.readLine` returned false
   * **because** (not 100% sure here) `fillBufferFromChannel` returned -1 and 
we are looking for the next chunk size
   * **because** the last 106 bytes of the encrypted stream (see below) have 
never been decrypted
   * **because** the loop inside `SSLIOSession#decryptData` gets exited early 
if the `SSLIOSession#endOfStream` flag is set _even if the input stream still 
contains encrypted data_
   
   ```
   state of the SSLIOSession in `this.channel` when the exception occurs: 
   
192.168.13.204:48888<->10.0.29.106:443[ACTIVE][r:r][ACTIVE][r][NOT_HANDSHAKING][EOF][][106][0][0][0]
   ```
   
   Looking at past issues/commit messages, I understand why we have an early 
exit here (avoid infinite loops on connections that broke in the middle of a 
TLS packet). I'm not sure if just exiting is correct here. The documentation 
for the java 
[`SSLEngine#unwrap`](https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/SSLEngine.html)
 method says that the engine tries to decrypt "one complete TLS network 
packet". There is no guarantee that it decrypts all remaining packets in the 
buffer.
   
   In our particular case (106 still encrypted bytes), we have to run the 
`unwrap` method two more times until we have the complete response from the 
server (including the final chunk "`0\r\n\r\n`"). After the first additional 
call, there are still 53 encrypted bytes left.
   
   I have attached a patch that solves this issue for us. The patch monitors 
whether the decryption loop is making progress and keeps looping even if 
`endOfStream` is set. It aborts after a number of iterations without clear 
progress. With this patch, we can run our entire integration test suite without 
error. Without this patch, about 1 in SOAP 200 calls fails.
   
   The "progress measurement" logic could perhaps be simplified (only measure 
plain bytes, not also consumed encrypted bytes), but I'm not sure if that would 
still be correct.
   
   I could also imagine that the actual bug is in the ChunkDecoder, that 
doesn't give the Channel another chance at filling the buffer, but the 
SSLIOSession code looks more suspicious to me (signalling EOF with 106 bytes 
left encrypted).
   
   What do you think?
   
   This issue occurs with all versions from 4.4.4 to the 4.4.x branch.
   
   
   ---
   
   **Affects:** 4.4.4, 4.4.5, 4.4.6
   
   **Attachments:**
   - 
[httpcore-nio-SSLIOSession-eof.path](https://issues.apache.org/jira/secure/attachment/12848533/httpcore-nio-SSLIOSession-eof.path)
 (_2.52 kB_)
   
   **Backported to:** 
[4.4.7](https://github.com/apache/httpcomponents-core/milestone/44?closed=1)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to