Repository navigation
Expand file tree
/
Copy pathcontext_test.go
More file actions
107 lines (91 loc) · 3.61 KB
/
Copy pathcontext_test.go
File metadata and controls
107 lines (91 loc) · 3.61 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
// Root-level test (justified): exercises buildAllocatorOptions and flagTestType — the Chrome flags are built before launch and no public API returns them (guards the SPKI/test-type flags).
package devbrowser
import (
"reflect"
"testing"
"unsafe"
"webtyp.com/devbrowser/chromedp"
)
// The value a consumer actually passes comes from httpd.LocalCertSPKI(). This
// test uses a fixed 44-character base64 string of the same shape, asserts it
// reaches the allocator verbatim, and asserts the flag name is the SPKI list
// and not a blanket bypass.
func parseExecAllocatorOptions(opts []chromedp.ExecAllocatorOption) map[string]any {
alloc := &chromedp.ExecAllocator{}
// Initialize alloc.initFlags (unexported map[string]interface{})
v := reflect.ValueOf(alloc).Elem()
field := v.FieldByName("initFlags")
initFlags := make(map[string]interface{})
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Set(reflect.ValueOf(initFlags))
for _, opt := range opts {
opt(alloc)
}
flags := make(map[string]any)
for k, val := range initFlags {
flags[k] = val
}
return flags
}
func TestBuildAllocatorOptions_SPKI(t *testing.T) {
t.Run("empty TrustDevCertSPKI -> no spki flag", func(t *testing.T) {
b := &DevBrowser{}
opts := b.buildAllocatorOptions()
flags := parseExecAllocatorOptions(opts)
if val, exists := flags[FlagSPKIList]; exists {
t.Errorf("expected no %s flag, but found: %v", FlagSPKIList, val)
}
})
t.Run("set TrustDevCertSPKI -> exactly spki list flag with value", func(t *testing.T) {
testPin := "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="
b := &DevBrowser{
TrustDevCertSPKI: testPin,
}
opts := b.buildAllocatorOptions()
flags := parseExecAllocatorOptions(opts)
val, exists := flags[FlagSPKIList]
if !exists {
t.Fatalf("expected flag %s to be set, but it was missing", FlagSPKIList)
}
if val != testPin {
t.Errorf("expected flag %s value %q, got %q", FlagSPKIList, testPin, val)
}
})
t.Run("ResolveTrustDevCertSPKI overrides stale TrustDevCertSPKI", func(t *testing.T) {
const rotatedPin = "rotatedPinValue12345678901234567890123456="
b := &DevBrowser{
TrustDevCertSPKI: "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
ResolveTrustDevCertSPKI: func() (string, error) { return rotatedPin, nil },
}
flags := parseExecAllocatorOptions(b.buildAllocatorOptions())
if val := flags[FlagSPKIList]; val != rotatedPin {
t.Errorf("expected rotated SPKI %q, got %q", rotatedPin, val)
}
})
// The SPKI flag is "unsupported" in Chrome's eyes, so without test-type every
// dev window opens under a yellow "You are using an unsupported command-line
// flag … Stability and security will suffer." bar.
t.Run("set TrustDevCertSPKI -> test-type hides the unsupported-flag infobar", func(t *testing.T) {
b := &DevBrowser{TrustDevCertSPKI: "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="}
flags := parseExecAllocatorOptions(b.buildAllocatorOptions())
if val, exists := flags[flagTestType]; !exists || val != true {
t.Errorf("expected flag %s=true, got %v (present=%v)", flagTestType, val, exists)
}
})
t.Run("security regression guard -> no blanket bypass flags", func(t *testing.T) {
b := &DevBrowser{
TrustDevCertSPKI: "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
}
opts := b.buildAllocatorOptions()
flags := parseExecAllocatorOptions(opts)
forbiddenFlags := []string{
"ignore-certificate-errors",
"allow-insecure-localhost",
"disable-web-security",
}
for _, flag := range forbiddenFlags {
if val, exists := flags[flag]; exists {
t.Errorf("security regression: forbidden flag %q was set with value %v", flag, val)
}
}
})
}