Skip to content

Commit 80fb783

Browse files
author
Philip Z
committed
Merge remote-tracking branch 'origin/main' into fix/repository-metadata-fail-134
2 parents cf3109e + 2c5d2a8 commit 80fb783

6 files changed

Lines changed: 255 additions & 31 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Only successful main-branch updater runs can supply vulnerability data to CI.
2+
// A PR-scoped cache is not acceptable provenance for a security gate.
3+
module.exports = async function selectOdcSnapshot({ github, context, core }) {
4+
const { owner, repo } = context.repo;
5+
const fullName = `${owner}/${repo}`;
6+
const artifactName = 'teaql-odc-v13.0.0-Linux';
7+
const { data } = await github.rest.actions.listWorkflowRuns({
8+
owner,
9+
repo,
10+
workflow_id: 'dependency-check-data.yml',
11+
branch: 'main',
12+
status: 'success',
13+
per_page: 30,
14+
});
15+
16+
for (const run of data.workflow_runs) {
17+
if (
18+
run.conclusion !== 'success' ||
19+
run.head_branch !== 'main' ||
20+
!['schedule', 'workflow_dispatch'].includes(run.event) ||
21+
run.head_repository?.full_name !== fullName
22+
) {
23+
continue;
24+
}
25+
const response = await github.rest.actions.listWorkflowRunArtifacts({
26+
owner,
27+
repo,
28+
run_id: run.id,
29+
per_page: 100,
30+
});
31+
const artifact = response.data.artifacts.find(
32+
(entry) => entry.name === artifactName && !entry.expired && entry.size_in_bytes > 1_000_000,
33+
);
34+
if (artifact) {
35+
core.info(`Using trusted NVD snapshot from updater run ${run.id}`);
36+
core.setOutput('run_id', String(run.id));
37+
return run.id;
38+
}
39+
}
40+
throw new Error('No successful main-branch Dependency Check updater run has a usable snapshot');
41+
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const assert = require('node:assert/strict');
2+
const test = require('node:test');
3+
const select = require('./select-odc-snapshot.cjs');
4+
5+
function fixture(runs, artifactsByRun = {}) {
6+
const outputs = new Map();
7+
const visited = [];
8+
return {
9+
outputs,
10+
visited,
11+
input: {
12+
context: { repo: { owner: 'teaql', repo: 'teaql-java' } },
13+
core: {
14+
info() {},
15+
setOutput(name, value) { outputs.set(name, value); },
16+
},
17+
github: {
18+
rest: {
19+
actions: {
20+
// Match the real Octokit REST method used by actions/github-script.
21+
async listWorkflowRuns(args) {
22+
assert.equal(args.workflow_id, 'dependency-check-data.yml');
23+
assert.equal(args.branch, 'main');
24+
assert.equal(args.status, 'success');
25+
return { data: { workflow_runs: runs } };
26+
},
27+
async listWorkflowRunArtifacts({ run_id }) {
28+
visited.push(run_id);
29+
return { data: { artifacts: artifactsByRun[run_id] ?? [] } };
30+
},
31+
},
32+
},
33+
},
34+
},
35+
};
36+
}
37+
38+
const trustedRun = {
39+
id: 42,
40+
conclusion: 'success',
41+
head_branch: 'main',
42+
event: 'schedule',
43+
head_repository: { full_name: 'teaql/teaql-java' },
44+
};
45+
const goodArtifact = {
46+
name: 'teaql-odc-v13.0.0-Linux',
47+
expired: false,
48+
size_in_bytes: 2_000_000,
49+
};
50+
51+
test('selects a successful updater artifact from main', async () => {
52+
const state = fixture([trustedRun], { 42: [goodArtifact] });
53+
assert.equal(await select(state.input), 42);
54+
assert.equal(state.outputs.get('run_id'), '42');
55+
});
56+
57+
test('rejects PR and fork provenance even if an artifact exists', async () => {
58+
const state = fixture([
59+
{ ...trustedRun, id: 1, event: 'pull_request' },
60+
{ ...trustedRun, id: 2, head_repository: { full_name: 'other/teaql-java' } },
61+
{ ...trustedRun, id: 3, head_branch: 'feature' },
62+
], { 1: [goodArtifact], 2: [goodArtifact], 3: [goodArtifact] });
63+
await assert.rejects(select(state.input), /No successful main-branch/);
64+
assert.deepEqual(state.visited, []);
65+
});
66+
67+
test('fails closed for missing, expired or undersized artifacts', async () => {
68+
const state = fixture([trustedRun], {
69+
42: [
70+
{ ...goodArtifact, expired: true },
71+
{ ...goodArtifact, size_in_bytes: 100 },
72+
],
73+
});
74+
await assert.rejects(select(state.input), /No successful main-branch/);
75+
assert.deepEqual(state.visited, [42]);
76+
assert.equal(state.outputs.size, 0);
77+
});
78+
79+
test('skips an unusable recent run and selects the next trusted artifact', async () => {
80+
const newest = { ...trustedRun, id: 43, event: 'workflow_dispatch' };
81+
const state = fixture([newest, trustedRun], {
82+
43: [{ ...goodArtifact, expired: true }],
83+
42: [goodArtifact],
84+
});
85+
86+
assert.equal(await select(state.input), 42);
87+
assert.deepEqual(state.visited, [43, 42]);
88+
assert.equal(state.outputs.get('run_id'), '42');
89+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
data_dir=${1:?snapshot directory is required}
5+
stamp_file="$data_dir/teaql-nvd-refresh.epoch"
6+
if [[ ! -f "$stamp_file" ]]; then
7+
echo "Trusted NVD snapshot has no refresh timestamp" >&2
8+
exit 1
9+
fi
10+
stamp=$(<"$stamp_file")
11+
if [[ ! "$stamp" =~ ^[0-9]{10}$ ]]; then
12+
echo "Trusted NVD snapshot has an invalid refresh timestamp" >&2
13+
exit 1
14+
fi
15+
age=$(( $(date -u +%s) - stamp ))
16+
if (( age < 0 || age > 86400 )); then
17+
echo "Trusted NVD snapshot is stale or has a future timestamp: age=${age}s" >&2
18+
exit 1
19+
fi
20+
if ! find "$data_dir" -type f -name '*.mv.db' -size +1M -print -quit | grep -q .; then
21+
echo "Trusted NVD snapshot has no populated H2 database" >&2
22+
exit 1
23+
fi
24+
echo "Trusted NVD snapshot age: ${age}s"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
test_dir=$(mktemp -d)
5+
trap 'rm -r -- "$test_dir"' EXIT
6+
verify_script="$(dirname "$0")/verify-odc-snapshot.sh"
7+
truncate -s 2M "$test_dir/odc.mv.db"
8+
9+
date -u +%s > "$test_dir/teaql-nvd-refresh.epoch"
10+
bash "$verify_script" "$test_dir"
11+
12+
printf '%s\n' "$(( $(date -u +%s) - 90000 ))" > "$test_dir/teaql-nvd-refresh.epoch"
13+
if bash "$verify_script" "$test_dir"; then
14+
echo "Stale snapshot was accepted" >&2
15+
exit 1
16+
fi
17+
18+
printf '%s\n' invalid > "$test_dir/teaql-nvd-refresh.epoch"
19+
if bash "$verify_script" "$test_dir"; then
20+
echo "Invalid timestamp was accepted" >&2
21+
exit 1
22+
fi
23+
24+
printf '%s\n' "$(( $(date -u +%s) + 60 ))" > "$test_dir/teaql-nvd-refresh.epoch"
25+
if bash "$verify_script" "$test_dir"; then
26+
echo "Future timestamp was accepted" >&2
27+
exit 1
28+
fi
29+
30+
rm -- "$test_dir/teaql-nvd-refresh.epoch"
31+
if bash "$verify_script" "$test_dir"; then
32+
echo "Missing timestamp was accepted" >&2
33+
exit 1
34+
fi
35+
36+
date -u +%s > "$test_dir/teaql-nvd-refresh.epoch"
37+
truncate -s 0 "$test_dir/odc.mv.db"
38+
if bash "$verify_script" "$test_dir"; then
39+
echo "Empty database was accepted" >&2
40+
exit 1
41+
fi
42+
43+
echo "Snapshot freshness policy tests passed"

‎.github/workflows/ci.yml‎

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ jobs:
1111
name: Build and Test
1212
runs-on: ubuntu-latest
1313
timeout-minutes: 150
14+
permissions:
15+
contents: read
16+
actions: read
1417
steps:
1518
- uses: actions/checkout@v4
1619
- name: Set up JDK 21
@@ -29,37 +32,34 @@ jobs:
2932
# Generated examples are verified by the dedicated Example Gate. They
3033
# intentionally do not inherit the runtime's SpotBugs build plugin.
3134
run: mvn spotbugs:check -pl '!examples/conformance/lib,!examples/conformance'
32-
- name: Select UTC cache day
33-
id: odc-day
34-
run: echo "value=$(date -u +%Y%m%d)" >> "$GITHUB_OUTPUT"
35-
- name: Restore Dependency Check data
36-
id: odc-cache
37-
uses: actions/cache/restore@v4
35+
- name: Verify trusted snapshot selection policy
36+
run: node --test .github/scripts/select-odc-snapshot.test.cjs
37+
- name: Verify snapshot freshness policy
38+
run: bash .github/scripts/verify-odc-snapshot.test.sh
39+
- name: Select trusted NVD snapshot
40+
id: odc-snapshot
41+
uses: actions/github-script@v7
3842
with:
43+
script: |
44+
const select = require('./.github/scripts/select-odc-snapshot.cjs');
45+
await select({ github, context, core });
46+
- name: Download trusted NVD snapshot
47+
uses: actions/download-artifact@v4
48+
with:
49+
name: teaql-odc-v13.0.0-${{ runner.os }}
3950
path: ${{ runner.temp }}/teaql-odc-data
40-
key: odc-v13.0.0-${{ runner.os }}-${{ steps.odc-day.outputs.value }}
41-
restore-keys: |
42-
odc-v13.0.0-${{ runner.os }}-
43-
- name: Refresh vulnerability data
44-
env:
45-
NVD_API_KEY: ${{ secrets.NVD_API_KEY }}
46-
run: >-
47-
mvn -B org.owasp:dependency-check-maven:13.0.0:update-only
48-
-DnvdApiKeyEnvironmentVariable=NVD_API_KEY
49-
-DdataDirectory=${{ runner.temp }}/teaql-odc-data
50-
-DfailOnError=true
51+
github-token: ${{ github.token }}
52+
repository: ${{ github.repository }}
53+
run-id: ${{ steps.odc-snapshot.outputs.run_id }}
54+
- name: Verify NVD snapshot freshness
55+
run: bash .github/scripts/verify-odc-snapshot.sh "${{ runner.temp }}/teaql-odc-data"
5156
- name: OWASP Dependency Check
52-
env:
53-
NVD_API_KEY: ${{ secrets.NVD_API_KEY }}
54-
# Pass only the environment variable NAME on the command line, never
55-
# the secret value. Keep its NVD database separate from setup-java's
56-
# Maven cache, whose immutable key cannot retain daily data updates.
5757
run: >-
5858
mvn -B org.owasp:dependency-check-maven:13.0.0:check
59-
-DnvdApiKeyEnvironmentVariable=NVD_API_KEY
6059
-DdataDirectory=${{ runner.temp }}/teaql-odc-data
60+
-DautoUpdate=false
6161
-DfailOnError=true
62-
-Dformat=HTML,JSON
62+
-Dformats=HTML,JSON
6363
- name: Retain Dependency Check reports
6464
if: always()
6565
uses: actions/upload-artifact@v4
@@ -70,9 +70,3 @@ jobs:
7070
**/target/dependency-check-report.json
7171
if-no-files-found: warn
7272
retention-days: 30
73-
- name: Save updated Dependency Check data
74-
if: success() && steps.odc-cache.outputs.cache-hit != 'true'
75-
uses: actions/cache/save@v4
76-
with:
77-
path: ${{ runner.temp }}/teaql-odc-data
78-
key: odc-v13.0.0-${{ runner.os }}-${{ steps.odc-day.outputs.value }}

‎.github/workflows/dependency-check-data.yml‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ jobs:
1212
update:
1313
name: Refresh NVD data
1414
runs-on: ubuntu-latest
15-
timeout-minutes: 150
15+
# A first-time NVD API import exceeded 150 minutes before reaching 70%.
16+
# The mirror bootstrap below normally avoids this, while the longer bound
17+
# leaves room for an unusually large authenticated API delta.
18+
timeout-minutes: 360
19+
concurrency:
20+
group: trusted-nvd-update
21+
cancel-in-progress: false
1622
steps:
1723
- uses: actions/checkout@v4
1824
- uses: actions/setup-java@v4
@@ -30,14 +36,41 @@ jobs:
3036
key: odc-v13.0.0-${{ runner.os }}-${{ steps.odc-day.outputs.value }}
3137
restore-keys: |
3238
odc-v13.0.0-${{ runner.os }}-
39+
- name: Bootstrap empty cache from Dependency-Check data feed
40+
run: |
41+
data_dir="${{ runner.temp }}/teaql-odc-data"
42+
if find "$data_dir" -type f -name '*.mv.db' -size +1M -print -quit 2>/dev/null | grep -q .; then
43+
echo "Populated Dependency-Check database restored; skipping full feed bootstrap"
44+
exit 0
45+
fi
46+
mvn -B org.owasp:dependency-check-maven:13.0.0:update-only \
47+
'-DnvdDatafeedUrl=https://dependency-check.github.io/DependencyCheck_Builder/nvd_cache/nvdcve-{0}.json.gz' \
48+
"-DdataDirectory=$data_dir" \
49+
-DfailOnError=true
3350
- name: Refresh NVD data
3451
env:
3552
NVD_API_KEY: ${{ secrets.NVD_API_KEY }}
3653
run: >-
3754
mvn -B org.owasp:dependency-check-maven:13.0.0:update-only
3855
-DnvdApiKeyEnvironmentVariable=NVD_API_KEY
56+
-DnvdValidForHours=0
3957
-DdataDirectory=${{ runner.temp }}/teaql-odc-data
4058
-DfailOnError=true
59+
- name: Verify and timestamp refreshed database
60+
run: |
61+
data_dir="${{ runner.temp }}/teaql-odc-data"
62+
if ! find "$data_dir" -type f -name '*.mv.db' -size +1M -print -quit | grep -q .; then
63+
echo "Dependency-Check did not produce a populated H2 database" >&2
64+
exit 1
65+
fi
66+
date -u +%s > "$data_dir/teaql-nvd-refresh.epoch"
67+
- name: Retain trusted NVD snapshot
68+
uses: actions/upload-artifact@v4
69+
with:
70+
name: teaql-odc-v13.0.0-${{ runner.os }}
71+
path: ${{ runner.temp }}/teaql-odc-data
72+
if-no-files-found: error
73+
retention-days: 3
4174
- name: Save refreshed Dependency Check data
4275
if: success() && steps.odc-cache.outputs.cache-hit != 'true'
4376
uses: actions/cache/save@v4

0 commit comments

Comments
 (0)