Skip to content

Commit f3dac32

Browse files
authored
Merge pull request #761 from actions/juxtin/fix-allow-dependencies-licenses
Fix package-url parsing for allow-dependencies-licenses
2 parents e58c696 + 49fbbe0 commit f3dac32

File tree

6 files changed

+75
-44
lines changed

6 files changed

+75
-44
lines changed

__tests__/config.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ test('it raises an error if an empty allow list is specified', async () => {
5454
)
5555
})
5656

57+
test('it successfully parses allow-dependencies-licenses', async () => {
58+
setInput(
59+
'allow-dependencies-licenses',
60+
'pkg:npm/@test/[email protected],pkg:npm/example'
61+
)
62+
const config = await readConfig()
63+
expect(config.allow_dependencies_licenses).toEqual([
64+
'pkg:npm/@test/[email protected]',
65+
'pkg:npm/example'
66+
])
67+
})
68+
69+
test('it raises an error when an invalid package-url is used for allow-dependencies-licenses', async () => {
70+
setInput('allow-dependencies-licenses', 'not-a-purl')
71+
await expect(readConfig()).rejects.toThrow(`Error parsing package-url`)
72+
})
73+
74+
test('it raises an error when a nameless package-url is used for allow-dependencies-licenses', async () => {
75+
setInput('allow-dependencies-licenses', 'pkg:npm/@namespace/')
76+
await expect(readConfig()).rejects.toThrow(
77+
`Error parsing package-url: name is required`
78+
)
79+
})
80+
5781
test('it raises an error when an invalid package-url is used for deny-packages', async () => {
5882
setInput('deny-packages', 'not-a-purl')
5983

__tests__/test-helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function clearInputs(): void {
1111
'FAIL-ON-SEVERITY',
1212
'FAIL-ON-SCOPES',
1313
'ALLOW-LICENSES',
14+
'ALLOW-DEPENDENCIES-LICENSES',
1415
'DENY-LICENSES',
1516
'ALLOW-GHSAS',
1617
'LICENSE-CHECK',

dist/index.js

Lines changed: 32 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as core from '@actions/core'
55
import * as z from 'zod'
66
import {ConfigurationOptions, ConfigurationOptionsSchema} from './schemas'
77
import {isSPDXValid, octokitClient} from './utils'
8-
import {parsePURL} from './purl'
98

109
type ConfigurationOptionsPartial = Partial<ConfigurationOptions>
1110

@@ -53,7 +52,6 @@ function readInlineConfig(): ConfigurationOptionsPartial {
5352
'warn-on-openssf-scorecard-level'
5453
)
5554

56-
validatePURL(allow_dependencies_licenses)
5755
validateLicenses('allow-licenses', allow_licenses)
5856
validateLicenses('deny-licenses', deny_licenses)
5957

@@ -184,11 +182,6 @@ function parseConfigFile(configData: string): ConfigurationOptionsPartial {
184182
validateLicenses(key, data[key])
185183
}
186184

187-
// validate purls from the allow-dependencies-licenses
188-
if (key === 'allow-dependencies-licenses') {
189-
validatePURL(data[key])
190-
}
191-
192185
// get rid of the ugly dashes from the actions conventions
193186
if (key.includes('-')) {
194187
data[key.replace(/-/g, '_')] = data[key]
@@ -227,19 +220,3 @@ async function getRemoteConfig(configOpts: {
227220
throw new Error('Error fetching remote config file')
228221
}
229222
}
230-
function validatePURL(allow_dependencies_licenses: string[] | undefined): void {
231-
//validate that the provided elements of the string are in valid purl format
232-
if (allow_dependencies_licenses === undefined) {
233-
return
234-
}
235-
const invalid_purls = allow_dependencies_licenses.filter(
236-
purl => !parsePURL(purl).error
237-
)
238-
239-
if (invalid_purls.length > 0) {
240-
throw new Error(
241-
`Invalid purl(s) in allow-dependencies-licenses: ${invalid_purls}`
242-
)
243-
}
244-
return
245-
}

src/schemas.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ const PackageURLWithNamespace = z
4646
}
4747
})
4848

49+
const PackageURLString = z.string().superRefine((value, context) => {
50+
const purl = parsePURL(value)
51+
if (purl.error) {
52+
context.addIssue({
53+
code: z.ZodIssueCode.custom,
54+
message: `Error parsing package-url: ${purl.error}`
55+
})
56+
}
57+
if (!purl.name) {
58+
context.addIssue({
59+
code: z.ZodIssueCode.custom,
60+
message: `Error parsing package-url: name is required`
61+
})
62+
}
63+
})
64+
4965
export const ChangeSchema = z.object({
5066
change_type: z.enum(['added', 'removed']),
5167
manifest: z.string(),
@@ -81,7 +97,7 @@ export const ConfigurationOptionsSchema = z
8197
fail_on_scopes: z.array(z.enum(SCOPES)).default(['runtime']),
8298
allow_licenses: z.array(z.string()).optional(),
8399
deny_licenses: z.array(z.string()).optional(),
84-
allow_dependencies_licenses: z.array(z.string()).optional(),
100+
allow_dependencies_licenses: z.array(PackageURLString).optional(),
85101
allow_ghsas: z.array(z.string()).default([]),
86102
deny_packages: z.array(PackageURL).default([]),
87103
deny_groups: z.array(PackageURLWithNamespace).default([]),

0 commit comments

Comments
 (0)