Skip to content

feat: redact encryption key pragmas in trace logs - #1501

Merged
gotson merged 1 commit into
xerial:masterfrom
hsdfat:fix/redact-pragma-key-in-trace-1482
Nov 9, 2026
Merged

gotson merged 1 commit into
xerial:masterfrom
hsdfat:fix/redact-pragma-key-in-trace-1482

Conversation

@hsdfat

@hsdfat hsdfat commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1482

When TRACE logging is on, NativeDB logs every statement it executes or prepares as [SQLite EXEC] .... For encrypted databases that includes pragma key = '...' (for example the one issued from the password property), so the key ended up in the log in plain text.

What changed, all in NativeDB.java:

  • Added a precompiled, case-insensitive pattern for the key, hexkey, textkey, rekey, hexrekey and textrekey pragmas, in both the pragma key = value and pragma key(value) forms. It allows an optional schema prefix and quoted or bare values, and uses possessive quantifiers so a very long quoted value cannot overflow the regex stack.
  • Added a package-private redactSecrets(String) that replaces the value with '***'. It keeps the pragma name and any statements that follow, and returns null for null.
  • The two [SQLite EXEC] trace messages (in _exec and prepare) now log redactSecrets(sql). This runs inside the log supplier, so nothing extra happens when TRACE is off.
  • The SQL actually sent to SQLite is unchanged, and there is no public API change.

Only the pragma forms are covered. SQLCipher's ATTACH ... KEY '...' is not redacted.

Tests, in the new NativeDBTraceLogTest:

  • redactSecretsHidesKeyValue (parameterized, 9 cases)
  • redactSecretsKeepsPragmaNameAndFollowingStatements
  • redactSecretsLeavesOtherStatementsUnchanged (parameterized, 4 cases)
  • redactSecretsHandlesNull
  • redactSecretsHandlesVeryLongKey (an 800k-character quoted key; a plain (?:[^']|'')* pattern throws StackOverflowError here)
  • traceLogDoesNotContainPassword opens a connection with the password property, runs pragma rekey, and checks that neither secret shows up in the TRACE output while the '***' lines do.

traceLogDoesNotContainPassword fails without the fix. With the old logging, the trace output contains pragma key = 'supersecret'. NativeDBTraceLogTest, CodingRulesTest and SQLiteConfigTest pass locally, and spotless:check passes.

Mask the value of the key, hexkey, textkey, rekey, hexrekey and textrekey
pragmas in the [SQLite EXEC] trace output, so the connection password is
not written to the logs. The redaction runs inside the log supplier, so it
has no cost when TRACE is disabled. The executed SQL is unchanged.

Closes xerial#1482
@gotson

gotson commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR!

@gotson gotson changed the title fix(core): redact encryption key pragmas in trace logs feat: redact encryption key pragmas in trace logs Nov 9, 2026
@gotson
gotson merged commit ac2f10d into xerial:master Nov 9, 2026
32 checks passed
@Macadoshis

Copy link
Copy Markdown

thanks a lot @hsdfat

(NB: I had a local org.apache.logging.log4j.core.filter.AbstractFilter to fix the issue and also considered the cipher pragma not to disclose the encryption algorithm to help a hacker craft his way to the pwd)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

redact database encryption key and sensitive information in logs at TRACE level

3 participants