Skip to content

Commit 842cd07

Browse files
authored
Add data sources and rule types for OSV and Sonatype OSS index (#217)
These two are samples of how to use data sources in your rule types. Signed-off-by: Juan Antonio Osorio <ozz@stacklok.com>
1 parent 19df782 commit 842cd07

5 files changed

Lines changed: 231 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ reference profile recommended for GitHub, use the following command:
2121
```bash
2222
minder profile create -f profiles/github/profile.yaml
2323
```
24+
25+
# Data Sources
26+
27+
Reference data sources are available in the `data-sources` directory. To take a data source
28+
into use, you'll need to instantiate it in a Minder instance. For example, to instantiate the
29+
reference data source for using OSV as a data source, use the following command:
30+
31+
```bash
32+
minder datasource create -f data-sources osv.yaml
33+
```
34+

data-sources/osv.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
version: v1
3+
type: data-source
4+
name: osv
5+
context: {}
6+
rest:
7+
def:
8+
query:
9+
endpoint: 'https://api.osv.dev/v1/query'
10+
parse: json
11+
method: POST
12+
body_from_field: query
13+
input_schema:
14+
type: object
15+
properties:
16+
query:
17+
type: object
18+
properties:
19+
version:
20+
type: string
21+
package:
22+
type: object
23+
properties:
24+
ecosystem:
25+
type: string
26+
description: The ecosystem the dependency belongs to
27+
name:
28+
type: string
29+
description: The name of the dependency
30+
required:
31+
- query
32+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
version: v1
3+
type: data-source
4+
name: sonatype_oss_index
5+
context: {}
6+
rest:
7+
def:
8+
query:
9+
endpoint: 'https://ossindex.sonatype.org/api/v3/component-report'
10+
parse: json
11+
method: POST
12+
body_from_field: request
13+
headers:
14+
Content-Type: application/json
15+
input_schema:
16+
type: object
17+
properties:
18+
request:
19+
type: object
20+
properties:
21+
coordinates:
22+
type: array
23+
items:
24+
type: string
25+
uniqueItems: true
26+
required:
27+
- coordinates
28+
required:
29+
- request
30+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
version: v1
3+
type: rule-type
4+
name: osv_vulnerabilities
5+
display_name: Detect Vulnerable Dependencies in OSV
6+
context: {}
7+
severity:
8+
value: low # TODO: We should derive the severity from the rule type output itself.
9+
release_phase: alpha
10+
short_failure_message: Vulnerable dependencies found in this repository matching the OSV database.
11+
description: |
12+
This rule identifies dependencies in the repository with known vulnerabilities according to the OSV (Open Source Vulnerabilities) database. It helps ensure your project avoids using libraries or packages that could introduce security risks.
13+
14+
By regularly scanning and updating dependencies, you can mitigate the risk of exploits and maintain secure development practices.
15+
16+
Documentation: https://osv.dev/
17+
guidance: |
18+
Check the dependencies in the repository for updates or patches. Resolve vulnerabilities by upgrading to a secure version. Refer to the following documentation for remediation steps:
19+
20+
- OSV: https://osv.dev
21+
def:
22+
in_entity: repository
23+
param_schema: {}
24+
rule_schema: {}
25+
ingest:
26+
type: deps
27+
deps: {}
28+
eval:
29+
type: rego
30+
data_sources:
31+
- name: osv
32+
rego:
33+
type: constraints
34+
def: |
35+
package minder
36+
37+
import rego.v1
38+
39+
default skip = false
40+
41+
violations[{"msg": msg}] if {
42+
node := input.ingested.node_list.nodes[_]
43+
44+
name := node.name
45+
version := node.version
46+
ecosystem := get_ecosystem(node.properties)
47+
48+
reqparams := {
49+
"query": {
50+
"version": version,
51+
"package": {
52+
"name": name,
53+
"ecosystem": ecosystem
54+
}
55+
}
56+
}
57+
58+
out := minder.datasource.osv.query(reqparams)
59+
vulns := out.body.vulns
60+
61+
count(vulns) > 0
62+
63+
vulnid := vulns[_].id
64+
65+
msg := sprintf("Package %v version %v has a vulnerability %v", [name, version, vulnid])
66+
}
67+
68+
get_ecosystem(properties) := eco if {
69+
count(properties) >= 1
70+
prop := properties[_]
71+
72+
prop.name == "sourceFile"
73+
eco := get_ecosystem_from_file(prop.data)
74+
}
75+
76+
get_ecosystem_from_file(file) = "PyPI" if {
77+
file == "requirements.txt"
78+
}
79+
80+
get_ecosystem_from_file(file) = "npm" if {
81+
file == "package.json"
82+
}
83+
84+
get_ecosystem_from_file(file) = "RubyGems" if {
85+
file == "Gemfile"
86+
}
87+
88+
get_ecosystem_from_file(file) = "Go" if {
89+
file == "go.mod"
90+
}
91+
92+
get_ecosystem_from_file(file) = "crates.io" if {
93+
file == "Cargo.toml"
94+
}
95+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
version: v1
3+
type: rule-type
4+
name: sonatype_oss_index_vulnerabilities
5+
display_name: Detect Vulnerable Dependencies in Sonatype OSS Index
6+
context: {}
7+
severity:
8+
value: low
9+
release_phase: alpha
10+
short_failure_message: Vulnerable dependencies found in this repository matching the Sonatype OSS Index database.
11+
description: |
12+
This rule identifies dependencies in the repository with known vulnerabilities as reported by the Sonatype OSS Index database. It helps ensure your project avoids using libraries or packages that could introduce security risks.
13+
14+
By regularly scanning and updating dependencies, you can mitigate the risk of exploits and maintain secure development practices.
15+
16+
Documentation: https://ossindex.sonatype.org/
17+
guidance: |
18+
Check the dependencies in the repository for updates or patches. Resolve vulnerabilities by upgrading to a secure version. Refer to the following documentation for remediation steps:
19+
20+
- Sonatype OSS Index: https://ossindex.sonatype.org/
21+
- Dependency management best practices: [link to relevant guide]
22+
def:
23+
in_entity: repository
24+
param_schema: {}
25+
rule_schema: {}
26+
ingest:
27+
type: deps
28+
deps: {}
29+
eval:
30+
type: rego
31+
data_sources:
32+
- name: sonatype-oss-index
33+
rego:
34+
type: constraints
35+
def: |
36+
package minder
37+
38+
import rego.v1
39+
40+
default skip = false
41+
42+
violations[{"msg": msg}] if {
43+
node := input.ingested.node_list.nodes[_]
44+
45+
purl := node.identifiers["1"]
46+
47+
reqparams := {
48+
"request": {
49+
"coordinates": [
50+
purl
51+
]
52+
}
53+
}
54+
55+
out := minder.datasource.sonatype_oss_index.query(reqparams)
56+
vulns := out.body[0].vulnerabilities
57+
58+
count(vulns) > 0
59+
60+
vulnid := vulns[_].id
61+
62+
msg := sprintf("Package '%v' has a vulnerability %v", [purl, vulnid])
63+
}

0 commit comments

Comments
 (0)