Skip to content

feat(sqlite): [SQL Sanitizing 3] Sanitize SQL span descriptions in the Android SQLite integration - #6802

Draft
lbloder wants to merge 3 commits into
feat/sql-sanitizing-jdbcfrom
feat/sql-sanitizing-android-sqlite
Draft

lbloder wants to merge 3 commits into
feat/sql-sanitizing-jdbcfrom
feat/sql-sanitizing-android-sqlite

Conversation

@lbloder

@lbloder lbloder commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

PR Stack (SQL Sanitizing)


📜 Description

Wires the SQL query analyzer from #6795 into sentry-android-sqlite, for both the SupportSQLiteOpenHelper integration (OpenHelperSpans) and the SQLiteDriver integration (DriverSpans).

  • The span description is the sanitized query (at most 8 KiB, cut at whitespace). The span also gets db.query.text, db.query.summary and db.stored_procedure.name when present, and db.system.name (sqlite, also for in-memory databases) and db.namespace next to the existing db.system and db.name. If the analysis fails, the span keeps no query text at all, never the raw statement.
  • No active parent span, or a no-op parent, means no analysis and no span. Before, a no-op parent gave a no-op child, so only the analysis is saved.
  • OpenHelperSpans now ends the span at the time the database call returned or threw, as DriverSpans already did. Before, the span ended after the post-processing, so the analysis (and on the main thread the call-stack walk) would have counted as query time.
  • dbMetadataFromFileName cuts the name passed to SQLiteDriver.open at the first ? or #, so URI parameters such as an encryption key can't end up in db.name/db.namespace when a driver that opens URI filenames is wrapped. The default BundledSQLiteDriver.open(String) doesn't open URIs.

💡 Motivation and Context

SQLite spans carry the raw SQL statement today, including literal values from statements that don't use bind parameters.

💚 How did you test it?

  • ./gradlew :sentry-android-sqlite:check: 112 tests, 0 failures. As in feat(jdbc): [SQL Sanitizing 2] Sanitize SQL span descriptions in the JDBC integration #6798, the tests check wiring only: span fields equal SqlQueryAnalyzer's output for sqlite, so a grammar sync never needs re-baselining. Per integration: sanitized description and query keys (OpenHelper on the success and the error path), the 8 KiB limit (checked to differ from the output at 32 KiB), analysis failure, no analysis without a parent or with a no-op parent, the new keys. The OpenHelper span end is tested with a fake clock that advances during the database call and during the analysis. Each new test was checked against a mutation (raw SQL as description or fallback, no applyTo, no no-op check, limit 32 KiB, end time read after the analysis, parameters cut at ? only, …).
  • Sample app on a Zenfone 10 (Android 15), OpenHelper → Room 2 and SQLiteDriver → Room 3: all db.sql.query spans have sanitized descriptions and the new keys, e.g. INSERT OR ABORT INTO `song` (`id`,`title`,`artist`) VALUES (nullif(?, ?),?,?) with summary INSERT `song` ; text and summary match the analyzer's JVM output for the same statements.
  • On-device timing with and without sanitization: see the comment below.

📝 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

  • The changelog entry for the stack goes into the collection PR (feat(sql): SQL Sanitizing #6794).
  • Follow-up PR: ignoredSpanOrigins containing auto.db.sqlite most likely has no effect today (pre-existing, found by reading): SentryTracer.createChild checks the origin passed in SpanOptions, and both integrations set it only after startChild. Fix: pass the origin at startChild and skip the analysis for ignored origins, as SentryJdbcEventListener does.

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

#skip-changelog

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 23db52f.

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

Rules that block low also fired:

  • R6: build or release configuration changed: sentry-android-sqlite/build.gradle.kts

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

@github-actions

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 23db52f

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

lbloder commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

On-device timing with and without sanitization

Per call, in microseconds (µs). Measured on 23db52f232. Reported, not gated.

SupportSQLiteOpenHelper (framework SQLite)

Statement Plain DB (µs) Span, no sanitization (µs) Current (µs) Sanitization adds (µs) Analyzer alone (µs)
Room insert, 1 row (77 chars) 41–43 49–57 74–84 +25–27 9.9–11.2
select with IN (?,?,?) 30 39–43 69–70 +27–30 16.9–19.4
SELECT count(*) 16 24–30 41–47 +16–17 6.6–9.9
BEGIN IMMEDIATE TRANSACTION 9.5–9.8 16–19 28–32 +12–13 6.5–6.8
SELECT * FROM song, 100 rows 45–48 51–59 69–78 +18 6.7–6.9
insert, 2,000 parameters (8,037 chars) 1,445–1,644 1,475–1,663 1,980–2,230 +505–568 445–469
insert, 1,100 literal rows (34,137 chars) 294–359 306–370 1,196–1,410 +890–1,040 814–864

SQLiteDriver (BundledSQLiteDriver)

Statement Plain DB (µs) Span, no sanitization (µs) Current (µs) Sanitization adds (µs) Analyzer alone (µs)
Room insert, 1 row (77 chars) 41–51 49–58 71–82 +22–24 9.9–11.2
select with IN (?,?,?) 23–29 30–37 58–67 +27–30 16.9–19.4
SELECT count(*) 9.1–10.5 17–19 29–33 +12–13 6.6–9.9
BEGIN IMMEDIATE TRANSACTION 4.8–5.7 13–15 26–29 +13–14 6.5–6.8
SELECT * FROM song, 100 rows 20–25 43–54 54–69 +11–15 6.7–6.9
insert, 2,000 parameters (8,037 chars) 1,241–1,432 1,263–1,456 1,762–2,034 +499–578 445–469
insert, 1,100 literal rows (34,137 chars) 1,162–1,349 1,178–1,370 2,078–2,421 +899–1,051 814–864

Over all 14 short Room-style statements of the harness, sanitization adds +11 to +35 µs (OpenHelper) and +8 to +30 µs (driver); the analyzer alone takes 5.4–21.6 µs on 75 typical statements (median 10.6–11.9 µs).

Device: ASUS Zenfone 10 (ASUS_AI2302), Android 15 (API 35), on AC power, screen on, thermal status 0 before and after each run. The test APK is a release build, not debuggable.

How often: 2 runs of 3 rounds each. Per round and statement: 2,000 measured calls per variant after 200 warm-up calls; analyzer alone: 10,000 calls after 2,000 warm-up. Each call is timed on its own with System.nanoTime(). Each value is the median of the 3 round medians of one run; a range spans the two runs.

Setup:

  • A scratch instrumented test in sentry-android-sqlite, not part of the PR. SentryAndroid.init with a no-op transport, tracing at 1.0, ANR, sessions, breadcrumbs and frame tracking off; a transaction bound to the scope.
  • A file database in WAL mode, synchronous=NORMAL, on both paths. Reads call getCount() and then iterate all rows.
  • Three variants per statement, interleaved per call with a rotating order:
    • Plain DB: the framework helper or the bundled driver without the Sentry wrapper.
    • Span, no sanitization: this PR's wrapper with sanitization switched off by a scratch flag (raw SQL as description, no analysis). The flag is not part of the PR.
    • Current: this PR's wrapper.

Reading:

  • Sanitizing a short statement costs about 10–35 µs on this device, often more than SQLite's own execution of that statement (2–51 µs) and more than the span without sanitization (about 5–30 µs).
  • The analyzer is most of the increment. Statements with IN (…) cost more: the grammar normalizes IN lists with a regex, which on Android goes through ICU.
  • Long statements: the analysis is bounded by the 8 KiB output limit but still reads the input; the 34 KiB literal insert adds about 1 ms.
  • Not measured: low-end devices, the main thread, the first query of a process (the lexer's class initialization), and Room's statement reuse.

@sentry

sentry Bot commented Nov 27, 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

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