From 854d8bb2bc201fa845843a59443c05aa4f27d54d Mon Sep 17 00:00:00 2001 From: GhosTnever <286396892+GhosTnever-lkm@users.noreply.github.com> Date: Sun, 25 Oct 2026 01:04:55 +0400 Subject: [PATCH] fix: reject malformed max findings input --- CHANGELOG.md | 5 +++++ package.json | 2 +- src/scan.mjs | 5 +++-- test/scan.test.mjs | 17 ++++++++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f91efba..ded1b47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/package.json b/package.json index eb42716..36f45bd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/scan.mjs b/src/scan.mjs index d4bcd8f..c71ce14 100644 --- a/src/scan.mjs +++ b/src/scan.mjs @@ -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 = []; @@ -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); diff --git a/test/scan.test.mjs b/test/scan.test.mjs index 36f5d25..67c847b 100644 --- a/test/scan.test.mjs +++ b/test/scan.test.mjs @@ -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'; @@ -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 }; } @@ -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'); + } +});