Repository navigation
feat(bash): harden extractor — literal filtering, entrypoint nodes, broader skip list - #892
hypnwtykvmpr wants to merge 1 commit into
Conversation
…, broader skip list Builds on tree-sitter-bash extractor from #866 with three correctness/security improvements: 1. literal() filter — command names containing shell metacharacters ($, `, $(, <(, redirections, pipes, sequencers) are now rejected at parse time. Prevents command substitutions like $(rm -rf /) from being tokenized as command names and polluting the graph. 2. Entrypoint node — every .sh file now produces both a file node and a bash_entrypoint node, with a "contains" edge between them. Top-level commands attach to the entrypoint rather than being orphaned. Matches the entrypoint pattern other-language extractors use. Node metadata also gains language+kind for downstream filtering. 3. Broader skip list — _BASH_SKIP (~30 entries) replaced with _BASH_EXTERNAL_OR_BUILTIN_COMMANDS (~85 entries), separating bash builtins/keywords (alias, declare, mapfile, etc.) from common external commands (awk, jq, curl, etc.). Prevents external programs from being recorded as user-defined function calls. walk_calls also now skips nested function_definition children so calls inside nested functions aren't double-counted at enclosing scopes. Devtools: includes pyproject.toml sections for ruff, pyright, pre-commit, hypothesis, pip-audit under [dependency-groups] dev plus minimal [tool.ruff], [tool.ruff.lint], [tool.pyright] configs targeting py311. Tests: 821/821 pass; 10/10 bash-relevant tests pass.
There was a problem hiding this comment.
Pull request overview
Hardens the recently added bash extractor in graphify/extract.py with three correctness/security improvements (literal filtering of shell metacharacters in command tokens, a bash_entrypoint node so top-level commands aren't orphaned, and a much broader skip list distinguishing builtins/keywords from common external commands), and bundles a new [dependency-groups] dev plus minimal ruff/pyright configs in pyproject.toml.
Changes:
- New
literal()helper rejects command tokens containing shell metacharacters;walk_callsskips nestedfunction_definitionchildren and a top-level pass attributes script-level calls to a new entrypoint node. _BASH_SKIPreplaced with the broader_BASH_EXTERNAL_OR_BUILTIN_COMMANDS; node metadata gainslanguage/kind.pyproject.tomlgets adevdependency group and[tool.ruff]/[tool.pyright]configs targeting Python 3.10.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| graphify/extract.py | Adds entrypoint node, literal-filter, broader skip list, and nested-function-aware call walk for the bash extractor. |
| pyproject.toml | Adds dev dependency group and ruff/pyright configuration. |
Comments suppressed due to low confidence (2)
pyproject.toml:109
- The PR description states the new dev tooling configs target py311 ("targeting py311"), but both
[tool.ruff] target-versionand[tool.pyright] pythonVersionare set to"py310"/"3.10". The values match the project'srequires-python = ">=3.10"(so the configs themselves look correct), but the PR description is inaccurate — please update either the description or the version values so they agree.
target-version = "py310"
[tool.ruff.lint]
# Keep the committed baseline conservative until upstream adopts a broader lint policy.
select = ["E9", "F63", "F7", "F82"]
[tool.pyright]
include = ["graphify", "tests"]
pythonVersion = "3.10"
graphify/extract.py:5803
literal()strips outer quotes once and then checks for metacharacters, but it does not re-validate the result against a plain-identifier shape. A double-quoted command like"foo bar"will pass through as the literalfoo bar(a name containing a space), which is then used asname in defined_functions/ passed to_make_id. While this won't currently match any defined function, it does meanliteral()can return values the rest of the code treats as command names but that aren't valid bash identifiers. Consider validating the final result with_BASH_NAME_RE(which is otherwise unused) before returning, to make the function live up to its "plain command name" contract described in the docstring.
def literal(node) -> str | None:
# Filters out command tokens containing shell metacharacters
# ($, `, $(, <(, redirections, pipes, sequencers). Strips matched
# outer quotes. Returns None for anything that looks like a
# substitution, expansion, or compound construct rather than a
# plain command name — prevents graph pollution from constructs
# like `$(rm -rf /)` being recorded as command names.
raw = text(node).strip()
if not raw:
return None
if raw[0:1] in {"'", '"'} and raw[-1:] == raw[0]:
raw = raw[1:-1]
if any(token in raw for token in ("$", "`", "$(", "<(", ">", "|", ";", "&")):
return None
return raw
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| add_node(entry_nid, f"{path.name} script", 1, kind="bash_entrypoint") | ||
| add_edge(file_nid, entry_nid, "contains", 1) | ||
|
|
||
| _BASH_NAME_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_-]*$") |
| [dependency-groups] | ||
| dev = [ | ||
| "bandit>=1.9.4", | ||
| "hypothesis>=6.152.7", | ||
| "pip-audit>=2.10.0", | ||
| "pre-commit>=4.6.0", | ||
| "pyright>=1.1.409", | ||
| "pytest>=9.0.3", | ||
| "pytest-cov>=7.1.0", | ||
| "ruff>=0.15.13", | ||
| "safety>=3.7.0", | ||
| ] |
|
Closing — internal review (Codex audit |
Builds on #866 (tree-sitter bash + JSON extractors). Three correctness/security improvements to the bash extraction in
graphify/extract.py:Changes
1.
literal()filtering (security) — command names containing shell metacharacters ($,`,$(,<(,>,|,;,&) are now rejected at parse time. Previously, command substitutions like$(rm -rf /)were tokenized as command names and could pollute the graph or trigger graph-resolution paths the extractor wasn't designed for. Strips matched outer quotes; returnsNonefor anything that looks like a substitution, expansion, or compound construct rather than a plain command name.2. Entrypoint node — every
.shfile now produces both afilenode and abash_entrypointnode, with acontainsedge between them. Top-level commands in the script attach to the entrypoint via a new top-level call walk, rather than being orphaned. Matches the entrypoint pattern other-language extractors use. Node metadata gainslanguage+kindfor downstream filtering.3. Broader skip list —
_BASH_SKIP(~30 entries) replaced with_BASH_EXTERNAL_OR_BUILTIN_COMMANDS(~85 entries), separating bash builtins/keywords (alias, declare, mapfile, etc.) from common external commands (awk, jq, curl, sed, etc.). Prevents external programs from being recorded as user-defined function calls when no matching function exists, reducing false-positive call edges in the graph.Implementation notes
walk_callsnow skips nestedfunction_definitionchildren so calls inside nested functions aren't double-counted at the enclosing scope.walk_calls(root, entry_nid, ...)pass attributes top-level command calls to the entrypoint node._BASH_NAME_RE,_BASH_SOURCE_COMMANDS,_BASH_EXTERNAL_OR_BUILTIN_COMMANDSlifted to module-level insideextract_bashso they're declared once per call.Devtools (separate concern in same commit)
pyproject.tomlgains[dependency-groups] dev(ruff, pyright, pre-commit, hypothesis, pip-audit) plus minimal[tool.ruff],[tool.ruff.lint],[tool.pyright]configs targeting py311. Bundled here rather than as a standalone PR since these are the tools that verify this change locally.Verification
pytest -p no:cacheprovider)pytest -k bash)Diff
86 insertions, 16 deletions across 2 files.