Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consider purl subpath when validating golang package #112

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/core/validate-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ export function validatePackageURL(pkg: types.PkgInfo): void {
);
break;

case 'golang': {
let expected = purlPkg.namespace
? `${purlPkg.namespace}/${purlPkg.name}`
: purlPkg.name;
if (purlPkg.subpath) expected += `/${purlPkg.subpath}`;
assert(pkg.name === expected, `name and packageURL name do not match`);
break;
}

case 'composer':
case 'golang':
case 'npm':
case 'swift':
assert(
Expand Down
24 changes: 24 additions & 0 deletions test/core/validate-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ describe('validatePackageURL', () => {
purl: 'pkg:golang/foo@1.2.3',
},
],
[
'golang package with subpath',
{
name: 'github.com/foo/bar/pkg/baz',
version: '1.2.3',
purl: 'pkg:golang/github.com/foo/bar@1.2.3#pkg/baz',
},
],
])('validates golang Purls: %s', (name, pkg) => {
expect(() => validatePackageURL(pkg)).not.toThrow();
});
Expand All @@ -179,6 +187,14 @@ describe('validatePackageURL', () => {
purl: 'pkg:golang/google.golang.org/bar@1.2.3',
},
],
[
'package name does not match purl subpath',
{
name: 'bar/baz',
version: '1.2.3',
purl: 'pkg:golang/bar@1.2.3#pkg/baz',
},
],
[
'package name does not include purl namespace',
{
Expand All @@ -187,6 +203,14 @@ describe('validatePackageURL', () => {
purl: 'pkg:golang/google.golang.org/bar@1.2.3',
},
],
[
'package name does not include purl subpath',
{
name: 'bar',
version: '1.2.3',
purl: 'pkg:golang/bar@1.2.3#pkg/baz',
},
],
])('should throw on invalid purl: %s', (name, pkg) => {
expect(() => validatePackageURL(pkg)).toThrow();
});
Expand Down