seawinde opened a new pull request, #62339:
URL: https://github.com/apache/doris/pull/62339

   ### What problem does this PR solve?
   
   Issue Number: N/A
   
   Related PR: #44621
   
   Problem Summary:
   
   When a query uses a Common Table Expression (CTE) that references an 
unauthorized table,
   the privilege check is incorrectly skipped, allowing the query to succeed 
without proper
   authorization.
   
   **Root cause:** `CheckPrivileges.rewriteRoot()` uses 
`StatementContext.isPrivChecked()` as
   a guard to run only once. However, CTE processing 
(`RewriteCteChildren.visitLogicalCTEAnchor()`)
   creates separate `CascadesContext` subtrees for consumer and producer that 
**share** the same
   `StatementContext`. The consumer subtree is processed first, its 
`CheckPrivileges` sets
   `privChecked=true` on the shared `StatementContext`, and when the producer 
subtree runs,
   `CheckPrivileges` sees the flag and skips — leaving unauthorized tables in 
the CTE body
   unchecked.
   
   ```
   StatementContext (shared)
   ├─ CascadesContext (consumer) ── CheckPrivileges runs ──> sets 
privChecked=true
   └─ CascadesContext (producer) ── CheckPrivileges sees true ──> SKIPPED (BUG)
   ```
   
   **Fix:** Move the `privChecked` flag from `StatementContext` to 
`CascadesContext`.
   Since `newSubtreeContext()` creates a **new** `CascadesContext` instance for 
each CTE
   subtree, each subtree now has its own independent flag. This ensures both 
producer and
   consumer run their own privilege checks. Within the same `CascadesContext`, 
the flag
   still prevents duplicate checks (e.g., after view inlining — preserving 
#44621 semantics).
   
   ```
   StatementContext (shared, no privChecked)
   ├─ CascadesContext (consumer) ── privChecked=false ── CheckPrivileges runs 
── privChecked=true
   └─ CascadesContext (producer) ── privChecked=false ── CheckPrivileges runs 
── privChecked=true ✓
   ```
   
   | File | Change Description |
   |------|-------------------|
   | `CascadesContext.java` | Add `privChecked` boolean field + getter/setter |
   | `StatementContext.java` | Remove `privChecked` field and accessors |
   | `CheckPrivileges.java` | Read/write `privChecked` on `CascadesContext` 
instead of `StatementContext` |
   | `TestCheckPrivileges.java` | Add `testCtePrivilegeCheck()` with 4 CTE 
authorization scenarios |
   | `TestCtePrivCheckGranularity.java` (new) | 4 unit tests verifying 
per-CascadesContext flag independence |
   | `test_cte_privilege_check.groovy` (new) | 6 regression test cases covering 
CTE privilege bypass |
   
   ### Release note
   
   Fixed a privilege bypass where queries using CTE (Common Table Expression) 
could access
   unauthorized tables without being denied. The privilege check was 
incorrectly shared across
   CTE subtrees, causing the producer's check to be skipped after the 
consumer's check ran.
   
   ### Check List (For Author)
   
   - Test
       - [x] Regression test
       - [x] Unit Test
   
   - Behavior changed:
       - [x] No.
   
   - Does this need documentation?
       - [x] No.
   
   ### Check List (For Reviewer who merge this PR)
   
   - [ ] Confirm the release note
   - [ ] Confirm test cases
   - [ ] Confirm document
   - [ ] Add branch pick label
   


-- 
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]

Reply via email to