Skip to content

Commit 45a8276

Browse files
authored
Basic tests for PURL validation in config
1 parent 6406179 commit 45a8276

File tree

5 files changed

+112
-10
lines changed

5 files changed

+112
-10
lines changed

__tests__/config.test.ts

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

57+
test('it raises an error when an invalid package-url is used for deny-packages', async () => {
58+
setInput('deny-packages', 'not-a-purl')
59+
60+
await expect(readConfig()).rejects.toThrow(`Error parsing purl`)
61+
})
62+
63+
test('it raises an error when an argument to deny-groups is missing a namespace', async () => {
64+
setInput('deny-groups', 'pkg:npm/my-fun-org')
65+
66+
await expect(readConfig()).rejects.toThrow(`purl must have a namespace`)
67+
})
68+
5769
test('it raises an error when given an unknown severity', async () => {
5870
setInput('fail-on-severity', 'zombies')
5971

__tests__/test-helpers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export function clearInputs(): void {
1919
'BASE-REF',
2020
'HEAD-REF',
2121
'COMMENT-SUMMARY-IN-PR',
22-
'WARN-ONLY'
22+
'WARN-ONLY',
23+
'DENY-GROUPS',
24+
'DENY-PACKAGES'
2325
]
2426

2527
// eslint-disable-next-line github/array-foreach

dist/index.js

Lines changed: 62 additions & 4 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/schemas.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,39 @@ export const SCOPES = ['unknown', 'runtime', 'development'] as const
66

77
export const SeveritySchema = z.enum(SEVERITIES).default('low')
88

9-
const PackageURL = z.string().transform(purlString => {
10-
return parsePURL(purlString)
11-
})
9+
const PackageURL = z
10+
.string()
11+
.transform(purlString => {
12+
return parsePURL(purlString)
13+
})
14+
.superRefine((purl, context) => {
15+
if (purl.error) {
16+
context.addIssue({
17+
code: z.ZodIssueCode.custom,
18+
message: `Error parsing purl`
19+
})
20+
}
21+
})
22+
23+
const PackageURLWithNamespace = z
24+
.string()
25+
.transform(purlString => {
26+
return parsePURL(purlString)
27+
})
28+
.superRefine((purl, context) => {
29+
if (purl.error) {
30+
context.addIssue({
31+
code: z.ZodIssueCode.custom,
32+
message: `Error parsing purl`
33+
})
34+
}
35+
if (purl.namespace === null) {
36+
context.addIssue({
37+
code: z.ZodIssueCode.custom,
38+
message: `purl must have a namespace, and the namespace must be followed by '/'`
39+
})
40+
}
41+
})
1242

1343
export const ChangeSchema = z.object({
1444
change_type: z.enum(['added', 'removed']),
@@ -48,7 +78,7 @@ export const ConfigurationOptionsSchema = z
4878
allow_dependencies_licenses: z.array(z.string()).optional(),
4979
allow_ghsas: z.array(z.string()).default([]),
5080
deny_packages: z.array(PackageURL).default([]),
51-
deny_groups: z.array(PackageURL).default([]),
81+
deny_groups: z.array(PackageURLWithNamespace).default([]),
5282
license_check: z.boolean().default(true),
5383
vulnerability_check: z.boolean().default(true),
5484
config_file: z.string().optional(),

0 commit comments

Comments
 (0)