mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
* fix: keep the default Sentry filters when the app config extends them The app config replaced ignoreErrors and beforeSend instead of extending them, so deployments that set their own dropped Baserow's silenced error patterns, the handled API error filter and the private link redaction. * fix: preserve Sentry redaction across app hooks
164 lines
5.5 KiB
JavaScript
164 lines
5.5 KiB
JavaScript
import { describe, expect, onTestFinished, test, vi } from 'vitest'
|
||
import {
|
||
BrowserClient,
|
||
createTransport,
|
||
defaultStackParser,
|
||
eventFiltersIntegration,
|
||
} from '@sentry/browser'
|
||
|
||
import { buildSentryConfig } from '@baserow/modules/core/utils/sentryConfig'
|
||
import { makeFakeTransport } from '@baserow/modules/core/utils/sentryFakeTransport'
|
||
|
||
const TOKEN = 'a'.repeat(64)
|
||
const privateUrl = `https://baserow.io/template/secret-slug/private-link/${TOKEN}`
|
||
const redactedUrl =
|
||
'https://baserow.io/template/[redacted]/private-link/[redacted]'
|
||
const tracingIntegration = { name: 'BrowserTracing' }
|
||
|
||
const configFor = (userConfig, overrides = {}) =>
|
||
buildSentryConfig({
|
||
publicConfig: { version: '1.0', sentryDsn: 'https://key@localhost/1' },
|
||
tracingIntegration,
|
||
isDev: false,
|
||
userConfig,
|
||
...overrides,
|
||
})
|
||
|
||
const startClient = (userConfig) => {
|
||
const sent = []
|
||
const config = configFor({
|
||
// A standalone BrowserClient needs its event filters installed explicitly.
|
||
integrations: [eventFiltersIntegration()],
|
||
...userConfig,
|
||
transport: (options) =>
|
||
createTransport(options, (request) => {
|
||
sent.push(String(request.body))
|
||
return Promise.resolve({ statusCode: 200 })
|
||
}),
|
||
})
|
||
const client = new BrowserClient({
|
||
...config,
|
||
stackParser: defaultStackParser,
|
||
})
|
||
client.init()
|
||
onTestFinished(() => client.close())
|
||
return { client, sent }
|
||
}
|
||
|
||
const exceptionEvent = (value) => ({
|
||
exception: { values: [{ type: 'UnhandledRejection', value }] },
|
||
})
|
||
|
||
describe('buildSentryConfig', () => {
|
||
test.each([undefined, null])(
|
||
'uses the defaults with app config %s',
|
||
(app) => {
|
||
const config = configFor(app)
|
||
expect(config.release).toBe('baserow-web-frontend@1.0')
|
||
expect(config.integrations).toEqual([tracingIntegration])
|
||
expect(config.beforeSend({ request: { url: privateUrl } })).toEqual({
|
||
request: { url: redactedUrl },
|
||
})
|
||
}
|
||
)
|
||
|
||
test('extends integrations and denyUrls and lets the app override the release', () => {
|
||
const appIntegration = { name: 'AppIntegration' }
|
||
const config = configFor({
|
||
release: 'saas@1.0',
|
||
integrations: [appIntegration],
|
||
denyUrls: [/extensions\//i],
|
||
})
|
||
|
||
expect(config.release).toBe('saas@1.0')
|
||
expect(config.integrations).toEqual([tracingIntegration, appIntegration])
|
||
expect(config.denyUrls).toEqual([/extensions\//i])
|
||
})
|
||
|
||
test('drops both the extension rejection and the app’s ignored error', async () => {
|
||
const { client, sent } = startClient({
|
||
ignoreErrors: ['ResizeObserver loop limit exceeded'],
|
||
})
|
||
|
||
client.captureEvent(
|
||
exceptionEvent(
|
||
'Non-Error promise rejection captured with value: Object Not Found Matching Id:1, MethodName:update, ParamCount:4'
|
||
)
|
||
)
|
||
client.captureEvent(exceptionEvent('ResizeObserver loop limit exceeded'))
|
||
client.captureEvent(exceptionEvent('A real error'))
|
||
await client.flush()
|
||
|
||
expect(sent).toHaveLength(1)
|
||
expect(sent[0]).toContain('A real error')
|
||
})
|
||
|
||
test('filters handled errors and redacts both the app hook input and its result', async () => {
|
||
const beforeSend = vi.fn(async (event) => ({
|
||
...event,
|
||
tags: { page: privateUrl },
|
||
}))
|
||
const { client, sent } = startClient({ beforeSend })
|
||
const networkError = Object.assign(new Error('Network Error'), {
|
||
name: 'AxiosError',
|
||
})
|
||
client.captureException(networkError, { originalException: networkError })
|
||
client.captureEvent({ message: 'boom', request: { url: privateUrl } })
|
||
await client.flush()
|
||
|
||
expect(beforeSend).toHaveBeenCalledTimes(1)
|
||
expect(beforeSend.mock.calls[0][0].request.url).toBe(redactedUrl)
|
||
expect(sent).toHaveLength(1)
|
||
expect(sent[0]).toContain(`"page":"${redactedUrl}"`)
|
||
expect(sent[0]).not.toContain(TOKEN)
|
||
expect(sent[0]).not.toContain('secret-slug')
|
||
})
|
||
|
||
test('cleans transaction URLs and route params before and after the app hook', async () => {
|
||
const beforeSendTransaction = vi.fn(async (event) => ({
|
||
...event,
|
||
spans: [{ data: { 'params.slug': 'secret-slug', page: privateUrl } }],
|
||
}))
|
||
const config = configFor({ beforeSendTransaction })
|
||
const result = await config.beforeSendTransaction({
|
||
type: 'transaction',
|
||
contexts: {
|
||
trace: { data: { 'params.token': TOKEN, page: privateUrl } },
|
||
},
|
||
})
|
||
|
||
expect(beforeSendTransaction.mock.calls[0][0].contexts.trace.data).toEqual({
|
||
page: redactedUrl,
|
||
})
|
||
expect(result.spans).toEqual([{ data: { page: redactedUrl } }])
|
||
})
|
||
|
||
test.each(['beforeSend', 'beforeSendTransaction'])(
|
||
'preserves an async drop from %s',
|
||
async (hook) => {
|
||
const config = configFor({ [hook]: async () => null })
|
||
expect(await config[hook]({})).toBeNull()
|
||
}
|
||
)
|
||
|
||
test('runs the app hook locally and keeps the fake transport', async () => {
|
||
const log = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||
onTestFinished(() => log.mockRestore())
|
||
const beforeSend = vi.fn((event) => event)
|
||
const config = configFor(
|
||
{ beforeSend, transport: vi.fn() },
|
||
{ publicConfig: { sentryDsn: 'fake' }, isDev: true }
|
||
)
|
||
const event = { message: 'local error' }
|
||
const hint = { originalException: new Error(event.message) }
|
||
|
||
expect(await config.beforeSend(event, hint)).toEqual(event)
|
||
expect(beforeSend).toHaveBeenCalledWith(event, hint)
|
||
expect(log).toHaveBeenCalledWith(
|
||
'[Sentry captured error]',
|
||
'Error: local error'
|
||
)
|
||
expect(config.transport).toBe(makeFakeTransport)
|
||
})
|
||
})
|