diff --git a/packages/config-yaml/src/load/unroll.test.ts b/packages/config-yaml/src/load/unroll.test.ts index 9a308dd9b..3357e9cfc 100644 --- a/packages/config-yaml/src/load/unroll.test.ts +++ b/packages/config-yaml/src/load/unroll.test.ts @@ -1,5 +1,7 @@ import { PackageIdentifier } from "../interfaces/slugs.js"; import { + fillTemplateVariables, + getTemplateVariables, parseMarkdownRuleOrAssistantUnrolled, replaceInputsWithSecrets, } from "./unroll.js"; @@ -274,3 +276,73 @@ data: expect(result).not.toContain("inputs.dbPassword"); }); }); + +describe("getTemplateVariables edge cases", () => { + it("handles undefined input gracefully", () => { + const result = getTemplateVariables(undefined as any); + expect(result).toEqual([]); + }); + + it("handles null input gracefully", () => { + const result = getTemplateVariables(null as any); + expect(result).toEqual([]); + }); + + it("handles non-string input gracefully", () => { + const result = getTemplateVariables(123 as any); + expect(result).toEqual([]); + }); + + it("handles empty string correctly", () => { + const result = getTemplateVariables(""); + expect(result).toEqual([]); + }); + + it("extracts template variables from valid input", () => { + const result = getTemplateVariables("\${{ secrets.apiKey }}"); + expect(result).toEqual(["secrets.apiKey"]); + }); + + it("extracts multiple template variables", () => { + const result = getTemplateVariables( + "\${{ secrets.key1 }} and \${{ inputs.key2 }}", + ); + expect(result).toContain("secrets.key1"); + expect(result).toContain("inputs.key2"); + expect(result.length).toBe(2); + }); +}); + +describe("fillTemplateVariables edge cases", () => { + it("handles undefined input gracefully", () => { + const result = fillTemplateVariables(undefined as any, {}); + expect(result).toBe(""); + }); + + it("handles null input gracefully", () => { + const result = fillTemplateVariables(null as any, {}); + expect(result).toBe(""); + }); + + it("handles non-string input gracefully", () => { + const result = fillTemplateVariables(123 as any, {}); + expect(result).toBe(""); + }); + + it("handles empty string correctly", () => { + const result = fillTemplateVariables("", {}); + expect(result).toBe(""); + }); + + it("fills template variables with valid input", () => { + const result = fillTemplateVariables("\${{ secrets.apiKey }}", { + "secrets.apiKey": "my-secret-key", + }); + expect(result).toBe("my-secret-key"); + }); + + it("leaves unfilled variables unchanged", () => { + const result = fillTemplateVariables("\${{ secrets.apiKey }}", {}); + expect(result).toBe("\${{ secrets.apiKey }}"); + }); +}); diff --git a/packages/config-yaml/src/load/unroll.ts b/packages/config-yaml/src/load/unroll.ts index eb2794965..49476925e 100644 --- a/packages/config-yaml/src/load/unroll.ts +++ b/packages/config-yaml/src/load/unroll.ts @@ -86,6 +86,11 @@ export function parseBlock(configYaml: string): Block { export const TEMPLATE_VAR_REGEX = /\${{[\s]*([^}\s]+)[\s]*}}/g; export function getTemplateVariables(templatedYaml: string): string[] { + // Defensive guard against undefined/null/non-string values + if (!templatedYaml || typeof templatedYaml !== "string") { + return []; + } + const variables = new Set(); const matches = templatedYaml.matchAll(TEMPLATE_VAR_REGEX); for (const match of matches) { @@ -98,6 +103,11 @@ export function fillTemplateVariables( templatedYaml: string, data: { [key: string]: string }, ): string { + // Defensive guard against undefined/null/non-string values + if (!templatedYaml || typeof templatedYaml !== "string") { + return ""; + } + return templatedYaml.replace(TEMPLATE_VAR_REGEX, (match, variableName) => { // Inject data if (variableName in data) { @@ -701,7 +711,7 @@ function injectLocalSourceFile( export async function resolveBlock( id: PackageIdentifier, - inputs: Record | undefined, + inputs: Record | undefined, registry: Registry, ): Promise { // Retrieve block raw yaml @@ -777,11 +787,19 @@ function parseYamlOrMarkdownRule( } function inputsToFQSNs( - inputs: Record, + inputs: Record, blockIdentifier: PackageIdentifier, ): Record { const renderedInputs: Record = {}; for (const [key, value] of Object.entries(inputs)) { + // Skip undefined, null, or non-string values + if (value === undefined || value === null || typeof value !== "string") { + console.warn( + `Skipping input "${key}" with invalid value type: ${typeof value}. Expected string.`, + ); + continue; + } + renderedInputs[key] = renderTemplateData(value, { secrets: extractFQSNMap(value, [blockIdentifier]), });