Skip to content

Commit 7c6b5c6

Browse files
Copilotpelikhan
andauthored
fix: validate agentDir is a directory and use dynamic label in checkDir logs
- Guard against findArtifactDir returning a file path: check os.Stat IsDir() before probing for audit files inside the agent artifact directory - Derive log labels from filepath.Base(agentDir) so debug output reflects the actual on-disk name (agent-artifacts, abc123-agent, etc.) instead of the hardcoded "agent/..." string - Add test: file named "agent" does not panic or falsely match Agent-Logs-Url: https://github.com/github/gh-aw/sessions/5534ec16-9018-4e51-8a41-c13438eecaf6 Co-authored-by: pelikhan <[email protected]>
1 parent e7dba46 commit 7c6b5c6

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

pkg/cli/firewall_policy.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,21 @@ func detectFirewallAuditArtifacts(runDir string) (manifestPath, auditJSONLPath s
443443
// e.g., when the audit command is run on a directory populated via `gh run download`).
444444
// Handles "agent", "agent-artifacts", and workflow_call prefixed names (e.g. "hash-agent").
445445
if agentDir := findArtifactDir(runDir, "agent", "agent-artifacts"); agentDir != "" {
446-
// New artifact structure (actions/upload-artifact v4+, /tmp/gh-aw/ prefix stripped):
447-
// agent/sandbox/firewall/audit/
448-
if !checkDir(filepath.Join(agentDir, "sandbox", "firewall", "audit"), "agent/sandbox/firewall/audit") {
449-
// Old artifact structure (/tmp/gh-aw/ prefix preserved inside the artifact):
450-
// agent/tmp/gh-aw/sandbox/firewall/audit/
451-
checkDir(filepath.Join(agentDir, "tmp", "gh-aw", "sandbox", "firewall", "audit"), "agent/tmp/gh-aw/sandbox/firewall/audit")
452-
}
453-
if manifestPath != "" && auditJSONLPath != "" {
454-
return
446+
// Guard: findArtifactDir checks existence but not type; skip if it resolved to a file.
447+
if info, err := os.Stat(agentDir); err != nil || !info.IsDir() {
448+
firewallPolicyLog.Printf("Skipping agent artifact path (not a directory): %s", agentDir)
449+
} else {
450+
agentBase := filepath.Base(agentDir)
451+
// New artifact structure (actions/upload-artifact v4+, /tmp/gh-aw/ prefix stripped):
452+
// <agentDir>/sandbox/firewall/audit/
453+
if !checkDir(filepath.Join(agentDir, "sandbox", "firewall", "audit"), agentBase+"/sandbox/firewall/audit") {
454+
// Old artifact structure (/tmp/gh-aw/ prefix preserved inside the artifact):
455+
// <agentDir>/tmp/gh-aw/sandbox/firewall/audit/
456+
checkDir(filepath.Join(agentDir, "tmp", "gh-aw", "sandbox", "firewall", "audit"), agentBase+"/tmp/gh-aw/sandbox/firewall/audit")
457+
}
458+
if manifestPath != "" && auditJSONLPath != "" {
459+
return
460+
}
455461
}
456462
}
457463

pkg/cli/firewall_policy_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,17 @@ func TestDetectFirewallAuditArtifacts(t *testing.T) {
655655
assert.Empty(t, foundManifest, "Should not find manifest")
656656
assert.Empty(t, foundAudit, "Should not find audit JSONL")
657657
})
658+
659+
t.Run("file named 'agent' does not panic or falsely match", func(t *testing.T) {
660+
// If a plain file happens to be named "agent" in the run directory (e.g., a flattened
661+
// single-file artifact from a different upload), the lookup must skip it gracefully.
662+
dir := t.TempDir()
663+
require.NoError(t, os.WriteFile(filepath.Join(dir, "agent"), []byte("not a directory"), 0644))
664+
665+
foundManifest, foundAudit := detectFirewallAuditArtifacts(dir)
666+
assert.Empty(t, foundManifest, "Should not find manifest when 'agent' is a file")
667+
assert.Empty(t, foundAudit, "Should not find audit JSONL when 'agent' is a file")
668+
})
658669
}
659670

660671
func TestAnalyzeFirewallPolicy(t *testing.T) {

0 commit comments

Comments
 (0)