Skip to content

Commit e2c5bb7

Browse files
committed
fix: consider purl subpath when validating golang package
1 parent 58ab39c commit e2c5bb7

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/core/validate-graph.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@ export function validatePackageURL(pkg: types.PkgInfo): void {
6060
);
6161
break;
6262

63+
case 'golang': {
64+
let expected = purlPkg.namespace
65+
? `${purlPkg.namespace}/${purlPkg.name}`
66+
: purlPkg.name;
67+
if (purlPkg.subpath) expected += `/${purlPkg.subpath}`;
68+
assert(pkg.name === expected, `name and packageURL name do not match`);
69+
break;
70+
}
71+
6372
case 'composer':
64-
case 'golang':
6573
case 'npm':
6674
case 'swift':
6775
assert(

test/core/validate-graph.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ describe('validatePackageURL', () => {
158158
purl: 'pkg:golang/[email protected]',
159159
},
160160
],
161+
[
162+
'golang package with subpath',
163+
{
164+
name: 'github.com/foo/bar/pkg/baz',
165+
version: '1.2.3',
166+
purl: 'pkg:golang/github.com/foo/[email protected]#pkg/baz',
167+
},
168+
],
161169
])('validates golang Purls: %s', (name, pkg) => {
162170
expect(() => validatePackageURL(pkg)).not.toThrow();
163171
});
@@ -179,6 +187,14 @@ describe('validatePackageURL', () => {
179187
purl: 'pkg:golang/google.golang.org/[email protected]',
180188
},
181189
],
190+
[
191+
'package name does not match purl subpath',
192+
{
193+
name: 'bar/baz',
194+
version: '1.2.3',
195+
purl: 'pkg:golang/[email protected]#pkg/baz',
196+
},
197+
],
182198
[
183199
'package name does not include purl namespace',
184200
{
@@ -187,6 +203,14 @@ describe('validatePackageURL', () => {
187203
purl: 'pkg:golang/google.golang.org/[email protected]',
188204
},
189205
],
206+
[
207+
'package name does not include purl subpath',
208+
{
209+
name: 'bar',
210+
version: '1.2.3',
211+
purl: 'pkg:golang/[email protected]#pkg/baz',
212+
},
213+
],
190214
])('should throw on invalid purl: %s', (name, pkg) => {
191215
expect(() => validatePackageURL(pkg)).toThrow();
192216
});

0 commit comments

Comments
 (0)