-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(core): Apply dataCollection.databaseQueryData to inline SQL literals
#24089
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
Draft
s1gr1d
wants to merge
1
commit into
develop
Choose a base branch
from
sig/dc-database-query-data
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/core/src/utils/data-collection/filterCollectedDbQueryText.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { Client } from '../../client'; | ||
| import { getClient } from '../../currentScopes'; | ||
| import type { SqlDialect } from '../sql'; | ||
| import { sanitizeSqlQuery } from '../sql'; | ||
|
|
||
| /** | ||
| * Applies `dataCollection.databaseQueryData` to a SQL statement the SDK collected itself, for use as | ||
| * `db.query.text`. | ||
| * | ||
| * A statement can carry inline literal values (`WHERE email = 'jane@example.com'`), which the spec | ||
| * counts as database query data. Sanitized statements are not gated, so with the option off the | ||
| * literals are replaced with `?` rather than the attribute being dropped — the shape of the query | ||
| * stays available for debugging. | ||
| * | ||
| * Pass the `client` the statement belongs to whenever one is at hand; falling back to `getClient()` | ||
| * resolves against the current scope, which is the wrong client in a multi-client setup. | ||
| */ | ||
| export function filterCollectedDbQueryText(query: string, dialect?: SqlDialect, client?: Client): string; | ||
| export function filterCollectedDbQueryText( | ||
| query: string | undefined, | ||
| dialect?: SqlDialect, | ||
| client?: Client, | ||
| ): string | undefined; | ||
| export function filterCollectedDbQueryText( | ||
| query: string | undefined, | ||
| dialect?: SqlDialect, | ||
| client?: Client, | ||
| ): string | undefined { | ||
| if (query === undefined) { | ||
| return undefined; | ||
| } | ||
| // Instrumentation can run before a client exists; collecting is the documented default. | ||
| const collect = (client ?? getClient())?.getDataCollectionOptions().databaseQueryData !== false; | ||
| return collect ? query : sanitizeSqlQuery(query, dialect); | ||
| } |
29 changes: 29 additions & 0 deletions
29
packages/core/test/lib/utils/data-collection/filterCollectedDbQueryText.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import type { Client } from '../../../../src/client'; | ||
| import type { DataCollection } from '../../../../src/types/datacollection'; | ||
| import { filterCollectedDbQueryText } from '../../../../src/utils/data-collection/filterCollectedDbQueryText'; | ||
| import { resolveDataCollectionOptions } from '../../../../src/utils/data-collection/resolveDataCollectionOptions'; | ||
|
|
||
| function mockClient(dataCollection?: DataCollection): Client { | ||
| return { getDataCollectionOptions: () => resolveDataCollectionOptions({ dataCollection }) } as unknown as Client; | ||
| } | ||
|
|
||
| describe('filterCollectedDbQueryText', () => { | ||
| it('returns undefined for an absent statement', () => { | ||
| expect(filterCollectedDbQueryText(undefined, undefined, mockClient())).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('keeps inline literals by default', () => { | ||
| expect( | ||
| filterCollectedDbQueryText("SELECT * FROM users WHERE email = 'jane@example.com'", undefined, mockClient()), | ||
| ).toBe("SELECT * FROM users WHERE email = 'jane@example.com'"); | ||
| }); | ||
|
|
||
| it('sanitizes inline literals when databaseQueryData is off', () => { | ||
| expect( | ||
| filterCollectedDbQueryText("SELECT * FROM users WHERE email = 'jane@example.com'", undefined, { | ||
| ...mockClient({ databaseQueryData: false }), | ||
| }), | ||
| ).toBe('SELECT * FROM users WHERE email = ?'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nuxt DB breadcrumbs and static span names bypass databaseQueryData redaction
Nuxt's database instrumentation filters only the
db.query.textspan attribute; successful queries still place the raw SQL in breadcrumb fields and use it as the span name when span streaming is disabled. Inline literals derived from caller or request data can therefore be sent despitedataCollection.databaseQueryData: false; apply the helper to the breadcrumb and non-streaming span-name paths.Evidence
createBreadcrumb()passes the unfilteredqueryto bothmessageanddata['db.query.text'];addBreadcrumb()stores these values on the current isolation scope for subsequent events.createStartSpanOptions(), the non-streaming branch setsnamedirectly toquery, while only thedb.query.textattribute usesfilterCollectedDbQueryText.db.execplus prepared-statement methods accept statements containing inline literals.databaseQueryData, so a successful query followed by an error event or a static transaction can expose the raw literal.Identified by Warden · security-review · ZWT-URM