|
|
|
|
@ -64,22 +64,19 @@ function expectResolveIncludeError(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("resolveConfigIncludes", () => {
|
|
|
|
|
it("passes through non-include values unchanged", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{ value: "hello", expected: "hello" },
|
|
|
|
|
{ value: 42, expected: 42 },
|
|
|
|
|
{ value: true, expected: true },
|
|
|
|
|
{ value: null, expected: null },
|
|
|
|
|
{ value: [1, 2, { a: 1 }], expected: [1, 2, { a: 1 }] },
|
|
|
|
|
{
|
|
|
|
|
value: { foo: "bar", nested: { x: 1 } },
|
|
|
|
|
expected: { foo: "bar", nested: { x: 1 } },
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
for (const { value, expected } of cases) {
|
|
|
|
|
expect(resolve(value)).toEqual(expected);
|
|
|
|
|
}
|
|
|
|
|
it.each([
|
|
|
|
|
{ name: "string", value: "hello", expected: "hello" },
|
|
|
|
|
{ name: "number", value: 42, expected: 42 },
|
|
|
|
|
{ name: "boolean", value: true, expected: true },
|
|
|
|
|
{ name: "null", value: null, expected: null },
|
|
|
|
|
{ name: "array", value: [1, 2, { a: 1 }], expected: [1, 2, { a: 1 }] },
|
|
|
|
|
{
|
|
|
|
|
name: "nested object",
|
|
|
|
|
value: { foo: "bar", nested: { x: 1 } },
|
|
|
|
|
expected: { foo: "bar", nested: { x: 1 } },
|
|
|
|
|
},
|
|
|
|
|
] as const)("passes through non-include $name values unchanged", ({ value, expected }) => {
|
|
|
|
|
expect(resolve(value)).toEqual(expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects absolute path outside config directory (CWE-22)", () => {
|
|
|
|
|
@ -89,82 +86,77 @@ describe("resolveConfigIncludes", () => {
|
|
|
|
|
expectResolveIncludeError(() => resolve(obj, files), /escapes config directory/);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("resolves single and array include merges", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{
|
|
|
|
|
name: "single file include",
|
|
|
|
|
files: { [configPath("agents.json")]: { list: [{ id: "main" }] } },
|
|
|
|
|
obj: { agents: { $include: "./agents.json" } },
|
|
|
|
|
expected: {
|
|
|
|
|
agents: { list: [{ id: "main" }] },
|
|
|
|
|
},
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
|
|
|
|
name: "single file include",
|
|
|
|
|
files: { [configPath("agents.json")]: { list: [{ id: "main" }] } },
|
|
|
|
|
obj: { agents: { $include: "./agents.json" } },
|
|
|
|
|
expected: {
|
|
|
|
|
agents: { list: [{ id: "main" }] },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "array include deep merge",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("a.json")]: { "group-a": ["agent1"] },
|
|
|
|
|
[configPath("b.json")]: { "group-b": ["agent2"] },
|
|
|
|
|
},
|
|
|
|
|
obj: { broadcast: { $include: ["./a.json", "./b.json"] } },
|
|
|
|
|
expected: {
|
|
|
|
|
broadcast: {
|
|
|
|
|
"group-a": ["agent1"],
|
|
|
|
|
"group-b": ["agent2"],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "array include deep merge",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("a.json")]: { "group-a": ["agent1"] },
|
|
|
|
|
[configPath("b.json")]: { "group-b": ["agent2"] },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "array include overlapping keys",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("a.json")]: { agents: { defaults: { workspace: "~/a" } } },
|
|
|
|
|
[configPath("b.json")]: { agents: { list: [{ id: "main" }] } },
|
|
|
|
|
obj: { broadcast: { $include: ["./a.json", "./b.json"] } },
|
|
|
|
|
expected: {
|
|
|
|
|
broadcast: {
|
|
|
|
|
"group-a": ["agent1"],
|
|
|
|
|
"group-b": ["agent2"],
|
|
|
|
|
},
|
|
|
|
|
obj: { $include: ["./a.json", "./b.json"] },
|
|
|
|
|
expected: {
|
|
|
|
|
agents: {
|
|
|
|
|
defaults: { workspace: "~/a" },
|
|
|
|
|
list: [{ id: "main" }],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "array include overlapping keys",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("a.json")]: { agents: { defaults: { workspace: "~/a" } } },
|
|
|
|
|
[configPath("b.json")]: { agents: { list: [{ id: "main" }] } },
|
|
|
|
|
},
|
|
|
|
|
obj: { $include: ["./a.json", "./b.json"] },
|
|
|
|
|
expected: {
|
|
|
|
|
agents: {
|
|
|
|
|
defaults: { workspace: "~/a" },
|
|
|
|
|
list: [{ id: "main" }],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
expect(resolve(testCase.obj, testCase.files), testCase.name).toEqual(testCase.expected);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
] as const)("resolves include merges: $name", ({ obj, files, expected }) => {
|
|
|
|
|
expect(resolve(obj, files)).toEqual(expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("merges include content with sibling keys and sibling overrides", () => {
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
|
|
|
|
name: "adds sibling keys after include",
|
|
|
|
|
obj: { $include: "./base.json", c: 3 },
|
|
|
|
|
expected: { a: 1, b: 2, c: 3 },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "lets siblings override included keys",
|
|
|
|
|
obj: { $include: "./base.json", b: 99 },
|
|
|
|
|
expected: { a: 1, b: 99 },
|
|
|
|
|
},
|
|
|
|
|
] as const)("merges include content with sibling keys: $name", ({ obj, expected }) => {
|
|
|
|
|
const files = { [configPath("base.json")]: { a: 1, b: 2 } };
|
|
|
|
|
const cases = [
|
|
|
|
|
{
|
|
|
|
|
obj: { $include: "./base.json", c: 3 },
|
|
|
|
|
expected: { a: 1, b: 2, c: 3 },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
obj: { $include: "./base.json", b: 99 },
|
|
|
|
|
expected: { a: 1, b: 99 },
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
expect(resolve(testCase.obj, files)).toEqual(testCase.expected);
|
|
|
|
|
}
|
|
|
|
|
expect(resolve(obj, files)).toEqual(expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("throws when sibling keys are used with non-object includes", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{ includeFile: "list.json", included: ["a", "b"] },
|
|
|
|
|
{ includeFile: "value.json", included: "hello" },
|
|
|
|
|
] as const;
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
const files = { [configPath(testCase.includeFile)]: testCase.included };
|
|
|
|
|
const obj = { $include: `./${testCase.includeFile}`, extra: true };
|
|
|
|
|
it.each([
|
|
|
|
|
{ includeFile: "list.json", included: ["a", "b"] },
|
|
|
|
|
{ includeFile: "value.json", included: "hello" },
|
|
|
|
|
] as const)(
|
|
|
|
|
"throws when sibling keys are used with non-object include $includeFile",
|
|
|
|
|
({ includeFile, included }) => {
|
|
|
|
|
const files = { [configPath(includeFile)]: included };
|
|
|
|
|
const obj = { $include: `./${includeFile}`, extra: true };
|
|
|
|
|
expectResolveIncludeError(
|
|
|
|
|
() => resolve(obj, files),
|
|
|
|
|
/Sibling keys require included content to be an object/,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it("resolves nested includes", () => {
|
|
|
|
|
const files = {
|
|
|
|
|
@ -177,25 +169,23 @@ describe("resolveConfigIncludes", () => {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("surfaces include read and parse failures", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{
|
|
|
|
|
run: () => resolve({ $include: "./missing.json" }),
|
|
|
|
|
pattern: /Failed to read include file/,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
run: () =>
|
|
|
|
|
resolveConfigIncludes({ $include: "./bad.json" }, DEFAULT_BASE_PATH, {
|
|
|
|
|
readFile: () => "{ invalid json }",
|
|
|
|
|
parseJson: JSON.parse,
|
|
|
|
|
}),
|
|
|
|
|
pattern: /Failed to parse include file/,
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
expectResolveIncludeError(testCase.run, testCase.pattern);
|
|
|
|
|
}
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
|
|
|
|
name: "read failures",
|
|
|
|
|
run: () => resolve({ $include: "./missing.json" }),
|
|
|
|
|
pattern: /Failed to read include file/,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "parse failures",
|
|
|
|
|
run: () =>
|
|
|
|
|
resolveConfigIncludes({ $include: "./bad.json" }, DEFAULT_BASE_PATH, {
|
|
|
|
|
readFile: () => "{ invalid json }",
|
|
|
|
|
parseJson: JSON.parse,
|
|
|
|
|
}),
|
|
|
|
|
pattern: /Failed to parse include file/,
|
|
|
|
|
},
|
|
|
|
|
] as const)("surfaces include $name", ({ run, pattern }) => {
|
|
|
|
|
expectResolveIncludeError(run, pattern);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("throws CircularIncludeError for circular includes", () => {
|
|
|
|
|
@ -227,30 +217,30 @@ describe("resolveConfigIncludes", () => {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("throws on invalid include value/item types", () => {
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
|
|
|
|
name: "rejects scalar include value",
|
|
|
|
|
obj: { $include: 123 },
|
|
|
|
|
expectedPattern: /expected string or array/,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "rejects number in include array",
|
|
|
|
|
obj: { $include: ["./valid.json", 123] },
|
|
|
|
|
expectedPattern: /expected string, got number/,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "rejects null in include array",
|
|
|
|
|
obj: { $include: ["./valid.json", null] },
|
|
|
|
|
expectedPattern: /expected string, got object/,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "rejects boolean in include array",
|
|
|
|
|
obj: { $include: ["./valid.json", false] },
|
|
|
|
|
expectedPattern: /expected string, got boolean/,
|
|
|
|
|
},
|
|
|
|
|
] as const)("throws on invalid include value/item types: $name", ({ obj, expectedPattern }) => {
|
|
|
|
|
const files = { [configPath("valid.json")]: { valid: true } };
|
|
|
|
|
const cases = [
|
|
|
|
|
{
|
|
|
|
|
obj: { $include: 123 },
|
|
|
|
|
expectedPattern: /expected string or array/,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
obj: { $include: ["./valid.json", 123] },
|
|
|
|
|
expectedPattern: /expected string, got number/,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
obj: { $include: ["./valid.json", null] },
|
|
|
|
|
expectedPattern: /expected string, got object/,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
obj: { $include: ["./valid.json", false] },
|
|
|
|
|
expectedPattern: /expected string, got boolean/,
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
expectResolveIncludeError(() => resolve(testCase.obj, files), testCase.expectedPattern);
|
|
|
|
|
}
|
|
|
|
|
expectResolveIncludeError(() => resolve(obj, files), expectedPattern);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("respects max depth limit", () => {
|
|
|
|
|
@ -289,32 +279,34 @@ describe("resolveConfigIncludes", () => {
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("handles relative paths and nested-include override ordering", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("clients", "mueller", "agents.json")]: { id: "mueller" },
|
|
|
|
|
},
|
|
|
|
|
obj: { agent: { $include: "./clients/mueller/agents.json" } },
|
|
|
|
|
expected: {
|
|
|
|
|
agent: { id: "mueller" },
|
|
|
|
|
},
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
|
|
|
|
name: "resolves nested relative file path",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("clients", "mueller", "agents.json")]: { id: "mueller" },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("base.json")]: { nested: { $include: "./nested.json" } },
|
|
|
|
|
[configPath("nested.json")]: { a: 1, b: 2 },
|
|
|
|
|
},
|
|
|
|
|
obj: { $include: "./base.json", nested: { b: 9 } },
|
|
|
|
|
expected: {
|
|
|
|
|
nested: { a: 1, b: 9 },
|
|
|
|
|
},
|
|
|
|
|
obj: { agent: { $include: "./clients/mueller/agents.json" } },
|
|
|
|
|
expected: {
|
|
|
|
|
agent: { id: "mueller" },
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
expect(resolve(testCase.obj, testCase.files)).toEqual(testCase.expected);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "preserves nested override ordering",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("base.json")]: { nested: { $include: "./nested.json" } },
|
|
|
|
|
[configPath("nested.json")]: { a: 1, b: 2 },
|
|
|
|
|
},
|
|
|
|
|
obj: { $include: "./base.json", nested: { b: 9 } },
|
|
|
|
|
expected: {
|
|
|
|
|
nested: { a: 1, b: 9 },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
] as const)(
|
|
|
|
|
"handles relative paths and nested include ordering: $name",
|
|
|
|
|
({ obj, files, expected }) => {
|
|
|
|
|
expect(resolve(obj, files)).toEqual(expected);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it("enforces traversal boundaries while allowing safe nested-parent paths", () => {
|
|
|
|
|
expectResolveIncludeError(
|
|
|
|
|
@ -342,91 +334,87 @@ describe("resolveConfigIncludes", () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("real-world config patterns", () => {
|
|
|
|
|
it("supports common modular include layouts", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{
|
|
|
|
|
name: "per-client agent includes",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("clients", "mueller.json")]: {
|
|
|
|
|
agents: [
|
|
|
|
|
{
|
|
|
|
|
id: "mueller-screenshot",
|
|
|
|
|
workspace: "~/clients/mueller/screenshot",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "mueller-transcribe",
|
|
|
|
|
workspace: "~/clients/mueller/transcribe",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
broadcast: {
|
|
|
|
|
"group-mueller": ["mueller-screenshot", "mueller-transcribe"],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
[configPath("clients", "schmidt.json")]: {
|
|
|
|
|
agents: [
|
|
|
|
|
{
|
|
|
|
|
id: "schmidt-screenshot",
|
|
|
|
|
workspace: "~/clients/schmidt/screenshot",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
broadcast: { "group-schmidt": ["schmidt-screenshot"] },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
obj: {
|
|
|
|
|
gateway: { port: 18789 },
|
|
|
|
|
$include: ["./clients/mueller.json", "./clients/schmidt.json"],
|
|
|
|
|
},
|
|
|
|
|
expected: {
|
|
|
|
|
gateway: { port: 18789 },
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
|
|
|
|
name: "per-client agent includes",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("clients", "mueller.json")]: {
|
|
|
|
|
agents: [
|
|
|
|
|
{ id: "mueller-screenshot", workspace: "~/clients/mueller/screenshot" },
|
|
|
|
|
{ id: "mueller-transcribe", workspace: "~/clients/mueller/transcribe" },
|
|
|
|
|
{ id: "schmidt-screenshot", workspace: "~/clients/schmidt/screenshot" },
|
|
|
|
|
{
|
|
|
|
|
id: "mueller-screenshot",
|
|
|
|
|
workspace: "~/clients/mueller/screenshot",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "mueller-transcribe",
|
|
|
|
|
workspace: "~/clients/mueller/transcribe",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
broadcast: {
|
|
|
|
|
"group-mueller": ["mueller-screenshot", "mueller-transcribe"],
|
|
|
|
|
"group-schmidt": ["schmidt-screenshot"],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "modular config structure",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("gateway.json")]: {
|
|
|
|
|
gateway: { port: 18789, bind: "loopback" },
|
|
|
|
|
},
|
|
|
|
|
[configPath("channels", "whatsapp.json")]: {
|
|
|
|
|
channels: { whatsapp: { dmPolicy: "pairing", allowFrom: ["+49123"] } },
|
|
|
|
|
},
|
|
|
|
|
[configPath("agents", "defaults.json")]: {
|
|
|
|
|
agents: { defaults: { sandbox: { mode: "all" } } },
|
|
|
|
|
},
|
|
|
|
|
[configPath("clients", "schmidt.json")]: {
|
|
|
|
|
agents: [
|
|
|
|
|
{
|
|
|
|
|
id: "schmidt-screenshot",
|
|
|
|
|
workspace: "~/clients/schmidt/screenshot",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
broadcast: { "group-schmidt": ["schmidt-screenshot"] },
|
|
|
|
|
},
|
|
|
|
|
obj: {
|
|
|
|
|
$include: ["./gateway.json", "./channels/whatsapp.json", "./agents/defaults.json"],
|
|
|
|
|
},
|
|
|
|
|
obj: {
|
|
|
|
|
gateway: { port: 18789 },
|
|
|
|
|
$include: ["./clients/mueller.json", "./clients/schmidt.json"],
|
|
|
|
|
},
|
|
|
|
|
expected: {
|
|
|
|
|
gateway: { port: 18789 },
|
|
|
|
|
agents: [
|
|
|
|
|
{ id: "mueller-screenshot", workspace: "~/clients/mueller/screenshot" },
|
|
|
|
|
{ id: "mueller-transcribe", workspace: "~/clients/mueller/transcribe" },
|
|
|
|
|
{ id: "schmidt-screenshot", workspace: "~/clients/schmidt/screenshot" },
|
|
|
|
|
],
|
|
|
|
|
broadcast: {
|
|
|
|
|
"group-mueller": ["mueller-screenshot", "mueller-transcribe"],
|
|
|
|
|
"group-schmidt": ["schmidt-screenshot"],
|
|
|
|
|
},
|
|
|
|
|
expected: {
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "modular config structure",
|
|
|
|
|
files: {
|
|
|
|
|
[configPath("gateway.json")]: {
|
|
|
|
|
gateway: { port: 18789, bind: "loopback" },
|
|
|
|
|
},
|
|
|
|
|
[configPath("channels", "whatsapp.json")]: {
|
|
|
|
|
channels: { whatsapp: { dmPolicy: "pairing", allowFrom: ["+49123"] } },
|
|
|
|
|
},
|
|
|
|
|
[configPath("agents", "defaults.json")]: {
|
|
|
|
|
agents: { defaults: { sandbox: { mode: "all" } } },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
expect(resolve(testCase.obj, testCase.files), testCase.name).toEqual(testCase.expected);
|
|
|
|
|
}
|
|
|
|
|
obj: {
|
|
|
|
|
$include: ["./gateway.json", "./channels/whatsapp.json", "./agents/defaults.json"],
|
|
|
|
|
},
|
|
|
|
|
expected: {
|
|
|
|
|
gateway: { port: 18789, bind: "loopback" },
|
|
|
|
|
channels: { whatsapp: { dmPolicy: "pairing", allowFrom: ["+49123"] } },
|
|
|
|
|
agents: { defaults: { sandbox: { mode: "all" } } },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
] as const)("supports common modular include layouts: $name", ({ obj, files, expected }) => {
|
|
|
|
|
expect(resolve(obj, files)).toEqual(expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
describe("security: path traversal protection (CWE-22)", () => {
|
|
|
|
|
function expectRejectedTraversalPaths(
|
|
|
|
|
cases: ReadonlyArray<{ includePath: string; expectEscapesMessage: boolean }>,
|
|
|
|
|
) {
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
const obj = { $include: testCase.includePath };
|
|
|
|
|
expect(() => resolve(obj, {}), testCase.includePath).toThrow(ConfigIncludeError);
|
|
|
|
|
if (testCase.expectEscapesMessage) {
|
|
|
|
|
expect(() => resolve(obj, {}), testCase.includePath).toThrow(/escapes config directory/);
|
|
|
|
|
for (const { includePath, expectEscapesMessage } of cases) {
|
|
|
|
|
const obj = { $include: includePath };
|
|
|
|
|
expect(() => resolve(obj, {}), includePath).toThrow(ConfigIncludeError);
|
|
|
|
|
if (expectEscapesMessage) {
|
|
|
|
|
expect(() => resolve(obj, {}), includePath).toThrow(/escapes config directory/);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -458,39 +446,38 @@ describe("security: path traversal protection (CWE-22)", () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("legitimate includes (should work)", () => {
|
|
|
|
|
it("allows legitimate include paths under config root", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{
|
|
|
|
|
name: "same-directory with ./ prefix",
|
|
|
|
|
includePath: "./sub.json",
|
|
|
|
|
files: { [configPath("sub.json")]: { key: "value" } },
|
|
|
|
|
expected: { key: "value" },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "same-directory without ./ prefix",
|
|
|
|
|
includePath: "sub.json",
|
|
|
|
|
files: { [configPath("sub.json")]: { key: "value" } },
|
|
|
|
|
expected: { key: "value" },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "subdirectory",
|
|
|
|
|
includePath: "./sub/nested.json",
|
|
|
|
|
files: { [configPath("sub", "nested.json")]: { nested: true } },
|
|
|
|
|
expected: { nested: true },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "deep subdirectory",
|
|
|
|
|
includePath: "./a/b/c/deep.json",
|
|
|
|
|
files: { [configPath("a", "b", "c", "deep.json")]: { deep: true } },
|
|
|
|
|
expected: { deep: true },
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
const obj = { $include: testCase.includePath };
|
|
|
|
|
expect(resolve(obj, testCase.files), testCase.name).toEqual(testCase.expected);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
|
|
|
|
name: "same-directory with ./ prefix",
|
|
|
|
|
includePath: "./sub.json",
|
|
|
|
|
files: { [configPath("sub.json")]: { key: "value" } },
|
|
|
|
|
expected: { key: "value" },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "same-directory without ./ prefix",
|
|
|
|
|
includePath: "sub.json",
|
|
|
|
|
files: { [configPath("sub.json")]: { key: "value" } },
|
|
|
|
|
expected: { key: "value" },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "subdirectory",
|
|
|
|
|
includePath: "./sub/nested.json",
|
|
|
|
|
files: { [configPath("sub", "nested.json")]: { nested: true } },
|
|
|
|
|
expected: { nested: true },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "deep subdirectory",
|
|
|
|
|
includePath: "./a/b/c/deep.json",
|
|
|
|
|
files: { [configPath("a", "b", "c", "deep.json")]: { deep: true } },
|
|
|
|
|
expected: { deep: true },
|
|
|
|
|
},
|
|
|
|
|
] as const)(
|
|
|
|
|
"allows legitimate include path under config root: $name",
|
|
|
|
|
({ includePath, files, expected }) => {
|
|
|
|
|
const obj = { $include: includePath };
|
|
|
|
|
expect(resolve(obj, files)).toEqual(expected);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Note: Upward traversal from nested configs is restricted for security.
|
|
|
|
|
// Each config file can only include files from its own directory and subdirectories.
|
|
|
|
|
@ -498,62 +485,53 @@ describe("security: path traversal protection (CWE-22)", () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("error properties", () => {
|
|
|
|
|
it("preserves error type/path/message details", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{
|
|
|
|
|
includePath: "/etc/passwd",
|
|
|
|
|
expectedMessageIncludes: ["escapes config directory", "/etc/passwd"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
includePath: "/etc/shadow",
|
|
|
|
|
expectedMessageIncludes: ["/etc/shadow"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
includePath: "../../etc/passwd",
|
|
|
|
|
expectedMessageIncludes: ["escapes config directory", "../../etc/passwd"],
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
const obj = { $include: testCase.includePath };
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
|
|
|
|
includePath: "/etc/passwd",
|
|
|
|
|
expectedMessageIncludes: ["escapes config directory", "/etc/passwd"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
includePath: "/etc/shadow",
|
|
|
|
|
expectedMessageIncludes: ["/etc/shadow"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
includePath: "../../etc/passwd",
|
|
|
|
|
expectedMessageIncludes: ["escapes config directory", "../../etc/passwd"],
|
|
|
|
|
},
|
|
|
|
|
] as const)(
|
|
|
|
|
"preserves error type/path/message details for $includePath",
|
|
|
|
|
({ includePath, expectedMessageIncludes }) => {
|
|
|
|
|
const obj = { $include: includePath };
|
|
|
|
|
try {
|
|
|
|
|
resolve(obj, {});
|
|
|
|
|
expect.fail("Should have thrown");
|
|
|
|
|
} catch (err) {
|
|
|
|
|
expect(err, testCase.includePath).toBeInstanceOf(ConfigIncludeError);
|
|
|
|
|
expect(err, testCase.includePath).toHaveProperty("name", "ConfigIncludeError");
|
|
|
|
|
expect((err as ConfigIncludeError).includePath, testCase.includePath).toBe(
|
|
|
|
|
testCase.includePath,
|
|
|
|
|
);
|
|
|
|
|
for (const messagePart of testCase.expectedMessageIncludes) {
|
|
|
|
|
expect((err as Error).message, `${testCase.includePath}: ${messagePart}`).toContain(
|
|
|
|
|
messagePart,
|
|
|
|
|
);
|
|
|
|
|
expect(err, includePath).toBeInstanceOf(ConfigIncludeError);
|
|
|
|
|
expect(err, includePath).toHaveProperty("name", "ConfigIncludeError");
|
|
|
|
|
expect((err as ConfigIncludeError).includePath, includePath).toBe(includePath);
|
|
|
|
|
for (const messagePart of expectedMessageIncludes) {
|
|
|
|
|
expect((err as Error).message, `${includePath}: ${messagePart}`).toContain(messagePart);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("array includes with malicious paths", () => {
|
|
|
|
|
it("rejects arrays that contain malicious include paths", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{
|
|
|
|
|
name: "one malicious path",
|
|
|
|
|
files: { [configPath("good.json")]: { good: true } },
|
|
|
|
|
includePaths: ["./good.json", "/etc/passwd"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "multiple malicious paths",
|
|
|
|
|
files: {},
|
|
|
|
|
includePaths: ["/etc/passwd", "/etc/shadow"],
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
const obj = { $include: testCase.includePaths };
|
|
|
|
|
expect(() => resolve(obj, testCase.files), testCase.name).toThrow(ConfigIncludeError);
|
|
|
|
|
}
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
|
|
|
|
name: "one malicious path",
|
|
|
|
|
files: { [configPath("good.json")]: { good: true } },
|
|
|
|
|
includePaths: ["./good.json", "/etc/passwd"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "multiple malicious paths",
|
|
|
|
|
files: {},
|
|
|
|
|
includePaths: ["/etc/passwd", "/etc/shadow"],
|
|
|
|
|
},
|
|
|
|
|
] as const)("rejects arrays with malicious include paths: $name", ({ includePaths, files }) => {
|
|
|
|
|
const obj = { $include: includePaths };
|
|
|
|
|
expect(() => resolve(obj, files)).toThrow(ConfigIncludeError);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("allows array with all legitimate paths", () => {
|
|
|
|
|
@ -586,29 +564,26 @@ describe("security: path traversal protection (CWE-22)", () => {
|
|
|
|
|
},
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
const result = deepMerge(testCase.base, testCase.incoming);
|
|
|
|
|
for (const { base, incoming, expected } of cases) {
|
|
|
|
|
const result = deepMerge(base, incoming);
|
|
|
|
|
expect((Object.prototype as Record<string, unknown>).polluted).toBeUndefined();
|
|
|
|
|
expect(result).toEqual(testCase.expected);
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("edge cases", () => {
|
|
|
|
|
it("rejects malformed include paths", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
{ includePath: "./file\x00.json", expectedError: undefined },
|
|
|
|
|
{ includePath: "//etc/passwd", expectedError: ConfigIncludeError },
|
|
|
|
|
] as const;
|
|
|
|
|
for (const testCase of cases) {
|
|
|
|
|
const obj = { $include: testCase.includePath };
|
|
|
|
|
if (testCase.expectedError) {
|
|
|
|
|
expectResolveIncludeError(() => resolve(obj, {}));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// Path with null byte should be rejected or handled safely.
|
|
|
|
|
expect(() => resolve(obj, {}), testCase.includePath).toThrow();
|
|
|
|
|
it.each([
|
|
|
|
|
{ includePath: "./file\x00.json", expectedError: undefined },
|
|
|
|
|
{ includePath: "//etc/passwd", expectedError: ConfigIncludeError },
|
|
|
|
|
] as const)("rejects malformed include path $includePath", ({ includePath, expectedError }) => {
|
|
|
|
|
const obj = { $include: includePath };
|
|
|
|
|
if (expectedError) {
|
|
|
|
|
expectResolveIncludeError(() => resolve(obj, {}));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Path with null byte should be rejected or handled safely.
|
|
|
|
|
expect(() => resolve(obj, {}), includePath).toThrow();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("allows child include when config is at filesystem root", () => {
|
|
|
|
|
|