Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions loopx/capabilities/benchmark_toolkit/integrity.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@
)
_NETWORK_COMMAND_PATTERN = re.compile(r"(?is)\b(?:curl|wget)\b|\bgit\s+clone\b")
_HTTP_URL_PATTERN = re.compile(r"(?is)https?://[^\s\"'<>]+")
_GIT_CLONE_COMMAND_PATTERN = re.compile(r"(?is)\bgit\s+clone\b")
_GIT_REMOTE_PATTERN = re.compile(
r"(?is)(?:\b(?:git|ssh)://[^\s\"'<>]+|(?<![A-Za-z0-9_.@/-])[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+:[^\s\"'<>]+)"

Check warning on line 117 in loopx/capabilities/benchmark_toolkit/integrity.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove duplicates in this character class.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaAtJpKuh8df51YGRMJa&open=AaAtJpKuh8df51YGRMJa&pullRequest=3515

Check warning on line 117 in loopx/capabilities/benchmark_toolkit/integrity.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Simplify this regular expression to reduce its runtime, as it has super-linear performance due to backtracking.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaAtJpKuh8df51YGRMJd&open=AaAtJpKuh8df51YGRMJd&pullRequest=3515

Check warning on line 117 in loopx/capabilities/benchmark_toolkit/integrity.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove duplicates in this character class.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaAtJpKuh8df51YGRMJb&open=AaAtJpKuh8df51YGRMJb&pullRequest=3515

Check warning on line 117 in loopx/capabilities/benchmark_toolkit/integrity.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove duplicates in this character class.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaAtJpKuh8df51YGRMJc&open=AaAtJpKuh8df51YGRMJc&pullRequest=3515
)
_COMMAND_TEXT_FIELDS = ("cmd", "command")
_COMMAND_ARGUMENT_FIELDS = ("args", "argv")
# ATIF currently carries a function name rather than a typed side-effect class.
Expand Down Expand Up @@ -350,7 +354,7 @@
return NetworkRequestScope.EXTERNAL


def _network_request_scope(arguments: object) -> NetworkRequestScope:

Check failure on line 357 in loopx/capabilities/benchmark_toolkit/integrity.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaAtJpKuh8df51YGRMJe&open=AaAtJpKuh8df51YGRMJe&pullRequest=3515
"""Classify common shell HTTP requests while preserving argv association."""

scope = NetworkRequestScope.NONE
Expand All @@ -358,6 +362,8 @@
for text in command_texts:
if not _NETWORK_COMMAND_PATTERN.search(text):
continue
if _GIT_CLONE_COMMAND_PATTERN.search(text) and _GIT_REMOTE_PATTERN.search(text):
return NetworkRequestScope.EXTERNAL
for url in _HTTP_URL_PATTERN.finditer(text):
current = _scope_for_url(url.group(0))
if current is NetworkRequestScope.EXTERNAL:
Expand Down
89 changes: 89 additions & 0 deletions tests/capabilities/test_benchmark_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,95 @@ def test_structured_external_network_requests_remain_fail_closed(
assert receipt["evidence_counts"]["external_network_request"] == 1


@pytest.mark.parametrize(
"command",
[
"git clone git@github.com:owner/repo.git",
"git clone ssh://git@github.com/owner/repo.git",
"git clone git://github.com/owner/repo.git",
],
)
def test_non_http_git_clone_requests_remain_fail_closed(command: str) -> None:
receipt = build_benchmark_integrity_qualification(
trajectory=_trajectory(command=command),
runtime_attestation=_attestation(),
)

assert receipt["integrity_qualified"] is False
assert receipt["classification"] == "integrity_policy_violation"
assert receipt["evidence_counts"]["external_network_request"] == 1


def test_structured_non_http_git_clone_request_remains_fail_closed() -> None:
receipt = build_benchmark_integrity_qualification(
trajectory=_trajectory(
arguments={"argv": ["git", "clone", "git@github.com:owner/repo.git"]}
),
runtime_attestation=_attestation(),
)

assert receipt["integrity_qualified"] is False
assert receipt["classification"] == "integrity_policy_violation"
assert receipt["evidence_counts"]["external_network_request"] == 1


def test_local_git_clone_path_is_not_network_access() -> None:
receipt = build_benchmark_integrity_qualification(
trajectory=_trajectory(command="git clone ../fixture-repo worktree"),
runtime_attestation=_attestation(),
)

assert receipt["integrity_qualified"] is True
assert receipt["evidence_counts"]["external_network_request"] == 0


def test_http_git_clone_remains_external_network_access() -> None:
receipt = build_benchmark_integrity_qualification(
trajectory=_trajectory(command="git clone https://github.com/owner/repo.git"),
runtime_attestation=_attestation(),
)

assert receipt["integrity_qualified"] is False
assert receipt["classification"] == "integrity_policy_violation"
assert receipt["evidence_counts"]["external_network_request"] == 1


@pytest.mark.parametrize(
"command",
[
"git clone -b main --depth 1 git@github.com:owner/repo.git",
"git clone git+ssh://git@github.com/owner/repo.git",
],
)
def test_non_http_git_clone_variants_remain_fail_closed(command: str) -> None:
receipt = build_benchmark_integrity_qualification(
trajectory=_trajectory(command=command),
runtime_attestation=_attestation(),
)

assert receipt["integrity_qualified"] is False
assert receipt["classification"] == "integrity_policy_violation"
assert receipt["evidence_counts"]["external_network_request"] == 1


@pytest.mark.parametrize(
"command",
[
"git clone /abs/path/repo.git",
"git clone file:///abs/path/repo.git",
"git clone /tmp/cache@2/repo.git",
],
)
def test_local_git_clone_paths_are_not_external_network_access(command: str) -> None:
receipt = build_benchmark_integrity_qualification(
trajectory=_trajectory(command=command),
runtime_attestation=_attestation(),
)

assert receipt["integrity_qualified"] is True
assert receipt["evidence_counts"]["external_network_request"] == 0


def test_structured_loopback_argv_uses_explicit_loopback_scope() -> None:
receipt = build_benchmark_integrity_qualification(
trajectory=_trajectory(
Expand Down