Skip to content

VoiceStudio is the open-source, fully-local ElevenLabs alternative — voice cloning, voice design, video dubbing, dictation, transcription & audiobook creation in 646 languages.

fix(mobile): safe extraction of native source archives (CodeQL py/tarslip)

debpalash458bd0f39816
diff --git a/mobile/native/android/fetch.py b/mobile/native/android/fetch.py
index c18709ba1..b5a04f6f5 100644
--- a/mobile/native/android/fetch.py
+++ b/mobile/native/android/fetch.py
@@ -35,18 +35,26 @@ def extract(archive, destination):
temporary = destination.with_name(destination.name + '.extract')
shutil.rmtree(temporary, ignore_errors=True)
temporary.mkdir(parents=True)
+ root = temporary.resolve()
with tarfile.open(archive) as source:
- # Reject path escapes and symlinks out of the source tree, including on
- # Python versions predating tarfile's data extraction filter.
- for member in source.getmembers():
- path = pathlib.PurePosixPath(member.name)
- if path.is_absolute() or '..' in path.parts:
- raise ValueError('Unsafe source archive path')
- if member.islnk() or member.issym():
- link = pathlib.PurePosixPath(member.linkname)
- if link.is_absolute() or '..' in link.parts:
- raise ValueError('Unsafe source archive link')
- source.extractall(temporary)
+ if hasattr(tarfile, 'data_filter'):
+ # Python's own safe-extraction rules: no absolute paths, no '..',
+ # no links leaving the tree, no device files.
+ source.extractall(root, filter='data')
+ else:
+ # Older Pythons: check every entry's resolved destination (and link
+ # target) stays inside the tree, then extract that entry alone.
+ for member in source.getmembers():
+ target = (root / member.name).resolve()
+ if target != root and root not in target.parents:
+ raise ValueError('Unsafe source archive path')
+ if member.isdev() or member.isfifo():
+ raise ValueError('Unsupported source archive entry')
+ if member.issym() or member.islnk():
+ link = ((target.parent if member.issym() else root) / member.linkname).resolve()
+ if link != root and root not in link.parents:
+ raise ValueError('Unsafe source archive link')
+ source.extract(member, root)
entries = list(temporary.iterdir())
if len(entries) != 1:
raise ValueError('Unexpected source archive layout')
diff --git a/tests/test_mobile_contract.py b/tests/test_mobile_contract.py
index 3acb10302..b51096001 100644
--- a/tests/test_mobile_contract.py
+++ b/tests/test_mobile_contract.py
@@ -329,3 +329,43 @@ def test_mobile_strings_are_not_placeholder_only():
if isinstance(value, str) and placeholder.search(value) and not re.search(r"\w", placeholder.sub("", value)):
bad.append(f"{path.name}: {key} = {value!r}")
assert not bad, "write the words around the placeholders:\n" + "\n".join(bad)
+
+
[email protected]("data_filter", [True, False])
[email protected]("entry", ["../escape.txt", "/abs.txt", "link-out"])
+def test_android_native_fetch_rejects_unsafe_archives(tmp_path, monkeypatch, data_filter, entry):
+ # Pinned source tarballs are checksum-verified, but extraction must still refuse
+ # path escapes and links out of the tree, on Pythons with and without
+ # tarfile's data filter (CodeQL py/tarslip).
+ import importlib.util
+ import io
+ import tarfile
+
+ spec = importlib.util.spec_from_file_location("vs_android_fetch", ROOT / "mobile/native/android/fetch.py")
+ fetch = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(fetch)
+ if not data_filter:
+ monkeypatch.delattr(tarfile, "data_filter", raising=False)
+ archive = tmp_path / "src.tar.gz"
+ with tarfile.open(archive, "w:gz") as tar:
+ if entry == "link-out":
+ info = tarfile.TarInfo("src/link-out")
+ info.type, info.linkname = tarfile.SYMTYPE, "../../outside"
+ tar.addfile(info)
+ else:
+ payload = b"x"
+ info = tarfile.TarInfo(entry)
+ info.size = len(payload)
+ tar.addfile(info, io.BytesIO(payload))
+ out = tmp_path / "out"
+ try:
+ fetch.extract(archive, out)
+ except Exception:
+ assert not out.exists()
+ else:
+ # The data filter may instead neutralize an entry (e.g. strip a leading
+ # '/'); then everything must have landed inside the target.
+ assert entry == "/abs.txt" and data_filter
+ assert all(out.resolve() in p.resolve().parents or p.resolve() == out.resolve() for p in out.rglob("*"))
+ assert not (tmp_path / "escape.txt").exists()
+ assert not (tmp_path.parent / "outside").exists()