Repository navigation
Commit 1b8ec34
Fix path traversal when saving Bark voice preset embeddings (#46237)
* Fix path traversal when saving Bark voice preset embeddings
The speaker_embeddings dict that BarkProcessor.from_pretrained loads from
the model repo's speaker_embeddings_path.json is fully untrusted, but its
top-level keys are written out verbatim as `<prompt_key>_<inner>.npy`
files by save_pretrained. A key like "../../foo" flows straight into
os.path.join(save_directory, speaker_embeddings_directory,
f"{prompt_key}_{key}") and lets a saved repo write a file outside the
target directory. This is a sibling of the chat_template path traversal
fixed in #46191.
Before calling np.save, reject any prompt_key whose resolved path escapes
the speaker_embeddings subdirectory. Add a regression test.
* Allow voice preset subdirectories and guard the read path
Address review feedback:
- The save-side check was too strict: it required each file's parent to
equal the speaker_embeddings directory, which rejects legitimately
nested presets such as the `v2/...` voice presets in ylacombe/bark-large.
Switch to a containment check that allows subdirectories but still
rejects any key that escapes the directory (path traversal, CWE-22).
- The per-prompt path values in speaker_embeddings_path.json are equally
untrusted and are joined onto repo_or_path before being read via
cached_file, which performs no containment check for local repos. Guard
_load_voice_preset so a "../x" value cannot traverse on the read side.
Add regression tests for the allowed-subdirectory case and the read-side
escape.
* Make Bark voice-preset traversal check lexical so symlinked caches load
`_reject_path_traversal` resolved symlinks via `Path.resolve()`, which made
`_load_voice_preset` wrongly reject legitimate loads from a populated HF cache:
snapshot files are symlinks into a sibling `blobs/` dir outside `repo_or_path`,
so the resolved target escaped the containment check.
Switch to a purely lexical `os.path.abspath`/`os.path.commonpath` check (the only
untrusted input is the relative path string from speaker_embeddings_path.json, so
no symlink resolution is needed or wanted) and drop the now-unused `Path` import.
Path traversal and absolute paths are still rejected. Add a regression test that
loads from a `snapshots/<rev> -> blobs/` symlink layout.
---------
Co-authored-by: LinZiyu <[email protected]>1 parent e820678 commit 1b8ec34
2 files changed
Lines changed: 131 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| 148 | + | |
148 | 149 | | |
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
152 | 153 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
160 | 157 | | |
161 | 158 | | |
162 | 159 | | |
| |||
166 | 163 | | |
167 | 164 | | |
168 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
169 | 185 | | |
170 | 186 | | |
171 | 187 | | |
172 | 188 | | |
173 | 189 | | |
| 190 | + | |
174 | 191 | | |
175 | 192 | | |
176 | 193 | | |
177 | 194 | | |
178 | 195 | | |
179 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
180 | 200 | | |
181 | 201 | | |
182 | 202 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 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 | + | |
117 | 221 | | |
118 | 222 | | |
119 | 223 | | |
| |||
0 commit comments