Skip to content

Commit 1b8ec34

Browse files
LinZiyuuLinZiyu
andauthored
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

File tree

‎src/transformers/models/bark/processing_bark.py‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,15 @@ def save_pretrained(
145145

146146
embeddings_dict["repo_or_path"] = save_directory
147147

148+
embeddings_subdir = os.path.join(save_directory, speaker_embeddings_directory)
148149
for prompt_key in self.available_voice_presets:
149150
voice_preset = self._load_voice_preset(prompt_key)
150151

151152
tmp_dict = {}
152153
for key in self.speaker_embeddings[prompt_key]:
153-
np.save(
154-
os.path.join(
155-
embeddings_dict["repo_or_path"], speaker_embeddings_directory, f"{prompt_key}_{key}"
156-
),
157-
voice_preset[key],
158-
allow_pickle=False,
159-
)
154+
target_filepath = os.path.join(embeddings_subdir, f"{prompt_key}_{key}")
155+
self._reject_path_traversal(embeddings_subdir, target_filepath, prompt_key)
156+
np.save(target_filepath, voice_preset[key], allow_pickle=False)
160157
tmp_dict[key] = os.path.join(speaker_embeddings_directory, f"{prompt_key}_{key}.npy")
161158

162159
embeddings_dict[prompt_key] = tmp_dict
@@ -166,17 +163,40 @@ def save_pretrained(
166163

167164
super().save_pretrained(save_directory, push_to_hub, **kwargs)
168165

166+
@staticmethod
167+
def _reject_path_traversal(base_dir: str, target_path: str, offending_value: str):
168+
# base_dir/target_path derive from the untrusted speaker_embeddings json; allow nested
169+
# subdirectories but reject any value that escapes base_dir (path traversal, CWE-22).
170+
# The only untrusted input is the relative path string, so this is a purely lexical check:
171+
# we use os.path.abspath (not realpath/Path.resolve) and must NOT follow symlinks here.
172+
# When repo_or_path points at a populated HF cache, the referenced snapshot files are
173+
# symlinks into a sibling blobs/ directory that sits outside base_dir, so a resolve()-based
174+
# containment check would wrongly reject perfectly legitimate loads.
175+
base = os.path.abspath(base_dir)
176+
target = os.path.abspath(target_path)
177+
try:
178+
contained = os.path.commonpath([base, target]) == base
179+
except ValueError:
180+
# e.g. different Windows drives: definitely an escape.
181+
contained = False
182+
if not contained:
183+
raise ValueError(f"Invalid voice preset path: {offending_value!r}")
184+
169185
def _load_voice_preset(self, voice_preset: str | None = None, **kwargs):
170186
voice_preset_paths = self.speaker_embeddings[voice_preset]
171187

172188
voice_preset_dict = {}
173189
token = kwargs.get("token")
190+
repo_or_path = self.speaker_embeddings.get("repo_or_path", "/")
174191
for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]:
175192
if key not in voice_preset_paths:
176193
raise ValueError(
177194
f"Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}]."
178195
)
179196

197+
self._reject_path_traversal(
198+
repo_or_path, os.path.join(repo_or_path, voice_preset_paths[key]), voice_preset_paths[key]
199+
)
180200
path = cached_file(
181201
self.speaker_embeddings.get("repo_or_path", "/"),
182202
voice_preset_paths[key],

‎tests/models/bark/test_processing_bark.py‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,110 @@ def test_speaker_embeddings(self):
114114
# test loading voice preset from the hub
115115
inputs = processor(text=self.input_string, voice_preset=self.voice_preset)
116116

117+
def test_speaker_embeddings_saving_rejects_path_traversal(self):
118+
# A malicious speaker_embeddings_path.json dict key must not be usable to escape the save
119+
# directory and write attacker-controlled content to an arbitrary path (path traversal, CWE-22).
120+
tokenizer = self.get_tokenizer()
121+
seq_len = 5
122+
with tempfile.TemporaryDirectory() as tmp_dir_name:
123+
# Plant the per-prompt npy files the malicious "repo" claims to provide so that
124+
# `_load_voice_preset` succeeds and we reach the vulnerable `np.save` call.
125+
np.save(os.path.join(tmp_dir_name, "s.npy"), np.ones(seq_len))
126+
np.save(os.path.join(tmp_dir_name, "c.npy"), np.ones((2, seq_len)))
127+
np.save(os.path.join(tmp_dir_name, "f.npy"), np.ones((8, seq_len)))
128+
speaker_embeddings = {
129+
"repo_or_path": tmp_dir_name,
130+
"../../PWNED": {"semantic_prompt": "s.npy", "coarse_prompt": "c.npy", "fine_prompt": "f.npy"},
131+
}
132+
processor = BarkProcessor(tokenizer=tokenizer, speaker_embeddings=speaker_embeddings)
133+
134+
save_dir = os.path.join(tmp_dir_name, "save")
135+
# Where the "../../PWNED" key would land if traversal succeeded.
136+
canary = os.path.join(tmp_dir_name, "PWNED_semantic_prompt.npy")
137+
with self.assertRaises(ValueError):
138+
processor.save_pretrained(save_dir)
139+
self.assertFalse(os.path.exists(canary))
140+
141+
def test_speaker_embeddings_saving_allows_subdirectories(self):
142+
# Voice presets are legitimately stored in subdirectories (e.g. the `v2/...` presets in
143+
# ylacombe/bark-large), so saving a nested key must be allowed - the guard rejects escapes only.
144+
tokenizer = self.get_tokenizer()
145+
seq_len = 5
146+
with tempfile.TemporaryDirectory() as tmp_dir_name:
147+
np.save(os.path.join(tmp_dir_name, "s.npy"), np.ones(seq_len))
148+
np.save(os.path.join(tmp_dir_name, "c.npy"), np.ones((2, seq_len)))
149+
np.save(os.path.join(tmp_dir_name, "f.npy"), np.ones((8, seq_len)))
150+
speaker_embeddings = {
151+
"repo_or_path": tmp_dir_name,
152+
"v2/en_speaker_0": {"semantic_prompt": "s.npy", "coarse_prompt": "c.npy", "fine_prompt": "f.npy"},
153+
}
154+
processor = BarkProcessor(tokenizer=tokenizer, speaker_embeddings=speaker_embeddings)
155+
156+
save_dir = os.path.join(tmp_dir_name, "save")
157+
processor.save_pretrained(save_dir)
158+
self.assertTrue(
159+
os.path.exists(os.path.join(save_dir, "speaker_embeddings", "v2", "en_speaker_0_semantic_prompt.npy"))
160+
)
161+
162+
def test_load_voice_preset_rejects_path_traversal(self):
163+
# The per-prompt paths in speaker_embeddings_path.json are also untrusted and are joined onto
164+
# repo_or_path before being read, so a "../x" value must be rejected before the file is loaded.
165+
tokenizer = self.get_tokenizer()
166+
seq_len = 5
167+
with tempfile.TemporaryDirectory() as tmp_dir_name:
168+
repo_dir = os.path.join(tmp_dir_name, "repo")
169+
os.makedirs(repo_dir)
170+
# A readable .npy outside the repo dir that the traversal would otherwise reach.
171+
np.save(os.path.join(tmp_dir_name, "PWNED.npy"), np.ones(seq_len))
172+
speaker_embeddings = {
173+
"repo_or_path": repo_dir,
174+
"evil": {
175+
"semantic_prompt": "../PWNED.npy",
176+
"coarse_prompt": "../PWNED.npy",
177+
"fine_prompt": "../PWNED.npy",
178+
},
179+
}
180+
processor = BarkProcessor(tokenizer=tokenizer, speaker_embeddings=speaker_embeddings)
181+
with self.assertRaises(ValueError):
182+
processor._load_voice_preset("evil")
183+
184+
def test_load_voice_preset_allows_symlinked_cache_files(self):
185+
# The path-traversal guard must be lexical, not symlink-resolving: the HF hub cache stores each
186+
# snapshot file as a symlink into a sibling `blobs/` dir (snapshots/<rev>/f -> ../../blobs/<sha>),
187+
# which sits outside repo_or_path. A resolve()/realpath()-based check would follow that symlink
188+
# out of repo_or_path and wrongly reject a legitimate load, so loading must still succeed here.
189+
tokenizer = self.get_tokenizer()
190+
seq_len = 5
191+
with tempfile.TemporaryDirectory() as tmp_dir_name:
192+
# Mimic the hub cache layout: models--org--model/{blobs,snapshots/<rev>/...}.
193+
repo_cache = os.path.join(tmp_dir_name, "models--dummy--bark")
194+
blobs_dir = os.path.join(repo_cache, "blobs")
195+
snapshot_dir = os.path.join(repo_cache, "snapshots", "deadbeef")
196+
os.makedirs(blobs_dir)
197+
os.makedirs(snapshot_dir)
198+
199+
arrays = {
200+
"semantic_prompt": np.ones(seq_len),
201+
"coarse_prompt": np.ones((2, seq_len)),
202+
"fine_prompt": np.ones((8, seq_len)),
203+
}
204+
voice_preset_paths = {}
205+
for key, array in arrays.items():
206+
blob = os.path.join(blobs_dir, key) # real content lives in blobs/
207+
np.save(blob, array, allow_pickle=False)
208+
# ...and the snapshot exposes it as a relative symlink, exactly like the real cache.
209+
link = os.path.join(snapshot_dir, f"{key}.npy")
210+
os.symlink(os.path.relpath(blob + ".npy", snapshot_dir), link)
211+
voice_preset_paths[key] = f"{key}.npy"
212+
self.assertTrue(os.path.islink(os.path.join(snapshot_dir, "semantic_prompt.npy")))
213+
214+
speaker_embeddings = {"repo_or_path": snapshot_dir, "preset": voice_preset_paths}
215+
processor = BarkProcessor(tokenizer=tokenizer, speaker_embeddings=speaker_embeddings)
216+
217+
voice_preset = processor._load_voice_preset("preset")
218+
for key, array in arrays.items():
219+
self.assertTrue(np.array_equal(voice_preset[key], array))
220+
117221
def test_tokenizer(self):
118222
tokenizer = self.get_tokenizer()
119223

0 commit comments

Comments
 (0)