-
Notifications
You must be signed in to change notification settings - Fork 375
Add validation for safe-outputs.allowed-domains #9017
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
+1,022
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f451054
Initial plan
Copilot dd320b8
Add validation for safe-outputs.allowed-domains
Copilot e092a6c
Fix linting issues in domain validation
Copilot 9bb3125
Add comprehensive wildcard domain tests for JavaScript sanitization
Copilot 229978a
Add comprehensive regex tests for domain pattern validation
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
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
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,104 @@ | ||
| package workflow | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/githubnext/gh-aw/pkg/logger" | ||
| ) | ||
|
|
||
| var safeOutputsDomainsValidationLog = logger.New("workflow:safe_outputs_domains_validation") | ||
|
|
||
| // domainPattern validates domain patterns including wildcards | ||
| // Valid patterns: | ||
| // - Plain domains: github.com, api.github.com | ||
| // - Wildcard domains: *.github.com | ||
| // Invalid patterns: | ||
| // - Multiple wildcards: *.*.github.com | ||
| // - Wildcard not at start: github.*.com | ||
| // - Empty or malformed domains | ||
| var domainPattern = regexp.MustCompile(`^(\*\.)?[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$`) | ||
|
|
||
| // validateSafeOutputsAllowedDomains validates the allowed-domains configuration in safe-outputs | ||
| func validateSafeOutputsAllowedDomains(config *SafeOutputsConfig) error { | ||
| if config == nil || len(config.AllowedDomains) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| safeOutputsDomainsValidationLog.Printf("Validating %d allowed domains", len(config.AllowedDomains)) | ||
|
|
||
| for i, domain := range config.AllowedDomains { | ||
| if err := validateDomainPattern(domain); err != nil { | ||
| return fmt.Errorf("safe-outputs.allowed-domains[%d]: %w", i, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // validateDomainPattern validates a single domain pattern | ||
| func validateDomainPattern(domain string) error { | ||
| // Check for empty domain | ||
| if domain == "" { | ||
| return fmt.Errorf("domain cannot be empty") | ||
| } | ||
|
|
||
| // Check for wildcard-only pattern | ||
| if domain == "*" { | ||
| return fmt.Errorf("wildcard-only domain '*' is not allowed, use a specific wildcard pattern like '*.example.com'") | ||
| } | ||
|
|
||
| // Check for wildcard without base domain (must be done before regex) | ||
| if domain == "*." { | ||
| return fmt.Errorf("wildcard pattern '%s' must have a domain after '*.' (e.g., '*.example.com')", domain) | ||
| } | ||
|
|
||
| // Check for multiple wildcards | ||
| if strings.Count(domain, "*") > 1 { | ||
| return fmt.Errorf("domain pattern '%s' contains multiple wildcards, only one wildcard at the start is allowed (e.g., '*.example.com')", domain) | ||
| } | ||
|
|
||
| // Check for wildcard not at the start | ||
| if strings.Contains(domain, "*") && !strings.HasPrefix(domain, "*.") { | ||
| return fmt.Errorf("domain pattern '%s' has wildcard in invalid position, wildcard must be at the start followed by a dot (e.g., '*.example.com')", domain) | ||
| } | ||
|
|
||
| // Additional validation for wildcard patterns | ||
| if strings.HasPrefix(domain, "*.") { | ||
| baseDomain := domain[2:] // Remove "*." | ||
| if baseDomain == "" { | ||
| return fmt.Errorf("wildcard pattern '%s' must have a domain after '*.' (e.g., '*.example.com')", domain) | ||
| } | ||
| // Ensure the base domain doesn't start with a dot | ||
| if strings.HasPrefix(baseDomain, ".") { | ||
| return fmt.Errorf("wildcard pattern '%s' has invalid format, use '*.example.com' instead of '*.*.example.com'", domain) | ||
| } | ||
| } | ||
|
|
||
| // Validate domain pattern format | ||
| if !domainPattern.MatchString(domain) { | ||
| // Provide specific error messages for common issues | ||
| if strings.HasSuffix(domain, ".") { | ||
| return fmt.Errorf("domain pattern '%s' cannot end with a dot", domain) | ||
| } | ||
| if strings.Contains(domain, "..") { | ||
| return fmt.Errorf("domain pattern '%s' cannot contain consecutive dots", domain) | ||
| } | ||
| if strings.HasPrefix(domain, ".") && !strings.HasPrefix(domain, "*.") { | ||
| return fmt.Errorf("domain pattern '%s' cannot start with a dot (except for wildcard patterns like '*.example.com')", domain) | ||
| } | ||
| // Check for invalid characters | ||
| for _, char := range domain { | ||
| if (char < 'a' || char > 'z') && | ||
| (char < 'A' || char > 'Z') && | ||
| (char < '0' || char > '9') && | ||
| char != '-' && char != '.' && char != '*' { | ||
| return fmt.Errorf("domain pattern '%s' contains invalid character '%c', only alphanumeric, hyphens, dots, and wildcards are allowed", domain, char) | ||
| } | ||
| } | ||
| return fmt.Errorf("domain pattern '%s' is not a valid domain format", domain) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.