-
-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly encode PURLs in SBOM output
- Loading branch information
Showing
7 changed files
with
38 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# Changelog | ||
|
||
## [5.2.2] | ||
|
||
### Bugfix | ||
|
||
- Encode purls correctly in SBOM | ||
|
||
## [5.2.1] | ||
|
||
### Bugfix | ||
|
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { should } from 'chai'; | ||
import 'mocha'; | ||
should(); | ||
import { generatePURL } from '../../lib/reporters/utils'; | ||
|
||
describe('purl encoding', () => { | ||
it('should not touch a simple string', () => { | ||
generatePURL({ component: 'jquery', version: '1.2.3' }).should.equal('pkg:npm/jquery@1.2.3'); | ||
}); | ||
it('should encode @ in package scopes', () => { | ||
generatePURL({ component: '@angular/core', version: '1.2.3' }).should.equal('pkg:npm/%40angular/core@1.2.3'); | ||
}); | ||
it('should not doulbe encode', () => { | ||
generatePURL({ component: '%40angular/core', version: '1.2.3' }).should.equal('pkg:npm/%40angular/core@1.2.3'); | ||
}); | ||
}); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
import { Component } from '../types'; | ||
|
||
function encodePURLchars(str: string): string { | ||
return str.replace( | ||
/[^A-Za-z0-9.+/=-%]/g, | ||
(match) => '%' + ('0' + match.charCodeAt(0).toString(16).toUpperCase()).slice(-2), | ||
); | ||
} | ||
|
||
export function generatePURL(component: Component): string { | ||
if (component.basePurl) { | ||
return component.basePurl + '@' + component.version; | ||
const [pType, ...rest] = component.basePurl.split(':'); | ||
const pathElements = rest.join(':').split('/').map(encodePURLchars).join('/'); | ||
return `${pType}:${pathElements}@${encodePURLchars(component.version)}`; | ||
} | ||
const compName = component.npmname || component.component; | ||
return `pkg:npm/${compName}@${component.version}`; | ||
return `pkg:npm/${encodePURLchars(compName)}@${encodePURLchars(component.version)}`; | ||
} |