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

+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)