-
Notifications
You must be signed in to change notification settings - Fork 368
Refactor dispatch workflow validation by extracting file-resolution utilities #26574
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
Merged
+173
−191
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7fdc685
Initial plan
Copilot 2d94352
chore: outline plan for dispatch workflow file-resolution extraction
Copilot ab488a8
Revert "chore: outline plan for dispatch workflow file-resolution ext…
Copilot ae22928
refactor: extract dispatch workflow file resolver utilities
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package workflow | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/github/gh-aw/pkg/fileutil" | ||
| "github.com/github/gh-aw/pkg/parser" | ||
| ) | ||
|
|
||
| // getCurrentWorkflowName extracts the workflow name from the file path | ||
| func getCurrentWorkflowName(workflowPath string) string { | ||
| filename := filepath.Base(workflowPath) | ||
| // Remove .md or .lock.yml extension | ||
| filename = strings.TrimSuffix(filename, ".md") | ||
| filename = strings.TrimSuffix(filename, ".lock.yml") | ||
| return filename | ||
| } | ||
|
|
||
| // isPathWithinDir checks if a path is within a given directory (prevents path traversal) | ||
| func isPathWithinDir(path, dir string) bool { | ||
| // Get absolute paths | ||
| absPath, err := filepath.Abs(path) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| absDir, err := filepath.Abs(dir) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| // Get the relative path from dir to path | ||
| rel, err := filepath.Rel(absDir, absPath) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| // Check if the relative path tries to go outside the directory | ||
| // If it starts with "..", it's trying to escape | ||
| return !strings.HasPrefix(rel, ".."+string(filepath.Separator)) && rel != ".." | ||
| } | ||
|
|
||
| // findWorkflowFileResult holds the result of finding a workflow file | ||
| type findWorkflowFileResult struct { | ||
| mdPath string | ||
| lockPath string | ||
| ymlPath string | ||
| mdExists bool | ||
| lockExists bool | ||
| ymlExists bool | ||
| } | ||
|
|
||
| // findWorkflowFile searches for a workflow file in .github/workflows directory only | ||
| // Returns paths and existence flags for .md, .lock.yml, and .yml files | ||
| func findWorkflowFile(workflowName string, currentWorkflowPath string) (*findWorkflowFileResult, error) { | ||
| dispatchWorkflowValidationLog.Printf("Finding workflow file: name=%s, current_path=%s", workflowName, currentWorkflowPath) | ||
| result := &findWorkflowFileResult{} | ||
|
|
||
| // Get the current workflow's directory | ||
| currentDir := filepath.Dir(currentWorkflowPath) | ||
|
|
||
| // Get repo root by going up from current directory | ||
| // Assume structure: <repo-root>/.github/workflows/file.md or <repo-root>/.github/aw/file.md | ||
| githubDir := filepath.Dir(currentDir) // .github | ||
| repoRoot := filepath.Dir(githubDir) // repo root | ||
|
|
||
| // Only search in .github/workflows (standard GitHub Actions location) | ||
| searchDir := filepath.Join(repoRoot, ".github", "workflows") | ||
|
|
||
| // Build paths for the workflows directory | ||
| mdPath := filepath.Clean(filepath.Join(searchDir, workflowName+".md")) | ||
| lockPath := filepath.Clean(filepath.Join(searchDir, workflowName+".lock.yml")) | ||
| ymlPath := filepath.Clean(filepath.Join(searchDir, workflowName+".yml")) | ||
|
|
||
| // Validate paths are within the search directory (prevent path traversal) | ||
| if !isPathWithinDir(mdPath, searchDir) || !isPathWithinDir(lockPath, searchDir) || !isPathWithinDir(ymlPath, searchDir) { | ||
| return result, fmt.Errorf("invalid workflow name '%s' (path traversal not allowed)", workflowName) | ||
| } | ||
|
|
||
| // Check which files exist | ||
| result.mdPath = mdPath | ||
| result.lockPath = lockPath | ||
| result.ymlPath = ymlPath | ||
| result.mdExists = fileutil.FileExists(mdPath) | ||
| result.lockExists = fileutil.FileExists(lockPath) | ||
| result.ymlExists = fileutil.FileExists(ymlPath) | ||
|
|
||
| dispatchWorkflowValidationLog.Printf("Workflow file search results: md_exists=%v, lock_exists=%v, yml_exists=%v", result.mdExists, result.lockExists, result.ymlExists) | ||
| return result, nil | ||
| } | ||
|
|
||
| // mdHasWorkflowDispatch reads a .md workflow file's frontmatter and reports whether | ||
| // the workflow includes a workflow_dispatch trigger in its 'on:' section. | ||
| // This is used to validate same-batch dispatch-workflow targets whose .lock.yml has | ||
| // not yet been generated. | ||
| func mdHasWorkflowDispatch(mdPath string) (bool, error) { | ||
| dispatchWorkflowValidationLog.Printf("Checking for workflow_dispatch trigger in: %s", mdPath) | ||
| content, err := os.ReadFile(mdPath) // #nosec G304 -- mdPath is validated via isPathWithinDir in findWorkflowFile | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| result, err := parser.ExtractFrontmatterFromContent(string(content)) | ||
| if err != nil || result == nil { | ||
| return false, err | ||
| } | ||
| onSection, hasOn := result.Frontmatter["on"] | ||
| if !hasOn { | ||
| return false, nil | ||
| } | ||
| return containsWorkflowDispatch(onSection), nil | ||
| } | ||
|
|
||
| // extractMDWorkflowDispatchInputs reads a .md workflow file's frontmatter and extracts | ||
| // the workflow_dispatch inputs schema, mirroring extractWorkflowDispatchInputs for .md sources. | ||
| func extractMDWorkflowDispatchInputs(mdPath string) (map[string]any, error) { | ||
| content, err := os.ReadFile(mdPath) // #nosec G304 -- mdPath is validated via isPathWithinDir in findWorkflowFile | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| result, err := parser.ExtractFrontmatterFromContent(string(content)) | ||
| if err != nil || result == nil { | ||
| return make(map[string]any), nil | ||
| } | ||
| onSection, hasOn := result.Frontmatter["on"] | ||
| if !hasOn { | ||
| return make(map[string]any), nil | ||
| } | ||
| onMap, ok := onSection.(map[string]any) | ||
| if !ok { | ||
| return make(map[string]any), nil | ||
| } | ||
| workflowDispatch, hasWorkflowDispatch := onMap["workflow_dispatch"] | ||
| if !hasWorkflowDispatch { | ||
| return make(map[string]any), nil | ||
| } | ||
| workflowDispatchMap, ok := workflowDispatch.(map[string]any) | ||
| if !ok { | ||
| return make(map[string]any), nil | ||
| } | ||
| inputs, hasInputs := workflowDispatchMap["inputs"] | ||
| if !hasInputs { | ||
| return make(map[string]any), nil | ||
| } | ||
| inputsMap, ok := inputs.(map[string]any) | ||
| if !ok { | ||
| return make(map[string]any), nil | ||
| } | ||
| return inputsMap, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
dispatch_workflow_file_resolver.gonow contains utilities that are used outside dispatch-workflow as well (e.g.,pkg/workflow/call_workflow_validation.gocallsgetCurrentWorkflowName/findWorkflowFile). Renaming this file (and potentially the logger it uses) to something more generic would make navigation/debugging less confusing.