Skip to content

feat(sql): [SQL Sanitizing 1] Vendor OpenTelemetry's SQL grammar - #6795

Draft
lbloder wants to merge 4 commits into
feat/sql-sanitizingfrom
feat/sql-sanitizing-vendor-otel
Draft

lbloder wants to merge 4 commits into
feat/sql-sanitizingfrom
feat/sql-sanitizing-vendor-otel

Conversation

@lbloder

@lbloder lbloder commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

PR Stack (SQL Sanitizing)


📜 Description

Adds the SQL query analyzer to sentry core, built on OpenTelemetry Java Instrumentation's SQL grammar with its rules unchanged. Nothing calls it yet; the JDBC and Android SQLite PRs in this stack wire it in.

  • scripts/sync-otel-sql.sh <ref> vendors SqlSanitizer.jflex, SqlQuery, SqlDialect and StringUtils into io.sentry.vendor.otel.sql. Every change (license header, package, imports, annotations, AutoValue removed, %scanerror) is applied by the script; the pinned upstream commit is in io/sentry/vendor/otel/sql/UPSTREAM. --check re-derives the files from upstream and fails on any hand edit; a new workflow runs it on PRs that touch these paths.
  • JFlex generates the lexer at build time (generateJflex); JFlex is not a runtime dependency.
  • Hand-written AutoValue_SqlQuery and AutoValue_SqlDialect replace the annotation processor. Error Prone and NullAway now check our classes in the vendor package; the upstream files and the generated lexer stay excluded.
  • io.sentry.util.sql: SqlQueryAnalyzer.analyze(sql, dbSystemName, maxQueryTextLength) skips input over 1 MiB, cuts the output at the last whitespace within the limit, and fails closed on a RuntimeException. Plus SqlQuery, SqlSpanData and new SpanDataConvention keys. All new types are @ApiStatus.Internal.

💡 Motivation and Context

Maintaining a patched grammar is not viable long term; upstream changes often. Vendoring it unchanged behind a script keeps syncs mechanical, and whatever breaks on an update breaks loudly (script, compile error or test), never silently. Upstream's known leaks are accepted and listed in sentry/src/test/resources/sql/known-leaks.txt (131 corpus cases); fixes are to be contributed upstream.

💚 How did you test it?

  • ./gradlew :sentry:check :sentry:javadoc: 3835 tests, 0 failures.
  • The new tests assert no grammar outputs, so a sync never needs re-baselining. They cover only what upstream's own tests don't: our wrapper (SqlQueryAnalyzerTest) and SqlSpanData; a known-leak test over 361 corpus inputs with declared secrets, analyzed without a length limit, that fails on a leak not in the list and on any internal failure; and three deep or large inputs (nested parens, a 10,001-item IN list, a literal just under 1 MiB) on a 512 KiB stack with a time bound. Grammar robustness beyond that is left to upstream's test suite.
  • scripts/sync-otel-sql.sh --check passes; a hand edit to a vendored file makes it fail.
  • JFlex is absent from runtimeClasspath and runtimeElements; the sentry jar grows by 48 KB.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

JDBC integration ([SQL Sanitizing 2]), then Android SQLite integration ([SQL Sanitizing 3]).

⚠️ Merge this PR using a merge commit (not squash). Only the collection branch is squash-merged into 9.x.x.

#skip-changelog

@lbloder lbloder mentioned this pull request Nov 26, 2026
9 tasks
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against ccff9f4

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit ccff9f4.

Main concerns: dependencies customers inherit, public contract, data sent to Sentry.

Rules that block low also fired:

  • R1: code was truncated, omitted or a reduced context was used: sentry/src/main/jflex/SqlSanitizer.jflex, sentry/src/test/java/io/sentry/util/sql/SqlQueryAnalyzerKnownLeakTest.kt, sentry/src/test/java/io/sentry/util/sql/SqlQueryAnalyzerPathologicalTest.kt, sentry/src/test/java/io/sentry/util/sql/SqlQueryAnalyzerTest.kt, sentry/src/test/java/io/sentry/util/sql/SqlSpanDataTest.kt and 2 more
  • R4: a public API record changed: sentry/api/sentry.api
  • R5: dependencies or bundled SDK versions changed: gradle/libs.versions.toml
  • R6: build or release configuration changed: .github/workflows/check-vendored-otel-sql.yml, gradle/libs.versions.toml, scripts/sync-otel-sql.sh, sentry/build.gradle.kts

This label is a hint for reviewers, not an approval.

@sentry

sentry Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.65.1 (1) release

⚙️ sentry-android Build Distribution Settings

@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 27, 2026
if (sql == null || sql.length() > MAX_INPUT_LENGTH) {
return SqlQuery.EMPTY;
}
try {

@lbloder lbloder Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open question: the try also covers cut(...), so a bug in our own cut is swallowed like a grammar failure. Concretely, a negative maxQueryTextLength throws StringIndexOutOfBoundsException in StringUtils.truncate, which ends up as "no text" plus an "analysis failed" log; 0 returns "".

Option: wrap only OtelSqlSanitizer.sanitize(...), and throw IllegalArgumentException for maxQueryTextLength <= 0. The limit is a constant in our integrations (32 KiB JDBC, 8 KiB Android), not a user setting, so only our own bug can trip it, and the integration tests would catch it immediately. Downside: analyze would no longer be "never throws", and a bad constant would fail the user's query instead of only dropping the span data.

Does narrowing + IllegalArgumentException make sense here, or keep the current fail-closed behaviour?

@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant