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
Draft
mafredri
wants to merge
19
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
Draft
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1223d27
fix(close): send fallback close frame for invalid codes
mafredri 5427a1f
feat(close): truncate close reasons over 123 bytes
mafredri 550d8cf
fix(wasm): truncate close reasons over 123 bytes
mafredri db23bea
docs(close): limit the Close guarantee to the first call
mafredri f7d9efa
fix: stop the truncation error from claiming bytes were sent
mafredri fd190bf
docs(ws_js.go): note that invalid UTF-8 reasons can still be rejected
mafredri 052f77c
test: cover U+FFFD at the limit and truncation with a write failure
mafredri 3c08db6
fix: replace invalid UTF-8 in close reasons with U+FFFD
mafredri e9a0c93
docs(ws_js.go): document U+FFFD replacement in the Close reason
mafredri f788d53
fix: report the replaced reason length in the truncation error
mafredri 007027a
fix: put the close failure before the truncation error
mafredri 9726c86
refactor: move wireCloseReason out of errors.go
mafredri 2bd2b4a
refactor: derive maxCloseReason from maxControlPayload again
mafredri 7daec2c
docs: drop comments that repeat the Close docs
mafredri 0b09d99
docs: restore the comments that explain the close error handling
mafredri a747f21
fix: say why a truncated reason is longer than the caller's
mafredri b8f74f0
fix: return the truncation error only when Close succeeds
mafredri ec299b6
fix: encode every wireCloseError result in CloseError.bytes
mafredri ac78959
docs(close): drop the Close doc line about non-nil errors
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 a reason that can always be sent, and an error if it | ||
| // 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.