Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jobs:
pattern: "TestMCP|TestTool|TestSkill|TestPlaywright|TestFirewall"
- name: "Workflow Validation"
packages: "./pkg/workflow"
pattern: "TestValidat|TestLock|TestError|TestWarning"
pattern: "TestValidat|TestLock|TestError|TestWarning|TestSecurity"
- name: "Workflow Safe Outputs"
packages: "./pkg/workflow"
pattern: "SafeOutputs|CreatePullRequest|OutputLabel|HasSafeOutputs"
Expand Down
12 changes: 7 additions & 5 deletions pkg/cli/compile_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ var sensitivePatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)(secret|password|token|key|credential|api[_-]?key)s?\s*[:=]\s*['"]?[^\s'"]+['"]?`),
// Match GitHub token patterns
regexp.MustCompile(`gh[ps]_[a-zA-Z0-9]{36,}`),
// Match potential API keys
regexp.MustCompile(`['\"][a-zA-Z0-9_\-]{32,}['\"]`),
// Match GitHub Actions secret expressions
regexp.MustCompile(`\$\{\{\s*secrets\.[a-zA-Z0-9_-]+\}\}`),
// Match quoted strings of 40+ chars preceded by secret-like keywords
regexp.MustCompile(`(?i)(secret|password|token|key|credential|api[_-]?key)s?\s*[:=]\s*['"][-a-zA-Z0-9_]{40,}['\"]`),
}

// sanitizeErrorMessage removes potentially sensitive information from error messages
Expand All @@ -72,9 +74,9 @@ func sanitizeErrorMessage(msg string) string {
sanitized = pattern.ReplaceAllStringFunc(sanitized, func(match string) string {
// Try to preserve the structure of the message while redacting the value
if strings.Contains(match, ":") || strings.Contains(match, "=") {
parts := regexp.MustCompile(`[:=]`).Split(match, 2)
if len(parts) == 2 {
return parts[0] + ": [REDACTED]"
idx := strings.IndexAny(match, ":=")
if idx != -1 && idx+1 < len(match) {
return match[:idx+1] + " [REDACTED]"
}
}
return "[REDACTED]"
Expand Down
8 changes: 8 additions & 0 deletions pkg/workflow/compiler_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/githubnext/gh-aw/pkg/constants"
Expand All @@ -14,6 +15,9 @@ import (

var compilerJobsLog = logger.New("workflow:compiler_jobs")

// secretExpressionPattern matches GitHub Actions secret expressions like ${{ secrets.MY_TOKEN }}
var secretExpressionPattern = regexp.MustCompile(`^\$\{\{\s*secrets\.[a-zA-Z0-9_-]+(\s*\|\|[^}]*)?\s*\}\}$`)

// This file contains job building functions extracted from compiler.go
// These functions are responsible for constructing the various jobs that make up
// a compiled agentic workflow, including activation, main, safe outputs, and custom jobs.
Expand Down Expand Up @@ -1393,6 +1397,10 @@ func (c *Compiler) buildCustomJobs(data *WorkflowData, activationJobCreated bool
job.Secrets = make(map[string]string)
for key, val := range secretsMap {
if valStr, ok := val.(string); ok {
// Validate that secret values are GitHub Actions expressions
if !secretExpressionPattern.MatchString(valStr) {
return fmt.Errorf("job '%s': secret '%s' must be a GitHub Actions expression like ${{ secrets.MY_SECRET }}, got: %s", jobName, key, valStr)
}
job.Secrets[key] = valStr
}
}
Expand Down
88 changes: 88 additions & 0 deletions pkg/workflow/compiler_jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,91 @@ Test content`
t.Error("Expected 'result' output")
}
}

// TestSecuritySecretValidation tests that secret values must be GitHub Actions expressions
// This is a security test to prevent plain text secrets from being used
func TestSecuritySecretValidation(t *testing.T) {
tests := []struct {
name string
secretValue string
shouldError bool
errorMsg string
}{
{
name: "valid secret expression",
secretValue: "${{ secrets.MY_TOKEN }}",
shouldError: false,
},
{
name: "valid secret with default",
secretValue: "${{ secrets.DD_SITE || 'datadoghq.com' }}",
shouldError: false,
},
{
name: "valid secret with spaces",
secretValue: "${{ secrets.API_KEY }}",
shouldError: false,
},
{
name: "plain text secret - should fail",
secretValue: "my-plain-text-secret",
shouldError: true,
errorMsg: "must be a GitHub Actions expression",
},
{
name: "wrong context - should fail",
secretValue: "${{ github.token }}",
shouldError: true,
errorMsg: "must be a GitHub Actions expression",
},
{
name: "vars instead of secrets - should fail",
secretValue: "${{ vars.MY_VAR }}",
shouldError: true,
errorMsg: "must be a GitHub Actions expression",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := testutil.TempDir(t, "secret-validation-test")

frontmatter := `---
on: push
permissions:
contents: read
engine: copilot
strict: false
jobs:
test-job:
uses: owner/repo/.github/workflows/reusable.yml@main
secrets:
token: ` + tt.secretValue + `
---

# Test Workflow

Test content`

testFile := filepath.Join(tmpDir, "test.md")
if err := os.WriteFile(testFile, []byte(frontmatter), 0644); err != nil {
t.Fatal(err)
}

compiler := NewCompiler(false, "", "test")
err := compiler.CompileWorkflow(testFile)

if tt.shouldError {
if err == nil {
t.Errorf("Expected error for secret value %q, but got none", tt.secretValue)
} else if !strings.Contains(err.Error(), tt.errorMsg) {
t.Errorf("Expected error message to contain %q, got: %v", tt.errorMsg, err)
}
} else {
if err != nil {
t.Errorf("Expected no error for secret value %q, but got: %v", tt.secretValue, err)
}
}
})
}
}
Loading