Repository navigation
Expand file tree
/
Copy pathmodule_test.go
More file actions
129 lines (121 loc) · 5.05 KB
/
Copy pathmodule_test.go
File metadata and controls
129 lines (121 loc) · 5.05 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
package starbox
import (
"reflect"
"sort"
"testing"
"github.com/1set/starlet"
)
// TestModuleSetsGolden pins the predefined module sets so that upgrading
// starlet can never silently change what a script in a given set may load.
//
// This is the BOX-05 guard. The Safe set used to be defined by subtraction
// from starlet's full module set, so a new starlet module (e.g. net) slipped
// into Safe the moment the dependency was bumped. The sets are now explicit
// allowlists, and this test:
// 1. pins the exact contents of every predefined set;
// 2. proves, via starlet's own capability classification, that no Safe
// member can reach the network or filesystem (and no Network member the
// filesystem); and
// 3. fails closed - every starlet builtin module must be consciously
// classified, so a newly added one trips this test instead of silently
// entering Safe.
func TestModuleSetsGolden(t *testing.T) {
sorted := func(ss []string) []string {
out := append([]string(nil), ss...)
sort.Strings(out)
return out
}
// 1. Exact contents of each predefined set.
wantSafe := []string{
"atom", "base64", "csv", "go_idiomatic", "hashlib", "json", "math",
"random", "re", "regex", "serial", "stats", "string", "struct", "time",
}
wantNetwork := []string{
"atom", "base64", "csv", "go_idiomatic", "hashlib", "http", "json",
"log", "math", "net", "random", "re", "regex", "serial", "stats",
"string", "struct", "time",
}
for _, tc := range []struct {
name string
set ModuleSetName
want []string
}{
{"empty", EmptyModuleSet, nil},
{"safe", SafeModuleSet, wantSafe},
{"network", NetworkModuleSet, wantNetwork},
{"full", FullModuleSet, starlet.GetAllBuiltinModuleNames()},
} {
if got, want := sorted(moduleSets[tc.set]), sorted(tc.want); !reflect.DeepEqual(got, want) {
t.Errorf("%s set: want %v, got %v", tc.name, want, got)
}
}
// 2. Security invariant cross-checked against starlet's capability map:
// Safe must contain no network/filesystem module; Network no filesystem
// module. go_idiomatic carries CapLog|CapProcess and is a deliberate Safe
// member, so the line is drawn at network/filesystem, not "pure".
assertNoCap := func(set ModuleSetName, forbidden starlet.ModuleCapability) {
for _, m := range moduleSets[set] {
mc, ok := starlet.GetBuiltinModuleCapability(m)
if !ok {
t.Errorf("%s: module %q has no starlet capability classification", set, m)
continue
}
if mc.Intersects(forbidden) {
t.Errorf("%s: module %q carries forbidden capability %v", set, m, mc)
}
}
}
assertNoCap(SafeModuleSet, starlet.CapNetwork|starlet.CapFileSystem)
assertNoCap(NetworkModuleSet, starlet.CapFileSystem)
// 3. Fail-closed coverage: every starlet builtin module must be accounted
// for by an explicit classification - Safe, the network extras, or the
// full-only filesystem/process modules. A new (or removed) starlet module
// makes the union differ from the full set and trips this, forcing a
// conscious decision instead of a silent change to Safe.
fullOnly := []string{"file", "path", "runtime"}
classified := append(append(append([]string(nil), safeModuleNames...), networkExtraNames...), fullOnly...)
if got, want := sorted(classified), sorted(fullModuleNames); !reflect.DeepEqual(got, want) {
t.Errorf("module classification drifted from starlet's full set:\n classified: %v\n full: %v\n(classify the new/removed starlet module in module.go)", got, want)
}
}
// TestBuiltinModuleMembers covers the member-enumeration helper across the
// three loader shapes Starlet returns, plus the unknown-module path. It backs
// DescribeSurface (STAR-9 / BOX-09).
func TestBuiltinModuleMembers(t *testing.T) {
has := func(ss []string, want string) bool {
for _, s := range ss {
if s == want {
return true
}
}
return false
}
// Module shape: members come from the module value's attributes.
if m := builtinModuleMembers("math"); !has(m, "sqrt") || !has(m, "pi") {
t.Errorf("math members: %v", m)
}
// Flat shape: the top-level binding names (go_idiomatic).
if m := builtinModuleMembers("go_idiomatic"); !has(m, "sleep") || !has(m, "exit") {
t.Errorf("go_idiomatic members: %v", m)
}
// Single bare builtin: the binding name itself.
if m := builtinModuleMembers("struct"); !reflect.DeepEqual(m, []string{"struct"}) {
t.Errorf("struct members: want [struct], got %v", m)
}
// Unknown module: no loader, so nil members.
if m := builtinModuleMembers("nonexistent_xyz"); m != nil {
t.Errorf("unknown module members: want nil, got %v", m)
}
// builtinModuleGlobalNames returns the injected top-level names (the module
// name for a namespaced module, the flat bindings for go_idiomatic, nil for
// an unknown module).
if g := builtinModuleGlobalNames("math"); !reflect.DeepEqual(g, []string{"math"}) {
t.Errorf("math globals: want [math], got %v", g)
}
if g := builtinModuleGlobalNames("go_idiomatic"); !has(g, "sleep") {
t.Errorf("go_idiomatic globals missing sleep: %v", g)
}
if g := builtinModuleGlobalNames("nonexistent_xyz"); g != nil {
t.Errorf("unknown globals: want nil, got %v", g)
}
}