Skip to content

Commit

Permalink
Revert "🔊 [RUMF-1345] log first untrusted events of each type" (#1940)
Browse files Browse the repository at this point in the history
This reverts commit 3a3b9c8.
  • Loading branch information
BenoitZugmeyer authored Jan 11, 2023
1 parent bd35427 commit 353193c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 74 deletions.
50 changes: 1 addition & 49 deletions packages/core/src/browser/addEventListener.spec.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
import { createNewEvent } from '../../test/specHelper'
import { stubZoneJs } from '../../test/stubZoneJs'
import type { RawTelemetryEvent } from '../domain/telemetry'
import { resetTelemetry, startFakeTelemetry } from '../domain/telemetry'
import { noop } from '../tools/utils'

import { addEventListener, DOM_EVENT, resetUntrustedEventsCount } from './addEventListener'
import { addEventListener, DOM_EVENT } from './addEventListener'

describe('addEventListener', () => {
describe('untrusted events reporting', () => {
let originalJasmine: typeof jasmine
let telemetryEvents: RawTelemetryEvent[]
let eventTarget: EventTarget

beforeEach(() => {
eventTarget = document.createElement('div')
originalJasmine = window.jasmine
delete (window as any).jasmine
telemetryEvents = startFakeTelemetry()
})

afterEach(() => {
window.jasmine = originalJasmine
resetTelemetry()
resetUntrustedEventsCount()
})

it('sends a telemetry debug log when an untrusted event is dispatched', () => {
addEventListener(eventTarget, DOM_EVENT.CLICK, noop)
eventTarget.dispatchEvent(createNewEvent(DOM_EVENT.CLICK))

expect(telemetryEvents).toEqual([
{
message: 'Untrusted event',
status: 'debug',
type: 'log',
event_type: 'click',
},
])
})

it('only reports the first untrusted event of each type', () => {
addEventListener(eventTarget, DOM_EVENT.CLICK, noop)
addEventListener(eventTarget, DOM_EVENT.MOUSE_UP, noop)

eventTarget.dispatchEvent(createNewEvent(DOM_EVENT.CLICK))
eventTarget.dispatchEvent(createNewEvent(DOM_EVENT.CLICK))

eventTarget.dispatchEvent(createNewEvent(DOM_EVENT.MOUSE_UP))

expect(telemetryEvents.length).toBe(2)
})
})

describe('Zone.js support', () => {
let zoneJsStub: ReturnType<typeof stubZoneJs>

Expand Down
33 changes: 8 additions & 25 deletions packages/core/src/browser/addEventListener.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { monitor } from '../tools/monitor'
import { getZoneJsOriginalValue } from '../tools/getZoneJsOriginalValue'
import { addTelemetryDebug } from '../domain/telemetry'

export const enum DOM_EVENT {
BEFORE_UNLOAD = 'beforeunload',
Expand Down Expand Up @@ -81,13 +80,14 @@ export function addEventListeners<E extends Event>(
listener: (event: E) => void,
{ once, capture, passive }: AddEventListenerOptions = {}
) {
const wrappedListener = monitor((event: Event) => {
if (once) {
stop()
}
reportUntrustedEvent(event)
listener(event as E)
})
const wrappedListener = monitor(
once
? (event: Event) => {
stop()
listener(event as E)
}
: (listener as (event: Event) => void)
)

const options = passive ? { capture, passive } : capture

Expand All @@ -103,20 +103,3 @@ export function addEventListeners<E extends Event>(
stop,
}
}

const reportedUntrustedEventTypes = new Set<string>()

function reportUntrustedEvent(event: Event) {
const eventType = event.type

if (event.isTrusted || (window as any).jasmine || reportedUntrustedEventTypes.has(eventType)) {
return
}

reportedUntrustedEventTypes.add(eventType)
addTelemetryDebug('Untrusted event', { event_type: eventType })
}

export function resetUntrustedEventsCount() {
reportedUntrustedEventTypes.clear()
}

0 comments on commit 353193c

Please sign in to comment.