Skip to content
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

👷 [RUMF-1470] Enable sanitize for user-provided data #2175

Merged
merged 3 commits into from
Apr 24, 2023
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
9 changes: 2 additions & 7 deletions packages/core/src/domain/console/consoleObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { mergeObservables, Observable } from '../../tools/observable'
import { ConsoleApiName } from '../../tools/display'
import { callMonitored } from '../../tools/monitor'
import { sanitize } from '../../tools/serialisation/sanitize'
import { ExperimentalFeature, isExperimentalFeatureEnabled } from '../../tools/experimentalFeatures'
import { find } from '../../tools/utils/polyfills'
import { jsonStringify } from '../../tools/serialisation/jsonStringify'

Expand Down Expand Up @@ -71,14 +70,10 @@ function buildConsoleLog(params: unknown[], api: ConsoleApiName, handlingStack:

function formatConsoleParameters(param: unknown) {
if (typeof param === 'string') {
return isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS) ? sanitize(param) : param
return sanitize(param)
}
if (param instanceof Error) {
return formatErrorMessage(computeStackTrace(param))
}
return jsonStringify(
isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS) ? sanitize(param) : param,
undefined,
2
)
return jsonStringify(sanitize(param), undefined, 2)
}
26 changes: 26 additions & 0 deletions packages/core/src/domain/error/error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ describe('computeRawError', () => {
expect(causes[1].type).toEqual(deepNestedError.name)
expect(causes[1].stack).toContain('Error: fiz: buz')
})

it('should propagate the original error without modifications', () => {
const error = { description: 'syntax error' }
const stackTrace: StackTrace = {
message: 'some syntax message',
name: 'SyntaxError',
stack: [],
}

const formattedWithoutStackTrace = computeRawError({
...DEFAULT_RAW_ERROR_PARAMS,
stackTrace: NOT_COMPUTED_STACK_TRACE,
originalError: error,
handling: ErrorHandling.HANDLED,
})

const formattedWithStackTrace = computeRawError({
...DEFAULT_RAW_ERROR_PARAMS,
stackTrace,
originalError: error,
handling: ErrorHandling.HANDLED,
})

expect(formattedWithStackTrace.originalError).toBe(error)
expect(formattedWithoutStackTrace.originalError).toBe(error)
})
})

describe('getFileFromStackTraceString', () => {
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/domain/error/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { StackTrace } from '../tracekit'
import { computeStackTrace } from '../tracekit'
import { ExperimentalFeature, isExperimentalFeatureEnabled } from '../../tools/experimentalFeatures'
import { callMonitored } from '../../tools/monitor'
import { sanitize } from '../../tools/serialisation/sanitize'
import type { ClocksState } from '../../tools/utils/timeUtils'
Expand Down Expand Up @@ -31,15 +30,12 @@ export function computeRawError({
handling,
}: RawErrorParams): RawError {
if (!stackTrace || (stackTrace.message === undefined && !(originalError instanceof Error))) {
const sanitizedError = isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS)
? sanitize(originalError)
: originalError
return {
startClocks,
source,
handling,
originalError: sanitizedError,
message: `${nonErrorPrefix} ${jsonStringify(sanitizedError)!}`,
originalError,
message: `${nonErrorPrefix} ${jsonStringify(sanitize(originalError))!}`,
stack: NO_ERROR_STACK_PRESENT_MESSAGE,
handlingStack,
type: stackTrace && stackTrace.name,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/tools/experimentalFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export enum ExperimentalFeature {
FEATURE_FLAGS = 'feature_flags',
RESOURCE_PAGE_STATES = 'resource_page_states',
COLLECT_FLUSH_REASON = 'collect_flush_reason',
SANITIZE_INPUTS = 'sanitize_inputs',
}

const enabledExperimentalFeatures: Set<ExperimentalFeature> = new Set()
Expand Down
9 changes: 2 additions & 7 deletions packages/core/src/tools/serialisation/contextManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ExperimentalFeature, isExperimentalFeatureEnabled } from '../experimentalFeatures'
import { computeBytesCount } from '../utils/byteUtils'
import { throttle } from '../utils/functionUtils'
import { deepClone } from '../mergeInto'
Expand Down Expand Up @@ -52,16 +51,12 @@ export function createContextManager(customerDataType: CustomerDataType, compute
getContext: () => deepClone(context),

setContext: (newContext: Context) => {
context = isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS)
? sanitize(newContext)
: deepClone(newContext)
context = sanitize(newContext)
computeBytesCountThrottled(context)
},

setContextProperty: (key: string, property: any) => {
context[key] = isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS)
? sanitize(property)
: deepClone(property)
context[key] = sanitize(property)
computeBytesCountThrottled(context)
},

Expand Down
8 changes: 2 additions & 6 deletions packages/logs/src/boot/logsPublicApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { Context, InitConfiguration, User } from '@datadog/browser-core'
import {
ExperimentalFeature,
isExperimentalFeatureEnabled,
CustomerDataType,
assign,
BoundedBuffer,
Expand Down Expand Up @@ -122,12 +120,10 @@ export function makeLogsPublicApi(startLogsImpl: StartLogs) {
createLogger: monitor((name: string, conf: LoggerConfiguration = {}) => {
customLoggers[name] = new Logger(
(...params) => handleLogStrategy(...params),
isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS) ? sanitize(name) : name,
sanitize(name),
conf.handler,
conf.level,
isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS)
? (sanitize(conf.context) as object)
: conf.context
sanitize(conf.context) as object
)

return customLoggers[name]!
Expand Down
11 changes: 2 additions & 9 deletions packages/logs/src/domain/logger.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { Context } from '@datadog/browser-core'
import {
ExperimentalFeature,
isExperimentalFeatureEnabled,
clocksNow,
computeRawError,
ErrorHandling,
computeStackTrace,
CustomerDataType,
deepClone,
assign,
combine,
createContextManager,
Expand Down Expand Up @@ -84,19 +81,15 @@ export class Logger {
}
}

const sanitizedMessageContext = (
isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS)
? sanitize(messageContext)
: deepClone(messageContext)
) as Context
const sanitizedMessageContext = sanitize(messageContext) as Context

const context = errorContext
? (combine({ error: errorContext }, sanitizedMessageContext) as Context)
: sanitizedMessageContext

this.handleLogStrategy(
{
message: isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS) ? sanitize(message)! : message,
message: sanitize(message)!,
context,
status,
},
Expand Down
22 changes: 5 additions & 17 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Context, InitConfiguration, TimeStamp, RelativeTime, User } from '@datadog/browser-core'
import {
ExperimentalFeature,
noop,
isExperimentalFeatureEnabled,
CustomerDataType,
willSyntheticsInjectRum,
assign,
Expand Down Expand Up @@ -213,10 +211,8 @@ export function makeRumPublicApi(

addAction: monitor((name: string, context?: object) => {
addActionStrategy({
name: isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS) ? sanitize(name)! : name,
context: (isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS)
? sanitize(context)
: deepClone(context)) as Context,
name: sanitize(name)!,
context: sanitize(context) as Context,
startClocks: clocksNow(),
type: ActionType.CUSTOM,
})
Expand All @@ -228,19 +224,14 @@ export function makeRumPublicApi(
addErrorStrategy({
error, // Do not sanitize error here, it is needed unserialized by computeRawError()
handlingStack,
context: (isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS)
? sanitize(context)
: deepClone(context)) as Context,
context: sanitize(context) as Context,
startClocks: clocksNow(),
})
})
},

addTiming: monitor((name: string, time?: number) => {
addTimingStrategy(
isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS) ? sanitize(name)! : name,
time as RelativeTime | TimeStamp | undefined
)
addTimingStrategy(sanitize(name)!, time as RelativeTime | TimeStamp | undefined)
}),

setUser: monitor((newUser: User) => {
Expand Down Expand Up @@ -275,10 +266,7 @@ export function makeRumPublicApi(
* This feature is currently in beta. For more information see the full [feature flag tracking guide](https://docs.datadoghq.com/real_user_monitoring/feature_flag_tracking/).
*/
addFeatureFlagEvaluation: monitor((key: string, value: any) => {
addFeatureFlagEvaluationStrategy(
isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS) ? sanitize(key)! : key,
isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS) ? sanitize(value) : value
)
addFeatureFlagEvaluationStrategy(sanitize(key)!, sanitize(value))
}),
getSessionReplayLink: monitor(() => getSessionReplayLinkStrategy()),
})
Expand Down
3 changes: 0 additions & 3 deletions packages/rum-core/src/domain/limitModification.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ExperimentalFeature, resetExperimentalFeatures, addExperimentalFeatures } from '@datadog/browser-core'
import type { Context } from '@datadog/browser-core'
import { limitModification } from './limitModification'

Expand Down Expand Up @@ -158,7 +157,6 @@ describe('limitModification', () => {
})

it('should call sanitize on newly provided values', () => {
addExperimentalFeatures([ExperimentalFeature.SANITIZE_INPUTS])
const object: Context = { bar: { baz: 42 } }

const modifier = (candidate: any) => {
Expand All @@ -167,6 +165,5 @@ describe('limitModification', () => {

limitModification(object, ['bar'], modifier)
expect(() => JSON.stringify(object)).not.toThrowError()
resetExperimentalFeatures()
})
})
8 changes: 2 additions & 6 deletions packages/rum-core/src/domain/limitModification.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExperimentalFeature, isExperimentalFeatureEnabled, sanitize, deepClone, getType } from '@datadog/browser-core'
import { sanitize, deepClone, getType } from '@datadog/browser-core'
import type { Context } from '@datadog/browser-core'

/**
Expand All @@ -18,11 +18,7 @@ export function limitModification<T extends Context, Result>(
const originalType = getType(originalValue)
const newType = getType(newValue)
if (newType === originalType) {
set(
object,
path,
isExperimentalFeatureEnabled(ExperimentalFeature.SANITIZE_INPUTS) ? sanitize(newValue) : newValue
)
set(object, path, sanitize(newValue))
} else if (originalType === 'object' && (newType === 'undefined' || newType === 'null')) {
set(object, path, {})
}
Expand Down