morningman opened a new pull request, #71115:
URL: https://github.com/apache/doris/pull/71115
### Background
Before a statement reaches the audit log or an FE log, Doris masks its
credentials. `NereidsParser.parseForEncryption` parses the statement and
reports the ranges to replace, and `BaseViewInfo.rewriteSql` replaces them. The
parser counts a supplementary character, such as an emoji, as one position,
while a Java string holds it as two UTF-16 units, so one of the two has to
convert.
### What problem does this PR solve?
Issue Number: None
Related PR: #70366 (made `rewriteSql` convert parser positions to UTF-16
offsets), #69467 (made `parseForEncryption` return UTF-16 offsets; with both,
the ranges are converted twice)
PR Type: Bugfix
Problem Summary:
On master, masking converts the ranges twice. When a supplementary character
comes before a masked value, the replacement lands at a shifted offset. The
audit text is then garbled and can keep part of the secret, or `rewriteSql`
throws `IndexOutOfBoundsException` and the statement never reaches the audit
log. `parseForEncryption` now reports the parser's positions, which is what
every one of its callers passes to `rewriteSql`.
```sql
/* 😀 nightly */ SELECT count(*) FROM numbers('number' = '2', 's3.secret_key'
= 'emojibefore-secret');
SELECT count(*) /* 😀😀😀 */ FROM numbers('number' = '2', 's3.secret_key' =
'emojibefore-secret2');
```
| | `stmt` in `__internal_schema.audit_log` |
|---|---|
| Before | `/* 😀 nightly */ SELECT count(*) FROM numbers('"number" = "2",
"s3.secret_key" = "*XXX"` (the closing `')` is lost); the second statement is
missing, and `fe.warn.log` has `Failed to write audit log.` with
`java.lang.IndexOutOfBoundsException at java.lang.Character.offsetByCodePoints`
|
| After | `/* 😀 nightly */ SELECT count(*) FROM numbers("number" = "2",
"s3.secret_key" = "*XXX")` and `SELECT count(*) /* 😀😀😀 */ FROM numbers("number"
= "2", "s3.secret_key" = "*XXX")` |
### Details
- **Frequency:** Always, for a statement that is masked (a credential
property, a table-valued function, a password) and holds a supplementary
character before the masked text, in the audit log and in FE logs.
- **Root cause:** #70366 made `BaseViewInfo.rewriteSql` take the parser's
code-point positions and convert them to UTF-16 offsets, for all of its
callers: view and materialized view definitions, sync materialized views and
masking. #69467 then made `parseForEncryption` convert its ranges to UTF-16
offsets itself, so for masking `rewriteSql` converts offsets that are already
UTF-16.
- **Why existing tests missed it:** `EncryptSQLTest` has ten cases with
supplementary characters, from both PRs, and all ten fail on master:
`testEncryption`, `testEncryptionAfterSupplementaryCharacters`,
`testPasswordMaskingUsesUtf16OffsetsInBothModes` (3),
`testLogPasswordMaskingWithSupplementaryCharacters`,
`testLogMaskingPreservesPropertyFormatting`,
`testAuditPropertyMaskingKeepsExistingFormattingWithUnicode` and
`testEncryptionParserReturnsUtf16Ranges` (2). The FE UT pipeline lists them as
muted (build #13698 of #70971), so FE UT passes. No regression suite masks a
statement with a supplementary character.
- **Fix:** `parseForEncryption` passes its replacement map to
`LogicalPlanBuilderForEncryption` directly, as it did before #69467, and
documents that the ranges are code-point positions for `rewriteSql`. The three
callers of `parseForEncryption` (`NeedAuditEncryption.maskSql`,
`StreamingInsertTask`, `StreamingInsertJob`) all pass its ranges to
`rewriteSql`.
- **Other paths and branches:** The other callers of `rewriteSql` pass
parser positions and were right before this change. Only master has the double
conversion: branch-4.2 has the conversion of #70366 but not that of #69467, and
branch-4.1 and branch-4.0 have neither.
### Testing
- `EncryptSQLTest`: the ten cases above fail on master and pass with the
fix, 13 of 13, run locally. `testEncryptionParserReturnsUtf16Ranges` asserted
that `parseForEncryption` returns UTF-16 offsets, the contract that breaks the
composition; it is renamed `testEncryptionParserReturnsCodePointRanges` and now
converts the ranges before taking substrings. Its assertion on the masked text
is unchanged.
- `SupplementaryCharacterOffsetTest`, `CreateViewTest`,
`CreateMTMVCommandTest` (the view and materialized view rewrites of #70366),
`StreamingInsertTaskAuditTest`, `StmtExecutorTest` and the other masking tests
(`RepositoryAuditEncryptionTest`, `AuditEncryptionSessionVariableTest`,
`DatasourcePropertyMaskingTest`, `DatasourcePrintableMapTest`) pass locally.
- The example above ran on a local cluster, one FE and one BE: the BE built
from master, the FE from master plus the separate fix for masking table
functions in CTAS and other statements, which doesn't touch these offsets,
before and after this fix.
### Release note
None
### Check List (For Author)
- Behavior changed: No
- Does this need documentation: No
- Pre-submission status: Ready
### AI Disclosure
- Tool: Claude Code (Claude Opus 5.5)
- Generated: the code change, the test change and this description
- Human review: not stated
- Prompts: The author asked to fix directly on master, without a private
report, the audit masking gap found while adding Doris query lineage to
OpenMetadata, and to open the PRs; this failure turned up in the tests of that
fix.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]