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);
});
@@ -128,3 +128,5 @@ hose-use workflow eval-script --code="return a + b;" --input='{"a":1,"b":2}'
- 借助 Kimi 网桥(`kimi-webbridge`)建立真实微前端会话,自动探测并解析不同应用类型下的非费用类表单元数据;
- 提取出动态列、关联实体 ID 与系统字段字典,在 CLI 层面提供对非费用类业务对象的只读探针与结构化校验能力。
草稿补丁路径仅遍历对象自身属性,禁止 `__proto__`、`constructor`、`prototype` 路径段;非法路径在写入前拒绝。CLI 选项仅识别自身属性,继承的确认标记无效。
+4 -1
View File
@@ -21,6 +21,9 @@ function pathParts(path: string): string[] {
.map((part) => part.trim())
.filter(Boolean);
if (!parts.length) throw new ValidationError("工作流配置路径不能为空");
if (parts.some(part => ["__proto__", "constructor", "prototype"].includes(part))) {
throw new ValidationError("工作流配置路径禁止访问对象原型");
}
if (parts.some((part) => !/^[A-Za-z_$][\w$-]*$/.test(part) && !/^\d+$/.test(part))) {
throw new ValidationError(`工作流配置路径非法: ${path}`);
}
@@ -40,7 +43,7 @@ export function applyWorkflowPathPatch(
const patched = structuredClone(workflow) as Record<string, any>;
let cursor: any = patched;
for (const part of parts.slice(0, -1)) {
if (cursor === null || typeof cursor !== "object" || !(part in cursor)) {
if (cursor === null || typeof cursor !== "object" || !Object.hasOwn(cursor, part)) {
throw new NotFoundError(`工作流配置路径不存在: ${path}`);
}
cursor = cursor[part];