Commit 2f45f75
fix(tools/clickhouse,tools/bigquery): validate identifier parameters to prevent injection (#3219)
## Summary
This PR fixes identifier-injection vulnerabilities in three built-in
tools that interpolate caller-supplied parameters directly into SQL
without sanitisation.
---
### 1. `clickhouse-list-tables` — database identifier injection
(original fix)
`Tool.Invoke` interpolates the caller-supplied `database` parameter
directly into a `SHOW TABLES FROM %s` statement:
```go
query := fmt.Sprintf("SHOW TABLES FROM %s", database)
```
The existing comment acknowledges the risk. The fix adds a
`validIdentifier` regex (`^[A-Za-z_][A-Za-z0-9_]*$`) that must match
before interpolation.
Concrete payloads on a default ClickHouse deployment:
| `database` parameter | rendered query | effect |
|---|---|---|
| `default LIKE '%secret%'` | `SHOW TABLES FROM default LIKE '%secret%'`
| filters listing by attacker-chosen pattern |
| `system FORMAT JSONEachRow` | `SHOW TABLES FROM system FORMAT
JSONEachRow` | enumerates `system.*` (system.users, etc.) |
| `default INTO OUTFILE '/tmp/x.tsv'` | `SHOW TABLES FROM default INTO
OUTFILE '/tmp/x.tsv'` | writes to disk on ClickHouse host |
---
### 2. `bigquery-forecast` — backtick + column-name injection
Two injection classes:
**a. Backtick injection in `history_data` (table identifier path)**
```go
historyDataSource = fmt.Sprintf("TABLE `%s`", historyData)
```
When `allowed_datasets` is not configured (the default), no validation
runs before this line. An attacker-supplied `history_data` containing a
backtick closes the opening delimiter and appends arbitrary SQL:
```
history_data = "ds.tbl` UNION ALL SELECT secret FROM private.pii --"
→ TABLE `ds.tbl` UNION ALL SELECT secret FROM private.pii --`
```
**b. Column-name injection in `data_col`, `timestamp_col`, `id_cols`**
```go
sql := fmt.Sprintf(`... data_col => '%s', timestamp_col => '%s' ...`, dataCol, timestampCol, ...)
```
Column names are interpolated as single-quoted strings with no
validation. A value containing `'` breaks out of the string literal
context.
**Fix:** `ValidTableID` (restricts `history_data` to `[a-zA-Z0-9_]+`
components with 1–2 dots) applied in the table-ID else-branch;
`ValidColumnName` (`^[a-zA-Z_][a-zA-Z0-9_]*$`) applied to `data_col`,
`timestamp_col`, and each element of `id_cols`.
---
### 3. `bigquery-analyze-contribution` — backtick + OPTIONS injection
**a. Backtick injection in `input_data` (table identifier path)**
```go
inputDataSource = fmt.Sprintf("SELECT * FROM `%s`", inputData)
```
Same root cause as bigquery-forecast: when `allowed_datasets` is absent,
no validation before interpolation.
**b. Column-name and OPTIONS injection**
```go
options = append(options, fmt.Sprintf("IS_TEST_COL = '%s'", paramsMap["is_test_col"]))
// and each element of dimension_id_cols:
strCols = append(strCols, fmt.Sprintf("'%s'", c))
```
`is_test_col` and `dimension_id_cols` are column names interpolated into
BigQuery ML `OPTIONS()` string literals without validation.
`contribution_metric` (e.g. `SUM(col)/COUNT(DISTINCT col2)`) goes into
`OPTIONS(CONTRIBUTION_METRIC = '%s')`. A single-quote in this value
breaks the OPTIONS string literal.
**Fix:** `ValidTableID` for `input_data` in the table-ID else-branch;
`ValidColumnName` for `is_test_col` and each `dimension_id_cols`
element; single-quote check for `contribution_metric`.
---
## Shared validators in `bigquerycommon`
`ValidTableID` and `ValidColumnName` are exported from the
`bigquerycommon` package so both tools share the same rules. Tests are
in `validators_test.go`.
## Test plan
- [x] `go build ./...` clean
- [x] `go test ./internal/tools/bigquery/... -v` passes (all existing
tests + new `TestValidTableID`, `TestValidColumnName`)
- [x] `go test ./internal/tools/clickhouse/clickhouselisttables/... -v`
passes
## Not a dupe of #2811 / #779
Those cover the `templateParameter` flow
(`internal/util/parameters/parameters.go:applyEscape`). The three tools
fixed here use hard-coded `fmt.Sprintf` interpolation in their own
`Invoke` methods — none of the templateParameter mitigations apply.
---------
Co-authored-by: Yuan Teoh <45984206+Yuan325@users.noreply.github.com>1 parent c6f79e2 commit 2f45f75
6 files changed
Lines changed: 256 additions & 5 deletions
File tree
- internal/tools
- bigquery
- bigqueryanalyzecontribution
- bigquerycommon
- bigqueryforecast
- clickhouse/clickhouselisttables
Lines changed: 29 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
194 | 210 | | |
195 | 211 | | |
196 | | - | |
197 | | - | |
| 212 | + | |
| 213 | + | |
198 | 214 | | |
199 | 215 | | |
200 | 216 | | |
201 | 217 | | |
202 | 218 | | |
203 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
204 | 227 | | |
205 | 228 | | |
206 | 229 | | |
| |||
254 | 277 | | |
255 | 278 | | |
256 | 279 | | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
257 | 283 | | |
258 | 284 | | |
259 | 285 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
25 | 26 | | |
26 | 27 | | |
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 | + | |
28 | 54 | | |
29 | 55 | | |
30 | 56 | | |
| |||
Lines changed: 118 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 | + | |
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
194 | 206 | | |
195 | 207 | | |
196 | 208 | | |
| |||
232 | 244 | | |
233 | 245 | | |
234 | 246 | | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
235 | 250 | | |
236 | 251 | | |
237 | 252 | | |
| |||
Lines changed: 20 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
33 | 42 | | |
34 | 43 | | |
35 | 44 | | |
| |||
120 | 129 | | |
121 | 130 | | |
122 | 131 | | |
123 | | - | |
124 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
125 | 143 | | |
126 | 144 | | |
127 | 145 | | |
| |||
Lines changed: 48 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 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 | + | |
0 commit comments