Commit 62dc056
fix(security): bind public dashboard queries to their published shapes (#111)
## The problem
`PublicDashboardController.query` took the SQL to run as a
**caller-supplied body field** and never compared it against the
dashboard being shared:
```java
public record PublicQueryRequest(String sql, Integer limit) { }
```
The only check was `validateReadOnlySql`, which asks whether a statement
*reads* — not whether it is a query this dashboard was ever published to
run. The endpoint is `permitAll` via `/public/**`, so a link created to
publish **one chart** granted **anonymous, unauthenticated read of every
table on that connection**:
```bash
curl -X POST https://host/api/public/dashboards/$TOKEN/query \
-d '{"sql":"SELECT * FROM users","limit":5000}'
```
Share tokens are 192-bit, so this was never brute-forceable — the
exposure is to whoever receives or forwards a link, which is exactly the
population a share link is meant to be safe for.
## Why an exact match couldn't be the fix
Public dashboards are **interactive by design**.
`dashboard-design/SKILL.md:148` instructs the agent to build date
pickers that re-query on change, and `:54` states plainly:
> There is **no placeholder convention**. You write normal SQL strings
in JS.
So the exact string isn't knowable at publish time. Exact matching would
break interactive dashboards **only on the public link** — working for
the author, failing for the audience. That's the worst shape a
regression can take.
## The fix: match the shape, not the text
`DashboardQueryShapeService` extracts every query the artifact can
issue, normalizes each to a **shape** (literals → placeholders, via the
existing `QueryNormalizer` that already backs
`QueryFingerprintService`), and requires a match.
| Incoming query | Result |
|---|---|
| Same query, different date range | ✅ allowed — interactivity preserved
|
| Whitespace / newline / case variants | ✅ allowed |
| `SELECT * FROM users` | 🚫 refused |
| Same shape, different table | 🚫 refused |
| Same shape, different column | 🚫 refused |
| `OR 1=1` appended | 🚫 refused |
| Escaped-quote `UNION` inside a literal | 🚫 refused |
**The last row is refused for a non-obvious reason worth knowing.** The
payload `'2026-03-01 '' UNION SELECT password FROM users --'` normalizes
to `… between ? and ??` — **two** placeholders, not one — because the
`'[^']*'` rule doesn't model SQL's `''` escape and splits the literal
differently than the database would. The shape changes, so it misses.
**The imprecision fails in the safe direction:** smuggling structure
through a literal perturbs the shape.
### Extraction is static
`dashboard_config` stores the artifact as one HTML document with queries
as JS template literals, so the SQL is statically present — it just
carries `${…}` interpolation, which maps cleanly onto the normalizer's
placeholders. A test asserts directly that **the shape extracted from
the artifact equals the shape of the SQL issued at runtime** — that seam
is what the design rests on, so it's pinned rather than assumed.
Chosen over capturing shapes at first render: no extra step, and it
can't produce a partly-captured set that breaks a link for its audience.
### Fails closed
An unmatched shape is refused. The artifact already renders a per-widget
error, so one widget degrades alone and the rest of the dashboard keeps
working.
## Second defect: a policy added *after* sharing didn't apply
Enabling a share is refused while a connection has an active chat-access
policy (`SavedDashboardController:81`) — but **nothing re-checked
afterwards**. A link created *before* a policy was added stayed live,
and on that path `"public-share"` has no policy row, so
`resolveEffectivePolicy` returns `none()` and column protections and PII
redaction never ran.
Narrower than "public callers bypass all policies" (the normal flow
can't create that combination), but a real TOCTOU gap. Now re-checked
per query — same reason `is_public` already is: **revocation has to
reach an already-issued link.**
## Defence in depth
The shape gate is a new *primary* control, not a replacement.
`validateReadOnlySql`, `setReadOnly(true)`, the row cap and the
`is_public` re-check all remain. This matters because `QueryNormalizer`
was built for analytics grouping, where a collision is cosmetic; as a
security boundary it would be a vulnerability. It's deliberately one
layer among several.
## Verification
| Step | Result |
|---|---|
| Before the service existed (RED) | compilation failure — class not
found |
| After the fix (GREEN) | **13 pass** |
| `matches()` stubbed to `return true` (mutation) | **5 fail** —
exfiltration + fail-closed cases |
| Restored | green again |
| Related suites | **50 tests, 0 failures** |
| `mvn compile` | clean |
```bash
cd backend && mvn test -Dtest=DashboardQueryShapeServiceTest
```
## Residual work (deliberately not here)
The public path still has **no rate limit** —
`docker/nginx/default.conf` declares only `sqlexec`, scoped to
`^/api/connections/[^/]+/query$`. CLAUDE.md claimed a `dashq` limiter
existed; **it never did**. Shape binding bounds the damage (an anonymous
caller can now only re-run the dashboard's own queries), but a heavy
widget can still be hammered. Separate nginx change with its own blast
radius.
Design:
`docs/superpowers/specs/2026-09-11-public-dashboard-query-binding-design.md`
Write-up: `docs/security/2026-09-11-public-dashboard-arbitrary-sql.md`
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Venkat SF <venkatesh.sakamuri@stayflexi.com>1 parent 89432da commit 62dc056
6 files changed
Lines changed: 928 additions & 0 deletions
File tree
- backend/src
- main/java/com/dbaagent
- controller
- service
- test/java/com/dbaagent/service
- docs
- security
- superpowers/specs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
261 | 261 | | |
262 | 262 | | |
263 | 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 | + | |
264 | 288 | | |
265 | 289 | | |
266 | 290 | | |
| |||
Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| 23 | + | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
| |||
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
| 49 | + | |
| 50 | + | |
46 | 51 | | |
47 | 52 | | |
48 | 53 | | |
| |||
101 | 106 | | |
102 | 107 | | |
103 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
104 | 132 | | |
105 | 133 | | |
106 | 134 | | |
| |||
Lines changed: 240 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 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
0 commit comments