Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/validate-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ jobs:
mapfile -d '' -t candidates < <(git ls-files -z)
for file in "${candidates[@]}"; do
base="${file##*/}"
if [[ "$base" != *.* ]] && [ -f "$file" ]; then
# `read` fails at EOF even when it fills first_line, so the check reads the content regardless.
first_line=""
IFS= read -r first_line < "$file" || true
# Never follows a tracked symlink: -h checks the git-tracked path itself, before any read.
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Resolved
if [[ "$base" != *.* ]] && [ -f "$file" ] && [ ! -h "$file" ]; then
Comment thread
ptr727 marked this conversation as resolved.
Resolved
# `head` reads a no-trailing-newline file cleanly and still fails on a genuine read error.
first_line="$(head -n 1 -- "$file")"
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Resolved
if is_shell_shebang "$first_line"; then
scripts+=("$file")
fi
Expand Down
11 changes: 9 additions & 2 deletions scripts/docker_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,16 @@ def shell_shebang_interpreter(line: str) -> str | None:


def has_shell_shebang(root: Path, relative_path: str) -> bool:
"""Report whether a tracked file's shebang directly names bash or sh."""
"""Report whether a tracked file's shebang directly names bash or sh.

Never follows a tracked symlink: `is_symlink()` uses `lstat`, so checking it first
keeps a symlink pointing outside the checkout from ever reaching `open()`.
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Resolved
"""
path = root / relative_path
try:
with (root / relative_path).open("rb") as handle:
if path.is_symlink():
return False
with path.open("rb") as handle:
first_line = handle.readline(256)
except OSError:
return False
Expand Down
19 changes: 19 additions & 0 deletions scripts/tests/test_docker_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def track(self, name: str, body: str = "content\n") -> None:
path.write_text(body, encoding="utf-8")
subprocess.run(["git", "-C", str(self.root), "add", "--", name], check=True)

def track_symlink(self, name: str, target: Path) -> None:
path = self.root / name
path.parent.mkdir(parents=True, exist_ok=True)
path.symlink_to(target)
subprocess.run(["git", "-C", str(self.root), "add", "--", name], check=True)
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Resolved

def invoke(self, selected: set[str], runner: FakeRunner) -> tuple[int, str]:
output = io.StringIO()
with contextlib.redirect_stdout(output):
Expand Down Expand Up @@ -157,6 +163,19 @@ def test_extensionless_shebang_script_merges_with_glob_matched_scripts(self) ->
docker_lint.tracked_files(self.root, linter),
)

def test_extensionless_symlink_is_never_followed(self) -> None:
secret = self.root.parent / "secret"
secret.write_text("TOP SECRET\n#!/usr/bin/env bash\n", encoding="utf-8")
self.track_symlink("ops/evil-symlink", secret)
linter = next(linter for linter in docker_lint.LINTERS if linter.name == "shellcheck")
self.assertEqual([], docker_lint.tracked_files(self.root, linter))

def test_has_shell_shebang_reports_false_for_a_symlink_without_reading_it(self) -> None:
secret = self.root.parent / "secret"
secret.write_text("#!/usr/bin/env bash\n", encoding="utf-8")
self.track_symlink("ops/evil-symlink", secret)
self.assertFalse(docker_lint.has_shell_shebang(self.root, "ops/evil-symlink"))
Comment thread
ptr727 marked this conversation as resolved.
Resolved

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
def test_extensionless_untracked_shebang_script_is_not_picked_up(self) -> None:
path = self.root / "ops" / "vps-backup-pull"
path.parent.mkdir(parents=True, exist_ok=True)
Expand Down