ci(block): refuse workspace aliases before cleanup

This commit is contained in:
pingqiu committed 2026-12-05 00:34:43 -08:00
1 parent 12b9f2f037
commit 58347a55f0
6 files changed
+156 -61

No files matched your search

+16 -1
View File
@@ -21,7 +21,7 @@ def step(text, name):
def code(block):
return textwrap.dedent(block.split(" run: |\n", 1)[1]).split("python3 - <<'PY'\n", 1)[1].rsplit("\nPY", 1)[0]
return textwrap.dedent(block.split(" run: |\n", 1)[1]).split("\n", 1)[1].rsplit("\nPY", 1)[0]
def execute(source, workspace, temp, free=None):
@@ -49,6 +49,8 @@ class RunnerWorkspaceTests(unittest.TestCase):
self.assertNotIn("- name:", text.split("- name: Clean job workspace", 1)[1])
self.assertIn("working-directory: ${{ runner.temp }}", before)
self.assertIn("working-directory: ${{ runner.temp }}", after)
self.assertIn("python3 -I -", before)
self.assertIn("python3 -I -", after)
def test_low_space_refuses_and_boundary_admits(self):
for path in WORKFLOWS:
@@ -87,5 +89,18 @@ class RunnerWorkspaceTests(unittest.TestCase):
alias = root / "alias"; alias.symlink_to(work, target_is_directory=True)
self.assertEqual(execute(source, alias, root)[0], 1)
def test_cleanup_refuses_symlinked_ancestors_and_preserves_external_sentinel(self):
for path in WORKFLOWS:
with self.subTest(workflow=path.name), tempfile.TemporaryDirectory() as temp:
base = Path(temp)
external = base / "external/repo/repo"; external.mkdir(parents=True)
sentinel = external / "keep"; sentinel.write_text("outside checkout")
alias = base / "alias"; alias.symlink_to(base / "external", target_is_directory=True)
source = code(step(path.read_text(), "Clean job workspace"))
rc, out = execute(source, alias / "repo/repo", base)
self.assertTrue(sentinel.is_file(), "cleanup followed a workspace ancestor outside the checkout")
self.assertEqual(rc, 1, out)
self.assertIn("runner_workspace_cleanup_refused", out)
if __name__ == "__main__": unittest.main()
+28 -12
View File
@@ -29,7 +29,7 @@ jobs:
- name: Runner free space
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
import shutil
import sys
@@ -96,28 +96,44 @@ jobs:
if: always() && runner.environment == 'self-hosted'
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
from pathlib import Path
import shutil
import sys
workspace = Path(os.environ["GITHUB_WORKSPACE"])
root = workspace.resolve()
# Only this runner's repository checkout, never a parent, temp/cache root or
# a symlink supplied in place of the checkout itself.
if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve():
print(f"runner_workspace_cleanup_refused path={str(workspace)!r}")
root = Path(os.environ["GITHUB_WORKSPACE"])
# Refuse aliases rather than resolving them. Open EVERY component without
# following symlinks, then keep cleanup anchored to the verified directory fd.
if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]):
print(f"runner_workspace_cleanup_refused path={str(root)!r}")
sys.exit(1)
flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
fd = os.open(root.anchor, flags)
previous = os.open(".", flags)
errors = []
if root.exists():
for entry in os.scandir(root):
try:
for part in root.parts[1:]:
child = os.open(part, flags, dir_fd=fd)
os.close(fd)
fd = child
if not shutil.rmtree.avoids_symlink_attacks:
raise OSError("descriptor-safe directory removal is unavailable")
os.fchdir(fd)
for entry in os.scandir("."):
try:
if entry.is_dir(follow_symlinks=False):
shutil.rmtree(entry.path)
shutil.rmtree(entry.name)
else:
os.unlink(entry.path)
os.unlink(entry.name)
except OSError as err:
errors.append(f"{entry.name}: {err}")
except OSError as err:
print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}")
sys.exit(1)
finally:
os.fchdir(previous)
os.close(previous)
os.close(fd)
if errors:
print("runner_workspace_cleanup_failed " + "; ".join(errors))
sys.exit(1)
+28 -12
View File
@@ -42,7 +42,7 @@ jobs:
- name: Runner free space
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
import shutil
import sys
@@ -92,28 +92,44 @@ jobs:
if: always() && runner.environment == 'self-hosted'
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
from pathlib import Path
import shutil
import sys
workspace = Path(os.environ["GITHUB_WORKSPACE"])
root = workspace.resolve()
# Only this runner's repository checkout, never a parent, temp/cache root or
# a symlink supplied in place of the checkout itself.
if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve():
print(f"runner_workspace_cleanup_refused path={str(workspace)!r}")
root = Path(os.environ["GITHUB_WORKSPACE"])
# Refuse aliases rather than resolving them. Open EVERY component without
# following symlinks, then keep cleanup anchored to the verified directory fd.
if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]):
print(f"runner_workspace_cleanup_refused path={str(root)!r}")
sys.exit(1)
flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
fd = os.open(root.anchor, flags)
previous = os.open(".", flags)
errors = []
if root.exists():
for entry in os.scandir(root):
try:
for part in root.parts[1:]:
child = os.open(part, flags, dir_fd=fd)
os.close(fd)
fd = child
if not shutil.rmtree.avoids_symlink_attacks:
raise OSError("descriptor-safe directory removal is unavailable")
os.fchdir(fd)
for entry in os.scandir("."):
try:
if entry.is_dir(follow_symlinks=False):
shutil.rmtree(entry.path)
shutil.rmtree(entry.name)
else:
os.unlink(entry.path)
os.unlink(entry.name)
except OSError as err:
errors.append(f"{entry.name}: {err}")
except OSError as err:
print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}")
sys.exit(1)
finally:
os.fchdir(previous)
os.close(previous)
os.close(fd)
if errors:
print("runner_workspace_cleanup_failed " + "; ".join(errors))
sys.exit(1)
+28 -12
View File
@@ -24,7 +24,7 @@ jobs:
- name: Runner free space
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
import shutil
import sys
@@ -149,28 +149,44 @@ jobs:
if: always() && runner.environment == 'self-hosted'
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
from pathlib import Path
import shutil
import sys
workspace = Path(os.environ["GITHUB_WORKSPACE"])
root = workspace.resolve()
# Only this runner's repository checkout, never a parent, temp/cache root or
# a symlink supplied in place of the checkout itself.
if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve():
print(f"runner_workspace_cleanup_refused path={str(workspace)!r}")
root = Path(os.environ["GITHUB_WORKSPACE"])
# Refuse aliases rather than resolving them. Open EVERY component without
# following symlinks, then keep cleanup anchored to the verified directory fd.
if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]):
print(f"runner_workspace_cleanup_refused path={str(root)!r}")
sys.exit(1)
flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
fd = os.open(root.anchor, flags)
previous = os.open(".", flags)
errors = []
if root.exists():
for entry in os.scandir(root):
try:
for part in root.parts[1:]:
child = os.open(part, flags, dir_fd=fd)
os.close(fd)
fd = child
if not shutil.rmtree.avoids_symlink_attacks:
raise OSError("descriptor-safe directory removal is unavailable")
os.fchdir(fd)
for entry in os.scandir("."):
try:
if entry.is_dir(follow_symlinks=False):
shutil.rmtree(entry.path)
shutil.rmtree(entry.name)
else:
os.unlink(entry.path)
os.unlink(entry.name)
except OSError as err:
errors.append(f"{entry.name}: {err}")
except OSError as err:
print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}")
sys.exit(1)
finally:
os.fchdir(previous)
os.close(previous)
os.close(fd)
if errors:
print("runner_workspace_cleanup_failed " + "; ".join(errors))
sys.exit(1)
+28 -12
View File
@@ -39,7 +39,7 @@ jobs:
- name: Runner free space
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
import shutil
import sys
@@ -119,28 +119,44 @@ jobs:
if: always() && runner.environment == 'self-hosted'
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
from pathlib import Path
import shutil
import sys
workspace = Path(os.environ["GITHUB_WORKSPACE"])
root = workspace.resolve()
# Only this runner's repository checkout, never a parent, temp/cache root or
# a symlink supplied in place of the checkout itself.
if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve():
print(f"runner_workspace_cleanup_refused path={str(workspace)!r}")
root = Path(os.environ["GITHUB_WORKSPACE"])
# Refuse aliases rather than resolving them. Open EVERY component without
# following symlinks, then keep cleanup anchored to the verified directory fd.
if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]):
print(f"runner_workspace_cleanup_refused path={str(root)!r}")
sys.exit(1)
flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
fd = os.open(root.anchor, flags)
previous = os.open(".", flags)
errors = []
if root.exists():
for entry in os.scandir(root):
try:
for part in root.parts[1:]:
child = os.open(part, flags, dir_fd=fd)
os.close(fd)
fd = child
if not shutil.rmtree.avoids_symlink_attacks:
raise OSError("descriptor-safe directory removal is unavailable")
os.fchdir(fd)
for entry in os.scandir("."):
try:
if entry.is_dir(follow_symlinks=False):
shutil.rmtree(entry.path)
shutil.rmtree(entry.name)
else:
os.unlink(entry.path)
os.unlink(entry.name)
except OSError as err:
errors.append(f"{entry.name}: {err}")
except OSError as err:
print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}")
sys.exit(1)
finally:
os.fchdir(previous)
os.close(previous)
os.close(fd)
if errors:
print("runner_workspace_cleanup_failed " + "; ".join(errors))
sys.exit(1)
+28 -12
View File
@@ -61,7 +61,7 @@ jobs:
- name: Runner free space
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
import shutil
import sys
@@ -399,28 +399,44 @@ jobs:
if: always() && runner.environment == 'self-hosted'
working-directory: ${{ runner.temp }}
run: |
python3 - <<'PY'
python3 -I - <<'PY'
import os
from pathlib import Path
import shutil
import sys
workspace = Path(os.environ["GITHUB_WORKSPACE"])
root = workspace.resolve()
# Only this runner's repository checkout, never a parent, temp/cache root or
# a symlink supplied in place of the checkout itself.
if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve():
print(f"runner_workspace_cleanup_refused path={str(workspace)!r}")
root = Path(os.environ["GITHUB_WORKSPACE"])
# Refuse aliases rather than resolving them. Open EVERY component without
# following symlinks, then keep cleanup anchored to the verified directory fd.
if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]):
print(f"runner_workspace_cleanup_refused path={str(root)!r}")
sys.exit(1)
flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
fd = os.open(root.anchor, flags)
previous = os.open(".", flags)
errors = []
if root.exists():
for entry in os.scandir(root):
try:
for part in root.parts[1:]:
child = os.open(part, flags, dir_fd=fd)
os.close(fd)
fd = child
if not shutil.rmtree.avoids_symlink_attacks:
raise OSError("descriptor-safe directory removal is unavailable")
os.fchdir(fd)
for entry in os.scandir("."):
try:
if entry.is_dir(follow_symlinks=False):
shutil.rmtree(entry.path)
shutil.rmtree(entry.name)
else:
os.unlink(entry.path)
os.unlink(entry.name)
except OSError as err:
errors.append(f"{entry.name}: {err}")
except OSError as err:
print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}")
sys.exit(1)
finally:
os.fchdir(previous)
os.close(previous)
os.close(fd)
if errors:
print("runner_workspace_cleanup_failed " + "; ".join(errors))
sys.exit(1)