Commit fb0ba86
[fix](auth) Fix CTE privilege bypass due to shared privChecked flag on StatementContext
### 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.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 60f736e commit fb0ba86
6 files changed
Lines changed: 336 additions & 14 deletions
File tree
- fe/fe-core/src
- main/java/org/apache/doris/nereids
- rules/rewrite
- test/java/org/apache/doris/nereids/privileges
- regression-test/suites/auth_p0
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
| 124 | + | |
123 | 125 | | |
124 | 126 | | |
125 | 127 | | |
| |||
520 | 522 | | |
521 | 523 | | |
522 | 524 | | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
523 | 533 | | |
524 | 534 | | |
525 | 535 | | |
| |||
Lines changed: 0 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
263 | 263 | | |
264 | 264 | | |
265 | 265 | | |
266 | | - | |
267 | | - | |
268 | 266 | | |
269 | 267 | | |
270 | 268 | | |
| |||
1014 | 1012 | | |
1015 | 1013 | | |
1016 | 1014 | | |
1017 | | - | |
1018 | | - | |
1019 | | - | |
1020 | | - | |
1021 | | - | |
1022 | | - | |
1023 | | - | |
1024 | | - | |
1025 | 1015 | | |
1026 | 1016 | | |
1027 | 1017 | | |
| |||
Lines changed: 8 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
50 | 53 | | |
51 | 54 | | |
52 | 55 | | |
53 | 56 | | |
54 | 57 | | |
55 | 58 | | |
56 | | - | |
57 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
58 | 62 | | |
59 | 63 | | |
60 | 64 | | |
61 | 65 | | |
62 | | - | |
| 66 | + | |
63 | 67 | | |
64 | 68 | | |
65 | 69 | | |
| |||
Lines changed: 70 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
228 | 298 | | |
229 | 299 | | |
230 | 300 | | |
| |||
Lines changed: 97 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
0 commit comments