-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(tools): bound internal request serialization #7639
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
BillLeoutsakosvl346
wants to merge
2
commits into
staging
Choose a base branch
from
fix/platform-request-admission
base: staging
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,68 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { PayloadSizeLimitError } from '@/lib/core/utils/stream-limits' | ||
| import { stringifyRequestWithinLimit } from '@/tools/request-body-size.server' | ||
|
|
||
| describe('stringifyRequestWithinLimit', () => { | ||
| it.each([ | ||
| { 'escaped"key\n': '\u0000é😀\ud800', empty: {}, array: [1, null, true] }, | ||
| { missing: undefined, array: [undefined, () => 1, Symbol('omitted'), Number.NaN] }, | ||
| { date: new Date('2026-01-01T00:00:00Z'), boxed: [Object(1), Object('é'), Object(false)] }, | ||
| ])('preserves native JSON and its exact UTF-8 limit for %j', (value) => { | ||
| const expected = JSON.stringify(value) | ||
| const bytes = Buffer.byteLength(expected, 'utf8') | ||
| expect(stringifyRequestWithinLimit(value, bytes)).toBe(expected) | ||
| expect(() => stringifyRequestWithinLimit(value, bytes - 1)).toThrow(PayloadSizeLimitError) | ||
| }) | ||
|
|
||
| it('invokes getters, toJSON and boxed conversions only once', () => { | ||
| const getter = vi.fn(() => ({ toJSON })) | ||
| const toJSON = vi.fn(() => 'value') | ||
| const numberConversion = vi.fn(() => 3) | ||
| const stringConversion = vi.fn(() => 'text') | ||
| const value = { | ||
| get data() { | ||
| return getter() | ||
| }, | ||
| number: Object.assign(Object(1), { [Symbol.toPrimitive]: numberConversion }), | ||
| string: Object.assign(Object('original'), { [Symbol.toPrimitive]: stringConversion }), | ||
| } | ||
|
|
||
| expect(stringifyRequestWithinLimit(value, 100)).toBe( | ||
| '{"data":"value","number":3,"string":"text"}' | ||
| ) | ||
| for (const hook of [getter, toJSON, numberConversion, stringConversion]) { | ||
| expect(hook).toHaveBeenCalledTimes(1) | ||
| } | ||
| }) | ||
|
|
||
| it('stops native traversal before later getters on oversized strings', () => { | ||
| const later = vi.fn() | ||
| const value = { | ||
| large: '\u0000'.repeat(100), | ||
| get later() { | ||
| return later() | ||
| }, | ||
| } | ||
|
|
||
| expect(() => stringifyRequestWithinLimit(value, 50)).toThrow(PayloadSizeLimitError) | ||
| expect(later).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('preserves native omissions, shared objects, cycle errors and bigint conversion errors', () => { | ||
| const shared = { a: 1 } | ||
| expect(stringifyRequestWithinLimit([shared, shared], 100)).toBe( | ||
| JSON.stringify([shared, shared]) | ||
| ) | ||
| expect(stringifyRequestWithinLimit(undefined, 100)).toBeUndefined() | ||
| const cycle: { self?: unknown } = {} | ||
| cycle.self = cycle | ||
| expect(() => stringifyRequestWithinLimit(cycle, 100)).toThrow(TypeError) | ||
| const number = Object.assign(Object(1), { [Symbol.toPrimitive]: () => 1n }) | ||
| expect(() => JSON.stringify(number)).toThrow(TypeError) | ||
| expect(() => stringifyRequestWithinLimit(number, 100)).toThrow(TypeError) | ||
| expect(() => stringifyRequestWithinLimit(1n, 100)).toThrow(TypeError) | ||
| }) | ||
| }) |
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,88 @@ | ||
| import { types } from 'node:util' | ||
| import { assertKnownSizeWithinLimit } from '@/lib/core/utils/stream-limits' | ||
|
|
||
| /** | ||
| * Uses native JSON traversal with an incremental UTF-8 budget, stopping before | ||
| * the complete oversized JSON string is allocated. Getters and toJSON retain | ||
| * native invocation semantics; allocations inside those hooks are not bounded. | ||
| */ | ||
| export function stringifyRequestWithinLimit(value: unknown, maxBytes: number): string | undefined { | ||
| let bytes = 0 | ||
| const containers = new WeakMap<object, number>() | ||
| const charge = (size: number): void => { | ||
| bytes += size | ||
| assertKnownSizeWithinLimit(bytes, maxBytes, 'Request body') | ||
| } | ||
| const chargeString = (text: string): void => { | ||
| charge(2) | ||
| for (let index = 0; index < text.length; index++) { | ||
| const code = text.charCodeAt(index) | ||
| if (code === 0x22 || code === 0x5c) { | ||
| charge(2) | ||
| } else if (code < 0x20) { | ||
| charge(code === 8 || code === 9 || code === 10 || code === 12 || code === 13 ? 2 : 6) | ||
| } else if (code < 0x80) { | ||
| charge(1) | ||
| } else if (code < 0x800) { | ||
| charge(2) | ||
| } else if (code >= 0xd800 && code <= 0xdfff) { | ||
| const next = text.charCodeAt(index + 1) | ||
| if (code <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) { | ||
| charge(4) | ||
| index++ | ||
| } else { | ||
| charge(6) | ||
| } | ||
| } else { | ||
| charge(3) | ||
| } | ||
| } | ||
| } | ||
| const nativeJson = JSON as typeof JSON & { | ||
| isRawJSON?: (input: unknown) => input is { rawJSON: string } | ||
| } | ||
|
|
||
| const serialized = JSON.stringify(value, function (this: object, key, input: unknown) { | ||
| /** Native stringify unboxes these after calling the replacer. */ | ||
| let current = input | ||
| if (types.isNumberObject(current)) current = +current | ||
| else if (types.isStringObject(current)) current = String(current) | ||
| else if (types.isBooleanObject(current)) current = Boolean.prototype.valueOf.call(current) | ||
| else if (types.isBigIntObject(current)) current = BigInt.prototype.valueOf.call(current) | ||
|
|
||
| const arrayItem = Array.isArray(this) | ||
| const omitted = | ||
| current === undefined || typeof current === 'function' || typeof current === 'symbol' | ||
| if (omitted && !arrayItem) return current | ||
|
|
||
| const entries = containers.get(this) | ||
| if (entries !== undefined) { | ||
| if (entries > 0) charge(1) | ||
| if (!arrayItem) { | ||
| chargeString(key) | ||
| charge(1) | ||
| } | ||
| containers.set(this, entries + 1) | ||
| } | ||
|
|
||
| if (omitted || current === null) charge(4) | ||
| else if (typeof current === 'string') chargeString(current) | ||
| else if (typeof current === 'number') | ||
| charge(Number.isFinite(current) ? String(current).length : 4) | ||
| else if (typeof current === 'boolean') charge(current ? 4 : 5) | ||
| else if (typeof current === 'object') { | ||
| if (nativeJson.isRawJSON?.(current)) { | ||
| charge(Buffer.byteLength(current.rawJSON, 'utf8')) | ||
| } else { | ||
| charge(2) | ||
| containers.set(current, 0) | ||
| } | ||
| } | ||
| return current | ||
| }) | ||
|
|
||
| if (serialized !== undefined) { | ||
| assertKnownSizeWithinLimit(Buffer.byteLength(serialized, 'utf8'), maxBytes, 'Request body') | ||
| } | ||
| return serialized | ||
| } |
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.
Oversized internal-operation inputs now skip the structured log fields that recorded the observed body size and configured limit. The generic execution error is still logged, but without these measurements, request-limit incidents are harder to diagnose and quantify. Please preserve equivalent size-limit telemetry in the new bounded serialization path.
Knowledge Base Used: Integrations, connectors, and tools
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!