Repository navigation
Expand file tree
/
Copy pathmodelsource_wire_law_test.go
More file actions
128 lines (122 loc) · 4 KB
/
Copy pathmodelsource_wire_law_test.go
File metadata and controls
128 lines (122 loc) · 4 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
package remote
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"runtime"
"slices"
"sort"
"strings"
"testing"
)
var helloFields = map[string]bool{
"SessionInstance": true, "Headless": true, "Version": true, "Workspace": true, "Session": true, "Model": true,
"Level": true, "Launch": true, "Encodings": true, "Resume": true,
"Surface": true, "Back": true, "Join": true, "New": true, "Watch": true,
// ClientID is a random per-window id for redial focus (#1553), not a credential.
"ClientID": true,
// Window says the connection is a person's window, so the far machine holds
// automations presence for it (wire_automations.go). A boolean, not a credential.
"Window": true,
// LocalSavingReceipts is a boolean capability, with no key or address.
"LocalSavingReceipts": true,
// SyncNoticeWords is a boolean vocabulary capability, with no key or address.
"SyncNoticeWords": true,
}
// TestNoServiceKeyOrAddressCrossesTheWire refuses model-service credential
// fields throughout the remote stack. Generic pairing keys and relay addresses
// are different protocol facts and deliberately remain legal.
func TestNoServiceKeyOrAddressCrossesTheWire(t *testing.T) {
_, here, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("cannot locate remote wire law")
}
root := filepath.Clean(filepath.Join(filepath.Dir(here), "..", ".."))
forbidden := map[string]bool{
"APIKey": true, "BaseURL": true, "SourceKey": true, "SourceAddress": true,
"ServiceKey": true, "ServiceAddress": true, "ModelKey": true, "ModelAddress": true,
}
set := token.NewFileSet()
for _, directory := range []string{"internal/remote", "internal/enginehost", "internal/pair", "internal/relay"} {
err := filepath.WalkDir(filepath.Join(root, directory), func(path string, entry fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if entry.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return nil
}
file, err := parser.ParseFile(set, path, nil, 0)
if err != nil {
return err
}
ast.Inspect(file, func(node ast.Node) bool {
field, ok := node.(*ast.Field)
if !ok {
return true
}
for _, name := range field.Names {
if forbidden[name.Name] {
position := set.Position(name.Pos())
t.Errorf("%s:%d carries forbidden service credential field %s", filepath.ToSlash(path), position.Line, name.Name)
}
}
return true
})
return nil
})
if err != nil {
t.Fatal(err)
}
}
file, err := parser.ParseFile(set, filepath.Join(root, "internal", "remote", "wire.go"), nil, 0)
if err != nil {
t.Fatal(err)
}
if unexpected := unexpectedHelloFields(file); len(unexpected) > 0 {
t.Fatalf("remote.Hello gained fields outside its credential-reviewed allowlist: %s", strings.Join(unexpected, ", "))
}
}
func unexpectedHelloFields(file *ast.File) []string {
seen := make(map[string]bool, len(helloFields))
var unexpected []string
ast.Inspect(file, func(node ast.Node) bool {
typeSpec, ok := node.(*ast.TypeSpec)
if !ok || typeSpec.Name.Name != "Hello" {
return true
}
structure, ok := typeSpec.Type.(*ast.StructType)
if !ok {
unexpected = append(unexpected, "Hello is not a struct")
return false
}
for _, field := range structure.Fields.List {
for _, name := range field.Names {
seen[name.Name] = true
if !helloFields[name.Name] {
unexpected = append(unexpected, name.Name)
}
}
}
return false
})
for name := range helloFields {
if !seen[name] {
unexpected = append(unexpected, fmt.Sprintf("missing %s", name))
}
}
sort.Strings(unexpected)
return unexpected
}
func TestTheHelloAllowlistRejectsAnUnknownCredentialSpelling(t *testing.T) {
file, err := parser.ParseFile(token.NewFileSet(), "scratch.go", "package remote; type Hello struct { Version int; Credential string }", 0)
if err != nil {
t.Fatal(err)
}
unexpected := unexpectedHelloFields(file)
if !slices.Contains(unexpected, "Credential") {
t.Fatalf("unknown field escaped the allowlist: %v", unexpected)
}
}