-
Notifications
You must be signed in to change notification settings - Fork 784
fix(server): bound client-facing SSE frame retention #1241
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
92330b5
fix(server): add bounded client SSE frame buffer
Wibias 5230c3c
fix(server): bound WebSocket SSE frame retention
Wibias aaaa4f3
fix(server): bound HTTP SSE terminal framing
Wibias dfe511b
test(server): cover client SSE frame bounds
Wibias 40a6e5b
fix(server): widen typed-array tail storage
Wibias ac938a2
test(server): preserve raw split UTF-8 bytes
Wibias 6d4c16a
fix(server): bound SSE framer allocation count
Wibias 7ba0225
fix(server): bound inspector allocation count
Wibias c78de5a
fix(server): bound SSE frame-count amplification
Wibias af5c157
fix(server): release WS reader on framing errors
Wibias b9396b4
fix(server): harden SSE failure cleanup
Wibias 3e4725d
test(server): cover SSE framing failure paths
Wibias b0f79aa
chore(server): remove no-op WS expression
Wibias e348caa
fix(server): widen failed-tail typed array
Wibias 17dfaf8
fix(ws): preserve dropped-send rejection semantics
Wibias 73b30b0
test(ws): preserve dropped-send rejection contract
Wibias 33ac593
fix(sse): honour terminal before trailing framing errors
Wibias 97e3c87
test(sse): keep committed terminal ahead of trailing overflow
Wibias e1ff3b9
docs(proxy): document Responses SSE frame limit
Wibias 65bea21
docs(proxy): sync Japanese SSE frame limit
Wibias 49dabd9
docs(proxy): sync Korean SSE frame limit
Wibias 1f47d21
docs(proxy): sync Russian SSE frame limit
Wibias 55d1340
docs(proxy): sync Chinese SSE frame limit
Wibias 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,209 @@ | ||
| export const MAX_CLIENT_SSE_FRAME_BYTES = 4 * 1024 * 1024; | ||
|
Wibias marked this conversation as resolved.
|
||
|
|
||
| export class SseFrameTooLargeError extends Error { | ||
| readonly maxBytes: number; | ||
|
|
||
| constructor(maxBytes: number) { | ||
| super(`upstream SSE frame exceeded ${maxBytes} bytes`); | ||
| this.name = "SseFrameTooLargeError"; | ||
| this.maxBytes = maxBytes; | ||
| } | ||
| } | ||
|
|
||
| export type BoundedSseFrame = { | ||
| block: Uint8Array; | ||
| delimiter: Uint8Array; | ||
| }; | ||
|
|
||
| function delimiterLengthAt( | ||
| index: number, | ||
| length: number, | ||
| byteAt: (index: number) => number, | ||
| ): number | 0 | undefined { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| const first = byteAt(index); | ||
| if (first === 10) { | ||
| if (index + 1 >= length) return undefined; | ||
| const second = byteAt(index + 1); | ||
| if (second === 10) return 2; | ||
| if (second !== 13) return 0; | ||
| if (index + 2 >= length) return undefined; | ||
| return byteAt(index + 2) === 10 ? 3 : 0; | ||
| } | ||
| if (first !== 13) return 0; | ||
| if (index + 1 >= length) return undefined; | ||
| if (byteAt(index + 1) !== 10) return 0; | ||
| if (index + 2 >= length) return undefined; | ||
| const third = byteAt(index + 2); | ||
| if (third === 10) return 3; | ||
| if (third !== 13) return 0; | ||
| if (index + 3 >= length) return undefined; | ||
| return byteAt(index + 3) === 10 ? 4 : 0; | ||
| } | ||
|
|
||
| function copyRange( | ||
| start: number, | ||
| end: number, | ||
| tailLength: number, | ||
| previousTail: Uint8Array, | ||
| chunk: Uint8Array, | ||
| ): Uint8Array { | ||
| const out = new Uint8Array(end - start); | ||
| for (let index = start; index < end; index += 1) { | ||
| out[index - start] = index < tailLength | ||
| ? previousTail[index]! | ||
| : chunk[index - tailLength]!; | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * Byte-bounded SSE block framer for client-facing protocol paths. | ||
| * | ||
| * The delimiter scanner works on raw bytes, so fragmented UTF-8 cannot change | ||
| * accounting and a hostile upstream cannot grow an unterminated JS string | ||
| * without limit. Candidate bytes live in one geometrically grown buffer rather | ||
| * than one allocation per upstream chunk, bounding both bytes and object count. | ||
| * Complete blocks are returned without their delimiter; the exact delimiter | ||
| * bytes are returned separately so callers can relay bytes unchanged. | ||
| */ | ||
| export class BoundedSseFrameBuffer { | ||
| private readonly maxFrameBytes: number; | ||
| private delimiterTail: Uint8Array = new Uint8Array(0); | ||
| private candidate: Uint8Array = new Uint8Array(0); | ||
| private candidateBytes = 0; | ||
| private disposed = false; | ||
|
|
||
| constructor(maxFrameBytes = MAX_CLIENT_SSE_FRAME_BYTES) { | ||
| if (!Number.isSafeInteger(maxFrameBytes) || maxFrameBytes <= 0) { | ||
| throw new RangeError("maxFrameBytes must be a positive safe integer"); | ||
| } | ||
| this.maxFrameBytes = maxFrameBytes; | ||
| } | ||
|
|
||
| private clear(): void { | ||
| this.delimiterTail = new Uint8Array(0); | ||
| this.candidate = new Uint8Array(0); | ||
| this.candidateBytes = 0; | ||
| } | ||
|
|
||
| private ensureCapacity(requiredBytes: number): void { | ||
| if (this.candidate.byteLength >= requiredBytes) return; | ||
| let capacity = this.candidate.byteLength === 0 | ||
| ? Math.min(this.maxFrameBytes, Math.max(requiredBytes, 4096)) | ||
| : this.candidate.byteLength; | ||
| while (capacity < requiredBytes) { | ||
| capacity = Math.min(this.maxFrameBytes, Math.max(requiredBytes, capacity * 2)); | ||
| } | ||
| const grown = new Uint8Array(capacity); | ||
| if (this.candidateBytes > 0) { | ||
| grown.set(this.candidate.subarray(0, this.candidateBytes)); | ||
| } | ||
| this.candidate = grown; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| private retain(slice: Uint8Array): void { | ||
| if (slice.byteLength === 0) return; | ||
| const nextBytes = this.candidateBytes + slice.byteLength; | ||
| if (nextBytes > this.maxFrameBytes) { | ||
| this.clear(); | ||
| this.disposed = true; | ||
| throw new SseFrameTooLargeError(this.maxFrameBytes); | ||
| } | ||
| this.ensureCapacity(nextBytes); | ||
| this.candidate.set(slice, this.candidateBytes); | ||
| this.candidateBytes = nextBytes; | ||
| } | ||
|
|
||
| private takeCandidate(): Uint8Array { | ||
| if (this.candidateBytes === 0) return new Uint8Array(0); | ||
| const block = this.candidate.slice(0, this.candidateBytes); | ||
| this.candidate = new Uint8Array(0); | ||
| this.candidateBytes = 0; | ||
| return block; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| feed(chunk: Uint8Array): BoundedSseFrame[] { | ||
| if (this.disposed) return []; | ||
| if (chunk.byteLength === 0) return []; | ||
|
|
||
| const frames: BoundedSseFrame[] = []; | ||
| const previousTail = this.delimiterTail; | ||
| this.delimiterTail = new Uint8Array(0); | ||
| const tailLength = previousTail.byteLength; | ||
| const totalLength = tailLength + chunk.byteLength; | ||
| const byteAt = (index: number): number => index < tailLength | ||
| ? previousTail[index]! | ||
| : chunk[index - tailLength]!; | ||
| const retainRange = (start: number, end: number): void => { | ||
| if (end <= start) return; | ||
| if (start < tailLength) { | ||
| this.retain(previousTail.subarray(start, Math.min(end, tailLength))); | ||
| } | ||
| if (end > tailLength) { | ||
| this.retain(chunk.subarray(Math.max(0, start - tailLength), end - tailLength)); | ||
| } | ||
| }; | ||
|
|
||
| let index = 0; | ||
| let retainedThrough = 0; | ||
| while (index < totalLength) { | ||
| const delimiterLength = delimiterLengthAt(index, totalLength, byteAt); | ||
| if (delimiterLength === undefined) break; | ||
| if (delimiterLength > 0) { | ||
| retainRange(retainedThrough, index); | ||
| const block = this.takeCandidate(); | ||
| const delimiter = copyRange( | ||
| index, | ||
| index + delimiterLength, | ||
| tailLength, | ||
| previousTail, | ||
| chunk, | ||
| ); | ||
| frames.push({ block, delimiter }); | ||
| index += delimiterLength; | ||
| retainedThrough = index; | ||
| continue; | ||
| } | ||
| index += 1; | ||
| } | ||
|
|
||
| retainRange(retainedThrough, index); | ||
| if (index < totalLength) { | ||
| this.delimiterTail = copyRange(index, totalLength, tailLength, previousTail, chunk); | ||
| } | ||
| return frames; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** Return the final unterminated block bytes and release all retained state. */ | ||
| finish(): Uint8Array { | ||
| if (this.disposed) return new Uint8Array(0); | ||
| try { | ||
| this.retain(this.delimiterTail); | ||
| this.delimiterTail = new Uint8Array(0); | ||
| return this.takeCandidate(); | ||
| } finally { | ||
| this.clear(); | ||
| this.disposed = true; | ||
| } | ||
| } | ||
|
|
||
| dispose(): void { | ||
| if (this.disposed) return; | ||
| this.clear(); | ||
| this.disposed = true; | ||
| } | ||
| } | ||
|
|
||
| export function joinSseFrameBytes(parts: readonly Uint8Array[]): Uint8Array { | ||
| let byteLength = 0; | ||
| for (const part of parts) byteLength += part.byteLength; | ||
| if (byteLength === 0) return new Uint8Array(0); | ||
| if (parts.length === 1 && parts[0]!.byteLength === byteLength) return parts[0]!; | ||
| const joined = new Uint8Array(byteLength); | ||
| let offset = 0; | ||
| for (const part of parts) { | ||
| joined.set(part, offset); | ||
| offset += part.byteLength; | ||
| } | ||
| return joined; | ||
| } | ||
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.