Skip to content

fix(lp4): recognize Go capability calls in least-privilege check - #1124

Open
Varshith-Kali wants to merge 2 commits into
NVIDIA:mainfrom
Varshith-Kali:fix-1121-lp4-go-capability-patterns
Open

Varshith-Kali wants to merge 2 commits into
NVIDIA:mainfrom
Varshith-Kali:fix-1121-lp4-go-capability-patterns

Conversation

@Varshith-Kali

Copy link
Copy Markdown
Contributor

Fixes #1121.

Bug: LP4 reported env and file_read as over-declared in Go skills because the capability patterns in mcp_least_privilege.py only covered Python and JavaScript calls. A Go skill honestly declaring env and file_read got false LP4 findings, and the only way to clear them was to delete true declarations, leaving the manifest under-declared.

Fix: extend the env, file_read and file_write pattern tables with the idiomatic Go calls:

  • env: os.LookupEnv, os.ExpandEnv (os.Getenv already matched via the case-insensitive os.getenv pattern, and os.Environ via os.environ)
  • file_read: os.ReadFile, os.Open, os.ReadDir, filepath.Walk/WalkDir/Glob, io.ReadAll, plus the pre-1.16 ioutil.ReadFile alias
  • file_write: os.WriteFile, os.Create, os.CreateTemp, plus ioutil.WriteFile

Patterns follow the existing table conventions (qualified call names, case-insensitive), so Python and JavaScript detection is unchanged.

Repro (minimal Go skill from the issue, os.LookupEnv + os.ReadFile):

  • Before: 2 LP4 findings (env, file_read)
  • After: 0 findings

Tests: added TestLP4GoCapabilityPatterns in tests/test_mcp_least_privilege.py: 12 parametrized Go cases (one per new call, each fails before the fix and passes after), a negative control proving LP4 still fires for a Go skill that declares permissions its code never uses, an LP1 check proving undeclared Go writes are now caught, and Python/JavaScript unchanged checks.

  • tests/test_mcp_least_privilege.py: 76 passed
  • Related MCP analyzer suites (test_mcp_least_privilege.py, test_mcp_rug_pull.py, test_mcp_tool_poisoning.py): 694 passed, 6 deselected
  • ruff check and ruff format --check clean on both touched files
  • Full suite not run locally (takes about 2h on this VM); CI will run it.

LP4 reported env and file_read as over-declared in Go skills because the
capability patterns only covered Python and JavaScript calls. Extend the
env, file_read and file_write pattern tables with the idiomatic Go calls:
os.LookupEnv and os.ExpandEnv for env; os.ReadFile, os.Open, os.ReadDir,
filepath.Walk/WalkDir/Glob and io.ReadAll for file_read; os.WriteFile,
os.Create and os.CreateTemp for file_write, plus the pre-1.16 ioutil
aliases. Patterns follow the existing table conventions (qualified call
names, case-insensitive), so Python and JavaScript detection is unchanged.

Fixes NVIDIA#1121.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

CI note on the current head: test-unit shows only the 4 known pre-existing failures in tests/nodes/analyzers/test_json_decoded_views.py (unicode fence source coordinates, ~~~sh vs ~~~), which also fail on clean main. Everything else in the suite passes (26126 passed). Nothing in this PR touches that area, so this looks like a main-branch failure, not a regression from the LP4 Go change.

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[SkillSpector Review]

Decision — Request changes.

Value and readiness — Recognizing Go's direct file and environment calls fixes LP4 reports that can prompt authors to remove permissions their skills actually use. The shared capability table needs tighter evidence before this change is ready.

Material findings

  • mcp_least_privilege.py:94–112 adds io.ReadAll as file-read evidence. It can consume any io.Reader, including a network response or an in-memory buffer. This can clear a genuinely over-declared file-read permission or create an LP1 finding for an undeclared file-read capability that was never used. The new readall test passes an arbitrary reader, so it currently locks in that mistake. Keep the direct file APIs, drop the generic reader match unless its input is proven to be a file, and add a negative case with an in-memory reader. Capability patterns
  • os\.open\b is applied to every file through the shared case-insensitive table. A Python os.open call with write-only flags now counts as read evidence. Scope the new Go invocation patterns to Go components, or use an equivalent language-sensitive match, and cover a Python write-only call in the unchanged-behavior tests. Capability scan

PIC tradeoffs — Uses the existing bounded matcher. Restricting evidence to language and API pairs that establish the capability preserves LP1 precision and LP4 coverage without introducing Go data-flow analysis.

Verification and gaps — Reviewed the complete diff, the matching and reporting paths, PR discussion, and the unit-job logs. The recorded job reported 26,126 passes and four failures in Unicode fence source coordinates. The contributor reports reproducing those failures on the PR base. The Go fixtures do not cover the generic-reader or cross-language cases above. Static review only.

Reviewed head: 5e2137cda8ebb5e8a3a89691872840ef660a7e3f

io.ReadAll consumes any io.Reader, so it proved nothing about file
access: it could clear a genuinely over-declared file-read permission
or create LP1 for an undeclared file-read that was never used. Drop it
from the file-read table; a file opened with os.Open still counts
through os.Open.

Move the remaining Go patterns (os.ReadFile, os.Open, os.ReadDir,
filepath.Walk/Glob, os.WriteFile, os.Create, os.CreateTemp, os.LookupEnv,
os.ExpandEnv, ioutil aliases) into a Go-only table applied only to .go
files. The shared table is language-agnostic, so os.open previously
counted a Python write-only os.open call as read evidence.

Tests: remove the readall positive case that locked in the mistake;
add negative cases for an in-memory reader and a network response body,
a positive case for os.Open plus io.ReadAll, and Python write-only
os.open cases for LP4 and LP1.

Addresses review on NVIDIA#1124.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Thanks for the review, both findings are fixed in ec630e0.

io.ReadAll: dropped from the file-read table entirely. It consumes any io.Reader, so on its own it proves nothing about file access. A file read through os.Open plus io.ReadAll still counts, through os.Open itself. The old readall positive test is removed, and there are now negative cases for an in-memory buffer (bytes.NewBufferString) and a network response body, both of which keep LP4 and fire no LP1 for file_read.

os.open cross-language leak: all new Go patterns moved into a Go-only table that _detect_capabilities applies only to .go files (os.ReadFile, os.Open, os.ReadDir, filepath.Walk/Glob, os.WriteFile, os.Create, os.CreateTemp, os.LookupEnv, os.ExpandEnv, ioutil aliases). A Python os.open with write-only flags no longer counts as read evidence; covered by new tests for both LP4 (permission stays over-declared) and LP1 (no finding with an unrelated declaration). os.Getenv stays in the shared table since it was already covered there before this PR.

Tests: tests/test_mcp_least_privilege.py, 80 passed. The new negative tests fail on the previous head, so they catch the old behavior.

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.

LP4 reports env and file_read as over-declared in Go skills: capability patterns miss os.LookupEnv, os.ReadFile, os.Open

2 participants