-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(replay): Fix truncated JSON request/response bodies #59266
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 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 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
127 changes: 127 additions & 0 deletions
127
static/app/views/replays/detail/network/truncateJson/completeJson.ts
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,127 @@ | ||
| import type {JsonToken} from './constants'; | ||
| import { | ||
| ARR, | ||
| ARR_VAL, | ||
| ARR_VAL_COMPLETED, | ||
| ARR_VAL_STR, | ||
| OBJ, | ||
| OBJ_KEY, | ||
| OBJ_KEY_STR, | ||
| OBJ_VAL, | ||
| OBJ_VAL_COMPLETED, | ||
| OBJ_VAL_STR, | ||
| } from './constants'; | ||
|
|
||
| const ALLOWED_PRIMITIVES = ['true', 'false', 'null']; | ||
|
|
||
| /** | ||
| * Complete an incomplete JSON string. | ||
| * This will ensure that the last element always has a `"~~"` to indicate it was truncated. | ||
| * For example, `[1,2,` will be completed to `[1,2,"~~"]` | ||
| * and `{"aa":"b` will be completed to `{"aa":"b~~"}` | ||
| */ | ||
| export function completeJson(incompleteJson: string, stack: JsonToken[]): string { | ||
| if (!stack.length) { | ||
| return incompleteJson; | ||
| } | ||
|
|
||
| let json = incompleteJson; | ||
|
|
||
| // Most checks are only needed for the last step in the stack | ||
| const lastPos = stack.length - 1; | ||
| const lastStep = stack[lastPos]; | ||
|
|
||
| json = _fixLastStep(json, lastStep); | ||
|
|
||
| // Complete remaining steps - just add closing brackets | ||
| for (let i = lastPos; i >= 0; i--) { | ||
| const step = stack[i]; | ||
|
|
||
| // eslint-disable-next-line default-case | ||
| switch (step) { | ||
| case OBJ: | ||
| json = `${json}}`; | ||
| break; | ||
| case ARR: | ||
| json = `${json}]`; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return json; | ||
| } | ||
|
|
||
| function _fixLastStep(json: string, lastStep: JsonToken): string { | ||
| switch (lastStep) { | ||
| // Object cases | ||
| case OBJ: | ||
| return `${json}"~~":"~~"`; | ||
| case OBJ_KEY: | ||
| return `${json}:"~~"`; | ||
| case OBJ_KEY_STR: | ||
| return `${json}~~":"~~"`; | ||
| case OBJ_VAL: | ||
| return _maybeFixIncompleteObjValue(json); | ||
| case OBJ_VAL_STR: | ||
| return `${json}~~"`; | ||
| case OBJ_VAL_COMPLETED: | ||
| return `${json},"~~":"~~"`; | ||
|
|
||
| // Array cases | ||
| case ARR: | ||
| return `${json}"~~"`; | ||
| case ARR_VAL: | ||
| return _maybeFixIncompleteArrValue(json); | ||
| case ARR_VAL_STR: | ||
| return `${json}~~"`; | ||
| case ARR_VAL_COMPLETED: | ||
| return `${json},"~~"`; | ||
|
|
||
| default: | ||
| return json; | ||
| } | ||
| } | ||
|
|
||
| function _maybeFixIncompleteArrValue(json: string): string { | ||
| const pos = _findLastArrayDelimiter(json); | ||
|
|
||
| if (pos > -1) { | ||
| const part = json.slice(pos + 1); | ||
|
|
||
| if (ALLOWED_PRIMITIVES.includes(part.trim())) { | ||
| return `${json},"~~"`; | ||
| } | ||
|
|
||
| // Everything else is replaced with `"~~"` | ||
| return `${json.slice(0, pos + 1)}"~~"`; | ||
| } | ||
|
|
||
| // fallback, this shouldn't happen, to be save | ||
| return json; | ||
| } | ||
|
|
||
| function _findLastArrayDelimiter(json: string): number { | ||
| for (let i = json.length - 1; i >= 0; i--) { | ||
| const char = json[i]; | ||
|
|
||
| if (char === ',' || char === '[') { | ||
| return i; | ||
| } | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| function _maybeFixIncompleteObjValue(json: string): string { | ||
| const startPos = json.lastIndexOf(':'); | ||
|
|
||
| const part = json.slice(startPos + 1); | ||
|
|
||
| if (ALLOWED_PRIMITIVES.includes(part.trim())) { | ||
| return `${json},"~~":"~~"`; | ||
| } | ||
|
|
||
| // Everything else is replaced with `"~~"` | ||
| // This also means we do not have incomplete numbers, e.g `[1` is replaced with `["~~"]` | ||
| return `${json.slice(0, startPos + 1)}"~~"`; | ||
| } |
23 changes: 23 additions & 0 deletions
23
static/app/views/replays/detail/network/truncateJson/constants.ts
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,23 @@ | ||
| export const OBJ = 10; | ||
| export const OBJ_KEY = 11; | ||
| export const OBJ_KEY_STR = 12; | ||
| export const OBJ_VAL = 13; | ||
| export const OBJ_VAL_STR = 14; | ||
| export const OBJ_VAL_COMPLETED = 15; | ||
|
|
||
| export const ARR = 20; | ||
| export const ARR_VAL = 21; | ||
| export const ARR_VAL_STR = 22; | ||
| export const ARR_VAL_COMPLETED = 23; | ||
|
|
||
| export type JsonToken = | ||
| | typeof OBJ | ||
| | typeof OBJ_KEY | ||
| | typeof OBJ_KEY_STR | ||
| | typeof OBJ_VAL | ||
| | typeof OBJ_VAL_STR | ||
| | typeof OBJ_VAL_COMPLETED | ||
| | typeof ARR | ||
| | typeof ARR_VAL | ||
| | typeof ARR_VAL_STR | ||
| | typeof ARR_VAL_COMPLETED; |
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.
Is this exported from SDK at all?
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.
not right now! We can think about exporting this in the future. but probably this doesn't really make sense here, because we want to keep this broader - we want to keep all values that this could ever have here I think? as we also need to make sure to keep covering older versions?