Skip to content

Sanitize SQL statements before attaching them to database spans and breadcrumbs #5964

Done

Description

@jamescrosswell

Part of #5420.

The SDK attaches database statements exactly as the application sent them. Any literal inlined into the SQL, such as WHERE email = '[email protected]', goes to Sentry unchanged. Parameterized commands are fine, because their values aren't in the text. That covers EF Core's captured variables and @id-style ADO.NET parameters.

The data collection spec expects statements to be sanitized or parameterized, and to always be collected. #5433 had planned to gate the raw statement on DatabaseQueryData until we had a sanitizer. #5933 decided against that and closed #5433, so this issue tracks the sanitizing itself.

Where statements are attached

  • SqlClient (SentrySqlListener): Command.CommandText becomes the db.query span description.
  • EF Core command spans (EFCommandDiagnosticSourceHelper): the command text becomes the db.query span description.
  • EF Core query-compile spans (EFQueryCompilerDiagnosticSourceHelper): the description is the LINQ query expression, not SQL. Constants written in the LINQ query appear in it, so it needs handling of its own.
  • EF6 spans (SentryQueryPerformanceListener): the command text becomes the span description.
  • EF6 breadcrumbs (SentryCommandInterceptor → SentryQueryLogger): the command text is the breadcrumb message.

Proposal

Before attaching a statement, replace its string and numeric literals with ? and strip its comments. This is what sentry-go does.

Open questions

  • Lexer. Do we write a small SQL lexer, or take a dependency? Adding a package dependency needs a maintainer decision.
  • Dialects. Quoting differs between databases: SQL Server's [ident] and N'...', PostgreSQL's $$...$$, MySQL's backticks, SQLite's quoting rules. sentry-go handles these per database.
  • Failure. If a statement can't be lexed, do we drop it or keep it? The safe choice is to drop it, but then the span loses its description.
  • Rollout. Sanitizing changes the span descriptions every app sends today. Do we ship it as the default, or behind an option first?
  • Query summary. Should this also produce db.query.summary and the operation name, the structural metadata the spec always collects? sentry-java plans that separately in Generate low-cardinality SQL summaries for database spans sentry-java#5981.

Prior art

Activity

  1. added
    Feature
    .NET
    on Nov 18, 2026
  2. linear-code commented on Nov 18, 2026

    @linear-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETFeature

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions