8000 fix(replay): Ensure we still truncate large bodies if they are failed… · jchatard/sentry-javascript@390faf3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 390faf3

Browse files
authored
fix(replay): Ensure we still truncate large bodies if they are failed JSON (getsentry#7923)
1 parent 1d5430e commit 390faf3

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/replay/src/coreHandlers/util/networkUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ function normalizeNetworkBody(body: string | undefined): {
201201
};
202202
} catch {
203203
return {
204-
body,
205-
warnings: ['INVALID_JSON'],
204+
body: exceedsSizeLimit ? `${body.slice(0, NETWORK_BODY_MAX_SIZE)}…` : body,
205+
warnings: exceedsSizeLimit ? ['INVALID_JSON', 'TEXT_TRUNCATED'] : ['INVALID_JSON'],
206206
};
207207
}
208208
}

packages/replay/test/unit/coreHandlers/util/networkUtils.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TextEncoder } from 'util';
22

3+
import { NETWORK_BODY_MAX_SIZE } from '../../../../src/constants';
34
import {
45
buildNetworkRequestOrResponse,
56
getBodySize,
@@ -186,5 +187,36 @@ describe('Unit | coreHandlers | util | networkUtils', () => {
186187

187188
expect(actual).toEqual({ size: 1, headers: {}, body: expectedBody, _meta: expectedMeta });
188189
});
190+
191+
it.each([
192+
[
193+
'large JSON string',
194+
JSON.stringify({
195+
aa: 'a'.repeat(NETWORK_BODY_MAX_SIZE + 10),
196+
}),
197+
{
198+
aa: `${'a'.repeat(NETWORK_BODY_MAX_SIZE - 7)}~~`,
199+
},
200+
{ warnings: ['JSON_TRUNCATED'] },
201+
],
202+
[
203+
'large plain string',
204+
'a'.repeat(NETWORK_BODY_MAX_SIZE + 10),
205+
`${'a'.repeat(NETWORK_BODY_MAX_SIZE)}…`,
206+
{ warnings: ['TEXT_TRUNCATED'] },
207+
],
208+
[
209+
'large invalid JSON string',
210+
`{--${JSON.stringify({
211+
aa: 'a'.repeat(NETWORK_BODY_MAX_SIZE + 10),
212+
})}`,
213+
`{--{"aa":"${'a'.repeat(NETWORK_BODY_MAX_SIZE - 10)}…`,
214+
{ warnings: ['INVALID_JSON', 'TEXT_TRUNCATED'] },
215+
],
216+
])('works with %s', (label, input, expectedBody, expectedMeta) => {
217+
const actual = buildNetworkRequestOrResponse({}, 1, input);
218+
219+
expect(actual).toEqual({ size: 1, headers: {}, body: expectedBody, _meta: expectedMeta });
220+
});
189221
});
190222
});

0 commit comments

Comments
 (0)
0