Skip to content

ppm: switch vulnerability lookup to filter/packages - #17602

Merged
kevinushey merged 4 commits into
mainfrom
feature/17446-ppm-filter-packages
May 12, 2026
Merged

kevinushey merged 4 commits into
mainfrom
feature/17446-ppm-filter-packages

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Intent

Addresses #17446.

The Packages pane retrieves vulnerability info from Posit Package Manager via .rs.ppm.getVulnerabilityInformation. That function was calling the legacy GET /repos/{repo}/vulns endpoint, which was removed in Package Manager 2026.04.0 (and only restored in 2026.04.1). This PR migrates the call to the more general POST /filter/packages with has_vulns: true, which is the recommended long-term home for this query.

Implementation

src/cpp/session/modules/SessionPPM.R:

  • .rs.ppm.getVulnerabilityInformationImpl now POSTs to <root>/__api__/filter/packages via curl::curl_fetch_memory, mirroring the pattern already used by .rs.ppm.updateMetadataCache in the same file. Netrc auth, the PWB_PPM_CURL_VERBOSE toggle, and the per-repoUrl cache are preserved.
  • The NDJSON parse/group/dedup logic is factored into a new pure helper .rs.ppm.parseVulnerabilityResponse(contents) so it can be unit-tested without mocking the HTTP layer.
  • The endpoint returns one record per (package, version), so the helper groups vulns by package name (matching the existing {package -> [vulns]} shape consumed by SessionPackages.cpp and PackageVulnerabilityTypes.java) and dedupes by vuln id within each package.

Tests

New src/cpp/tests/testthat/test-ppm.R covers the parser:

  • empty body
  • single record / single vuln
  • multi-version dedup (same vuln id across versions)
  • multi-version with disjoint vuln ids retained
  • grouping across distinct packages
  • error record short-circuits to empty
  • records missing the vulns field skipped
  • records with empty vulns arrays skipped
  • records missing the name field skipped
  • blank lines between records tolerated
  • nested vuln fields (ranges, events, versions) preserved

Test plan

  • ./rstudio-tests --scope r --filter ppm -> [FAIL 0 | PASS 3314]
  • Manual verification against a live PPM instance (Packages pane vulnerability indicators still appear)

The legacy /repos/{repo}/vulns endpoint was removed in Package Manager
2026.04.0 and only restored in 2026.04.1. Migrate to the more general
POST /filter/packages with has_vulns:true, which is the long-term home
for this query.

Addresses #17446.
@kevinushey kevinushey added this to the Golden Wattle milestone May 11, 2026
@posit-snyk-bot

posit-snyk-bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@kevinushey
kevinushey requested a review from gtritchie May 11, 2026 21:56
@gtritchie

Copy link
Copy Markdown
Member

Review feedback

Ran a multi-agent review (code, tests, error handling, comments). Most of this is around silent failures on the new HTTP path; the structural choices (pure parser, mirroring updateMetadataCache, NDJSON dedup) all look right.

Critical — silent failure paths

  1. HTTP status code is never checked — SessionPPM.R:104-105
    The legacy download.file() raised on non-2xx and got caught by tryCatch. curl::curl_fetch_memory does not — a 401/403/404/500 returns a normal response with status_code set, and the (possibly HTML) error body gets fed straight to the parser. The parser's error field check only matches PPM's NDJSON error envelope; a Cloudflare/proxy HTML page or a non-PPM 404 silently yields zero vulns. This effectively makes the netrc auth case this PR is adding untestable from the user's side — a wrong netrc credential gives a clean Packages pane with no diagnostic.

    if (response$status_code < 200 || response$status_code >= 300) {
       .rs.logErrorMessage(sprintf("PPM vulnerability lookup returned HTTP %d from %s",
                                   response$status_code, endpoint))
       return(empty)
    }
  2. condition = identity swallows too much — SessionPPM.R:96-102
    The handler catches DNS/TLS/timeout and warnings and programmer errors (malformed handle, bad URL). All map to "no vulns" with no log. Narrowing to error = identity and logging conditionMessage() before returning empty would surface the underlying cause. Pre-existing pattern but the migration is a reasonable opportunity.

  3. A single error record discards all already-parsed vulns, silently — SessionPPM.R:120-125
    If the NDJSON stream has 999 valid records and 1 error line, the parser returns empty. The sibling .rs.ppm.updateMetadataCache (line 283-295) at least emits a warning() with the server's error message and code — the new code does not. Worth picking a policy (skip the error record, or bail with a warning) and applying it consistently to both functions. Compounded by PAM Session Prototype #4 below: the empty result is then cached.

Important

  1. Empty/failed results are cached — SessionPPM.R:107-108
    A transient PPM 503 or one bad NDJSON line at startup persists as "no vulns" for the entire session. Moving the assign() so it only runs on the success path would let the next refresh recover.

  2. PWB_PPM_CURL_VERBOSE env-var coercion has a bug — SessionPPM.R:59-60 and :236-237
    R correctly coerces "FALSE"/"TRUE"/"T"/"F"/"true"/"false", so the common case is fine. But any other value ("1", "yes", "0", empty string) raises argument is not interpretable as logical. That error propagates out, gets caught by the outer tryCatch at line 36-39, and silently returns empty. So PWB_PPM_CURL_VERBOSE=1 set by an admin trying to debug actually disables vuln lookup. Fix with isTRUE(as.logical(Sys.getenv("PWB_PPM_CURL_VERBOSE", unset = "FALSE"))).

  3. Outer tryCatch swallows errors without logging — SessionPPM.R:36-39
    Pre-existing, but the new code path adds many more failure modes (JSON parse per line, rawToChar, the coercion bug above). At minimum, log conditionMessage(cnd) before returning empty.

  4. Malformed JSON is silently dropped — no test pins the contract — SessionPPM.R:118
    .rs.fromJSON returns NULL on parse failure; a NULL record falls through the is.null(name) skip. A truncated response gives a partial-but-clean result with no signal. Worth a test documenting the intended behavior.

  5. Dedup of multiple id-less vulns collapses them — SessionPPM.R:147
    .rs.nullCoalesce(id, "") means two distinct vulns missing id get deduped. Low-likelihood in practice (PPM emits OSV ids) but worth a test pinning the contract.

Suggestions

  1. Same-id dedup ordering is unpinned. When V1 appears for version 1.0.0 with one summary and for 2.0.0 with another, current code keeps the first. Worth a test — the per-(package, version) endpoint shape makes this routine.

  2. CRLF line endings would break parsing. strsplit(contents, "\n") leaves trailing \r on each line, which then fails JSON parse. Either strip \r before splitting or confirm via test that PPM emits LF only.

  3. Cache layer is testable but untested. .rs.ppm.vulns is reachable directly from R; one test verifying the "don't cache error results" contract (once PAM Session Prototype #4 is fixed) is cheap insurance.

  4. Comments

    • SessionPPM.R:89: httpauth = 1L is libcurl's CURLAUTH_BASIC. The magic number isn't self-documenting; an inline note would help. Same at line 270 in updateMetadataCache.
    • SessionPPM.R:58: # build a curl handle for the request is pure restatement — could drop.
    • SessionPPM.R:83: "use netrc if available" could note why — the new endpoint requires auth on locked-down PPM instances where the legacy download.file path was effectively anonymous.
  5. .rs.ppm.fromRepositoryUrl has zero coverage. Out of scope but adjacent and cheap to add — regex with five capture groups.

Strengths

  • Right testability choice: extracting .rs.ppm.parseVulnerabilityResponse as a pure function avoids HTTP mocks and matches the codebase pattern (no other test in the repo mocks curl::curl_fetch_memory).
  • Tests are behavior-focused and refactor-resilient.
  • Mirrors .rs.ppm.updateMetadataCache for curl setup, netrc, env-keyed cache.
  • NEWS.md, branch naming, ASCII, style all conform.
  • expected() helper keeps assertions readable.

Recommendation

The structural change is good. Most of the silent-failure paths above existed pre-migration too, but #1 (HTTP status) and #3 (NDJSON error record policy) are arguably new regressions on the netrc path this PR is enabling. Worth addressing those two before merge; the rest can land as follow-ups.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall LGTM, review agent had some concerns that are worth a peek to see if they're worth addressing.

Coerce PWB_PPM_CURL_VERBOSE through isTRUE(as.logical(...)) so a
non-boolean value won't error inside the curl handle setup and silently
empty the vuln response. When deduping vulnerabilities by id, merge the
per-record versions maps so the UI's per-version check still flags
every affected installed version.
curl::curl_fetch_memory does not raise on non-2xx, so a 401/403/404/500
would silently feed a non-NDJSON error body to the parser, yield zero
vulns, and cache that empty result for the rest of the session. Add an
explicit status check, log the cause from both the inner and outer
tryCatch handlers, narrow the inner catch to error = identity, and have
the parser warn + return NULL on an NDJSON error record (mirroring
.rs.ppm.updateMetadataCache) so the caller can skip the cache and retry
on the next refresh. Also strip stray \\r before splitting in case a
proxy rewrites line endings.

Addresses review feedback on #17602.
Add tests covering corners of .rs.ppm.parseVulnerabilityResponse so a
future refactor makes a deliberate decision: malformed NDJSON lines are
silently skipped (.rs.fromJSON returns NULL); id-less vulns end up in
separate slots because R's list[[""]] always reads NULL while each
write appends positionally; same-id dedup keeps the first record's
fields and only merges the versions map; CRLF line endings parse
cleanly thanks to the leading gsub.
@kevinushey
kevinushey merged commit 6907d4e into main May 12, 2026
8 checks passed
@kevinushey
kevinushey deleted the feature/17446-ppm-filter-packages branch May 12, 2026 07:12
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.

3 participants