fix(workflow): prevent prototype traversal and inherited CLI authorization

This commit is contained in:
gitea committed 2026-10-20 17:52:14 +08:00
1 parent fac81b08e8
commit d2e0d564ca
6 files changed
+62 -31

No files matched your search

+2 -2
View File
@@ -25,12 +25,12 @@ export class MutationConfirmationRequired extends CliError {
}
export function isDryRun(parsed: CliParsedArgs, runtime: CliRuntime): boolean {
return runtime.dryRun === true || booleanOption(parsed.options, ["dry-run", "dryRun", "dryrun"]);
return (Object.hasOwn(runtime, "dryRun") && runtime.dryRun === true) || booleanOption(parsed.options, ["dry-run", "dryRun", "dryrun"]);
}
export function isConfirmed(parsed: CliParsedArgs, runtime: CliRuntime): boolean {
return (
runtime.confirmed === true ||
(Object.hasOwn(runtime, "confirmed") && runtime.confirmed === true) ||
booleanOption(parsed.options, ["yes", "y", "confirm", "force-write"])
);
}
+4 -1
View File
@@ -58,7 +58,7 @@ export function isJsonRequested(raw: string[]): boolean {
}
export function parseArgs(rawArgs: string[], areaOverride?: string): CliParsedArgs {
const options: Record<string, string | boolean | number> = {};
const options: Record<string, string | boolean | number> = Object.create(null);
const positional: string[] = [];
const context = findContext(rawArgs, areaOverride);
const flags = commandFlags(context.area, context.action);
@@ -115,6 +115,7 @@ export function stringOption(
fallback?: string
): string | undefined {
for (const k of keys) {
if (!Object.hasOwn(options, k)) continue;
const val = options[k];
if (typeof val === "string" && val.length > 0) return val;
}
@@ -127,6 +128,7 @@ export function booleanOption(
fallback: boolean = false
): boolean {
for (const k of keys) {
if (!Object.hasOwn(options, k)) continue;
const val = options[k];
if (typeof val === "boolean") return val;
if (typeof val === "string") {
@@ -144,6 +146,7 @@ export function numberOption(
fallback?: number
): number | undefined {
for (const k of keys) {
if (!Object.hasOwn(options, k)) continue;
const val = options[k];
if (typeof val === "number" && !Number.isNaN(val)) return val;
if (typeof val === "string") {
+23
View File
@@ -0,0 +1,23 @@
import { expect, test } from "bun:test";
import { parseArgs, booleanOption, stringOption, numberOption } from "./parser.js";
import { isConfirmed, isDryRun } from "./mutation.js";
import { applyWorkflowPathPatch } from "../domains/platform/workflow/state/index.js";
test("rejects prototype paths before mutation and keeps normal patches working", () => {
for (const path of ["__proto__.yes", "constructor.prototype.yes", "tasks.__proto__.yes"]) {
expect(() => applyWorkflowPathPatch({ tasks: [] } as any, path, true)).toThrow("对象原型");
}
expect(({} as any).yes).toBeUndefined();
expect(applyWorkflowPathPatch({ tasks: [{ code: "old" }] } as any, "tasks.0.code", "new").tasks[0].code).toBe("new");
});
test("inherited options and runtime fields cannot authorize or change a command", () => {
const inherited = Object.create({ yes: true, confirmed: true, dryRun: true, path: "secret", count: 99 });
const parsed = { options: inherited } as any;
expect(isConfirmed(parsed, inherited)).toBe(false);
expect(isDryRun(parsed, inherited)).toBe(false);
expect(booleanOption(inherited, ["yes"])).toBe(false);
expect(stringOption(inherited, ["path"])).toBeUndefined();
expect(numberOption(inherited, ["count"])).toBeUndefined();
expect(Object.getPrototypeOf(parseArgs(["version"]).options)).toBeNull();
expect(isConfirmed(parseArgs(["message", "read", "m", "--yes"]), {} as any)).toBe(true);
});