Skip to content

[fix](audit) Mask table function properties in all statements that use one - #71116

Open
morningman wants to merge 1 commit into
apache:masterfrom
morningman:fix-ctas-tvf-audit-masking
Open

morningman wants to merge 1 commit into
apache:masterfrom
morningman:fix-ctas-tvf-audit-masking

Conversation

@morningman

Copy link
Copy Markdown
Contributor

Background

The audit log (fe.audit.log and __internal_schema.audit_log) records the text of every statement. A statement whose text can hold credentials is masked first: each Nereids command says so through NeedAuditEncryption.needAuditEncryption(), and then the statement is parsed again and the sensitive property values, of a table-valued function such as s3() or of a catalog or table, are written as *XXX. INSERT and SELECT ask for it when their plan reads a table-valued function.

What problem does this PR solve?

Issue Number: None
Related PR: #47595 (added the per-command check, with the CTAS engine condition), #60719 (added INSERT INTO a table function), #71115 (fixes the ten supplementary-character cases of EncryptSQLTest, muted in CI)
PR Type: Bugfix

Problem Summary:

Many statements that read or write a table function keep its credentials in the audit log, where anyone who can read the log finds them: a CTAS that runs, a function in a CTE or a subquery, UPDATE, DELETE and MERGE, INSERT INTO s3(...), and an EXPLAIN of an INSERT, UPDATE, DELETE or MERGE. A shared check now finds the function wherever the statement has it, every command that can carry one uses it, the properties of the function an INSERT writes are masked, and the text of every EXPLAIN is masked.

CREATE TABLE ctas_plain AS SELECT * FROM numbers('number' = '3', 's3.secret_key' = 'before-secret-ctas');
stmt in __internal_schema.audit_log
Before CREATE TABLE ctas_plain AS SELECT * FROM numbers('number' = '3', 's3.secret_key' = 'before-secret-ctas')
After CREATE TABLE ctas_plain AS SELECT * FROM numbers("number" = "3", "s3.secret_key" = "*XXX")
A statement with a function holding s3.secret_key Before After
INSERT ... SELECT or SELECT reading it masked masked
CREATE TABLE ... AS SELECT reading it, when it runs key in clear masked
the function in a CTE: CTAS, INSERT, a CTE before the INSERT, SELECT key in clear masked
the function in a subquery: INSERT, SELECT, UPDATE ... SET, DELETE ... WHERE key in clear masked
UPDATE ... FROM, DELETE ... USING, MERGE INTO ... USING the function key in clear masked
INSERT INTO local(...) or s3(...): the function it writes key in clear masked
EXPLAIN of an INSERT, UPDATE, DELETE or MERGE that runs key in clear masked

Details

  • Frequency: Always, for the statements in the second table, whenever the audit log records them.
  • Root cause: CreateTableCommand.needAuditEncryption() asks only when the engine is not olap, and analysis sets the engine of a CTAS to olap, so a CTAS that runs is never masked. INSERT and SELECT search the plan's children for the function, and neither the queries of a CTE nor the plans of subqueries are children. UPDATE, DELETE, MERGE and INSERT INTO a function never ask, and the masking parser did not mask the function an INSERT writes. A running EXPLAIN is audited with the plan it explains.
  • Why existing tests missed it: EncryptSQLTest reads a function only directly from INSERT and SELECT, and does not execute, so its CTAS would keep an unset engine and be masked; testEncryption, which holds those cases, is muted in CI ([fix](fe) Convert SQL masking ranges to UTF-16 offsets once #71115). StmtExecutorTest.testParsedCtasLogsMaskedSqlWhenAuditGateSkipsIt took the CTAS gap as its premise. No regression suite audits a table function.
  • Fix: NeedAuditEncryption.readsTableValuedFunction(plan) also looks in the queries of CTEs and the plans of subqueries; each command that can carry a function asks through it, INSERT INTO a function always asks and that function's properties are masked, and AuditLogHelper masks every EXPLAIN. What masking writes is unchanged, and a statement without a function keeps its text.
  • Other paths and branches: CREATE VIEW and CREATE MATERIALIZED VIEW keep the properties in their definitions, which SHOW CREATE shows, so masking their audit text alone would not protect them; not changed. branch-4.2 and branch-4.1 have all of these gaps, branch-4.0 all but INSERT INTO a function, and branch-3.1 the CTAS check; they come from [Feature](nereids) support encrypt sql for audit log #47595.

Testing

  • EncryptSQLTest#testTableValuedFunctionPropertiesMasking (new; a method of its own since testEncryption is muted): sixteen statements go through MysqlConnectProcessor and AuditLogHelper, execution stubbed and the CTAS engine written out. On master all sixteen keep the key; with the fix all are masked, and a CTAS without a function keeps its text.
  • audit/test_audit_log_tvf_masking (new, p0,nonConcurrent): thirteen statements that run, the CTAS and the EXPLAINs among them, compared with the .out. It passes with the fix; with master's FE code it fails at its first row, CREATE TABLE audit_tvf_ctas PROPERTIES ('replication_num' = '1') AS SELECT * FROM numbers('number' = '3', 's3.secret_key' = 'tvfmaskingsecretvalue', 'case' = 'audit_tvf_masking_ctas'). Run locally with one BE built from master of 2026-11-25 (be_exec_version set to 19 for it).
  • StmtExecutorTest, StreamingInsertTaskAuditTest, RepositoryAuditEncryptionTest, AuditEncryptionSessionVariableTest, InsertIntoTVFCommandTest, MergeIntoCommandTest, ExplainRefreshMTMVCommandTest and NereidsSqlCacheDataPolicyTest pass locally. The ten supplementary-character cases of EncryptSQLTest fail as on master; with [fix](fe) Convert SQL masking ranges to UTF-16 offsets once #71115 as well, all fourteen pass.

Notes for Reviewers

  • StmtExecutorTest.testParsedCtasLogsMaskedSqlWhenAuditGateSkipsIt is renamed testParsedSqlLogsMaskedSqlWhenAuditGateSkipsIt and now uses an olap CREATE TABLE with a sensitive property, which the audit log still leaves to FE log masking.
  • EXPLAIN is handled in AuditLogHelper, not by each plan: the plan an EXPLAIN runs on can be an analyzed insert, a row-level DML plan or a function sink. An EXPLAIN with nothing to mask keeps its text.
  • readsTableValuedFunction reads the expressions of every plan node once per audited statement, the rows of INSERT ... VALUES among them.
  • The suite reads numbers(), which ignores properties it does not know; local() is disabled on master and s3() would need storage.

Release note

The audit log no longer keeps the credentials of a table function such as s3() when a CREATE TABLE ... AS SELECT, an UPDATE, DELETE or MERGE, an INSERT INTO a table function, or an EXPLAIN of an INSERT, UPDATE, DELETE or MERGE uses it, or when a statement reads it in a CTE or a subquery. They are written as *XXX, as INSERT and SELECT already did.

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 tests and this description
  • Human review: not stated
  • Prompts: The author asked to fix directly on master, without a private report, the CTAS masking gap found while adding Doris query lineage to OpenMetadata, and to open the PRs.

…e one

### Background

The audit log (`fe.audit.log` and `__internal_schema.audit_log`) records the text of every statement. A statement whose text can hold credentials is masked first: each Nereids command says so through `NeedAuditEncryption.needAuditEncryption()`, and then the statement is parsed again and the sensitive property values, of a table-valued function such as `s3()` or of a catalog or table, are written as `*XXX`. INSERT and SELECT ask for it when their plan reads a table-valued function.

### What problem does this PR solve?

Issue Number: None
Related PR: apache#47595 (added the per-command check, with the CTAS engine condition), apache#60719 (added `INSERT INTO` a table function), apache#71115 (fixes the ten supplementary-character cases of `EncryptSQLTest`, muted in CI)
PR Type: Bugfix

Problem Summary:

Many statements that read or write a table function keep its credentials in the audit log, where anyone who can read the log finds them: a CTAS that runs, a function in a CTE or a subquery, UPDATE, DELETE and MERGE, `INSERT INTO s3(...)`, and an EXPLAIN of an INSERT, UPDATE, DELETE or MERGE. A shared check now finds the function wherever the statement has it, every command that can carry one uses it, the properties of the function an INSERT writes are masked, and the text of every EXPLAIN is masked.

```sql
CREATE TABLE ctas_plain AS SELECT * FROM numbers('number' = '3', 's3.secret_key' = 'before-secret-ctas');
```

| | `stmt` in `__internal_schema.audit_log` |
|---|---|
| Before | `CREATE TABLE ctas_plain AS SELECT * FROM numbers('number' = '3', 's3.secret_key' = 'before-secret-ctas')` |
| After | `CREATE TABLE ctas_plain AS SELECT * FROM numbers("number" = "3", "s3.secret_key" = "*XXX")` |

| A statement with a function holding `s3.secret_key` | Before | After |
|---|---|---|
| `INSERT ... SELECT` or `SELECT` reading it | masked | masked |
| `CREATE TABLE ... AS SELECT` reading it, when it runs | key in clear | masked |
| the function in a CTE: CTAS, INSERT, a CTE before the INSERT, SELECT | key in clear | masked |
| the function in a subquery: INSERT, SELECT, `UPDATE ... SET`, `DELETE ... WHERE` | key in clear | masked |
| `UPDATE ... FROM`, `DELETE ... USING`, `MERGE INTO ... USING` the function | key in clear | masked |
| `INSERT INTO local(...)` or `s3(...)`: the function it writes | key in clear | masked |
| `EXPLAIN` of an INSERT, UPDATE, DELETE or MERGE that runs | key in clear | masked |

### Details

- **Frequency:** Always, for the statements in the second table, whenever the audit log records them.
- **Root cause:** `CreateTableCommand.needAuditEncryption()` asks only when the engine is not olap, and analysis sets the engine of a CTAS to olap, so a CTAS that runs is never masked. INSERT and SELECT search the plan's children for the function, and neither the queries of a CTE nor the plans of subqueries are children. UPDATE, DELETE, MERGE and `INSERT INTO` a function never ask, and the masking parser did not mask the function an INSERT writes. A running EXPLAIN is audited with the plan it explains.
- **Why existing tests missed it:** `EncryptSQLTest` reads a function only directly from INSERT and SELECT, and does not execute, so its CTAS would keep an unset engine and be masked; `testEncryption`, which holds those cases, is muted in CI (apache#71115). `StmtExecutorTest.testParsedCtasLogsMaskedSqlWhenAuditGateSkipsIt` took the CTAS gap as its premise. No regression suite audits a table function.
- **Fix:** `NeedAuditEncryption.readsTableValuedFunction(plan)` also looks in the queries of CTEs and the plans of subqueries; each command that can carry a function asks through it, `INSERT INTO` a function always asks and that function's properties are masked, and `AuditLogHelper` masks every EXPLAIN. What masking writes is unchanged, and a statement without a function keeps its text.
- **Other paths and branches:** `CREATE VIEW` and `CREATE MATERIALIZED VIEW` keep the properties in their definitions, which `SHOW CREATE` shows, so masking their audit text alone would not protect them; not changed. branch-4.2 and branch-4.1 have all of these gaps, branch-4.0 all but `INSERT INTO` a function, and branch-3.1 the CTAS check; they come from apache#47595.

### Testing

- `EncryptSQLTest#testTableValuedFunctionPropertiesMasking` (new; a method of its own since `testEncryption` is muted): sixteen statements go through `MysqlConnectProcessor` and `AuditLogHelper`, execution stubbed and the CTAS engine written out. On master all sixteen keep the key; with the fix all are masked, and a CTAS without a function keeps its text.
- `audit/test_audit_log_tvf_masking` (new, `p0,nonConcurrent`): thirteen statements that run, the CTAS and the EXPLAINs among them, compared with the `.out`. It passes with the fix; with master's FE code it fails at its first row, `CREATE TABLE audit_tvf_ctas PROPERTIES ('replication_num' = '1') AS SELECT * FROM numbers('number' = '3', 's3.secret_key' = 'tvfmaskingsecretvalue', 'case' = 'audit_tvf_masking_ctas')`. Run locally with one BE built from master of 2026-11-25 (`be_exec_version` set to 19 for it).
- `StmtExecutorTest`, `StreamingInsertTaskAuditTest`, `RepositoryAuditEncryptionTest`, `AuditEncryptionSessionVariableTest`, `InsertIntoTVFCommandTest`, `MergeIntoCommandTest`, `ExplainRefreshMTMVCommandTest` and `NereidsSqlCacheDataPolicyTest` pass locally. The ten supplementary-character cases of `EncryptSQLTest` fail as on master; with apache#71115 as well, all fourteen pass.

### Notes for Reviewers

- `StmtExecutorTest.testParsedCtasLogsMaskedSqlWhenAuditGateSkipsIt` is renamed `testParsedSqlLogsMaskedSqlWhenAuditGateSkipsIt` and now uses an olap `CREATE TABLE` with a sensitive property, which the audit log still leaves to FE log masking.
- EXPLAIN is handled in `AuditLogHelper`, not by each plan: the plan an EXPLAIN runs on can be an analyzed insert, a row-level DML plan or a function sink. An EXPLAIN with nothing to mask keeps its text.
- `readsTableValuedFunction` reads the expressions of every plan node once per audited statement, the rows of `INSERT ... VALUES` among them.
- The suite reads `numbers()`, which ignores properties it does not know; `local()` is disabled on master and `s3()` would need storage.

### Release note

The audit log no longer keeps the credentials of a table function such as `s3()` when a `CREATE TABLE ... AS SELECT`, an UPDATE, DELETE or MERGE, an `INSERT INTO` a table function, or an EXPLAIN of an INSERT, UPDATE, DELETE or MERGE uses it, or when a statement reads it in a CTE or a subquery. They are written as `*XXX`, as INSERT and SELECT already did.

### 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 tests and this description
- Human review: not stated
- Prompts: The author asked to fix directly on master, without a private report, the CTAS masking gap found while adding Doris query lineage to OpenMetadata, and to open the PRs.

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.

Source review completed across all 16 changed files and their parser, planner, execution and audit call paths. One P2 issue remains: the new shared detector misses TVF subqueries inside SELECT * REPLACE aliases, leaving that supported form outside this masking fix. The three review rounds converged with no distinct new finding in the final round. No builds or tests were run, and nothing was published.

Review converged after 3 round(s).


/** Whether the expression holds a subquery that reads a table-valued function */
static boolean subqueryReadsTableValuedFunction(Expression expression) {
return expression.anyMatch(node -> node instanceof SubqueryExpr

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.

[P2] Traverse star replacement aliases when looking for subqueries

For a table t(k BIGINT), SELECT * REPLACE ((SELECT max(number) FROM numbers('number'='1', 's3.secret_key'='secret')) AS k) FROM t still bypasses masking. UnboundStar stores getReplacedAlias() outside its expression children, so this anyMatch never reaches the scalar subquery and needAuditEncryption() returns false. Audit retains the original parsed tree after star expansion, and geneEncryptionSQL() consequently returns the SQL with the plaintext credential. Please recurse into replacement aliases too (as InsertUtils.resolveDefaultReferences already does) and add this form to the masking tests.

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