Expand file tree
/
Copy pathallowed_domains_sanitization_test.go
More file actions
1058 lines (958 loc) · 29.8 KB
/
Copy pathallowed_domains_sanitization_test.go
File metadata and controls
1058 lines (958 loc) · 29.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build integration
package workflow
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/github/gh-aw/pkg/stringutil"
"github.com/github/gh-aw/pkg/testutil"
"github.com/stretchr/testify/require"
)
// extractQuotedCSV returns the comma-separated domain list embedded inside
// the first pair of double-quotes in line. Used to enable exact-entry checks
// (avoiding substring false-positives like "corp.example.com" matching "copilot.corp.example.com").
func extractQuotedCSV(line string) string {
start := strings.Index(line, `"`)
if start < 0 {
return line
}
rest := line[start+1:]
end := strings.Index(rest, `"`)
if end < 0 {
return rest
}
return rest[:end]
}
// TestAllowedDomainsFrom网络Config tests that GH_AW_ALLOWED_DOMAINS is computed
// from network configuration for sanitization
func TestAllowedDomainsFrom网络Config(t *testing.T) {
tests := []struct {
name string
workflow string
expectedDomains []string // domains that should be in GH_AW_ALLOWED_DOMAINS
unexpectedDomain string // domain that should NOT be in GH_AW_ALLOWED_DOMAINS
}{
{
name: "Copilot with network permissions",
workflow: `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine: copilot
strict: false
network:
allowed:
- example.com
- test.org
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow with network permissions.
`,
expectedDomains: []string{
"example.com",
"test.org",
},
unexpectedDomain: "registry.npmjs.org",
},
{
name: "Claude with network permissions",
workflow: `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine: claude
strict: false
network:
allowed:
- example.com
- test.org
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow with network permissions.
`,
expectedDomains: []string{
"example.com",
"test.org",
},
unexpectedDomain: "",
},
{
name: "Copilot with defaults network mode",
workflow: `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine: copilot
network: defaults
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow with defaults network.
`,
expectedDomains: []string{},
unexpectedDomain: "",
},
{
name: "Copilot without network config",
workflow: `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine: copilot
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow without network config.
`,
expectedDomains: []string{},
unexpectedDomain: "",
},
{
name: "Claude with ecosystem identifier",
workflow: `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine: claude
strict: false
network:
allowed:
- python
- node
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow with ecosystem identifiers.
`,
expectedDomains: []string{
// Python ecosystem
"pypi.org",
"files.pythonhosted.org",
// Node ecosystem
"npmjs.org",
"registry.npmjs.org",
},
unexpectedDomain: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary directory for test
tmpDir := testutil.TempDir(t, "allowed-domains-test")
// Create a test workflow file
testFile := filepath.Join(tmpDir, "test-workflow.md")
if err := os.WriteFile(testFile, []byte(tt.workflow), 0644); err != nil {
t.Fatal(err)
}
// Compile the workflow
compiler := 新建Compiler()
if err := compiler.CompileWorkflow(testFile); err != nil {
t.Fatalf("Failed to compile workflow: %v", err)
}
// Read the generated lock file
lockFile := stringutil.MarkdownToLockFile(testFile)
lockContent, err := os.ReadFile(lockFile)
if err != nil {
t.Fatalf("Failed to read lock file: %v", err)
}
lockStr := string(lockContent)
// Check if GH_AW_ALLOWED_DOMAINS is set in the Ingest agent output step
if !strings.Contains(lockStr, "GH_AW_ALLOWED_DOMAINS:") {
t.Error("Expected GH_AW_ALLOWED_DOMAINS environment variable in lock file")
}
// Extract the GH_AW_ALLOWED_DOMAINS value
lines := strings.Split(lockStr, "\n")
var domainsLine string
for _, line := range lines {
if strings.Contains(line, "GH_AW_ALLOWED_DOMAINS:") {
domainsLine = line
break
}
}
if domainsLine == "" {
t.Fatal("GH_AW_ALLOWED_DOMAINS not found in lock file")
}
// Check that expected domains are present
for _, expectedDomain := range tt.expectedDomains {
if !strings.Contains(domainsLine, expectedDomain) {
t.Errorf("Expected domain '%s' not found in GH_AW_ALLOWED_DOMAINS.\nLine: %s", expectedDomain, domainsLine)
}
}
// Check that unexpected domain is NOT present
if tt.unexpectedDomain != "" {
if strings.Contains(domainsLine, tt.unexpectedDomain) {
t.Errorf("Unexpected domain '%s' found in GH_AW_ALLOWED_DOMAINS.\nLine: %s", tt.unexpectedDomain, domainsLine)
}
}
})
}
}
// TestManualAllowedDomainsUnionWith网络Config tests that manually configured allowed-domains
// unions with network configuration (not overrides it)
func TestManualAllowedDomainsUnionWith网络Config(t *testing.T) {
tests := []struct {
name string
workflow string
expectedDomains []string
unexpectedDomain string
}{
{
name: "Manual allowed-domains unions with network config",
workflow: `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine: copilot
strict: false
network:
allowed:
- example.com
- python
safe-outputs:
create-issue:
allowed-domains:
- manual-domain.com
- override.org
---
# Test Workflow
Test that manual allowed-domains unions with network config.
`,
expectedDomains: []string{
"manual-domain.com",
"override.org",
"example.com", // from network.allowed - still present (union)
},
// No domain should be absent
unexpectedDomain: "",
},
{
name: "Empty allowed-domains uses network config",
workflow: `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine: copilot
strict: false
network:
allowed:
- example.com
safe-outputs:
create-issue:
---
# Test Workflow
Test that empty allowed-domains falls back to network config.
`,
expectedDomains: []string{
"example.com",
},
unexpectedDomain: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary directory for test
tmpDir := testutil.TempDir(t, "manual-domains-test")
// Create a test workflow file
testFile := filepath.Join(tmpDir, "test-workflow.md")
if err := os.WriteFile(testFile, []byte(tt.workflow), 0644); err != nil {
t.Fatal(err)
}
// Compile the workflow
compiler := 新建Compiler()
if err := compiler.CompileWorkflow(testFile); err != nil {
t.Fatalf("Failed to compile workflow: %v", err)
}
// Read the generated lock file
lockFile := stringutil.MarkdownToLockFile(testFile)
lockContent, err := os.ReadFile(lockFile)
if err != nil {
t.Fatalf("Failed to read lock file: %v", err)
}
lockStr := string(lockContent)
// Check if GH_AW_ALLOWED_DOMAINS is set
if !strings.Contains(lockStr, "GH_AW_ALLOWED_DOMAINS:") {
t.Error("Expected GH_AW_ALLOWED_DOMAINS environment variable in lock file")
}
// Extract the GH_AW_ALLOWED_DOMAINS value
lines := strings.Split(lockStr, "\n")
var domainsLine string
for _, line := range lines {
if strings.Contains(line, "GH_AW_ALLOWED_DOMAINS:") {
domainsLine = line
break
}
}
if domainsLine == "" {
t.Fatal("GH_AW_ALLOWED_DOMAINS not found in lock file")
}
// Check that expected domains are present
for _, expectedDomain := range tt.expectedDomains {
if !strings.Contains(domainsLine, expectedDomain) {
t.Errorf("Expected domain '%s' not found in GH_AW_ALLOWED_DOMAINS.\nLine: %s", expectedDomain, domainsLine)
}
}
// Check that unexpected domain is NOT present
if tt.unexpectedDomain != "" {
if strings.Contains(domainsLine, tt.unexpectedDomain) {
t.Errorf("Unexpected domain '%s' found in GH_AW_ALLOWED_DOMAINS.\nLine: %s", tt.unexpectedDomain, domainsLine)
}
}
})
}
}
// TestComputeAllowedDomainsForSanitization tests the computeAllowedDomainsForSanitization function
func TestComputeAllowedDomainsForSanitization(t *testing.T) {
tests := []struct {
name string
engineID string
apiTarget string
networkPerms *网络Permissions
expectedDomains []string
unexpectedDomains []string
}{
{
name: "Copilot with custom domains",
engineID: "copilot",
networkPerms: &网络Permissions{
Allowed: []string{"example.com", "test.org"},
},
expectedDomains: []string{
"example.com",
"test.org",
},
},
{
name: "Claude with custom domains",
engineID: "claude",
networkPerms: &网络Permissions{
Allowed: []string{"example.com", "test.org"},
},
expectedDomains: []string{
"example.com",
"test.org",
},
},
{
name: "Copilot with nil network",
engineID: "copilot",
networkPerms: nil,
expectedDomains: []string{},
},
{
name: "Claude with nil network",
engineID: "claude",
networkPerms: nil,
expectedDomains: []string{},
},
{
name: "Codex with custom domains",
engineID: "codex",
networkPerms: &网络Permissions{
Allowed: []string{"example.com"},
},
expectedDomains: []string{
"example.com",
},
},
{
name: "Copilot with GHES api-target includes api and base domains",
engineID: "copilot",
apiTarget: "api.acme.ghe.com",
networkPerms: nil,
expectedDomains: []string{
"api.acme.ghe.com", // GHES API domain
"acme.ghe.com", // GHES base domain (derived from api-target)
},
},
{
name: "non-api prefix api-target only adds the configured hostname",
engineID: "copilot",
apiTarget: "copilot.corp.example.com",
networkPerms: nil,
expectedDomains: []string{
"copilot.corp.example.com", // configured hostname
},
unexpectedDomains: []string{
"corp.example.com", // base hostname should NOT be added for non-api. prefix
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a compiler and workflow data
compiler := 新建Compiler()
data := &WorkflowData{
EngineConfig: &EngineConfig{
ID: tt.engineID,
APITarget: tt.apiTarget,
},
网络Permissions: tt.networkPerms,
}
// Call the function
domainsStr, err := compiler.computeAllowedDomainsForSanitization(data)
require.NoError(t, err, "computeAllowedDomainsForSanitization should not return an error for valid test data")
// Verify expected domains are present (substring match is fine here since domain names
// in a CSV string that are exact entries won't appear as substrings of other entries
// when checking expected ones – we only need exact match for the negative "not present" check)
for _, expectedDomain := range tt.expectedDomains {
if !strings.Contains(domainsStr, expectedDomain) {
t.Errorf("Expected domain '%s' not found in result: %s", expectedDomain, domainsStr)
}
}
// Verify unexpected domains are absent using exact membership (not substring)
// to avoid false positives where "corp.example.com" matches "copilot.corp.example.com"
parts := strings.Split(domainsStr, ",")
for _, unexpectedDomain := range tt.unexpectedDomains {
if slices.Contains(parts, unexpectedDomain) {
t.Errorf("Unexpected domain '%s' found in result: %s", unexpectedDomain, domainsStr)
}
}
})
}
}
// TestAPITargetDomainsInCompiledWorkflow is a regression test verifying that when engine.api-target
// is configured, both --allow-domains (AWF firewall flag) and GH_AW_ALLOWED_DOMAINS (sanitization
// env var) in the compiled lock file contain the api-target hostname and its derived base hostname.
func TestAPITargetDomainsInCompiledWorkflow(t *testing.T) {
tests := []struct {
name string
workflow string
expectedDomains []string
unexpectedDomains []string
}{
{
name: "GHES api-target adds api and base domains to allow-domains and GH_AW_ALLOWED_DOMAINS",
workflow: `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine:
id: copilot
api-target: api.acme.ghe.com
strict: false
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow with GHES api-target.
`,
expectedDomains: []string{
"api.acme.ghe.com", // GHES API domain
"acme.ghe.com", // GHES base domain derived from api-target
},
},
{
name: "non-api prefix api-target only adds the configured hostname",
workflow: `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine:
id: copilot
api-target: copilot.corp.example.com
strict: false
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow with non-api prefix api-target.
`,
expectedDomains: []string{
"copilot.corp.example.com", // configured hostname
},
unexpectedDomains: []string{
"corp.example.com", // base hostname should NOT be added for non-api. prefix
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := testutil.TempDir(t, "api-target-domains-test")
testFile := filepath.Join(tmpDir, "test-workflow.md")
if err := os.WriteFile(testFile, []byte(tt.workflow), 0644); err != nil {
t.Fatal(err)
}
compiler := 新建Compiler()
if err := compiler.CompileWorkflow(testFile); err != nil {
t.Fatalf("Failed to compile workflow: %v", err)
}
lockFile := stringutil.MarkdownToLockFile(testFile)
lockContent, err := os.ReadFile(lockFile)
if err != nil {
t.Fatalf("Failed to read lock file: %v", err)
}
lockStr := string(lockContent)
// Check allowDomains in AWF JSON config contains expected domains.
// 网络 settings are now expressed via --config JSON file instead of
// --allow-domains CLI flag (see BuildAWFConfigJSON).
// The JSON is shell-escaped in the lock file, so try both the unescaped
// ("allowDomains":[) and escaped (\"allowDomains\":[) forms.
allowDomainsPrefix := `"allowDomains":[`
allowDomainsPrefixEscaped := `\"allowDomains\":[`
allowDomainsIdx := strings.Index(lockStr, allowDomainsPrefix)
if allowDomainsIdx < 0 {
allowDomainsPrefix = allowDomainsPrefixEscaped
allowDomainsIdx = strings.Index(lockStr, allowDomainsPrefixEscaped)
}
if allowDomainsIdx < 0 {
t.Fatal("allowDomains key not found in compiled lock file")
}
// Extract the JSON array content for more targeted checking.
arrayStart := allowDomainsIdx + len(allowDomainsPrefix)
allowDomainsEnd := strings.Index(lockStr[arrayStart:], "]")
if allowDomainsEnd < 0 {
allowDomainsEnd = len(lockStr) - arrayStart
}
allowDomainsSection := lockStr[arrayStart : arrayStart+allowDomainsEnd]
// containsJSONDomain checks for a domain as a JSON string value, handling both
// escaped (\"domain\") and unescaped ("domain") forms in the lock file.
containsJSONDomain := func(section, domain string) bool {
return strings.Contains(section, `"`+domain+`"`) ||
strings.Contains(section, `\"`+domain+`\"`)
}
for _, domain := range tt.expectedDomains {
if !containsJSONDomain(allowDomainsSection, domain) {
t.Errorf("Expected domain %q not found in allowDomains.\nSection: %s", domain, allowDomainsSection)
}
}
// Use exact JSON string matching for "not present" checks to avoid false positives
// (e.g. "corp.example.com" would substring-match "copilot.corp.example.com").
for _, domain := range tt.unexpectedDomains {
if containsJSONDomain(allowDomainsSection, domain) {
t.Errorf("Unexpected domain %q found in allowDomains.\nSection: %s", domain, allowDomainsSection)
}
}
// Check GH_AW_ALLOWED_DOMAINS env var contains expected domains
lines := strings.Split(lockStr, "\n")
var domainsLine string
for _, line := range lines {
if strings.Contains(line, "GH_AW_ALLOWED_DOMAINS:") {
domainsLine = line
break
}
}
if domainsLine == "" {
t.Fatal("GH_AW_ALLOWED_DOMAINS not found in compiled lock file")
}
for _, domain := range tt.expectedDomains {
if !strings.Contains(domainsLine, domain) {
t.Errorf("Expected domain %q not found in GH_AW_ALLOWED_DOMAINS.\nLine: %s", domain, domainsLine)
}
}
// Use exact CSV membership for "not present" checks
allowedDomainsEnvCSV := extractQuotedCSV(domainsLine)
allowedEnvParts := strings.Split(allowedDomainsEnvCSV, ",")
for _, domain := range tt.unexpectedDomains {
if slices.Contains(allowedEnvParts, domain) {
t.Errorf("Unexpected domain %q found in GH_AW_ALLOWED_DOMAINS.\nLine: %s", domain, domainsLine)
}
}
})
}
}
// TestGitHubCopilotBaseURLInCompiledWorkflow verifies that when GITHUB_COPILOT_BASE_URL is set
// in engine.env (without an explicit engine.api-target), the compiled lock file contains
// --copilot-api-target and includes the extracted hostname in both --allow-domains and
// GH_AW_ALLOWED_DOMAINS — matching the OPENAI_BASE_URL/ANTHROPIC_BASE_URL pattern for other engines.
func TestGitHubCopilotBaseURLInCompiledWorkflow(t *testing.T) {
workflow := `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine:
id: copilot
env:
GITHUB_COPILOT_BASE_URL: "https://copilot-proxy.corp.example.com"
strict: false
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow with GITHUB_COPILOT_BASE_URL in engine.env.
`
tmpDir := testutil.TempDir(t, "copilot-base-url-test")
testFile := filepath.Join(tmpDir, "test-workflow.md")
if err := os.WriteFile(testFile, []byte(workflow), 0644); err != nil {
t.Fatal(err)
}
compiler := 新建Compiler()
if err := compiler.CompileWorkflow(testFile); err != nil {
t.Fatalf("Failed to compile workflow: %v", err)
}
lockFile := stringutil.MarkdownToLockFile(testFile)
lockContent, err := os.ReadFile(lockFile)
if err != nil {
t.Fatalf("Failed to read lock file: %v", err)
}
lockStr := string(lockContent)
// The copilot API target should be derived from the env var and present in the
// AWF JSON config (apiProxy.targets.copilot.host) rather than as a --copilot-api-target
// CLI flag, since network/proxy settings are now expressed via --config JSON file.
copilotHostUnescaped := `"copilot":{"host":"copilot-proxy.corp.example.com"}`
copilotHostEscaped := `\"copilot\":{\"host\":\"copilot-proxy.corp.example.com\"}`
if !strings.Contains(lockStr, copilotHostUnescaped) && !strings.Contains(lockStr, copilotHostEscaped) {
t.Error("Expected copilot API target to be derived from GITHUB_COPILOT_BASE_URL in AWF config JSON")
}
// Extracted hostname should appear in the allowDomains list inside the AWF JSON config.
// The AWF JSON config embeds the allowDomains array as a comma-separated list.
// We search for the allowDomains key followed by its opening "[" and verify the hostname
// appears before the closing "]" of that specific array.
allowDomainsPrefix := `"allowDomains":[`
allowDomainsPrefixEscaped := `\"allowDomains\":[`
allowDomainsIdx := strings.Index(lockStr, allowDomainsPrefix)
if allowDomainsIdx < 0 {
allowDomainsPrefix = allowDomainsPrefixEscaped
allowDomainsIdx = strings.Index(lockStr, allowDomainsPrefixEscaped)
}
if allowDomainsIdx < 0 {
t.Fatal("allowDomains key not found in compiled lock file")
}
// Start searching for "]" from after the opening "[".
arrayStart := allowDomainsIdx + len(allowDomainsPrefix)
allowDomainsEnd := strings.Index(lockStr[arrayStart:], "]")
if allowDomainsEnd < 0 {
allowDomainsEnd = len(lockStr) - arrayStart
}
allowDomainsSection := lockStr[arrayStart : arrayStart+allowDomainsEnd]
if !strings.Contains(allowDomainsSection, "copilot-proxy.corp.example.com") {
t.Errorf("Expected hostname from GITHUB_COPILOT_BASE_URL in allowDomains.\nSection: %s", allowDomainsSection)
}
// Extracted hostname should appear in GH_AW_ALLOWED_DOMAINS
lines := strings.Split(lockStr, "\n")
var domainsLine string
for _, line := range lines {
if strings.Contains(line, "GH_AW_ALLOWED_DOMAINS:") {
domainsLine = line
break
}
}
if domainsLine == "" {
t.Fatal("GH_AW_ALLOWED_DOMAINS not found in compiled lock file")
}
if !strings.Contains(domainsLine, "copilot-proxy.corp.example.com") {
t.Errorf("Expected hostname from GITHUB_COPILOT_BASE_URL in GH_AW_ALLOWED_DOMAINS.\nLine: %s", domainsLine)
}
}
// TestAPITargetDomainsInThreatDetectionStep is a regression test verifying that when engine.api-target
// is configured, the threat detection AWF invocation in the compiled lock file also receives
// --copilot-api-target and includes the GHE domains in its --allow-domains list.
// Regression test for: Threat detection AWF run missing --copilot-api-target on data residency.
func TestAPITargetDomainsInThreatDetectionStep(t *testing.T) {
workflow := `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine:
id: copilot
api-target: api.contoso-aw.ghe.com
strict: false
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow with GHE data residency api-target and threat detection.
`
tmpDir := testutil.TempDir(t, "api-target-threat-detection-test")
testFile := filepath.Join(tmpDir, "test-workflow.md")
if err := os.WriteFile(testFile, []byte(workflow), 0644); err != nil {
t.Fatal(err)
}
compiler := 新建Compiler()
if err := compiler.CompileWorkflow(testFile); err != nil {
t.Fatalf("Failed to compile workflow: %v", err)
}
lockFile := stringutil.MarkdownToLockFile(testFile)
lockContent, err := os.ReadFile(lockFile)
if err != nil {
t.Fatalf("Failed to read lock file: %v", err)
}
lockStr := string(lockContent)
// Verify copilot api-target appears at least twice in the AWF JSON config:
// once for the main agent AWF run and once for the threat detection AWF run.
// API proxy settings are now expressed via --config JSON file instead of
// --copilot-api-target CLI flag (see BuildAWFConfigJSON).
// The JSON is shell-escaped in the lock file, so try both unescaped and escaped forms.
apiTargetUnescaped := `"copilot":{"host":"api.contoso-aw.ghe.com"}`
apiTargetEscaped := `\"copilot\":{\"host\":\"api.contoso-aw.ghe.com\"}`
apiTargetCount := strings.Count(lockStr, apiTargetUnescaped)
if apiTargetCount == 0 {
apiTargetCount = strings.Count(lockStr, apiTargetEscaped)
}
if apiTargetCount < 2 {
t.Errorf("Expected copilot api-target to appear in both the main agent and threat detection AWF JSON configs (at least 2 times), but found %d occurrence(s).", apiTargetCount)
}
// Find all allowDomains occurrences in AWF JSON config and verify each contains the GHE domains.
// api.contoso-aw.ghe.com triggers base-domain derivation, so both the API domain
// and the base domain (contoso-aw.ghe.com) must appear in each AWF invocation.
// The JSON is shell-escaped in the lock file, so try both unescaped and escaped key forms.
requiredDomains := []string{"api.contoso-aw.ghe.com", "contoso-aw.ghe.com"}
allowDomainsPrefix := `"allowDomains":[`
allowDomainsPrefixEscaped := `\"allowDomains\":[`
// Use whichever prefix form is present in the lock file.
if strings.Index(lockStr, allowDomainsPrefix) < 0 {
allowDomainsPrefix = allowDomainsPrefixEscaped
}
remaining := lockStr
occurrenceIdx := 0
for {
idx := strings.Index(remaining, allowDomainsPrefix)
if idx < 0 {
break
}
occurrenceIdx++
arrayStart := idx + len(allowDomainsPrefix)
arrayEnd := strings.Index(remaining[arrayStart:], "]")
if arrayEnd < 0 {
arrayEnd = len(remaining) - arrayStart
}
section := remaining[arrayStart : arrayStart+arrayEnd]
for _, domain := range requiredDomains {
// Handle both escaped (\"domain\") and unescaped ("domain") forms.
if !strings.Contains(section, `"`+domain+`"`) && !strings.Contains(section, `\"`+domain+`\"`) {
t.Errorf("allowDomains occurrence #%d is missing GHE domain %q.\nSection: %s", occurrenceIdx, domain, section)
}
}
remaining = remaining[arrayStart+arrayEnd:]
}
if occurrenceIdx < 2 {
t.Errorf("Expected at least 2 allowDomains occurrences (main agent + threat detection), found %d", occurrenceIdx)
}
}
func TestCopilotProviderBaseURLInThreatDetectionStep(t *testing.T) {
workflow := `---
on: push
permissions:
contents: read
issues: read
pull-requests: read
engine:
id: copilot
env:
COPILOT_PROVIDER_BASE_URL: ${{ secrets.PROVIDER_BASE_URL }}
network:
allowed:
- defaults
- llm.corp.example.com
strict: false
safe-outputs:
create-issue:
---
# Test Workflow
Test workflow with COPILOT_PROVIDER_BASE_URL in engine.env and provider host in network.allowed.
`
tmpDir := testutil.TempDir(t, "copilot-provider-threat-detection-test")
testFile := filepath.Join(tmpDir, "test-workflow.md")
if err := os.WriteFile(testFile, []byte(workflow), 0644); err != nil {
t.Fatal(err)
}
compiler := 新建Compiler()
if err := compiler.CompileWorkflow(testFile); err != nil {
t.Fatalf("Failed to compile workflow: %v", err)
}
lockFile := stringutil.MarkdownToLockFile(testFile)
lockContent, err := os.ReadFile(lockFile)
if err != nil {
t.Fatalf("Failed to read lock file: %v", err)
}
lockStr := string(lockContent)
requiredDomain := "llm.corp.example.com"
allowDomainsPrefix := `"allowDomains":[`
allowDomainsPrefixEscaped := `\"allowDomains\":[`
if !strings.Contains(lockStr, allowDomainsPrefix) {
allowDomainsPrefix = allowDomainsPrefixEscaped
}
remaining := lockStr
occurrenceIdx := 0
for {
idx := strings.Index(remaining, allowDomainsPrefix)
if idx < 0 {
break
}
occurrenceIdx++
arrayStart := idx + len(allowDomainsPrefix)
arrayEnd := strings.Index(remaining[arrayStart:], "]")
if arrayEnd < 0 {
arrayEnd = len(remaining) - arrayStart
}
section := remaining[arrayStart : arrayStart+arrayEnd]
if !strings.Contains(section, `"`+requiredDomain+`"`) && !strings.Contains(section, `\"`+requiredDomain+`\"`) {
t.Errorf("allowDomains occurrence #%d is missing BYOK provider domain %q.\nSection: %s", occurrenceIdx, requiredDomain, section)
}
remaining = remaining[arrayStart+arrayEnd:]
}
if occurrenceIdx < 2 {
t.Errorf("Expected at least 2 allowDomains occurrences (main agent + threat detection), found %d", occurrenceIdx)
}
lines := strings.Split(lockStr, "\n")
var domainsLine string
for _, line := range lines {
if strings.Contains(line, "GH_AW_ALLOWED_DOMAINS:") {
domainsLine = line
break
}
}
if domainsLine == "" {
t.Fatal("GH_AW_ALLOWED_DOMAINS not found in compiled lock file")
}
if !strings.Contains(domainsLine, requiredDomain) {
t.Errorf("Expected BYOK provider hostname in GH_AW_ALLOWED_DOMAINS.\nLine: %s", domainsLine)
}
}
// TestAllowedDomainsUnionWith网络Config tests that safe-outputs.allowed-domains
// is unioned with network.allowed and always includes localhost and github.com
func TestAllowedDomainsUnionWith网络Config(t *testing.T) {
tests := []struct {
name string
workflow string
expectedDomains []string
}{
{
name: "allowed-domains unioned with network config",
workflow: `---
on: push
permissions:
contents: read
issues: read
engine: copilot
strict: false
network:
allowed:
- example.com
safe-outputs:
create-issue:
allowed-domains:
- extra-domain.com
---
# Test Workflow
Test allowed-domains union with network config.
`,
expectedDomains: []string{
"extra-domain.com", // from allowed-domains
"example.com", // from network.allowed
"localhost", // always included
"github.com", // always included
},
},
{
name: "allowed-domains supports ecosystem identifiers",
workflow: `---
on: push
permissions:
contents: read
issues: read
engine: copilot
strict: false
safe-outputs:
create-issue:
allowed-domains:
- dev-tools
- python
---
# Test Workflow
Test allowed-domains with ecosystem identifiers.
`,
expectedDomains: []string{
"codecov.io", // from dev-tools ecosystem
"snyk.io", // from dev-tools ecosystem
"pypi.org", // from python ecosystem
"localhost", // always included
"github.com", // always included
},
},
{
name: "allowed-domains does not override network config",
workflow: `---
on: push
permissions:
contents: read
issues: read
engine: copilot
strict: false
network:
allowed:
- network-domain.com
safe-outputs:
create-issue:
allowed-domains:
- url-domain.com
---