This is an automated email from the ASF dual-hosted git repository.
morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 38afe2a7d0e [fix](fe) Convert SQL masking ranges to UTF-16 offsets
once (#71115)
38afe2a7d0e is described below
commit 38afe2a7d0ef3230ac6c352bc347b00ea52e2a71
Author: Mingyu Chen (Rayner) <[email protected]>
AuthorDate: Mon Nov 30 11:14:06 2026 +0800
[fix](fe) Convert SQL masking ranges to UTF-16 offsets once (#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 #71116, which doesn't touch
these offsets, before and after this fix.
---
.../apache/doris/nereids/parser/NereidsParser.java | 27 ++++++----------------
.../doris/nereids/parser/EncryptSQLTest.java | 11 +++++----
2 files changed, 13 insertions(+), 25 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java
index 8b1d81010db..eb5eb21c5ed 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java
@@ -63,7 +63,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
-import java.util.TreeMap;
import java.util.function.Function;
import javax.annotation.Nullable;
@@ -366,36 +365,24 @@ public class NereidsParser {
return (LogicalPlan) realLogicalPlanBuilder.visit(tree);
}
- /** Parse SQL for masking without applying execution hints; output ranges
use inclusive UTF-16 offsets. */
+ /** Parse SQL for masking without applying execution hints; see the
overload for the ranges. */
public LogicalPlan parseForEncryption(String sql, Map<Pair<Integer,
Integer>, String> indexInSqlToString) {
return parseForEncryption(sql, indexInSqlToString, false);
}
- /** Parse SQL for masking with inclusive UTF-16 output ranges, optionally
preserving property-list layout. */
+ /**
+ * Parse SQL for masking, optionally preserving property-list layout. The
replacement ranges are inclusive
+ * code-point positions, as the parser reports them;
BaseViewInfo.rewriteSql converts them to UTF-16 offsets.
+ */
public LogicalPlan parseForEncryption(String sql, Map<Pair<Integer,
Integer>, String> indexInSqlToString,
boolean preserveFormatting) {
CommonTokenStream tokenStream = parseLeanTokens(sql);
ParserRuleContext tree = toAst(tokenStream,
DorisParser::singleStatement);
// SQL masking must not apply SET_VAR hints to the current session.
// The original SQL, including its hints, is preserved by the property
replacements.
- TreeMap<Pair<Integer, Integer>, String> codePointRanges = new
TreeMap<>(new Pair.PairComparator<>());
LogicalPlanBuilder realLogicalPlanBuilder = new
LogicalPlanBuilderForEncryption(
- ImmutableMap.of(), codePointRanges, preserveFormatting);
- LogicalPlan plan = (LogicalPlan) realLogicalPlanBuilder.visit(tree);
- // ANTLR indexes code points, while SQL rewriting uses Java UTF-16
offsets. Normalize both
- // masking modes here so audit, FE logs and direct parser callers
share the same contract.
- // Walk sorted ranges once rather than rescanning the prefix for every
replacement.
- int codePointOffset = 0;
- int charOffset = 0;
- for (Map.Entry<Pair<Integer, Integer>, String> entry :
codePointRanges.entrySet()) {
- Pair<Integer, Integer> range = entry.getKey();
- int start = sql.offsetByCodePoints(charOffset, range.first -
codePointOffset);
- int end = sql.offsetByCodePoints(start, range.second - range.first
+ 1);
- indexInSqlToString.put(Pair.of(start, end - 1), entry.getValue());
- codePointOffset = range.second + 1;
- charOffset = end;
- }
- return plan;
+ ImmutableMap.of(), indexInSqlToString, preserveFormatting);
+ return (LogicalPlan) realLogicalPlanBuilder.visit(tree);
}
/** parseForSyncMv */
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/EncryptSQLTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/EncryptSQLTest.java
index a49371578cc..f383ec3c0f7 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/EncryptSQLTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/EncryptSQLTest.java
@@ -712,7 +712,7 @@ public class EncryptSQLTest extends ParserTestBase {
@ParameterizedTest
@ValueSource(booleans = {false, true})
- public void testEncryptionParserReturnsUtf16Ranges(boolean
preserveFormatting) {
+ public void testEncryptionParserReturnsCodePointRanges(boolean
preserveFormatting) {
String first = "'uri' = 's3://bucket/😀.csv', 'format' = 'csv',
's3.secret_key' = 'first-😀-secret'";
String second = "'uri' = 's3://bucket/other.csv', 'format' = 'csv',
's3.secret_key' = 'second-secret'";
String sql = "/* 😀😀 */ SELECT * FROM S3(" + first + ")\nUNION ALL /* 😀
*/ SELECT * FROM S3("
@@ -722,10 +722,11 @@ public class EncryptSQLTest extends ParserTestBase {
Assertions.assertEquals(2, replacements.size());
Pair<Integer, Integer> firstRange = replacements.firstKey();
Pair<Integer, Integer> secondRange = replacements.lastKey();
- Assertions.assertEquals(preserveFormatting ? "'first-😀-secret'" :
first,
- sql.substring(firstRange.first, firstRange.second + 1));
- Assertions.assertEquals(preserveFormatting ? "'second-secret'" :
second,
- sql.substring(secondRange.first, secondRange.second + 1));
+ // The ranges count a supplementary character once, as the parser
does; rewriteSql converts them
+ Assertions.assertEquals(preserveFormatting ? "'first-😀-secret'" :
first, sql.substring(
+ sql.offsetByCodePoints(0, firstRange.first),
sql.offsetByCodePoints(0, firstRange.second + 1)));
+ Assertions.assertEquals(preserveFormatting ? "'second-secret'" :
second, sql.substring(
+ sql.offsetByCodePoints(0, secondRange.first),
sql.offsetByCodePoints(0, secondRange.second + 1)));
String expected;
if (preserveFormatting) {
expected = sql.replace("'first-😀-secret'",
"'*XXX'").replace("'second-secret'", "'*XXX'");
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]