-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathfirewall_default_enablement_test.go
More file actions
515 lines (423 loc) · 14.8 KB
/
Copy pathfirewall_default_enablement_test.go
File metadata and controls
515 lines (423 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package workflow
import (
"strings"
"testing"
)
// TestEnableFirewallByDefaultForCopilot tests the automatic firewall enablement for copilot engine
func TestEnableFirewallByDefaultForCopilot(t *testing.T) {
t.Run("copilot engine with network restrictions enables firewall by default", func(t *testing.T) {
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com", "api.github.com"},
ExplicitlyDefined: true,
}
enableFirewallByDefaultForCopilot("copilot", networkPerms, nil)
if networkPerms.Firewall == nil {
t.Error("Expected firewall to be enabled by default for copilot engine with network restrictions")
}
if !networkPerms.Firewall.Enabled {
t.Error("Expected firewall.Enabled to be true")
}
})
t.Run("copilot engine with network:defaults enables firewall by default", func(t *testing.T) {
networkPerms := &NetworkPermissions{
Mode: "defaults",
ExplicitlyDefined: true,
}
enableFirewallByDefaultForCopilot("copilot", networkPerms, nil)
if networkPerms.Firewall == nil {
t.Error("Expected firewall to be enabled by default for copilot engine with network:defaults")
}
if !networkPerms.Firewall.Enabled {
t.Error("Expected firewall.Enabled to be true")
}
})
t.Run("copilot engine with empty network object enables firewall by default", func(t *testing.T) {
networkPerms := &NetworkPermissions{
ExplicitlyDefined: true,
Allowed: []string{},
}
enableFirewallByDefaultForCopilot("copilot", networkPerms, nil)
if networkPerms.Firewall == nil {
t.Error("Expected firewall to be enabled by default for copilot engine with empty network object")
}
if !networkPerms.Firewall.Enabled {
t.Error("Expected firewall.Enabled to be true")
}
})
t.Run("copilot engine with wildcard allowed does NOT enable firewall", func(t *testing.T) {
networkPerms := &NetworkPermissions{
Allowed: []string{"*"},
ExplicitlyDefined: true,
}
enableFirewallByDefaultForCopilot("copilot", networkPerms, nil)
if networkPerms.Firewall != nil {
t.Error("Expected firewall to NOT be enabled when allowed contains wildcard '*'")
}
})
t.Run("copilot engine with explicit firewall config is not overridden", func(t *testing.T) {
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
Firewall: &FirewallConfig{
Enabled: false,
},
}
enableFirewallByDefaultForCopilot("copilot", networkPerms, nil)
if networkPerms.Firewall.Enabled {
t.Error("Expected explicit firewall.Enabled=false to be preserved")
}
})
t.Run("non-copilot engine does not enable firewall", func(t *testing.T) {
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
}
enableFirewallByDefaultForCopilot("claude", networkPerms, nil)
if networkPerms.Firewall != nil {
t.Error("Expected firewall to remain nil for non-copilot engine")
}
})
t.Run("nil network permissions does not cause error", func(t *testing.T) {
// Should not panic
enableFirewallByDefaultForCopilot("copilot", nil, nil)
})
}
// TestCopilotFirewallDefaultIntegration tests the integration with workflow compilation
func TestCopilotFirewallDefaultIntegration(t *testing.T) {
t.Run("copilot workflow with network restrictions includes AWF installation", func(t *testing.T) {
frontmatter := map[string]any{
"on": "workflow_dispatch",
"permissions": map[string]any{
"contents": "read",
},
"engine": "copilot",
"network": map[string]any{
"allowed": []any{"example.com", "api.github.com"},
},
}
// Create compiler
c := NewCompiler(false, "", "test")
c.SetSkipValidation(true)
// Extract engine config
engineSetting, engineConfig := c.ExtractEngineConfig(frontmatter)
if engineSetting != "copilot" {
t.Fatalf("Expected engine 'copilot', got '%s'", engineSetting)
}
// Extract network permissions
networkPerms := c.extractNetworkPermissions(frontmatter)
if networkPerms == nil {
t.Fatal("Expected network permissions to be extracted")
}
// Enable firewall by default
enableFirewallByDefaultForCopilot(engineConfig.ID, networkPerms, nil)
// Verify firewall is enabled
if networkPerms.Firewall == nil {
t.Error("Expected firewall to be automatically enabled")
}
if !networkPerms.Firewall.Enabled {
t.Error("Expected firewall.Enabled to be true")
}
// Create workflow data
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: engineConfig,
NetworkPermissions: networkPerms,
}
// Get installation steps
engine := NewCopilotEngine()
steps := engine.GetInstallationSteps(workflowData)
// Verify AWF installation step is present
found := false
for _, step := range steps {
stepStr := strings.Join(step, "\n")
if strings.Contains(stepStr, "Install awf binary") || strings.Contains(stepStr, "awf --version") {
found = true
break
}
}
if !found {
t.Error("Expected AWF installation steps to be included")
}
})
t.Run("copilot workflow with explicit firewall:false does not include AWF", func(t *testing.T) {
frontmatter := map[string]any{
"on": "workflow_dispatch",
"permissions": map[string]any{
"contents": "read",
},
"engine": "copilot",
"network": map[string]any{
"allowed": []any{"example.com"},
"firewall": false,
},
}
// Create compiler
c := NewCompiler(false, "", "test")
c.SetSkipValidation(true)
// Extract engine config
engineSetting, engineConfig := c.ExtractEngineConfig(frontmatter)
if engineSetting != "copilot" {
t.Fatalf("Expected engine 'copilot', got '%s'", engineSetting)
}
// Extract network permissions
networkPerms := c.extractNetworkPermissions(frontmatter)
if networkPerms == nil {
t.Fatal("Expected network permissions to be extracted")
}
// Verify firewall is explicitly disabled
if networkPerms.Firewall == nil {
t.Error("Expected firewall config to be present")
}
if networkPerms.Firewall.Enabled {
t.Error("Expected firewall.Enabled to be false")
}
// Enable firewall by default (should not override explicit config)
enableFirewallByDefaultForCopilot(engineConfig.ID, networkPerms, nil)
// Verify firewall is still disabled
if networkPerms.Firewall.Enabled {
t.Error("Expected firewall to remain disabled when explicitly set to false")
}
// Create workflow data
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: engineConfig,
NetworkPermissions: networkPerms,
}
// Get installation steps
engine := NewCopilotEngine()
steps := engine.GetInstallationSteps(workflowData)
// Verify AWF installation step is NOT present
for _, step := range steps {
stepStr := strings.Join(step, "\n")
if strings.Contains(stepStr, "Install awf binary") {
t.Error("Expected AWF installation steps to NOT be included when firewall is explicitly disabled")
}
}
})
t.Run("claude engine with network restrictions does not enable firewall", func(t *testing.T) {
frontmatter := map[string]any{
"on": "workflow_dispatch",
"permissions": map[string]any{
"contents": "read",
},
"engine": "claude",
"network": map[string]any{
"allowed": []any{"example.com"},
},
}
// Create compiler
c := NewCompiler(false, "", "test")
c.SetSkipValidation(true)
// Extract engine config
engineSetting, engineConfig := c.ExtractEngineConfig(frontmatter)
if engineSetting != "claude" {
t.Fatalf("Expected engine 'claude', got '%s'", engineSetting)
}
// Extract network permissions
networkPerms := c.extractNetworkPermissions(frontmatter)
if networkPerms == nil {
t.Fatal("Expected network permissions to be extracted")
}
// Enable firewall by default (should not affect non-copilot engines)
enableFirewallByDefaultForCopilot(engineConfig.ID, networkPerms, nil)
// Verify firewall is NOT enabled for claude
if networkPerms.Firewall != nil {
t.Error("Expected firewall to remain nil for claude engine")
}
})
}
// TestDailyTeamStatusFirewallEnabled tests that daily-team-status workflow has firewall enabled
func TestDailyTeamStatusFirewallEnabled(t *testing.T) {
t.Run("daily-team-status workflow with network:defaults enables AWF firewall", func(t *testing.T) {
frontmatter := map[string]any{
"on": "workflow_dispatch",
"permissions": map[string]any{
"contents": "read",
},
// No explicit engine specified - should default to copilot
"network": "defaults",
}
// Create compiler
c := NewCompiler(false, "", "test")
c.SetSkipValidation(true)
// Extract engine config (should default to copilot)
_, engineConfig := c.ExtractEngineConfig(frontmatter)
// If no engine is specified, it should default to copilot
var engineID string
if engineConfig != nil {
engineID = engineConfig.ID
} else {
engineID = "copilot" // default engine
}
// Extract network permissions
networkPerms := c.extractNetworkPermissions(frontmatter)
if networkPerms == nil {
t.Fatal("Expected network permissions to be extracted")
}
if networkPerms.Mode != "defaults" {
t.Errorf("Expected network mode to be 'defaults', got '%s'", networkPerms.Mode)
}
// Enable firewall by default
enableFirewallByDefaultForCopilot(engineID, networkPerms, nil)
// Verify firewall is enabled
if networkPerms.Firewall == nil {
t.Error("Expected firewall to be automatically enabled for network:defaults with copilot")
}
if networkPerms.Firewall != nil && !networkPerms.Firewall.Enabled {
t.Error("Expected firewall.Enabled to be true")
}
})
}
// TestStrictModeFirewallValidation tests strict mode firewall validation
func TestStrictModeFirewallValidation(t *testing.T) {
t.Run("strict mode requires firewall for copilot with network restrictions", func(t *testing.T) {
compiler := NewCompiler(false, "", "test")
compiler.SetStrictMode(true)
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
// Firewall is NOT enabled
Firewall: nil,
}
err := compiler.validateStrictFirewall("copilot", networkPerms, nil)
if err == nil {
t.Error("Expected error in strict mode when firewall is not enabled")
}
if !strings.Contains(err.Error(), "firewall must be enabled") {
t.Errorf("Expected error about firewall requirement, got: %v", err)
}
})
t.Run("strict mode allows firewall disabled when allowed is wildcard", func(t *testing.T) {
compiler := NewCompiler(false, "", "test")
compiler.SetStrictMode(true)
networkPerms := &NetworkPermissions{
Allowed: []string{"*"},
ExplicitlyDefined: true,
Firewall: nil,
}
err := compiler.validateStrictFirewall("copilot", networkPerms, nil)
if err != nil {
t.Errorf("Expected no error when allowed is wildcard, got: %v", err)
}
})
t.Run("strict mode passes when firewall is enabled", func(t *testing.T) {
compiler := NewCompiler(false, "", "test")
compiler.SetStrictMode(true)
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
Firewall: &FirewallConfig{
Enabled: true,
},
}
err := compiler.validateStrictFirewall("copilot", networkPerms, nil)
if err != nil {
t.Errorf("Expected no error when firewall is enabled, got: %v", err)
}
})
t.Run("strict mode skips validation for non-copilot engines", func(t *testing.T) {
compiler := NewCompiler(false, "", "test")
compiler.SetStrictMode(true)
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
Firewall: nil,
}
err := compiler.validateStrictFirewall("claude", networkPerms, nil)
if err != nil {
t.Errorf("Expected no error for non-copilot engine, got: %v", err)
}
})
t.Run("strict mode refuses sandbox.agent: false for copilot", func(t *testing.T) {
compiler := NewCompiler(false, "", "test")
compiler.SetStrictMode(true)
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
Firewall: nil,
}
sandboxConfig := &SandboxConfig{
Agent: &AgentSandboxConfig{
Disabled: true,
},
}
err := compiler.validateStrictFirewall("copilot", networkPerms, sandboxConfig)
if err == nil {
t.Error("Expected error when sandbox.agent is false in strict mode for copilot")
}
expectedMsg := "sandbox.agent: false"
if !strings.Contains(err.Error(), expectedMsg) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedMsg, err)
}
})
t.Run("strict mode refuses sandbox.agent: false for all engines", func(t *testing.T) {
compiler := NewCompiler(false, "", "test")
compiler.SetStrictMode(true)
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
Firewall: nil,
}
sandboxConfig := &SandboxConfig{
Agent: &AgentSandboxConfig{
Disabled: true,
},
}
// All engines should refuse sandbox.agent: false in strict mode
err := compiler.validateStrictFirewall("claude", networkPerms, sandboxConfig)
if err == nil {
t.Error("Expected error for non-copilot engine with sandbox.agent: false in strict mode")
}
expectedMsg := "sandbox.agent: false"
if !strings.Contains(err.Error(), expectedMsg) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedMsg, err)
}
})
t.Run("strict mode skips validation when SRT is enabled", func(t *testing.T) {
compiler := NewCompiler(false, "", "test")
compiler.SetStrictMode(true)
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
Firewall: nil,
}
sandboxConfig := &SandboxConfig{
Type: SandboxTypeRuntime,
}
err := compiler.validateStrictFirewall("copilot", networkPerms, sandboxConfig)
if err != nil {
t.Errorf("Expected no error when SRT is enabled, got: %v", err)
}
})
t.Run("non-strict mode does not validate firewall", func(t *testing.T) {
compiler := NewCompiler(false, "", "test")
compiler.SetStrictMode(false)
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
Firewall: nil,
}
err := compiler.validateStrictFirewall("copilot", networkPerms, nil)
if err != nil {
t.Errorf("Expected no error in non-strict mode, got: %v", err)
}
})
t.Run("non-strict mode allows sandbox.agent: false for copilot", func(t *testing.T) {
compiler := NewCompiler(false, "", "test")
compiler.SetStrictMode(false)
networkPerms := &NetworkPermissions{
Allowed: []string{"example.com"},
ExplicitlyDefined: true,
Firewall: nil,
}
sandboxConfig := &SandboxConfig{
Agent: &AgentSandboxConfig{
Disabled: true,
},
}
err := compiler.validateStrictFirewall("copilot", networkPerms, sandboxConfig)
if err != nil {
t.Errorf("Expected no error in non-strict mode with sandbox.agent: false, got: %v", err)
}
})
}