Skip to content
Open
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
13 changes: 10 additions & 3 deletions src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6335,7 +6335,7 @@ def commandless_wrapper() -> tuple[bool, None]:
)

def next_command_word() -> tuple[str | None, bool]:
# Redirections may appear between command's options and executable.
# Redirections may appear between env or command options and executable.
# Consume their targets without treating them as executable operands.
nonlocal cursor
for _ in range(_SHELL_COMMAND_STRING_ARGUMENTS):
Expand Down Expand Up @@ -6372,6 +6372,13 @@ def next_command_word() -> tuple[str | None, bool]:
)
return None, True

def next_env_word() -> tuple[str | None, bool]:
# NAME=<value> is usually a placeholder, so a redirection attached to
# the previous word keeps the unresolved result.
if content[cursor : cursor + 1] in {"<", ">"}:
return next_word()
return next_command_word()

for _ in range(32):
while cursor < len(content) and content[cursor].isspace():
if cursor % 4096 == 0:
Expand Down Expand Up @@ -6599,13 +6606,13 @@ def next_command_word() -> tuple[str | None, bool]:
if command == "env":
wrapper_seen = True
for _ in range(_SHELL_COMMAND_STRING_ARGUMENTS):
option, limited = next_word()
option, limited = next_env_word()
if limited:
return failed()
if option is None:
return commandless_wrapper()
if option == "--":
pending, limited = next_word()
pending, limited = next_env_word()
if limited:
return failed()
if pending is None:
Expand Down
23 changes: 20 additions & 3 deletions tests/nodes/analyzers/test_command_wrapper_without_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,19 @@ def test_optional_command_wrappers_without_executable_are_complete(
assert not _exhausted(source + ending, "shell")


@pytest.mark.parametrize("wrapper", ["command", "command -p", "command --", "command -p --"])
@pytest.mark.parametrize(
"wrapper",
[
"command",
"command -p",
"command --",
"command -p --",
"env",
"env -i",
"env --",
"env NAME=value",
],
)
@pytest.mark.parametrize(
"redirect", ["> /tmp/x.txt", "2>>'output log'", "<input >output", ">output # comment"]
)
Expand All @@ -51,7 +63,10 @@ def test_command_with_only_redirections_is_complete(wrapper: str, redirect: str)
assert result["inspection_ledger"][0]["outcome"] is LedgerOutcome.COMPLETED


@pytest.mark.parametrize("source", ["command >", "command >\necho ready", "command >'unclosed"])
@pytest.mark.parametrize(
"source",
["command >", "command >\necho ready", "command >'unclosed", "env >", "env -i >\necho ready"],
)
def test_command_redirection_requires_a_complete_target(source: str) -> None:
assert _exhausted(source, "shell")

Expand All @@ -61,7 +76,9 @@ def test_command_redirection_count_is_bounded() -> None:
assert _exhausted(source, "shell")


@pytest.mark.parametrize("wrapper", ["command", "command --", "command -p"])
@pytest.mark.parametrize(
"wrapper", ["command", "command --", "command -p", "env", "env -i", "env --"]
)
def test_command_redirection_keeps_following_commands_visible(wrapper: str) -> None:
source = f"{wrapper} >output rm -rf /\n"
assert any(f.rule_id == "TM1" for f in tm_module.analyze(source, "script.sh", "shell"))
Expand Down
Loading