Skip to content

[fix](fe) Convert SQL masking ranges to UTF-16 offsets once - #71115

Open
morningman wants to merge 1 commit into
apache:masterfrom
morningman:fix-mask-offsets-converted-twice
Open

morningman wants to merge 1 commit into
apache:masterfrom
morningman:fix-mask-offsets-converted-twice

Conversation

@morningman

@morningman morningman commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

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.

/* 😀 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

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 [fix](nereids) Fix SQL masking and view rewrites after supplementary characters #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 [fix](audit) Mask table function properties in all statements that use one #71116, 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.

### 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: apache#70366 (made `rewriteSql` convert parser positions to UTF-16 offsets), apache#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:** apache#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. apache#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 apache#13698 of apache#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 apache#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 apache#70366 but not that of apache#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 apache#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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@morningman

Copy link
Copy Markdown
Contributor Author

run buildall

@morningman

Copy link
Copy Markdown
Contributor Author

/review

@hello-stephen hello-stephen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed both changed files at head 2906e8c. No actionable introduced issues found.

The parser now emits inclusive code-point ranges, matching all three production consumers and the single UTF-16 conversion in BaseViewInfo.rewriteSql. Both masking modes share the fix. The updated parameterized test retains independent final-SQL assertions with multiple replacements and supplementary characters.

The change is focused and removes an intermediate map and conversion pass. Caller maps are local, exception boundaries are preserved, and no new concurrency, lifecycle, configuration, persistence, protocol, or FE-BE concerns were identified.

Review converged in one round with two delegated reviews. Validation was limited to source inspection as requested; no builds or tests were run.

Review converged after 1 round(s).

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.

2 participants