fix(pd001,pd002): resolve imports per workspace member and ignore type-only imports - #1114
Open
osfv wants to merge 1 commit into
Open
fix(pd001,pd002): resolve imports per workspace member and ignore type-only imports#1114osfv wants to merge 1 commit into
osfv wants to merge 1 commit into
Conversation
…e-only imports Two independent false-positive sources in the phantom-dependency rules, both on the same comparison in the PD detectors. Workspace roots: imports were collected from the whole tree but declarations were resolved against the root manifest only, so scanning a monorepo root reported every dependency a member declares and imports in its own source as a transitive-only phantom. buildOverrideContext now discovers workspace members (root `workspaces`, pnpm-workspace.yaml) and their declared packages, and PD001/PD002 resolve each importing file against the nearest enclosing member before falling back to the root. The finding lists only the files whose owning package leaves the import undeclared. Type-only imports: the usage scanner is a regex pass over raw file text, so a JSDoc annotation such as `/** @type {import('postcss-load-config') .Config} */` counted as a dynamic import, and `import type { X } from 'pkg'` counted as a runtime import. Comments are now blanked out (string-aware) before matching, and `import type` / `export type` / all-`type` specifier lists are skipped. This applies to every consumer of the scanner: PD001, PD002, the OA009 guard, and the --usage filter, where a type-only reference to a vulnerable package no longer counts as usage. Closes OWASP#966
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed and why
Fixes both defects reported in #966. They land on the same comparison in the PD detectors, so they are in one change as suggested there.
A - workspace roots
Imports were collected from the whole tree while declarations were resolved against the root manifest only, so scanning a monorepo root reported every dependency a member declares and imports in its own source as a PD002 phantom (or PD001 when an override happened to exist).
buildOverrideContextnow discovers workspace members (rootworkspaces,pnpm-workspace.yaml) via a newreadWorkspaceMemberManifestsinsrc/utils/package-json.ts, reusing the same pattern expansionreadDirectDependencyNamesalready uses for the CVE scan, and exposesctx.workspaceMembers(dir + declared set).phantom-utils.undeclaredImportFiles.B - type-only imports counted as runtime
scanAllImports/scanProjectForPackageUsageare a regex pass over raw text, so/** @type {import('postcss-load-config').Config} */counted as a dynamic import andimport type { X } from 'pkg'counted as a runtime import.//,/* */) are blanked before matching, string-aware so'https://...'is not treated as a comment.import type ...,export type ..., and specifier lists where every entry istype-prefixed are skipped. A default import that happens to be namedtype, or a mixed list (import { type A, b }), still counts.This applies to every consumer of the scanner, as discussed in the issue: PD001, PD002, the OA009 guard, and the
--usagereachability filter. One existing assertion intests/usage.test.tstreatedimport typeas usage and was updated accordingly; a type-only reference to a vulnerable package no longer counts as "used".Verification
Against the minimal repro from the issue (pnpm workspace root,
apps/webdeclares and importsjs-yaml) plus a stockpostcss.config.mjswith the JSDoc annotation,mainreports two PD002 findings and this branch reports none. New unit coverage intests/usage.test.ts,tests/overrides/detectors/pd00{1,2}.test.ts, andtests/overrides/context-builder.test.ts. Rule docs for PD001/PD002 gained a short "What counts as an import" section.Note: #966 is assigned to @alamb-hex. The maintainer's check-in on Sept 6 had no reply, so I went ahead; happy to close this in favour of theirs if they are still working on it.
Closes #966