Skip to content

Python -c analysis - #134

Closed
nickdavies wants to merge 2 commits into
ldayton:mainfrom
nickdavies:python-c-analysis
Closed

Python -c analysis#134
nickdavies wants to merge 2 commits into
ldayton:mainfrom
nickdavies:python-c-analysis

Conversation

@nickdavies

Copy link
Copy Markdown
Contributor

This PR stacks on top of #122 and provides analysis of python -c usage. It attempts to deny any analysis on commands that contain any kind of expansion because that makes it too difficult to determine safety.

It is easier to review this diff once the lower one is merged but if you have any questions let me know, I am happy to add more tests or make changes

Nick Davies added 2 commits March 31, 2026 18:19
Add python-allow-module and python-deny-module config directives so
users can customize which Python modules are considered safe or
dangerous during static analysis.

- Config: parse and validate module names (dotted identifiers only),
  accumulate across config layers, support inline comments
- SafetyAnalyzer: parameterize with extra_safe_modules/extra_deny_modules
  merged with hardcoded sets. Allow overrides exact matches in the
  dangerous list (submodules must be allowed separately).
- Thread Config through HandlerContext so python handler can access
  configured module lists.

Example .dippy config:
  python-allow-module numpy
  python-allow-module pandas
  python-deny-module requests
- Statically analyze python -c inline code using the existing
  SafetyAnalyzer AST visitor. Safe code (no I/O, no dangerous imports)
  is auto-approved.
- Track per-word bash expansion flags (word_has_expansions) through
  HandlerContext so the python handler can detect when -c code contains
  $VAR or $(cmd) expansions and fall back to ask.
- Config module overrides (python-allow-module / python-deny-module)
  apply to inline code analysis as well.
@ldayton

ldayton commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Landed this in #149 — thank you, @nickdavies. Your commit is preserved and you're credited as co-author on the squash.

I rebased it onto main now that #122 has merged, and fixed one thing before landing: word_has_expansions was indexed against the full word list while the handler sees env-stripped tokens, so a leading FOO=bar shifted the flags and wrapper recursion (time/timeout/…) dropped them — meaning the expansion guard silently no-op'd in those cases and only the analyzer's syntax error caught $…. The flags are now re-sliced in lockstep with the tokens, with regression tests that assert the expansion reason fires.

Your #135 (secrets / opaque_positions) is next on my list.

ldayton added a commit to nickdavies/Dippy that referenced this pull request Jun 8, 2026
python -c was always ask. Now the inline body is run through the same AST
safety analyzer used for script files: expansion-free code that passes
analysis is auto-approved, dangerous imports/builtins ask, and any bash
expansion ($VAR, $(...)) in the -c argument falls back to ask because bash
rewrites the code at runtime.

Based on @nickdavies's ldayton#134 (rebased onto main after its base ldayton#122 landed),
plus a fix for a token-alignment bug: the per-token expansion flags were
indexed against the full word list while the handler sees env-stripped
tokens, so a leading FOO=bar shifted the flags and wrapper recursion dropped
them. The flags are now re-sliced in lockstep with tokens at both points.

Closes ldayton#134.

Co-authored-by: Nick Davies <github@nicolasdavies.com.au>
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