buildCustomSafeOutputScriptsJSON builds a JSON mapping of custom safe output script names to their .cjs filenames, for use in the GH_AW_SAFE_OUTPUT_SCRIPTS env var of the handler manager step. This allows the handler manager to load and dispatch messages to inline script handlers.
(data *WorkflowData)
| 88 | // .cjs filenames, for use in the GH_AW_SAFE_OUTPUT_SCRIPTS env var of the handler manager step. |
| 89 | // This allows the handler manager to load and dispatch messages to inline script handlers. |
| 90 | func buildCustomSafeOutputScriptsJSON(data *WorkflowData) string { |
| 91 | if data.SafeOutputs == nil || len(data.SafeOutputs.Scripts) == 0 { |
| 92 | return "" |
| 93 | } |
| 94 | |
| 95 | // Build mapping of normalized script names to their .cjs filenames |
| 96 | scriptMapping := make(map[string]string, len(data.SafeOutputs.Scripts)) |
| 97 | for scriptName := range data.SafeOutputs.Scripts { |
| 98 | normalizedName := stringutil.NormalizeSafeOutputIdentifier(scriptName) |
| 99 | // Reject names that could cause path traversal when the filename is passed to require() |
| 100 | if !isSafeScriptName(normalizedName) { |
| 101 | safeScriptsLog.Printf("Warning: skipping script %q — name contains unsafe path characters: %q", scriptName, normalizedName) |
| 102 | continue |
| 103 | } |
| 104 | scriptMapping[normalizedName] = safeOutputScriptFilename(normalizedName) |
| 105 | } |
| 106 | |
| 107 | // Sort keys for deterministic output |
| 108 | keys := sliceutil.SortedKeys(scriptMapping) |
| 109 | |
| 110 | ordered := make(map[string]string, len(keys)) |
| 111 | for _, k := range keys { |
| 112 | ordered[k] = scriptMapping[k] |
| 113 | } |
| 114 | |
| 115 | jsonBytes, err := json.Marshal(ordered) |
| 116 | if err != nil { |
| 117 | safeScriptsLog.Printf("Warning: failed to marshal custom safe output scripts: %v", err) |
| 118 | return "" |
| 119 | } |
| 120 | return string(jsonBytes) |
| 121 | } |
| 122 | |
| 123 | // safeOutputScriptFilename returns the .cjs filename for a normalized safe output script name. |
| 124 | func safeOutputScriptFilename(normalizedName string) string { |