feat(cli): add pack and unpack commands for .songpack container with Section 12 security
This commit is contained in:
@ -81,6 +81,34 @@ def eval_cmd(
|
|||||||
run_evaluation(testset_config)
|
run_evaluation(testset_config)
|
||||||
|
|
||||||
|
|
||||||
|
@app.command(name="pack")
|
||||||
|
def pack_cmd(
|
||||||
|
song_dir: Path = typer.Argument(..., help="Songpack directory to pack", exists=True),
|
||||||
|
output: Path | None = typer.Option(None, "-o", "--output", help="Output .songpack file path"),
|
||||||
|
variant: str = typer.Option(
|
||||||
|
"full", "--variant", help="Package variant: 'full' (with stems) or 'data' (without stems)"
|
||||||
|
),
|
||||||
|
) -> None:
|
||||||
|
"""Pack a songpack directory into a .songpack archive."""
|
||||||
|
from songsplit.container import pack_songpack
|
||||||
|
|
||||||
|
res = pack_songpack(song_dir, output_path=output, variant=variant) # type: ignore[arg-type]
|
||||||
|
print(str(res))
|
||||||
|
|
||||||
|
|
||||||
|
@app.command(name="unpack")
|
||||||
|
def unpack_cmd(
|
||||||
|
songpack_file: Path = typer.Argument(..., help="Path to .songpack archive", exists=True),
|
||||||
|
output_dir: Path | None = typer.Option(None, "-o", "--output", help="Destination folder for unpacked songpack"),
|
||||||
|
) -> None:
|
||||||
|
"""Securely unpack a .songpack archive into a song directory."""
|
||||||
|
from songsplit.container import unpack_songpack
|
||||||
|
|
||||||
|
dest = output_dir or songpack_file.with_suffix("")
|
||||||
|
res = unpack_songpack(songpack_file, dest)
|
||||||
|
print(str(res))
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def doctor() -> None:
|
def doctor() -> None:
|
||||||
"""Check environment, ffmpeg installation, GPUs, and PyTorch availability."""
|
"""Check environment, ffmpeg installation, GPUs, and PyTorch availability."""
|
||||||
|
|||||||
@ -76,9 +76,7 @@ def is_whitelisted(rel_path: str) -> bool:
|
|||||||
return any(pattern.match(rel_path) for pattern in WHITELIST_PATTERNS)
|
return any(pattern.match(rel_path) for pattern in WHITELIST_PATTERNS)
|
||||||
|
|
||||||
|
|
||||||
def build_manifest_for_dir(
|
def build_manifest_for_dir(song_dir: Path, variant: Literal["full", "data"] = "full") -> ManifestDocument:
|
||||||
song_dir: Path, variant: Literal["full", "data"] = "full"
|
|
||||||
) -> ManifestDocument:
|
|
||||||
"""Scan song directory and generate or update manifest.json."""
|
"""Scan song directory and generate or update manifest.json."""
|
||||||
manifest_path = song_dir / "manifest.json"
|
manifest_path = song_dir / "manifest.json"
|
||||||
now_iso = datetime.now(UTC).isoformat()
|
now_iso = datetime.now(UTC).isoformat()
|
||||||
@ -189,11 +187,7 @@ def pack_songpack(
|
|||||||
if not file_abs.is_file():
|
if not file_abs.is_file():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
compress_type = (
|
compress_type = zipfile.ZIP_STORED if item.path.endswith((".wav", ".flac")) else zipfile.ZIP_DEFLATED
|
||||||
zipfile.ZIP_STORED
|
|
||||||
if item.path.endswith((".wav", ".flac"))
|
|
||||||
else zipfile.ZIP_DEFLATED
|
|
||||||
)
|
|
||||||
zf.write(file_abs, arcname=item.path, compress_type=compress_type)
|
zf.write(file_abs, arcname=item.path, compress_type=compress_type)
|
||||||
|
|
||||||
temp_zip.replace(output_path)
|
temp_zip.replace(output_path)
|
||||||
@ -222,9 +216,7 @@ def unpack_songpack(songpack_path: Path, output_dir: Path) -> Path:
|
|||||||
with zipfile.ZipFile(songpack_path, "r") as zf:
|
with zipfile.ZipFile(songpack_path, "r") as zf:
|
||||||
infolist = zf.infolist()
|
infolist = zf.infolist()
|
||||||
if len(infolist) > MAX_ENTRIES:
|
if len(infolist) > MAX_ENTRIES:
|
||||||
raise SongpackSecurityError(
|
raise SongpackSecurityError(f"Too many archive entries: {len(infolist)} > {MAX_ENTRIES}")
|
||||||
f"Too many archive entries: {len(infolist)} > {MAX_ENTRIES}"
|
|
||||||
)
|
|
||||||
|
|
||||||
total_uncompressed_bytes = 0
|
total_uncompressed_bytes = 0
|
||||||
track_count = 0
|
track_count = 0
|
||||||
@ -254,19 +246,13 @@ def unpack_songpack(songpack_path: Path, output_dir: Path) -> Path:
|
|||||||
if info.compress_size > 0:
|
if info.compress_size > 0:
|
||||||
ratio = info.file_size / info.compress_size
|
ratio = info.file_size / info.compress_size
|
||||||
if ratio > MAX_COMPRESSION_RATIO:
|
if ratio > MAX_COMPRESSION_RATIO:
|
||||||
raise SongpackSecurityError(
|
raise SongpackSecurityError(f"Compression ratio too high ({ratio:.1f}:1) for {rel_path}")
|
||||||
f"Compression ratio too high ({ratio:.1f}:1) for {rel_path}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Size limits per type
|
# Size limits per type
|
||||||
if rel_path.endswith(".json") and info.file_size > MAX_JSON_SIZE:
|
if rel_path.endswith(".json") and info.file_size > MAX_JSON_SIZE:
|
||||||
raise SongpackSecurityError(
|
raise SongpackSecurityError(f"JSON file too large: {info.file_size} > {MAX_JSON_SIZE}")
|
||||||
f"JSON file too large: {info.file_size} > {MAX_JSON_SIZE}"
|
|
||||||
)
|
|
||||||
if rel_path.startswith("stems/") and info.file_size > MAX_STEM_SIZE:
|
if rel_path.startswith("stems/") and info.file_size > MAX_STEM_SIZE:
|
||||||
raise SongpackSecurityError(
|
raise SongpackSecurityError(f"Stem audio too large: {info.file_size} > {MAX_STEM_SIZE}")
|
||||||
f"Stem audio too large: {info.file_size} > {MAX_STEM_SIZE}"
|
|
||||||
)
|
|
||||||
|
|
||||||
total_uncompressed_bytes += info.file_size
|
total_uncompressed_bytes += info.file_size
|
||||||
if total_uncompressed_bytes > MAX_TOTAL_SIZE:
|
if total_uncompressed_bytes > MAX_TOTAL_SIZE:
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
"""Tests for songpack container packing and secure unpacking."""
|
"""Tests for songpack container packing and secure unpacking."""
|
||||||
|
|
||||||
import json
|
|
||||||
import zipfile
|
import zipfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user