Repository navigation
Commit 90573b4
authored
Do not evaluate imports when reading an algorithm score (#9254)
### Description
`BundleAlgo.get_score()` builds a `ConfigParser` from
`configs/hyper_parameters.yaml` in the algorithm output directory and
evaluates `ckpt_path` with `eval()`. That directory is not always one
this process wrote: `import_bundle_algo_history()` walks any work
directory (a collaborator's results archive, a shared mount, a
downloaded model-zoo artifact), and the caller wraps `get_score()` in a
bare `except Exception: pass`, so a `$` expression in that file runs
code and leaves no trace.
Reproduced before the change: a work directory with
```yaml
ckpt_path: "$__import__('pathlib').Path('/tmp/marker').write_text('x') and '/tmp/none'"
```
writes the marker when `import_bundle_algo_history()` is called, and the
resulting `FileNotFoundError` is swallowed, so the import looks like a
normal untrained directory.
`get_score()` no longer calls `eval` for this value. `ckpt_path` is
resolved by a small dedicated reader that accepts exactly the shapes the
shipped templates use:
- a plain string,
- `+` concatenation of string literals,
- `@references` to other entries, and `str()` around a single reference,
which covers the template forms `$@bundle_root + '/model'` (segresnet)
and `$@bundle_root + '/model_fold' + str(@fold)` (dints, swinunetr).
Any other call, attribute access, comprehensions and `_target_` mappings
raise a `ValueError`; an integer reference is accepted only inside
`str()`, never concatenated directly. Nothing is instantiated:
`get_parsed_content` is not used, so the `instantiate=True` default
cannot construct a component from the file.
This matters because two weaker variants do not hold. Emptying
`__builtins__` still leaves `eval` reachable through
`().__class__.__base__.__subclasses__()`, and `get_parsed_content` would
instantiate a `_target_` mapping before any type check.
`import_bundle_algo_history()` also logs the failure at warning level
instead of dropping it, so a refused config and a missing checkpoint
directory are no longer indistinguishable.
### Behaviour changes
A `ckpt_path` that relied on a call or an attribute no longer resolves;
no shipped template does either, and the plain and `@reference` forms
are covered by tests. A `ckpt_path` that fails to resolve previously
produced a silent `None` score and now also logs a warning. Directories
whose `progress.yaml` is missing behave as before.
### Tests
New `tests/auto3dseg/test_algo_score_config.py`:
- a `$__import__` payload raises and the marker is not written
- the `__subclasses__()` escape raises and the marker is not written
- a `_target_` mapping raises instead of being instantiated
- module access (`$str(torch.tensor(1))`) raises
- a plain path, string concatenation, `$@bundle_root + '/model'`,
`$@bundle_root + '/model_fold' + str(@fold)` and a whole-value
`@reference` all resolve and return the score
- an integer reference concatenated directly, a reference-table key that
was not a matched `@reference`, and a reference to an id missing from
the file all raise `ValueError`
- end to end, importing a work directory with a malicious config does
not execute it and logs a warning
The payload tests fail on the current `dev` (the expression runs) and
pass with the change. `pytest tests/auto3dseg -q` gives 49 passed, 1
skipped.
### Types of changes
- [x] Non-breaking change for documented bundle templates.
- [x] New tests added to cover the changes.
- [x] In-line docstrings updated.
Signed-off-by: R. Garcia-Dias <rafaelagd@gmail.com>
---------
Signed-off-by: R. Garcia-Dias <rafaelagd@gmail.com>1 parent 2cf2fc3 commit 90573b4
3 files changed
Lines changed: 404 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| 45 | + | |
44 | 46 | | |
| 47 | + | |
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
| |||
99 | 102 | | |
100 | 103 | | |
101 | 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 | + | |
102 | 223 | | |
103 | 224 | | |
104 | 225 | | |
| |||
119 | 240 | | |
120 | 241 | | |
121 | 242 | | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
122 | 252 | | |
123 | 253 | | |
124 | 254 | | |
| |||
377 | 507 | | |
378 | 508 | | |
379 | 509 | | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
380 | 525 | | |
381 | 526 | | |
382 | | - | |
| 527 | + | |
383 | 528 | | |
384 | | - | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
385 | 532 | | |
386 | 533 | | |
387 | 534 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | | - | |
63 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
64 | 67 | | |
65 | 68 | | |
66 | 69 | | |
| |||
0 commit comments