Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v1.0.5

- Reject malformed, fractional, and out-of-range `max-findings` values instead of silently accepting the integer prefix.
- Add regression coverage for invalid action configuration.

## v1.0.4

- Keep the report's tool version synchronized with `package.json` rather than a stale hard-coded value.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "diffshield-action",
"version": "1.0.4",
"version": "1.0.5",
"private": true,
"description": "Read-only GitHub pull request risk review action",
"type": "module",
Expand Down
5 changes: 3 additions & 2 deletions src/scan.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const SEVERITY = { critical: 4, high: 3, medium: 2, low: 1, info: 0 };
const BASE = process.env.DIFFSHIELD_BASE_SHA ?? '';
const HEAD = process.env.DIFFSHIELD_HEAD_SHA ?? '';
const failOn = (process.env.DIFFSHIELD_FAIL_ON ?? 'high').toLowerCase();
const maxFindings = Number.parseInt(process.env.DIFFSHIELD_MAX_FINDINGS ?? '100', 10);
const rawMaxFindings = process.env.DIFFSHIELD_MAX_FINDINGS ?? '100';
const maxFindings = Number(rawMaxFindings);
const packageVersion = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;
const findings = [];

Expand Down Expand Up @@ -125,7 +126,7 @@ function commandEscape(value) {
function main() {
if (!/^[a-f0-9]{40}$/i.test(BASE) || !/^[a-f0-9]{40}$/i.test(HEAD)) throw new Error('base-sha and head-sha must be full 40-character commit SHAs.');
if (!Object.hasOwn(SEVERITY, failOn)) throw new Error('fail-on must be critical, high, medium, or none.');
if (!Number.isInteger(maxFindings) || maxFindings < 1 || maxFindings > 1000) throw new Error('max-findings must be an integer between 1 and 1000.');
if (!/^\d+$/.test(rawMaxFindings) || !Number.isInteger(maxFindings) || maxFindings < 1 || maxFindings > 1000) throw new Error('max-findings must be an integer between 1 and 1000.');

const diff = git(['diff', '--no-ext-diff', '--no-renames', '--unified=0', BASE, HEAD, '--']);
const files = parseDiff(diff);
Expand Down
17 changes: 14 additions & 3 deletions test/scan.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict';
import { execFileSync, spawnSync } from 'node:child_process';
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
Expand All @@ -20,8 +20,9 @@ function fixture(files, options = {}) {
}
git('add', '-A'); git('commit', '-qm', 'head');
const head = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: dir, encoding: 'utf8' }).trim();
const result = spawnSync(process.execPath, [scanner], { cwd: dir, encoding: 'utf8', env: { ...process.env, DIFFSHIELD_BASE_SHA: base, DIFFSHIELD_HEAD_SHA: head, DIFFSHIELD_FAIL_ON: options.failOn ?? 'high' } });
const report = JSON.parse(readFileSync(path.join(dir, 'diffshield-report.json'), 'utf8'));
const result = spawnSync(process.execPath, [scanner], { cwd: dir, encoding: 'utf8', env: { ...process.env, DIFFSHIELD_BASE_SHA: base, DIFFSHIELD_HEAD_SHA: head, DIFFSHIELD_FAIL_ON: options.failOn ?? 'high', ...(options.maxFindings === undefined ? {} : { DIFFSHIELD_MAX_FINDINGS: options.maxFindings }) } });
const reportPath = path.join(dir, 'diffshield-report.json');
const report = existsSync(reportPath) ? JSON.parse(readFileSync(reportPath, 'utf8')) : null;
return { dir, result, report };
}

Expand Down Expand Up @@ -81,3 +82,13 @@ test('documented placeholders do not trigger credential assignment heuristic', t
const f = fixture({ '.env.example': 'API_KEY=YOUR_API_KEY_HERE\n', 'app.js': 'const password = "change_me_now";\n' }); t.after(() => rmSync(f.dir, { recursive: true, force: true }));
assert.equal(f.report.findings.filter(x => x.category === 'Possible secret' || x.category === 'Sensitive file').length, 0);
});

test('max-findings rejects malformed, fractional, and out-of-range values', t => {
for (const value of ['2oops', '1.5', '0', '1001']) {
const f = fixture({ 'src/app.js': 'export const value = 42;\n' }, { maxFindings: value });
t.after(() => rmSync(f.dir, { recursive: true, force: true }));
assert.notEqual(f.result.status, 0, `${value} must be rejected`);
assert.match(f.result.stderr, /max-findings must be an integer between 1 and 1000/);
assert.equal(f.report, null, 'invalid configuration must not write a report');
}
});
Loading