-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Discard trailing data after a deflated message #8610
Open
swankjesse
wants to merge
1
commit into
master
Choose a base branch
from
jwilson.1206.deflater
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.
+62
−8
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 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 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 |
---|---|---|
|
@@ -19,12 +19,15 @@ import assertk.assertThat | |
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isLessThan | ||
import java.io.EOFException | ||
import java.util.zip.Deflater | ||
import kotlin.test.assertFailsWith | ||
import okhttp3.TestUtil.fragmentBuffer | ||
import okio.Buffer | ||
import okio.ByteString | ||
import okio.ByteString.Companion.decodeHex | ||
import okio.ByteString.Companion.encodeUtf8 | ||
import okio.DeflaterSink | ||
import okio.use | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class MessageDeflaterInflaterTest { | ||
|
@@ -136,6 +139,41 @@ internal class MessageDeflaterInflaterTest { | |
assertThat(buffer.readUtf8()).isEqualTo("Hello inflation!") | ||
} | ||
|
||
/** | ||
* It's possible a self-terminating deflated message will contain trailing data that won't be | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth tying this back to the padding bytes in the spec? |
||
* processed during inflation. If this happens, we need to either reject the message or discard | ||
* the unreachable data. We choose to discard it! | ||
* | ||
* In practice this could happen if the encoder doesn't strip the [0x00, 0x00, 0xff, 0xff] suffix | ||
* and that ends up repeated. | ||
* | ||
* https://github.com/square/okhttp/issues/8551 | ||
*/ | ||
@Test | ||
fun `deflated data has too many bytes`() { | ||
val inflater = MessageInflater(true) | ||
val buffer = Buffer() | ||
|
||
val message1 = "hello".encodeUtf8() | ||
val message2 = "hello 2".encodeUtf8() | ||
|
||
DeflaterSink(buffer, Deflater(Deflater.DEFAULT_COMPRESSION, true)).use { sink -> | ||
sink.write(Buffer().write(message1), message1.size.toLong()) | ||
} | ||
buffer.writeByte(0x00) | ||
// Trailing data. We use the Okio segment size to make sure it's still in the input buffer. | ||
buffer.write(ByteArray(8192)) | ||
inflater.inflate(buffer) | ||
assertThat(buffer.readByteString()).isEqualTo(message1) | ||
|
||
DeflaterSink(buffer, Deflater(Deflater.DEFAULT_COMPRESSION, true)).use { sink -> | ||
sink.write(Buffer().write(message2), message2.size.toLong()) | ||
} | ||
buffer.writeByte(0x00) | ||
inflater.inflate(buffer) | ||
assertThat(buffer.readByteString()).isEqualTo(message2) | ||
} | ||
|
||
private fun MessageDeflater.deflate(byteString: ByteString): ByteString { | ||
val buffer = Buffer() | ||
buffer.write(byteString) | ||
|
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.
I’m gonna claim it’s really weird if you have a stream-terminating message followed by another message and
noContextTakeover = false
. But perhaps I should write a test for this? I’m not exactly sure what reasonable behavior is.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.
Also - I think the server in the case we’re addressing here is probably buggy. We might be better to follow @Fischiii’s lead and just reject the bogus data.
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.
I think that was my change to make his PR pass
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.
@swankjesse @yschimke in case it helps. The server used is a Kestrel (.net version 7) with the WebsocketSharp.Standard2 Framework for the WebwSocket connection. Data transferred is binary form Protobuffer.