Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .cursor/rules/sql_sanitizing.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
alwaysApply: false
description: SQL query sanitizing, syncing the vendored OpenTelemetry SQL grammar, and the SQL leak corpus
---

# SQL Query Sanitizing

SQL integrations put sanitized query text on their spans. The sanitizer is OpenTelemetry Java
Instrumentation's JFlex SQL grammar, vendored with its rules unchanged and pinned to one upstream
commit. Upstream's gaps are accepted and tracked as known leaks; grammar fixes go upstream and
arrive through a sync.

Layers: integration โ†’ `SqlSpanData` โ†’ `SqlQueryAnalyzer` (both in `io.sentry.util.sql`) โ†’
`OtelSqlSanitizer` โ†’ `AutoSqlSanitizer`, which `:sentry:generateJflex` generates at build time from
`sentry/src/main/jflex/SqlSanitizer.jflex`. The Javadoc of `SqlSpanData`, `SqlQueryAnalyzer` and
`OtelSqlSanitizer` states their contracts.

## File ownership

**Upstream-owned**, written only by `scripts/sync-otel-sql.sh`: `SqlSanitizer.jflex`; `SqlQuery`,
`SqlDialect`, `StringUtils` and `UPSTREAM` in `sentry/src/main/java/io/sentry/vendor/otel/sql/`; the
commit in the "SQL grammar" entry of `THIRD_PARTY_NOTICES.md`. Change them by changing the script's
transform and re-running it: the `check-vendored-otel-sql` workflow re-derives them from the pinned
commit and fails on any difference. Each file's `// Changes:` header lists the script's edits.
Spotless skips `io/sentry/vendor/**`, which keeps them byte-identical.

**Sentry-owned**, edited by hand: everything in `io.sentry.util.sql`, plus `AutoValue_SqlQuery`,
`AutoValue_SqlDialect`, `OtelSqlSanitizer` and `SqlLexerException` in the vendor folder. The
`AutoValue_*` classes stand in for the code AutoValue would generate.

## Syncing to a newer upstream

1. Check for upstream changes: diff the files listed in `UPSTREAM` between the pinned commit and the
target ref (`raw.githubusercontent.com`); the GitHub compare API lists at most 300 files. The
caller picks the ref; the current pin is a commit on upstream `main`.
2. Run `scripts/sync-otel-sql.sh <commit|tag|branch>` (`--help` for usage). If it stops with
`expected text not found`, upstream changed a line a transform edits, or the
`AutoValue_SqlQuery` constructor call it pins: adapt the substitution (for the call, also
`AutoValue_SqlQuery`) and re-run. Nothing is written until every edit applies.
3. Read the upstream diff the script prints. The tests check only leaks, so a change to query text
or summary passes them: list every behaviour change for the PR description.
4. Compare upstream's JFlex version (`jflex("de.jflex:jflex:โ€ฆ")` in
`instrumentation-api-incubator/build.gradle.kts`) with `jflex` in `gradle/libs.versions.toml`.
The script does not check it.
5. If the vendored files start depending on another upstream file, add it to `paths` and `targets`
in the script, to the Error Prone and Javadoc exclusion lists in `sentry/build.gradle.kts`, and
to the notices entry.
6. Run `./gradlew :sentry:test --tests 'io.sentry.util.sql.*'`. A compile error in an `AutoValue_*`
class means upstream changed a property: rewrite the class as AutoValue would generate it.
Settle `SqlQueryAnalyzerKnownLeakTest` as described under Leak corpus.
7. Add a changelog entry when the sync changes grammar rules.

Done when `scripts/sync-otel-sql.sh --check` passes, the SQL tests pass, and the PR description
lists the behaviour changes from step 3.

## Leak corpus

Cases live in `sentry/src/test/resources/sql/analyzer-cases.json`, each with `id`, `dialect` (the
`db.system` an integration reports, or `CONSERVATIVE` for an unknown database), `input`, `secrets`
(strings that must not reach the output) and `source` (`sentry`, `otel-java:<Test>#<method>` or
`otel-dotnet:<file>#<case>`).

`known-leaks.txt` is a ratchet: `SqlQueryAnalyzerKnownLeakTest` fails on a leaking case that is not
listed and on a listed case that no longer leaks.

- A listed case that no longer leaks: remove its id.
- A new leak: name its cause in the PR (an upstream change or our wrapper), then list its id.
- Exemption is per case: a listed case can start leaking another of its secrets without failing.
Give a new case one secret unless it needs more.
- Make new secrets distinctive (the `sentry` cases use a `Zq9xK7` marker) so a match cannot come
from the SQL itself.
- Upstream test cases are transcribed by hand. When taking cases from a newer upstream commit,
update the commit in the file's `attribution` block and in the matching "SQL analyzer tests" or
"SQL processor tests" entry of `THIRD_PARTY_NOTICES.md`.
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rule file in `.cursor/rules/`:
| `opentelemetry` | `sentry-opentelemetry-*`, agent vs agentless, span processing, sampling, context propagation |
| `new_module` | Adding a new integration or sample module |
| `e2e_tests` | End-to-end tests, `MockSentryServer`, integration test suites, the OpenTelemetry agent fixture |
| `sql_sanitizing` | SQL query sanitizing (`SqlQueryAnalyzer`, `SqlSpanData`), syncing the vendored OpenTelemetry SQL grammar (`sync-otel-sql.sh`), the SQL leak corpus |

Rules can be combined โ€” a tracing scope issue may need both `scopes` and `opentelemetry`.
There is no rule for Android profiling yet; read the `sentry-android-core` profiling code
Expand Down
Loading