Repository navigation
fix(tui): preserve npm binary executable modes #8330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ed2529b
test(tui): cover npm artifact executable modes
lawrencecchen 97de7f4
fix(tui): preserve npm binary executable modes
lawrencecchen 1d44800
test(tui): require npm archive transfer path
lawrencecchen 13721be
fix(tui): pin safe npm archive extraction
lawrencecchen 6ffc42d
test(tui): run npm artifact regression in CI
lawrencecchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| #!/usr/bin/env python3 | ||
| """Transfer generated npm package directories without losing executable modes.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import stat | ||
| import tarfile | ||
| from pathlib import Path, PurePosixPath | ||
|
|
||
|
|
||
| PACKAGE_ROOT = "npm-packages" | ||
| EXECUTABLES = ( | ||
| "cmux-tui-darwin-arm64/bin/cmux-tui", | ||
| "cmux-tui-darwin-x64/bin/cmux-tui", | ||
| "cmux-tui-linux-x64/bin/cmux-tui", | ||
| "cmux-tui-linux-arm64/bin/cmux-tui", | ||
| "cmux/bin/cmux.js", | ||
| ) | ||
| MAX_MEMBERS = 1_024 | ||
| MAX_EXPANDED_BYTES = 512 * 1024 * 1024 | ||
|
|
||
|
|
||
| def verify_executables(packages_dir: Path) -> None: | ||
| if not packages_dir.is_dir(): | ||
| raise SystemExit(f"missing npm package directory: {packages_dir}") | ||
| if packages_dir.name != PACKAGE_ROOT: | ||
| raise SystemExit(f"npm package directory must be named {PACKAGE_ROOT}") | ||
|
|
||
| for relative_path in EXECUTABLES: | ||
| executable = packages_dir / relative_path | ||
| if not executable.is_file(): | ||
| raise SystemExit(f"missing npm package executable: {executable}") | ||
| if not executable.stat().st_mode & stat.S_IXUSR: | ||
| raise SystemExit(f"npm package entry is not executable: {executable}") | ||
|
|
||
|
|
||
| def create_archive(packages_dir: Path, archive: Path) -> None: | ||
| packages_dir = packages_dir.resolve() | ||
| archive = archive.resolve() | ||
| verify_executables(packages_dir) | ||
| if archive == packages_dir or packages_dir in archive.parents: | ||
| raise SystemExit("npm package archive must be outside the package directory") | ||
| archive.parent.mkdir(parents=True, exist_ok=True) | ||
| if archive.exists(): | ||
| archive.unlink() | ||
| with tarfile.open(archive, "w:gz") as output: | ||
| output.add(packages_dir, arcname=PACKAGE_ROOT, recursive=True) | ||
|
|
||
|
|
||
| def validated_members(archive: tarfile.TarFile) -> list[tarfile.TarInfo]: | ||
| members = archive.getmembers() | ||
| if not members: | ||
| raise SystemExit("npm package archive is empty") | ||
| if len(members) > MAX_MEMBERS: | ||
| raise SystemExit("npm package archive has too many entries") | ||
|
|
||
| seen: set[str] = set() | ||
| expanded_bytes = 0 | ||
| for member in members: | ||
| path = PurePosixPath(member.name) | ||
| if path.is_absolute() or ".." in path.parts: | ||
| raise SystemExit(f"unsafe npm package archive path: {member.name}") | ||
| if not path.parts or path.parts[0] != PACKAGE_ROOT: | ||
| raise SystemExit(f"npm package archive entry is outside {PACKAGE_ROOT}") | ||
| normalized_name = path.as_posix() | ||
| if normalized_name in seen: | ||
| raise SystemExit(f"duplicate npm package archive entry: {member.name}") | ||
| seen.add(normalized_name) | ||
| if not (member.isfile() or member.isdir()): | ||
| raise SystemExit(f"unsupported npm package archive entry: {member.name}") | ||
| member.mode &= 0o777 | ||
| expanded_bytes += member.size | ||
| if expanded_bytes > MAX_EXPANDED_BYTES: | ||
| raise SystemExit("npm package archive exceeds expanded size limit") | ||
| return members | ||
|
|
||
|
|
||
| def extract_archive(archive_path: Path, out_dir: Path) -> None: | ||
| archive_path = archive_path.resolve() | ||
| out_dir = out_dir.resolve() | ||
| packages_dir = out_dir / PACKAGE_ROOT | ||
| if packages_dir.exists(): | ||
| raise SystemExit(f"refusing to replace existing directory: {packages_dir}") | ||
| out_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| with tarfile.open(archive_path, "r:gz") as archive: | ||
| members = validated_members(archive) | ||
| archive.extractall(out_dir, members=members, filter="tar") | ||
| verify_executables(packages_dir) | ||
|
|
||
|
|
||
| def parse_args() -> argparse.Namespace: | ||
| parser = argparse.ArgumentParser() | ||
| subparsers = parser.add_subparsers(dest="command", required=True) | ||
|
|
||
| create = subparsers.add_parser("create") | ||
| create.add_argument("--packages-dir", required=True, type=Path) | ||
| create.add_argument("--archive", required=True, type=Path) | ||
|
|
||
| extract = subparsers.add_parser("extract") | ||
| extract.add_argument("--archive", required=True, type=Path) | ||
| extract.add_argument("--out", required=True, type=Path) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main() -> None: | ||
| args = parse_args() | ||
| if args.command == "create": | ||
| create_archive(args.packages_dir, args.archive) | ||
| else: | ||
| extract_archive(args.archive, args.out) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import shutil | ||
| import stat | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| ROOT = Path(__file__).resolve().parents[1] | ||
| SCRIPT = ROOT / "cmux-tui/dist/scripts/package_npm_artifact.py" | ||
| EXECUTABLES = ( | ||
| "cmux-tui-darwin-arm64/bin/cmux-tui", | ||
| "cmux-tui-darwin-x64/bin/cmux-tui", | ||
| "cmux-tui-linux-x64/bin/cmux-tui", | ||
| "cmux-tui-linux-arm64/bin/cmux-tui", | ||
| "cmux/bin/cmux.js", | ||
| ) | ||
|
|
||
|
|
||
| def run_helper(*args: object) -> subprocess.CompletedProcess[str]: | ||
| return subprocess.run( | ||
| [sys.executable, str(SCRIPT), *(str(arg) for arg in args)], | ||
| cwd=ROOT, | ||
| check=False, | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
|
|
||
|
|
||
| def test_archive_round_trip_preserves_package_executables(tmp_path: Path) -> None: | ||
|
lawrencecchen marked this conversation as resolved.
|
||
| packages = tmp_path / "npm-packages" | ||
| for relative_path in EXECUTABLES: | ||
| executable = packages / relative_path | ||
| executable.parent.mkdir(parents=True, exist_ok=True) | ||
| executable.write_text("#!/bin/sh\nexit 0\n") | ||
| executable.chmod(0o755) | ||
|
|
||
| archive = tmp_path / "npm-packages.tar.gz" | ||
| created = run_helper( | ||
| "create", "--packages-dir", packages, "--archive", archive | ||
| ) | ||
| assert created.returncode == 0, created.stderr | ||
|
|
||
| # GitHub artifact transfer may normalize the outer file mode. The archive | ||
| # must still restore executable package entries after download. | ||
| archive.chmod(0o644) | ||
| shutil.rmtree(packages) | ||
|
|
||
| extracted = run_helper("extract", "--archive", archive, "--out", tmp_path) | ||
| assert extracted.returncode == 0, extracted.stderr | ||
| for relative_path in EXECUTABLES: | ||
| mode = (packages / relative_path).stat().st_mode | ||
| assert mode & stat.S_IXUSR, relative_path | ||
|
|
||
|
|
||
| def test_extract_rejects_paths_outside_package_root(tmp_path: Path) -> None: | ||
| archive = tmp_path / "npm-packages.tar.gz" | ||
| # Build the hostile archive without relying on the helper under test. | ||
| import io | ||
| import tarfile | ||
|
|
||
| with tarfile.open(archive, "w:gz") as tar: | ||
| info = tarfile.TarInfo("../outside") | ||
| contents = b"unexpected" | ||
| info.size = len(contents) | ||
| tar.addfile(info, io.BytesIO(contents)) | ||
|
|
||
| extracted = run_helper("extract", "--archive", archive, "--out", tmp_path) | ||
| assert extracted.returncode != 0 | ||
| assert not (tmp_path.parent / "outside").exists() | ||
|
|
||
|
|
||
| def test_publish_workflows_restore_the_mode_preserving_archive() -> None: | ||
| build = (ROOT / ".github/workflows/cmux-tui-build-package.yml").read_text() | ||
| stable = (ROOT / ".github/workflows/tui-publish-npm.yml").read_text() | ||
| nightly = (ROOT / ".github/workflows/cmux-tui-nightly.yml").read_text() | ||
|
|
||
| assert "package_npm_artifact.py create" in build | ||
| assert "path: dist/npm-packages.tar.gz" in build | ||
| for workflow in (stable, nightly): | ||
| assert "package_npm_artifact.py extract" in workflow | ||
| assert "--archive dist/npm-packages.tar.gz" in workflow | ||
|
|
||
|
|
||
| def main() -> None: | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| test_archive_round_trip_preserves_package_executables(Path(directory)) | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| test_extract_rejects_paths_outside_package_root(Path(directory)) | ||
| test_publish_workflows_restore_the_mode_preserving_archive() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.