diff --git a/internal/rebar/rebar.go b/internal/rebar/rebar.go index 331d364..e0d7469 100644 --- a/internal/rebar/rebar.go +++ b/internal/rebar/rebar.go @@ -14,9 +14,9 @@ func init() { type rebarLockParser struct{} var ( - // Match: {<<"pkg_name">>,{pkg,<<"pkg_name">>,<<"version">>},N} - rebarPkgRegex = regexp.MustCompile(`\{<<"([^"]+)">>,\{pkg,<<"[^"]+">>,<<"([^"]+)">>\},\d+\}`) - // Match: {<<"pkg_name">>, <<"sha256">>} + // Match: {<<"app_name">>,{pkg,<<"pkg_name">>,<<"version">>},N} + rebarPkgRegex = regexp.MustCompile(`\{<<"([^"]+)">>,\{pkg,<<"([^"]+)">>,<<"([^"]+)">>\},\d+\}`) + // Match: {<<"app_name">>, <<"sha256">>} rebarHashRegex = regexp.MustCompile(`\{<<"([^"]+)">>,\s*<<"([^"]+)">>\}`) ) @@ -26,16 +26,17 @@ func (p *rebarLockParser) Parse(filename string, content []byte) (*core.Result, hashes := rebarPackageHashes(text) for _, match := range rebarPkgRegex.FindAllStringSubmatch(text, -1) { - name := match[1] - version := match[2] + // A {pkg, Name} rename installs Hex package Name under the app name. + app, name, version := match[1], match[2], match[3] // Skip if name looks like a hash - if strings.HasPrefix(name, "sha") || len(name) > 50 { + if strings.HasPrefix(app, "sha") || len(app) > 50 { continue } + // rebar3 keys pkg_hash and pkg_hash_ext by the app name. integrity := "" - if hash := hashes[name]; hash != "" { + if hash := hashes[app]; hash != "" { integrity = "sha256-" + hash } diff --git a/internal/rebar/rebar_test.go b/internal/rebar/rebar_test.go index 4763794..5dab4d2 100644 --- a/internal/rebar/rebar_test.go +++ b/internal/rebar/rebar_test.go @@ -19,8 +19,8 @@ func TestRebarLock(t *testing.T) { t.Fatalf("Parse failed: %v", err) } - if len(res.Dependencies) != 3 { - t.Fatalf("expected 3 dependencies, got %d", len(res.Dependencies)) + if len(res.Dependencies) != 4 { + t.Fatalf("expected 4 dependencies, got %d", len(res.Dependencies)) } depMap := make(map[string]core.Dependency) @@ -40,6 +40,10 @@ func TestRebarLock(t *testing.T) { "1.1.1", "sha256-36B66CC8E9B6C30BCAEB35A4674ABAF9A4C4E5E30C8E7A0E4FC3E4F4F4E4E4F4", }, + "jiffy_fork": { + "1.1.1", + "sha256-E2B1A7C9D0F3E4A5B6C7D8E9F0A1B2C3D4E5F6A7B8C9D0E1F2A3B4C5D6E7F8A9", + }, "ssl_verify_fun": { "1.1.7", "sha256-BDB0D2471F453C88FF3908E7686F86F9BE327D065CC1EC16FA4540197EA04680", diff --git a/rebar_lock_test.go b/rebar_lock_test.go new file mode 100644 index 0000000..c37c23e --- /dev/null +++ b/rebar_lock_test.go @@ -0,0 +1,27 @@ +package manifests + +import ( + "os" + "testing" +) + +func TestRebarLockRenamedPackagePURL(t *testing.T) { + content, err := os.ReadFile("testdata/rebar/rebar.lock") + if err != nil { + t.Fatal(err) + } + got, err := Parse("rebar.lock", content) + if err != nil { + t.Fatal(err) + } + for _, dep := range got.Dependencies { + if dep.Name != "jiffy_fork" { + continue + } + if dep.Version != "1.1.1" || dep.PURL != "pkg:hex/jiffy_fork@1.1.1" || dep.Integrity == "" { + t.Errorf("renamed dependency = %+v", dep) + } + return + } + t.Errorf("jiffy_fork not found in %+v", got.Dependencies) +} diff --git a/testdata/rebar/rebar.lock b/testdata/rebar/rebar.lock index 91a09c8..f702add 100644 --- a/testdata/rebar/rebar.lock +++ b/testdata/rebar/rebar.lock @@ -1,14 +1,17 @@ {"1.2.0", [{<<"hex_core">>,{pkg,<<"hex_core">>,<<"0.10.3">>},0}, {<<"verl">>,{pkg,<<"verl">>,<<"1.1.1">>},0}, + {<<"jiffy">>,{pkg,<<"jiffy_fork">>,<<"1.1.1">>},0}, {<<"ssl_verify_fun">>,{pkg,<<"ssl_verify_fun">>,<<"1.1.7">>},1}]}. [ {pkg_hash,[ {<<"hex_core">>, <<"21E84B3AB21EEE6A1EAA56B69624E0D7D82F61F148B4C7441B4692FA2C48E0C1">>}, {<<"verl">>, <<"0925F3708B11CACC6FBA2CB35B5EFDCE78C6E79B9DB90A30BD75A3C6FE58A8A5">>}, + {<<"jiffy">>, <<"9B4C1D0E1F6A2B3C4D5E6F708192A3B4C5D6E7F8091A2B3C4D5E6F708192A3B4">>}, {<<"ssl_verify_fun">>, <<"FE4C190E8F37401D30167C8C405EDA19469F34577987C76DDE613E838BBC67F8">>}]}, {pkg_hash_ext,[ {<<"hex_core">>, <<"4516C43F1CD22C9E4ABBBC3C4F5EBBC84EF24F7D8BF0D3D6BF0F3D5818F8612D">>}, {<<"verl">>, <<"36B66CC8E9B6C30BCAEB35A4674ABAF9A4C4E5E30C8E7A0E4FC3E4F4F4E4E4F4">>}, + {<<"jiffy">>, <<"E2B1A7C9D0F3E4A5B6C7D8E9F0A1B2C3D4E5F6A7B8C9D0E1F2A3B4C5D6E7F8A9">>}, {<<"ssl_verify_fun">>, <<"BDB0D2471F453C88FF3908E7686F86F9BE327D065CC1EC16FA4540197EA04680">>}]} ].