Repository navigation
fix: send a close frame for over-long reasons and invalid codes #583
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
mafredri
wants to merge
20
commits into
master
Choose a base branch
from
mafredri/close-reason-truncate
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.
+287
−44
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6dd7707
fix(close): send fallback close frame for invalid codes
mafredri 8c4402d
feat(close): truncate close reasons over 123 bytes
mafredri b9bd0b1
fix(wasm): truncate close reasons over 123 bytes
mafredri 210a167
docs(close): limit the Close guarantee to the first call
mafredri bbff1f4
fix: stop the truncation error from claiming bytes were sent
mafredri 5acf068
docs(ws_js.go): note that invalid UTF-8 reasons can still be rejected
mafredri a0ecd2c
test: cover U+FFFD at the limit and truncation with a write failure
mafredri 2c06477
fix: replace invalid UTF-8 in close reasons with U+FFFD
mafredri 8aca92c
docs(ws_js.go): document U+FFFD replacement in the Close reason
mafredri 3f004c2
fix: report the replaced reason length in the truncation error
mafredri fb47d0a
fix: put the close failure before the truncation error
mafredri a72b2e3
refactor: move wireCloseReason out of errors.go
mafredri bc0ad88
refactor: derive maxCloseReason from maxControlPayload again
mafredri 4b824e1
docs: drop comments that repeat the Close docs
mafredri e8ee102
docs: restore the comments that explain the close error handling
mafredri 75456f3
fix: say why a truncated reason is longer than the caller's
mafredri fc60274
fix: return the truncation error only when Close succeeds
mafredri 2d8a8ee
fix: encode every wireCloseError result in CloseError.bytes
mafredri 04eeb2b
docs(close): drop the Close doc line about non-nil errors
mafredri 5fdd32d
fix: stop the invalid code error from claiming a frame was sent
mafredri 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,33 @@ | ||
| package websocket | ||
|
mafredri marked this conversation as resolved.
|
||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "unicode/utf8" | ||
| ) | ||
|
|
||
| // maxControlPayload is the maximum length of a control frame payload. | ||
| // See https://tools.ietf.org/html/rfc6455#section-5.5. | ||
| const maxControlPayload = 125 | ||
|
|
||
| // maxCloseReason is maxControlPayload minus the 2-byte status code. | ||
| const maxCloseReason = maxControlPayload - 2 | ||
|
|
||
| // wireCloseReason returns the reason to send, also when the error is non-nil. | ||
| // The error reports a truncated reason. | ||
| func wireCloseReason(reason string) (string, error) { | ||
| // RFC 6455 section 5.5.1 requires a UTF-8 reason. | ||
| valid := strings.ToValidUTF8(reason, "\uFFFD") | ||
| if len(valid) <= maxCloseReason { | ||
| return valid, nil | ||
| } | ||
| n := maxCloseReason | ||
| for !utf8.RuneStart(valid[n]) { | ||
| n-- | ||
| } | ||
| var replaced string | ||
| if len(valid) != len(reason) { | ||
| replaced = " after replacing invalid UTF-8" | ||
| } | ||
| return valid[:n], fmt.Errorf("%w from %d to %d bytes%s", ErrCloseReasonTruncated, len(valid), n, replaced) | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.