Commit Graph
3 Commits
Author SHA1 Message Date
Alexey Tkachenko c7f7f3c5d0 fix(python): analyze extensionless scripts with a CRLF Python shebang (#906)
A shebang that names Python but ends in CRLF kept the carriage return in
the interpreter name, so the file was classified as non-Python and the
Python analyzers skipped it, though `python3 <script>` runs it unchanged.

Read the shebang with its CRLF ending taken as LF as one more view next to
the truncated Linux ones. When the views disagree the source is ambiguous
and analyzed fail-closed; it is still not definitively Python.

Signed-off-by: alteralto <182909819+alteralto@users.noreply.github.com>
2026-10-28 09:03:18 -07:00
2c142611a8 feat(python): classify executable source surfaces (#579)
* fix(analyzer): stabilize TM1 window identity

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix(analyzer): track eager receiver side effects

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* feat(python): refresh execution surfaces on current main

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix(analyzer): preserve lexical TM1 when dataflow abstains

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix(analyzer): require a retained companion owner

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix(analyzer): preserve execution scope in binding evidence

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix(analyzer): preserve lexical TM1 across conservative Python analysis

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* style(analyzer): simplify binding event deduplication

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: preserve runtime and retained shell evidence

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: preserve runtime and retained shell evidence

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix(analyzer): preserve called-slot identity and cached method evidence

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix(analyzer): preserve called-slot identity and cached method evidence

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: preserve cached subprocess slot state across scoped imports

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix: preserve cached subprocess slot state across scoped imports

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix(analyzer): reconcile explicit cached-slot evidence across scopes

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix(analyzer): reconcile explicit cached-slot evidence across scopes

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: abstain from cached slot proof after eager effects

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix: abstain from cached slot proof after eager effects

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* test: retain lexical findings after unknown cached-slot effects

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* test: retain lexical findings after unknown cached-slot effects

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: bound direct called-slot evidence to known execution order

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: bound direct called-slot evidence to known execution order

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: limit direct slot proof to single assignment targets

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: limit direct slot proof to single assignment targets

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: invalidate slot evidence on protocols and unsafe releases

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix: invalidate slot evidence on protocols and unsafe releases

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix: keep legacy shadow proof isolated from later effects

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: keep legacy shadow proof isolated from later effects

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: abstain from cached replacement proof after unknown effects

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix: abstain from cached replacement proof after unknown effects

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix: retain native callable detection across cached slot stores

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix: retain native callable detection across cached slot stores

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix: retain shell findings for native receiver assignments

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: retain shell findings for native receiver assignments

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: preserve native Popen replacement warnings

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* fix: preserve native Popen replacement warnings

Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>

* test: retain occurrence metadata in prose shell reports

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>

* fix(analyzer): require proven replacement before revoking lexical TM1

Address the three remaining review findings on lexical TM1 reconciliation,
where a bound shell=True call still ran natively but lost its HIGH finding:

- Untaken expression stores: a walrus in a short-circuited BoolOp operand,
  an untaken conditional-expression arm, or a comprehension/generator body
  is no longer recorded as replacement evidence. Only constant operands
  and tests prove that the store executed.
- Identity-preserving stores: unpacked targets are paired with their RHS
  elements. Self-stores are transparent, truthy constants keep the flag,
  and native receiver aliases (saved = subprocess, import subprocess as
  saved, Popen = subprocess.Popen) keep the native receiver.
- Deferred bodies: an outer store only proves replacement when every
  invocation the owner scope can reach sees it. Any reference to the
  function, method or class (compound statements, main guards, callbacks,
  other bodies) can invoke it from that point on, and decorators or
  lambdas can run once defined. This also covers a re-import before a
  later invocation.

The release-trigger test for a called function now matches its
module-level twin: the companion abstains and the lexical HIGH remains.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(analyzer): require proven replacement before revoking lexical TM1

Address the three remaining review findings on lexical TM1 reconciliation,
where a bound shell=True call still ran natively but lost its HIGH finding:

- Untaken expression stores: a walrus in a short-circuited BoolOp operand,
  an untaken conditional-expression arm, or a comprehension/generator body
  is no longer recorded as replacement evidence. Only constant operands
  and tests prove that the store executed.
- Identity-preserving stores: unpacked targets are paired with their RHS
  elements. Self-stores are transparent, truthy constants keep the flag,
  and native receiver aliases (saved = subprocess, import subprocess as
  saved, Popen = subprocess.Popen) keep the native receiver.
- Deferred bodies: an outer store only proves replacement when every
  invocation the owner scope can reach sees it. Any reference to the
  function, method or class (compound statements, main guards, callbacks,
  other bodies) can invoke it from that point on, and decorators or
  lambdas can run once defined. This also covers a re-import before a
  later invocation.

The release-trigger test for a called function now matches its
module-level twin: the companion abstains and the lexical HIGH remains.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: account for TM1 Python parsing in marker ownership ledger

Main's #784 test expects the tool-misuse ledger event for an invalid
Python file to carry obfuscated_instruction_text. This PR makes tool
misuse parse Python for TM1 reconciliation, so the runner's
higher-precedence syntax_error now names that partial event. Keep the
fail-closed contract: assert the marker reading through the lexical-only
anti-refusal analyzer and keep tool misuse partial with syntax_error.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: account for TM1 Python parsing in marker ownership ledger

Main's #784 test expects the tool-misuse ledger event for an invalid
Python file to carry obfuscated_instruction_text. This PR makes tool
misuse parse Python for TM1 reconciliation, so the runner's
higher-precedence syntax_error now names that partial event. Keep the
fail-closed contract: assert the marker reading through the lexical-only
anti-refusal analyzer and keep tool misuse partial with syntax_error.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(analyzer): keep one TM1 owner for true-prefixed names without AST proof

Port the six true-prefixed TM1 regression tests from #577. This branch
already carries test_true_direct_calls_on_one_line_keep_distinct_locations
unchanged, so only the other five are added. Two of them reported the
same call twice on this branch:

- A variable window for a name spelled `true` (in any case) duplicated
  the case-insensitive direct `shell=True` owner at the same call. AST
  reconciliation removes that window only when the dataflow companion
  takes the call, so a file that does not parse, or a call after an
  unknown receiver effect, kept both findings. coalesce_path_findings
  now drops such a window when a direct lexical owner starts at the
  same call. A window for a call whose direct candidate was folded into
  an identical same-line call still reports that call.
- A call-anchored window for a longer true-prefixed name (`true_value`)
  used its raw text as identity, so the raw and normalized security
  views of one call did not collapse, and reconciliation then moved both
  to the call. The window now takes its identity from the normalized
  view text, as the direct owner already does.

Also cover both root causes with a test for a nested `true` call after
an unknown receiver effect and a normalized `true_value` call in a file
that does not parse.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(analyzer): keep one TM1 owner for true-prefixed names without AST proof

Port the six true-prefixed TM1 regression tests from #577. This branch
already carries test_true_direct_calls_on_one_line_keep_distinct_locations
as test_true_direct_calls_around_safe_comprehension_keep_distinct_locations,
so only the other five are added. Two of them reported the same call twice
on this branch:

- A variable window for a name spelled `true` (in any case) duplicated
  the case-insensitive direct `shell=True` owner at the same call. AST
  reconciliation removes that window only when the dataflow companion
  takes the call, so a file that does not parse, or a call after an
  unknown receiver effect, kept both findings. coalesce_path_findings
  now drops such a window when a direct lexical owner starts at the
  same call. A window for a call whose direct candidate was folded into
  an identical same-line call still reports that call.
- A call-anchored window for a longer true-prefixed name (`true_value`)
  used its raw text as identity, so the raw and normalized security
  views of one call did not collapse, and reconciliation then moved both
  to the call. The window now takes its identity from the normalized
  view text, as the direct owner already does.

Also cover both root causes with a test for a nested `true` call after
an unknown receiver effect and a normalized `true_value` call in a file
that does not parse.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(analyzer): report each true-prefixed shell call that #577 reports

The `\b`-bounded direct pattern does not match `shell=true_value`, so on
this branch such a call was reported only through a variable window. That
window pairs an assignment with a single call, skips an assignment
inside an earlier window, and was dropped outside Python. Compared with
#577 this lost:

- calls in JavaScript and Markdown files, fenced or plain;
- a later call that the AST companion abstains on, for example after an
  unknown receiver effect;
- all but the last same-line call in a file that does not parse, and the
  second of two identical same-line `shell=true` calls outside Python,
  which the direct candidate key folds into one;
- a call whose assignment sits inside the window of an earlier one.

_variable_shell_matches now pairs every assignment of a true-prefixed
name with each call its bounded window reaches, and the nearest
assignment owns each call. Candidate generation, analysis and AST
reconciliation share that list, and a window's candidate identity uses
its full text, so windows to different calls do not collapse on a shared
200-character preview. Outside Python, call-anchored windows for
true-prefixed names are kept, and coalescing still drops one when a
direct owner reports the same call. With an AST, each added window
passes the same visibility, counterevidence, cached replacement and
companion checks as before. Windows for other names are unchanged.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(analyzer): report each true-prefixed shell call that #577 reports

The `\b`-bounded direct pattern does not match `shell=true_value`, so on
this branch such a call was reported only through a variable window. That
window pairs an assignment with a single call, skips an assignment
inside an earlier window, and was dropped outside Python. Compared with
#577 this lost:

- calls in JavaScript and Markdown files, fenced or plain;
- a later call that the AST companion abstains on, for example after an
  unknown receiver effect;
- all but the last same-line call in a file that does not parse, and the
  second of two identical same-line `shell=true` calls outside Python,
  which the direct candidate key folds into one;
- a call whose assignment sits inside the window of an earlier one.

_variable_shell_matches now pairs every assignment of a true-prefixed
name with each call its bounded window reaches, and the nearest
assignment owns each call. Candidate generation, analysis and AST
reconciliation share that list, and a window's candidate identity uses
its full text, so windows to different calls do not collapse on a shared
200-character preview. Outside Python, call-anchored windows for
true-prefixed names are kept, and coalescing still drops one when a
direct owner reports the same call. With an AST, each added window
passes the same visibility, counterevidence, cached replacement and
companion checks as before. Windows for other names are unchanged.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(supply-chain): opt into Python source-type propagation

SC2's literal-XOR decoding and SC3 branch on file_type, but the
supply-chain analyzer was not in the USES_PYTHON_SOURCE_TYPE set, so
Python executed through an extensionless shebang file or a shebang
Markdown file received its suffix type and silently lost the decoded
SC2 HIGH while completeness stayed true. Opt the module in and add
graph-level parity tests for .py, .pyw, extensionless and .md surfaces.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(build-context): decode PEP 263 primary Python before UTF-8 rejection

A selected primary file (direct .py/.pyw or extensionless Python
shebang) declaring a supported PEP 263 encoding such as latin-1 was
rejected as unsupported_primary_content because the generic primary
check only accepts UTF-8. The later source-decoding pass then decoded
it correctly, but the artifact stayed FAILED with a fatal event and 0%
coverage. Waive the generic UTF-8 rejection when the bytes classify as
Python and decode under their declared encoding; an unknown codec or
bytes invalid for the declared codec remain fatal, and format checks
(BOMs, archives, NUL density) are unchanged. The waiver is evaluated
lazily and not after the shared deadline, so no post-cache Python work
starts once the budget is spent.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Signed-off-by: Christopher Kevin <christopherk@nvidia.com>
Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>
Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-authored-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com>
Co-authored-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 23:23:24 -07:00
Mohit Guptaandkeshavp e6ebe70297 fix(analyzers): share Python AST parsing for environment-read detection (#332)
* fix(analyzer): detect full environment reads via AST

Signed-off-by: Mohit Gupta <mohgupta@nvidia.com>

* refactor(analyzers): share Python AST parsing

Signed-off-by: Mohit Gupta <mohgupta@nvidia.com>

* fix(analyzer): detect keyword environment secret reads

Signed-off-by: keshavp <32313895+keshprad@users.noreply.github.com>

---------

Signed-off-by: Mohit Gupta <mohgupta@nvidia.com>
Signed-off-by: keshavp <32313895+keshprad@users.noreply.github.com>
Co-authored-by: keshavp <32313895+keshprad@users.noreply.github.com>
2026-08-04 18:06:57 -07:00