|
3 | 3 | package cli |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "context" |
6 | 7 | "encoding/json" |
7 | 8 | "testing" |
8 | 9 | ) |
@@ -112,3 +113,39 @@ func TestValidateWorkflowName_Empty(t *testing.T) { |
112 | 113 | t.Errorf("validateWorkflowName(\"\") returned error: %v", err) |
113 | 114 | } |
114 | 115 | } |
| 116 | + |
| 117 | +// TestCheckActorPermission_AllowsWhenValidationDisabled verifies that access is |
| 118 | +// always granted when validateActor is false, regardless of actor or repo context. |
| 119 | +func TestCheckActorPermission_AllowsWhenValidationDisabled(t *testing.T) { |
| 120 | + err := checkActorPermission(context.Background(), "", false, "logs") |
| 121 | + if err != nil { |
| 122 | + t.Errorf("checkActorPermission with validateActor=false: expected nil, got %v", err) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// TestCheckActorPermission_DeniesWhenNoActorAndValidationEnabled verifies that access |
| 127 | +// is denied when actor is empty and validation is enabled. |
| 128 | +func TestCheckActorPermission_DeniesWhenNoActorAndValidationEnabled(t *testing.T) { |
| 129 | + err := checkActorPermission(context.Background(), "", true, "logs") |
| 130 | + if err == nil { |
| 131 | + t.Error("checkActorPermission with empty actor and validateActor=true: expected error, got nil") |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// TestCheckActorPermission_DeniesWhenNoRepoContext verifies the fail-closed behavior |
| 136 | +// when no repository context is available (GITHUB_REPOSITORY unset, no git context). |
| 137 | +// This tests the security fix: permission check must fail closed, not open. |
| 138 | +func TestCheckActorPermission_DeniesWhenNoRepoContext(t *testing.T) { |
| 139 | + // Ensure no GITHUB_REPOSITORY is set (and cache is empty) so getRepository fails/returns "". |
| 140 | + t.Setenv("GITHUB_REPOSITORY", "") |
| 141 | + |
| 142 | + // Clear any cached repo value so getRepository does a fresh lookup. |
| 143 | + mcpCache.SetRepo("") |
| 144 | + |
| 145 | + // With GITHUB_REPOSITORY="" and no git context in the test environment, |
| 146 | + // getRepository will either return "" or an error — both paths must deny access. |
| 147 | + err := checkActorPermission(context.Background(), "someuser", true, "logs") |
| 148 | + if err == nil { |
| 149 | + t.Error("checkActorPermission with no repo context: expected error (fail closed), got nil (fail open)") |
| 150 | + } |
| 151 | +} |
0 commit comments