-
Notifications
You must be signed in to change notification settings - Fork 352
fix: path traversal sanitization for scriptFilename in safe_output_handler_manager #22280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
becf96e
b7879fb
5e116ab
870ee71
6384cb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package workflow | |
| import ( | ||
| "encoding/json" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/github/gh-aw/pkg/logger" | ||
| "github.com/github/gh-aw/pkg/stringutil" | ||
|
|
@@ -88,6 +89,15 @@ func extractSafeScriptsFromFrontmatter(frontmatter map[string]any) map[string]*S | |
| return make(map[string]*SafeScriptConfig) | ||
| } | ||
|
|
||
| // isSafeScriptName returns true if the script name is safe for use as a filename component. | ||
| // It rejects names that contain path separators or ".." sequences that could lead to | ||
| // path traversal when the generated filename is passed to require() at runtime. | ||
| func isSafeScriptName(name string) bool { | ||
| return !strings.Contains(name, "/") && | ||
| !strings.Contains(name, "\\") && | ||
| !strings.Contains(name, "..") | ||
| } | ||
|
|
||
| // 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. | ||
|
|
@@ -100,6 +110,11 @@ func buildCustomSafeOutputScriptsJSON(data *WorkflowData) string { | |
| scriptMapping := make(map[string]string, len(data.SafeOutputs.Scripts)) | ||
| for scriptName := range data.SafeOutputs.Scripts { | ||
| normalizedName := stringutil.NormalizeSafeOutputIdentifier(scriptName) | ||
| // Reject names that could cause path traversal when the filename is passed to require() | ||
| if !isSafeScriptName(normalizedName) { | ||
| safeScriptsLog.Printf("Warning: skipping script %q — name contains unsafe path characters: %q", scriptName, normalizedName) | ||
| continue | ||
| } | ||
| scriptMapping[normalizedName] = safeOutputScriptFilename(normalizedName) | ||
|
Comment on lines
110
to
118
|
||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sanitization still allows
scriptFilenamevalues like "" or "." which makenodePath.join(scriptBaseDir, safeFilename)resolve toscriptBaseDirand thenrequire(scriptBaseDir)will load a directory (package.json/main or index.js). To fully constrain what can be required, explicitly reject empty/"." (and consider enforcing the expected.cjsextension/prefix for custom scripts) before buildingscriptPath.