Repository navigation
fix(analyzer): keep checking command lines inside a word that spans lines #814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
elliottwaves-20
wants to merge
2
commits into
NVIDIA:main
Choose a base branch
from
elliottwaves-20:fix/multiline-word-shadow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """A word that spans lines never hides a later command line (#694).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from skillspector.inspection_ledger import LedgerOutcome | ||
| from skillspector.nodes.analyzers import static_patterns_tool_misuse as tm_module | ||
| from skillspector.nodes.analyzers import static_runner | ||
|
|
||
| _PADDING = "\n# ordinary padding\n" * 400 | ||
| _TAIL = '\nE\n$CMD"" -rf / # "\n' | ||
|
|
||
|
|
||
| def _exhausted(content: str, file_type: str = "shell") -> bool: | ||
| return tm_module.has_bounded_parse_exhaustion( | ||
| content, lambda: None, file_type=file_type, complete_context=True | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("content", "file_type"), | ||
| [ | ||
| ('# x "\n$CMD"" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi' + _TAIL, "shell"), | ||
| ("cat <<'E'\nhe said \"hi" + _TAIL, "shell"), | ||
| ('cat <<E\n"${X}' + _TAIL, "shell"), | ||
| ('cat <<E\n"${X}" "' + _TAIL, "shell"), | ||
| ('cat <<E\n"$(date)$()"e"' + _TAIL, "shell"), | ||
| ('cat <<E\n"`date`$()""' + _TAIL, "shell"), | ||
| ('cat <<E\r"hi\rE\r$CMD"" -rf / # "\r', "shell"), | ||
| ('Run "a\n```sh\n$CMD"" -rf / # "\n```\n', "markdown"), | ||
| ('cat <<E\nhe said "hi\nE\nsudo $CMD"" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi\nE\nsudo -u root $CMD"" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi\nE\nif true; then $CMD"" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi\nE\n{ $CMD"" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi\nE\n! $CMD"" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi\nE\nenv A=1 $CMD"" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi\nE\nxargs -0 $CMD"" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi\nE\nsudo "$CMD" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi\nE\nsudo `echo x`"" -rf / # "\n', "shell"), | ||
| ('cat <<E\nhe said "hi\nE\nthen "`echo x`" -rf / # "\n', "shell"), | ||
| ], | ||
| ids=[ | ||
| "comment", | ||
| "heredoc-prose-quote", | ||
| "quoted-delimiter", | ||
| "heredoc-parameter", | ||
| "heredoc-parameter-space", | ||
| "heredoc-letter-after-quote", | ||
| "heredoc-backtick", | ||
| "heredoc-carriage-return", | ||
| "markdown-prose", | ||
| "after-sudo", | ||
| "after-sudo-option", | ||
| "after-then", | ||
| "after-brace", | ||
| "after-bang", | ||
| "after-env-assignment", | ||
| "after-xargs-option", | ||
| "quoted-after-sudo", | ||
| "backtick-after-sudo", | ||
| "quoted-backtick-after-then", | ||
| ], | ||
| ) | ||
| def test_quote_spanning_lines_cannot_hide_a_later_command(content: str, file_type: str) -> None: | ||
| # A quote in a heredoc body, a comment or prose may pair with a quote on | ||
| # a later command line. The word between them must not hide that line. | ||
| assert _exhausted(content, file_type) | ||
| assert _exhausted(content + _PADDING, file_type) | ||
|
|
||
|
|
||
| def test_heredoc_quote_cannot_hide_a_runtime_rm_at_scan_level() -> None: | ||
| script = ( | ||
| '#!/bin/sh\nCMD="${TOOL:-rm}"\ncat <<EOF\nhe said "hi\nEOF\n' | ||
| '$CMD"" -rf / --no-preserve-root # done "\n' | ||
| ) | ||
|
|
||
| result = static_runner.run_static_patterns_with_ledger( | ||
| {"components": ["run.sh"], "file_cache": {"run.sh": script}}, [tm_module] | ||
| ) | ||
|
|
||
| event = result["inspection_ledger"][0] | ||
| assert event["outcome"] is not LedgerOutcome.COMPLETED | ||
|
|
||
|
|
||
| def test_multiline_string_with_parameter_stays_complete() -> None: | ||
| content = 'echo "line1\nline2 $HOME"\nls -la\n' | ||
| assert not _exhausted(content) | ||
| assert not _exhausted(content + _PADDING) | ||
|
|
||
|
|
||
| def test_dynamic_multiline_word_matches_main() -> None: | ||
| # Main never advanced past a dynamic ``$(`` word, so its inner commands | ||
| # were already independent candidates. | ||
| content = 'echo "$(date)\nnext"\nls\n' | ||
| assert not _exhausted(content) | ||
| assert not _exhausted(content + _PADDING) | ||
|
|
||
|
|
||
| def test_python_source_keeps_main_frontier() -> None: | ||
| content = 'x = """he said "hi\nmore"""\nprint(x)\n' | ||
| assert not _exhausted(content, "python") | ||
| assert not _exhausted(content + _PADDING, "python") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inside a shadow region, a runtime command after a prefix word is still hidden:
sudo $CMD"" -rf / # ",if true; then $CMD"" -rf / # "and{ $CMD"" -rf / # "are each partial on main when standalone. Each becomes complete aftercat <<E/he said "hi/E, and here the$aftersudo/then/{is not a command position, so it stays complete. Please also accept positions after_SHELL_CLAUSE_PREFIX_WORDS,if/while/until,{, a backtick, and the wrappers this module models (sudo,env,command,nohup,nice,timeout,xargs,exec). Alternatively, check$-led shadowed candidates with_is_shell_command_word_start. Please add these variants to the test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 8dd0d6a. Shadowed candidates that open a runtime word (
$,"$, or a single backtick) keep main's word-start check, which also coverssudo -u root $CMD,env A=1 $CMDandxargs -0 $CMD. The variants are intest_quote_spanning_lines_cannot_hide_a_later_command. Details in the PR comment.