Skip to content

feat(bash): harden extractor — literal filtering, entrypoint nodes, broader skip list - #892

Closed
hypnwtykvmpr wants to merge 1 commit into
Graphify-Labs:v8from
hypnwtykvmpr:v8
Closed

hypnwtykvmpr wants to merge 1 commit into
Graphify-Labs:v8from
hypnwtykvmpr:v8

Conversation

@hypnwtykvmpr

Copy link
Copy Markdown
Contributor

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; returns None for anything that looks like a substitution, expansion, or compound construct rather than a plain command name.

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 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 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, 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_calls now skips nested function_definition children so calls inside nested functions aren't double-counted at the enclosing scope.
  • A separate top-level 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_COMMANDS lifted to module-level inside extract_bash so they're declared once per call.

Devtools (separate concern in same commit)

pyproject.toml gains [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

  • 821/821 tests pass (pytest -p no:cacheprovider)
  • 10/10 bash-relevant tests pass (pytest -k bash)
  • No new pyright errors at the bash extractor lines

Diff

86 insertions, 16 deletions across 2 files.

…, 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.
Copilot AI review requested due to automatic review settings May 16, 2026 07:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_calls skips nested function_definition children and a top-level pass attributes script-level calls to a new entrypoint node.
  • _BASH_SKIP replaced with the broader _BASH_EXTERNAL_OR_BUILTIN_COMMANDS; node metadata gains language/kind.
  • pyproject.toml gets a dev dependency 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-version and [tool.pyright] pythonVersion are set to "py310"/"3.10". The values match the project's requires-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 literal foo bar (a name containing a space), which is then used as name in defined_functions / passed to _make_id. While this won't currently match any defined function, it does mean literal() 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.

Comment thread graphify/extract.py
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_-]*$")
Comment thread pyproject.toml
Comment on lines +71 to +82
[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",
]
@hypnwtykvmpr

Copy link
Copy Markdown
Contributor Author

Closing — internal review (Codex audit .dox/2026-05-16-codex-audit-cycle-2.1-bash-commit.md) found two blockers in this implementation: (1) the literal() filter doesn't detect command substitutions because $(cmd) exposes cmd as a child node with no metacharacters in its token, and (2) the broader skip list is checked before name in defined_functions so user-defined functions named like external commands are incorrectly suppressed. No tests cover the new behavior. Will re-open as a clean PR after fixing both issues and adding regression tests. Apologies for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants