fix(test): reject kova help value bypasses

This commit is contained in:
Vincent Koc 2026-06-21 19:01:53 +02:00
parent adcba85264
commit 0b28a72be1
No known key found for this signature in database
2 changed files with 40 additions and 4 deletions

View file

@ -3,8 +3,9 @@
import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
const knownArgKeys = new Set(["report", "output", "lane", "reporturl", "artifacturl"]);
const rawArgs = process.argv.slice(2);
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
if (shouldPrintHelp(rawArgs)) {
usage("", 0);
}
@ -245,18 +246,17 @@ function value(input) {
function parseArgs(argv) {
const parsed = {};
const knownKeys = new Set(["report", "output", "lane", "reporturl", "artifacturl"]);
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (!arg.startsWith("--")) {
usage(`unexpected argument: ${arg}`);
}
const key = arg.slice(2).replaceAll("-", "");
if (!knownKeys.has(key)) {
if (!knownArgKeys.has(key)) {
usage(`unknown argument: ${arg}`);
}
const valueLocal = argv[index + 1];
if (!valueLocal || valueLocal.startsWith("--")) {
if (!valueLocal || valueLocal.startsWith("-")) {
usage(`${arg} requires a value`);
}
parsed[key] = valueLocal;
@ -271,6 +271,28 @@ function parseArgs(argv) {
};
}
function shouldPrintHelp(argv) {
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === "--help" || arg === "-h") {
return true;
}
if (!arg.startsWith("--")) {
return false;
}
const key = arg.slice(2).replaceAll("-", "");
if (!knownArgKeys.has(key)) {
return false;
}
const optionValue = argv[index + 1];
if (!optionValue || optionValue.startsWith("-")) {
return false;
}
index += 1;
}
return false;
}
function usage(message, status = 2) {
const text =
"usage: node scripts/kova-ci-summary.mjs --report <report.json> [--output <summary.md>] [--lane <name>] [--report-url <url>] [--artifact-url <url>]\n";

View file

@ -38,6 +38,20 @@ describe("scripts/kova-ci-summary", () => {
expect(result.stdout).toContain("usage: node scripts/kova-ci-summary.mjs --report");
});
it.each([
["flag-shaped value", ["--report", "-h"]],
["option separator before help", ["--report", "--", "--help"]],
])("rejects %s before help handling", (_name, args) => {
const result = spawnSync(process.execPath, ["scripts/kova-ci-summary.mjs", ...args], {
cwd: process.cwd(),
encoding: "utf8",
});
expect(result.status).toBe(2);
expect(result.stdout).toBe("");
expect(result.stderr).toContain("error: --report requires a value");
});
it("rejects empty Kova reports instead of rendering unknown summaries", () => {
const empty = runSummary({});
expect(empty.result.status).toBe(1);