Files
wekan/tests/securityReportScript.test.cjs
T
Lauri Ojansivu e876b6029e Security report script: flatten the paginated pages, so the dumps are not silently empty.
`./pull-security-reports.sh` reported OK for every endpoint and then wrote empty
split files and blank counts:

  Counts (open / closed):
    CodeQuality/StandardFindings:  / -
    Dependabot/Vulnerabilities:  / -

`gh api --paginate --slurp` returns ONE ARRAY PER PAGE, so a list endpoint arrives
as [[alert,...],[alert,...]]. Nothing flattened that, so `.[]` yielded PAGES, not
alerts, and every `select(.state == "open")` was applied to an array. jq failed,
its error went to /dev/null, and the `>` redirect left a 0-byte file - which is
not valid JSON, so every later step on it failed too and printed nothing. A
security report that quietly claims there is nothing to see is worse than one
that fails, so:

- fetch() now concatenates the pages into a single list, but only when EVERY
  element is itself an array; endpoints returning one object (/repos/{o}/{r},
  the SBOM, default-setup) are slurped to [ {...} ] and are left alone, because
  their renders index .[0].
- split() writes through a temp file, keeps a valid empty ARRAY and the jq error
  on failure, and warns in the summary instead of truncating the destination.
- the secret-scanning redaction validates the redacted copy before deleting the
  file that still holds live secrets - it used to write nothing and delete the
  source anyway - and now removes that file on the failure path as well.
- count() always prints a number, so the summary can no longer show a blank
  column, and the closed column starts at "-" rather than an empty string.

The same shape also broke the newest-analysis lookup (`sort_by | last | .id`
over pages) and the codeql-databases dump; both come right with the flatten.

Verified offline with the fake-gh harness the README documents, replaying the
real fixtures from the failed run: code scanning 4 pages -> 392 alerts,
Dependabot 1 page -> 86, analyses 152 pages -> 15118, advisories -> 22, and the
splits land as 2 open code scanning / 386 closed and 2 open Dependabot / 83
closed, with no zero-byte file anywhere in the run.

Tests: tests/securityReportScript.test.cjs guards the flatten, the non-truncating
split, the redaction ordering and the count helper, and - when jq is installed -
runs the actual filters, including a check that select() on an array-of-pages
still fails, so the guard cannot quietly stop being what protects the split.

Thanks to xet7 !
2026-07-25 02:28:58 +03:00

133 lines
6.1 KiB
JavaScript

'use strict';
// docs/Security/gh/pull-security-reports.sh dumps every GitHub security and code
// quality alert for the repo. It fetches with `gh api --paginate --slurp`, which
// returns ONE ARRAY PER PAGE - so a list endpoint arrives as [[alert,...],[...]].
//
// Nothing flattened those pages, so `.[]` yielded PAGES rather than alerts and
// every `select(.state == "open")` ran against an array. jq failed, its error went
// to /dev/null, and the `>` redirect left a 0-byte file. The run therefore reported
// "OK" for every endpoint while writing empty split dumps and blank counts - a
// security report that silently claims there is nothing to see. The same shape also
// broke the secret-scanning redaction (empty output, source file deleted anyway) and
// the "newest analysis" lookup.
//
// This guards the fix: pages are flattened, single-object endpoints are left alone,
// a failed filter leaves a valid empty ARRAY plus a readable error, and the secrets
// dump is never kept around.
//
// Run: node tests/securityReportScript.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { execFileSync, spawnSync } = require('child_process');
let passed = 0;
let skipped = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
const root = path.join(__dirname, '..');
const scriptPath = path.join(root, 'docs/Security/gh/pull-security-reports.sh');
const sh = fs.readFileSync(scriptPath, 'utf8');
// The flatten filter exactly as the script runs it, so the test breaks if the
// script's filter is edited without updating this guard.
const GUARD = /jq -e 'type == "array" and length > 0 and all\(\.\[\]; type == "array"\)'/;
const FLATTEN_GUARD = "type == \"array\" and length > 0 and all(.[]; type == \"array\")";
const hasJq = spawnSync('jq', ['--version']).status === 0;
function jq(filter, input) {
return execFileSync('jq', ['-c', filter], { input, encoding: 'utf8' }).trim();
}
function jqExit(filter, input) {
return spawnSync('jq', ['-e', filter], { input, encoding: 'utf8' }).status;
}
console.log('securityReportScript:');
test('the script is executable and syntactically valid POSIX sh', () => {
assert.ok(fs.statSync(scriptPath).mode & 0o111, 'must stay executable');
const r = spawnSync('sh', ['-n', scriptPath], { encoding: 'utf8' });
assert.strictEqual(r.status, 0, `sh -n failed: ${r.stderr}`);
});
test('fetch() flattens the per-page arrays that --slurp produces', () => {
const fetchBody = sh.slice(sh.indexOf('fetch() {'), sh.indexOf('# split <src.json>'));
assert.ok(GUARD.test(fetchBody),
'fetch() must detect the array-of-pages shape before counting or splitting');
assert.ok(/jq 'add' < "\$raw"/.test(fetchBody),
'fetch() must concatenate the pages into one list');
assert.ok(/--paginate --slurp/.test(fetchBody),
'the flatten exists because of --slurp; keep them together');
});
test('split() never leaves a 0-byte file behind', () => {
const splitBody = sh.slice(sh.indexOf('split() {'), sh.indexOf('# render <dir>'));
assert.ok(/echo '\[\]' > "\$out\.tmp"/.test(splitBody),
'a failed filter must still produce a valid, empty JSON array');
assert.ok(/2>"\$out\.error\.txt"/.test(splitBody),
'the jq error must be kept, not discarded to /dev/null');
assert.ok(!/> "\$RUN\/\$dir\/\$name\.json" 2>\/dev\/null/.test(splitBody),
'writing straight to the destination truncates it to 0 bytes when jq fails');
});
test('the secret-scanning dump is redacted before the raw copy is dropped', () => {
const at = sh.indexOf('secret-scanning-alerts-with-secrets.json');
assert.ok(at > 0, 'the secret scanning step must exist');
const block = sh.slice(at, at + 1600);
assert.ok(/jq -e 'type == "array"'/.test(block),
'the redacted copy must be validated before the unredacted one is deleted');
// Whatever happens, the file holding live secrets must not survive the run.
const removals = (block.match(/rm -f "\$RUN\/Raw\/secret-scanning-alerts-with-secrets\.json"/g) || []);
assert.ok(removals.length >= 2,
'the unredacted dump must be removed on the failure path too');
});
test('counts always print a number, never an empty column', () => {
assert.ok(/^count\(\) \{$/m.test(sh), 'a count() helper must exist');
const countBody = sh.slice(sh.indexOf('count() {'), sh.indexOf('fetch() {'));
assert.ok(/\[ -n "\$n" \] \|\| n=0/.test(countBody),
'an unreadable or empty dump must count as 0, not blank');
assert.ok(!/o="-"; c_=""/.test(sh),
'the summary must not start the closed column as an empty string');
});
if (!hasJq) {
skipped += 1;
console.log(' -- skipped (jq not installed): behavioural checks of the jq filters');
} else {
test('the flatten filter concatenates pages but spares single objects', () => {
// Paginated list endpoint: two pages -> one list.
assert.strictEqual(jqExit(FLATTEN_GUARD, '[[{"a":1},{"a":2}],[{"a":3}]]'), 0);
assert.strictEqual(jq('add', '[[{"a":1},{"a":2}],[{"a":3}]]'),
'[{"a":1},{"a":2},{"a":3}]');
// Single-object endpoint (/repos/{o}/{r}, sbom, default-setup): slurped to
// [ {...} ] and must NOT be flattened, or the renders lose their .[0].
assert.notStrictEqual(jqExit(FLATTEN_GUARD, '[{"private":false}]'), 0);
// One empty page, and no pages at all.
assert.strictEqual(jq('add', '[[]]'), '[]');
assert.notStrictEqual(jqExit(FLATTEN_GUARD, '[]'), 0);
});
test('the state filter works on flattened alerts and failed on pages', () => {
const flat = '[{"state":"open"},{"state":"fixed"},{"state":"open"}]';
assert.strictEqual(jq('[ .[] | select(.state == "open") ] | length', flat), '2');
// The pre-fix shape: select() against a page array is an error, which is
// exactly how the dumps ended up empty.
const paged = `[${flat}]`;
const r = spawnSync('jq', ['[ .[] | select(.state == "open") ]'],
{ input: paged, encoding: 'utf8' });
assert.notStrictEqual(r.status, 0,
'selecting on an array-of-pages must fail - if this ever succeeds, the ' +
'flatten guard is no longer what protects the split step');
});
}
console.log(`\nsecurityReportScript: ${passed} tests passed` +
(skipped ? `, ${skipped} group skipped` : ''));