Skip to content

Commit

Permalink
Fix package-lock.json lockfile parsing failures (#1467)
Browse files Browse the repository at this point in the history
This fixes an issue where the lockfile parser for JavaScript's
`package-lock.json` was too restrictive and would fail parsing valid
lockfiles.

As a solution all packages without explicit `resolved` field are now
ignored, since these are the only packages we're capable of analyzing
anyway.
  • Loading branch information
cd-work authored Jul 8, 2024
1 parent 1239431 commit b8373c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

### Fixed

- `package-lock.json` parsing failing for dependencies without `resolved` field

## 6.6.4 - 2024-06-27

### Fixed
Expand Down
21 changes: 9 additions & 12 deletions lockfile/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use lockfile_generator::pnpm::Pnpm as PnpmGenerator;
use lockfile_generator::yarn::Yarn as YarnGenerator;
#[cfg(feature = "generator")]
use lockfile_generator::Generator;
use log::debug;
use nom::error::convert_error;
use nom::Finish;
use phylum_types::types::package::PackageType;
Expand Down Expand Up @@ -67,19 +68,15 @@ impl Parse for PackageLock {
None => continue,
};

// Ignore bundled dependencies.
if keys.get("inBundle").is_some() {
continue;
}

// Ignore extraneous dependencies.
if keys.get("extraneous").is_some() {
continue;
}

// Get dependency type.
let resolved = get_field(keys, "resolved")
.ok_or_else(|| anyhow!("Dependency '{name}' is missing \"resolved\" key"))?;
let resolved = match get_field(keys, "resolved") {
Some(resolved) => resolved,
// Ignore packages without clear resolution details.
None => {
debug!("ignoring package without `resolved` field: {name}");
continue;
},
};

// Handle aliased dependencies.
let name = get_field(keys, "name").unwrap_or_else(|| name.into());
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b8373c8

Please sign in to comment.