Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/tools/serialisation/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface SanitizedEvent extends Context {

// The maximum size of a single event is 256KiB. By default, we ensure that user-provided data
// going through sanitize fits inside our events, while leaving room for other contexts, metadata, ...
const SANITIZE_DEFAULT_MAX_CHARACTER_COUNT = 220 * ONE_KIBI_BYTE
export const SANITIZE_DEFAULT_MAX_CHARACTER_COUNT = 220 * ONE_KIBI_BYTE

// Symbol for the root element of the JSONPath used for visited objects
const JSON_PATH_ROOT_ELEMENT = '$'
Expand Down
34 changes: 33 additions & 1 deletion packages/rum-core/src/domain/limitModification.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context } from '@datadog/browser-core'
import { objectEntries } from '@datadog/browser-core'
import { display, noop, objectEntries, SANITIZE_DEFAULT_MAX_CHARACTER_COUNT } from '@datadog/browser-core'
import type { ModifiableFieldPaths } from './limitModification'
import { limitModification } from './limitModification'

Expand Down Expand Up @@ -194,6 +194,38 @@ describe('limitModification', () => {
limitModification(object, { bar: 'object' }, modifier)
expect(() => JSON.stringify(object)).not.toThrowError()
})

it("should not reset unsafe literal fields that the user didn't alter", () => {
spyOn(display, 'warn')
const wayTooLongUrl = `/${'a'.repeat(SANITIZE_DEFAULT_MAX_CHARACTER_COUNT + 1)}`
const object: Context = { resource: { url: wayTooLongUrl } }
const modifier = noop
limitModification(object, { 'resource.url': 'string' }, modifier)
expect(object).toEqual({ resource: { url: wayTooLongUrl } })
})

it('should sanitize object fields (fieldType "object") even with a noop modifier', () => {
spyOn(display, 'warn')
const wayTooLongUrl = `/${'a'.repeat(SANITIZE_DEFAULT_MAX_CHARACTER_COUNT + 1)}`
const object: Context = {
context: {
response: {
status: 200,
url: wayTooLongUrl,
},
},
}
const modifier = noop
limitModification(object, { context: 'object' }, modifier)
expect(object).toEqual({
context: {
response: {
status: 200,
},
},
})
expect(display.warn).toHaveBeenCalled()
})
})

function generateModifiableFieldPathsFrom(object: Record<string, string | object>) {
Expand Down
8 changes: 8 additions & 0 deletions packages/rum-core/src/domain/limitModification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export type ModifiableFieldPaths = Record<string, 'string' | 'object'>
* Allows declaring and enforcing modifications to specific fields of an object.
* Only supports modifying properties of an object (even if nested in an array).
* Does not support array manipulation (adding/removing items).
*
* Modifications of the object are sanitized only if the field was actually changed by the modifier function (i.e., value is different).
* This ensures consistent SDK behavior regardless of whether limitModification is called.
* Only string fields are handled this way, object fields are sanitized regardless of whether the modifier function was called or not.
*/
export function limitModification<T extends Context, Result>(
object: T,
Expand Down Expand Up @@ -52,6 +56,10 @@ function setNestedValue(
value: unknown,
fieldType: 'string' | 'object'
) {
if (object[field] === value) {
return
}

const newType = getType(value)

if (newType === fieldType) {
Expand Down
Loading