Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
7447f9c
feat: implement issue #1361 — SonarCloud: code security review (pytho…
don-petry Oct 27, 2026
68f9947
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 28, 2026
2536373
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 28, 2026
b44f0ae
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 28, 2026
4c6ba78
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 28, 2026
3b34378
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 28, 2026
01aa440
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 28, 2026
cb1ee18
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 28, 2026
26c2f75
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 29, 2026
e8036bd
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 29, 2026
73cc09c
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 29, 2026
8a7a0d0
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 30, 2026
c70268c
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 30, 2026
a1dd8db
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 31, 2026
c2e9af7
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Oct 31, 2026
3cd0ff1
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 1, 2026
b8c77ac
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 1, 2026
8177bb5
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 2, 2026
5b392f3
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 2, 2026
e1fdccc
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 2, 2026
84ab389
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 3, 2026
97cf6b6
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 3, 2026
cbee450
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 3, 2026
4eda369
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 3, 2026
ab1d4fe
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 3, 2026
d186ecf
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 3, 2026
2ab68f9
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 3, 2026
8c484c4
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 3, 2026
1d0576f
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 4, 2026
3557c19
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 4, 2026
7651141
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 4, 2026
03192e6
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 5, 2026
2614734
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 5, 2026
afb3b9c
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 5, 2026
862f3a3
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 6, 2026
3a1e836
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 6, 2026
c1c077e
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 8, 2026
68940e3
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 8, 2026
74097e3
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 9, 2026
637b56e
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 9, 2026
d3e3c89
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 9, 2026
8c6ee39
fix(bot): address CodeRabbit findings — remove .coverage and add docs…
don-petry Nov 10, 2026
8e1e27a
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 10, 2026
0da187c
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 12, 2026
b082fb2
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 12, 2026
357ec9a
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 12, 2026
aaf1198
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 13, 2026
e5436ef
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 15, 2026
7efd40c
Merge branch 'main' into dev-lead/issue-1361-20261027-2120
don-petry Nov 17, 2026
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
66 changes: 55 additions & 11 deletions .github/scripts/feature-ideation/validate-signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,47 @@ def _allowed_roots() -> list[str]:
return [r for r in roots if os.path.dirname(r) != r]


def _rebuild_from_root(root: str, rel: str) -> Path:
"""Re-derive ``root``/``rel`` from directory listings, starting at ``root``.

Each component of ``rel`` is matched by name against the real entries of
the directory reached so far, and the result is built only from those
entries — never from the CLI string itself. A confined argument therefore
cannot carry anything onto the file-read sink that the trusted root does
not actually contain (SonarCloud pythonsecurity:S8707 traced the argv taint
straight through a plain ``commonpath`` guard). ``rel`` comes from a
``realpath``, so it holds no ``..`` or symlink components.

Raises ``FileNotFoundError`` when a component does not exist.
"""
current = Path(root)
if rel == os.curdir:
return current
for part in Path(rel).parts:
wanted = os.path.normcase(part)
with os.scandir(current) as entries:
match = next(
(e for e in entries if os.path.normcase(e.name) == wanted), None
)
if match is None:
raise FileNotFoundError(os.path.join(root, rel))
current = Path(match.path)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return current


def _confine_path(raw: str | os.PathLike[str]) -> Path | None:
"""Resolve ``raw`` and return it only if it lies inside an allowed root.

Guards against path traversal (``../``, absolute paths, symlinks) via the
CLI arguments — SonarCloud pythonsecurity:S8707. Returns ``None`` when the
resolved path escapes every allowed root.
resolved path escapes every allowed root, and raises ``FileNotFoundError``
when it is inside one but does not exist.
"""
candidate = os.path.realpath(raw)
for root in _allowed_roots():
try:
if os.path.commonpath([root, candidate]) == root:
return Path(candidate)
return _rebuild_from_root(root, os.path.relpath(candidate, root))
except ValueError:
# On Windows, os.path.commonpath raises ValueError when paths are
# on different drives (e.g., C:\ and D:\). Treat this as outside.
Expand All @@ -114,6 +143,14 @@ def _confine_path(raw: str | os.PathLike[str]) -> Path | None:


def main(argv: list[str]) -> int:
"""Validate a signals.json file against a JSON schema.

Confines both the signals and schema file arguments to allowed directories
to prevent path traversal (S8707). Reads both files, reports missing files
or I/O errors as exit code 2, and validates the signals against the schema.
Reports validation errors to stderr and returns exit code 1 for validation
failures or 0 on success.
"""
if len(argv) < 2 or len(argv) > 3:
sys.stderr.write(
"usage: validate-signals.py <signals.json> [<schema.json>]\n"
Expand All @@ -123,26 +160,33 @@ def main(argv: list[str]) -> int:
raw_schema = argv[2] if len(argv) == 3 else os.path.join(
_REPO_ROOT, ".github", "schemas", "signals.schema.json"
)
signals_path = _confine_path(argv[1])
try:
signals_path = _confine_path(argv[1])
except FileNotFoundError as exc:
sys.stderr.write(f"[validate-signals] not found: {exc}\n")
return 2
except OSError as exc:
sys.stderr.write(f"[validate-signals] cannot read {argv[1]}: {exc}\n")
return 2
if signals_path is None:
sys.stderr.write(
f"[validate-signals] path is outside the allowed directories: {argv[1]}\n"
)
return 2
schema_path = _confine_path(raw_schema)
try:
schema_path = _confine_path(raw_schema)
except FileNotFoundError as exc:
sys.stderr.write(f"[validate-signals] schema not found: {exc}\n")
return 2
except OSError as exc:
sys.stderr.write(f"[validate-signals] cannot read schema {raw_schema}: {exc}\n")
return 2
if schema_path is None:
sys.stderr.write(
f"[validate-signals] schema path is outside the allowed directories: {raw_schema}\n"
)
return 2

if not signals_path.exists():
sys.stderr.write(f"[validate-signals] not found: {signals_path}\n")
return 2
if not schema_path.exists():
sys.stderr.write(f"[validate-signals] schema not found: {schema_path}\n")
return 2

try:
signals = json.loads(signals_path.read_text(encoding="utf-8"))
except OSError as exc:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,6 @@ actionlint.tar.gz
__pycache__/
*.py[cod]
*$py.class
.coverage
.coverage.*
.pytest_cache/
46 changes: 46 additions & 0 deletions test/feature-ideation/test_validate_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,52 @@ def test_schema_outside_root_is_rejected(self, confined, capsys):
assert rc == 2
assert "outside the allowed directories" in capsys.readouterr().err

def test_missing_signals_inside_root_reports_not_found(self, confined, capsys):
"""A missing file inside an allowed root keeps the ``not found`` exit 2."""
rc = module.main(["validate-signals.py", "missing.json", str(self.SCHEMA)])
assert rc == 2
assert "not found" in capsys.readouterr().err

def test_missing_schema_inside_root_reports_not_found(self, confined, capsys):
"""A missing schema inside an allowed root keeps the ``schema not found`` exit 2."""
(confined / "signals.json").write_text(
self.FIXTURE.read_text(encoding="utf-8"), encoding="utf-8"
)
rc = module.main(["validate-signals.py", "signals.json", "nope/schema.json"])
assert rc == 2
assert "schema not found" in capsys.readouterr().err

def test_confined_path_is_rebuilt_from_directory_entries(self, confined, monkeypatch):
"""The returned path is assembled from scandir entries under the root."""
nested = confined / "a" / "b"
nested.mkdir(parents=True)
target = nested / "signals.json"
target.write_text("{}", encoding="utf-8")
seen = []
real_scandir = module.os.scandir

def spy(path):
seen.append(str(path))
return real_scandir(path)

monkeypatch.setattr(module.os, "scandir", spy)
result = module._confine_path("a/b/../b/signals.json")
monkeypatch.undo()
assert result == target.resolve()
assert seen == [str(confined.resolve()), str(confined.resolve() / "a"),
str(confined.resolve() / "a" / "b")]

def test_confined_root_itself_is_returned(self, confined):
"""An argument naming an allowed root resolves to that root."""
assert module._confine_path(".") == confined.resolve()

def test_confined_missing_component_raises_not_found(self, confined):
"""A missing file or intermediate directory inside a root raises FileNotFoundError."""
with pytest.raises(FileNotFoundError):
module._confine_path("missing.json")
with pytest.raises(FileNotFoundError):
module._confine_path("no/such/dir/signals.json")

def test_default_allowed_roots_exclude_system_temp(self, tmp_path, monkeypatch):
"""Default roots cover CWD, $RUNNER_TEMP and the repo, not the system temp dir."""
import os
Expand Down
Loading