Repository navigation
Expand file tree
/
Copy pathaddon_host_policy_architecture_test.go
More file actions
533 lines (502 loc) · 20.4 KB
/
Copy pathaddon_host_policy_architecture_test.go
File metadata and controls
533 lines (502 loc) · 20.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
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
// SPDX-License-Identifier: MIT
package localai_test
import (
"encoding/json"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestHostPolicyArchitecture(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Host policy architecture")
}
var _ = Describe("generic host policy architecture", func() {
It("keeps official addon identities out of generic CLI and config policy code", func() {
// Tests may retain historical fixtures. Deployment policy is deliberately
// outside these packages; unrelated model fields (e.g. quantization) are not IDs.
ids := []string{"fine-tuning", "quantization", "sandbox", "office", "web", "telegram", "mail"}
for _, root := range []string{"core/cli", "core/config"} {
err := filepath.WalkDir(root, 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(token.NewFileSet(), path, nil, 0)
if err != nil {
return err
}
policyCode := strings.Contains(filepath.Base(path), "addon_host_") || path == "core/cli/run.go"
ast.Inspect(file, func(node ast.Node) bool {
literal, ok := node.(*ast.BasicLit)
if !ok || literal.Kind != token.STRING {
return true
}
value, err := strconv.Unquote(literal.Value)
Expect(err).NotTo(HaveOccurred())
for _, id := range ids {
Expect(value).NotTo(ContainSubstring(id+"="), "%s embeds an official grant", path)
if policyCode {
Expect(value).NotTo(Equal(id), "%s embeds an official addon identity", path)
}
}
Expect(value).NotTo(ContainSubstring("default:\"fine-tuning="), "%s embeds a CLI default", path)
return true
})
return nil
})
Expect(err).NotTo(HaveOccurred())
}
})
})
var _ = Describe("typed extension architecture", func() {
It("keeps provider and Home dispatch independent of official addon IDs", func() {
// Only generic live dispatch surfaces are guarded. Legacy URL redirects,
// pin migration, native Studio code, deployment policy, and tests are not
// live addon dispatch and deliberately do not belong to this list.
paths := []string{"core/services/addons/apps", "core/services/addons/gateway/binding.go", "core/services/studio/executionselection.go"}
for _, root := range paths {
Expect(filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return nil
}
f, err := parser.ParseFile(token.NewFileSet(), path, nil, 0)
if err != nil {
return err
}
ast.Inspect(f, func(n ast.Node) bool {
lit, ok := n.(*ast.BasicLit)
if !ok || lit.Kind != token.STRING {
return true
}
value, err := strconv.Unquote(lit.Value)
Expect(err).NotTo(HaveOccurred())
Expect(value).NotTo(BeElementOf("studio", "sandbox"), "%s hardcodes a product ID in generic dispatch", path)
return true
})
return nil
})).To(Succeed())
}
data, err := os.ReadFile("core/http/react-ui/src/addons/homeCommands.js")
Expect(err).NotTo(HaveOccurred())
for _, literal := range []string{"'studio'", `"studio"`, "'sandbox'", `"sandbox"`} {
Expect(string(data)).NotTo(ContainSubstring(literal))
}
})
})
var _ = Describe("provider-neutral host code", func() {
// officialIDs are the IDs of the add-ons the repository ships. Host code
// outside deployment policy must not name them as literals.
officialIDs := []string{
"face", "fine-tuning", "gdrive", "github", "gmail", "memory", "office", "quantization", "sandbox",
"slack", "studio", "tables", "talk", "telegram", "voice", "web",
}
// allowed maps "path: literal" to the reason that the literal is not an
// add-on identity. Keep every entry narrow and justified.
allowed := map[string]string{
"core/http/auth/models.go: github": "the host's own sign-in identity provider, not an add-on",
"core/http/auth/permissions.go: quantization": "built-in permission name for the host's native feature, not an add-on dispatch",
"core/http/auth/usage.go: web": "usage source label for a session-cookie request, not an add-on",
"core/services/studio/egress/registries.json: github": "egress preset id for well-known release hosts, not an add-on dispatch",
}
scan := func(root string) []string {
var hits []string
add := func(path, value string) {
for _, id := range officialIDs {
if value == id {
hits = append(hits, path+": "+id)
}
}
}
Expect(filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || strings.HasSuffix(path, "_test.go") {
return nil
}
switch {
case strings.HasSuffix(path, ".go"):
f, err := parser.ParseFile(token.NewFileSet(), path, nil, 0)
if err != nil {
return err
}
ast.Inspect(f, func(n ast.Node) bool {
if lit, ok := n.(*ast.BasicLit); ok && lit.Kind == token.STRING {
value, err := strconv.Unquote(lit.Value)
Expect(err).NotTo(HaveOccurred())
add(path, value)
}
return true
})
case strings.HasSuffix(path, ".json"):
data, err := os.ReadFile(path)
if err != nil {
return err
}
// The presets file allows comments and trailing commas, so read
// its quoted strings directly instead of parsing it.
for _, m := range quotedString.FindAllStringSubmatch(string(data), -1) {
add(path, m[1])
}
}
return nil
})).To(Succeed())
return hits
}
It("keeps official add-on IDs out of auth, connectors and egress presets", func() {
used := map[string]bool{}
for _, root := range []string{"core/http/auth", "core/services/studio/connectors", "core/services/studio/egress/registries.json"} {
for _, hit := range scan(root) {
if _, ok := allowed[hit]; ok {
used[hit] = true
continue
}
Fail("official add-on ID literal in host code: " + hit)
}
}
for entry := range allowed {
Expect(used).To(HaveKey(entry), "stale allowlist entry %q", entry)
}
})
})
var quotedString = regexp.MustCompile(`"([^"\\]*)"`)
// The OAuth logins of add-ons must work for any service, so nothing in the
// host or the contract may name one: only the add-on's manifest does. The
// files of the OAuth surface are found by these patterns, so a new OAuth file
// next to the others is checked without a change here. Each pattern must
// match a file: a pattern that matches none fails the guard, so a move is
// noticed. Test files are skipped.
var oauthGoGlobs = []string{
"pkg/addons/contract/*oauth*.go",
"core/services/studio/*oauth*.go",
"core/services/studio/connectorlogin*.go",
"core/services/studio/connectorcred*.go",
"core/services/studio/*/*oauth*.go",
"core/services/studio/oauthtoken/*.go",
"core/services/addons/gateway/*oauth*.go",
"core/http/endpoints/studio/*oauth*.go",
// The application wiring that reads the descriptor and answers calls.
"core/application/studio_connectors*.go",
"core/application/addon_gateway*.go",
}
var oauthUIGlobs = []string{
"core/http/react-ui/src/*/*OAuth*.js",
"core/http/react-ui/src/*/*/*OAuth*.jsx",
"core/http/react-ui/src/components/connectors/*.jsx",
"core/http/react-ui/src/pages/ChatConnectors*.jsx",
}
// oauthSurface lists the files the patterns match, without tests. A pattern
// that matches nothing is returned in missing.
func oauthSurface(globs []string) (files, missing []string) {
seen := map[string]bool{}
for _, g := range globs {
matches, err := filepath.Glob(g)
n := 0
for _, m := range matches {
if strings.HasSuffix(m, "_test.go") || strings.HasSuffix(m, ".test.mjs") {
continue
}
n++
if !seen[m] {
seen[m] = true
files = append(files, m)
}
}
if err != nil || n == 0 {
missing = append(missing, g)
}
}
return files, missing
}
// oauthLocaleFiles are the namespaces whose OAuth strings are checked, in
// every locale. Only the strings under a key that names OAuth (oauth,
// oauthClient) and the reconnect strings of the Tools menu are read: the rest
// of a namespace belongs to other features.
var oauthLocaleFiles = []string{"addons.json", "chats.json"}
// oauthDenied are names and host names of identity and cloud services, lower
// case. The list is test data: production code must not need it, and it is
// long on purpose. A name matches as a whole word, so "aws" does not match
// "draws"; a host name matches anywhere it is not part of a longer name.
var oauthDenied = []string{
// identity providers and single sign-on
"auth0", "okta", "onelogin", "jumpcloud", "keycloak", "fusionauth", "zitadel", "authentik", "ory hydra",
"pingidentity", "ping identity", "duo security", "cognito", "entra", "azure ad", "azuread", "adfs",
"clerk.com", "workos", "stytch", "descope", "frontegg", "supabase", "firebase", "gotrue",
// large platforms
"google", "gmail", "youtube", "microsoft", "azure", "outlook", "hotmail", "office365", "onedrive", "sharepoint",
"apple", "icloud", "amazon", "aws", "facebook", "instagram", "whatsapp", "twitter", "linkedin",
"yahoo", "yandex", "baidu", "alibaba", "aliyun", "tencent", "wechat", "naver", "kakao",
// developer and work tools
"github", "gitlab", "bitbucket", "atlassian", "jira", "confluence", "slack", "discord", "telegram",
"notion", "dropbox", "box.com", "salesforce", "hubspot", "zendesk", "freshdesk", "servicenow", "workday",
"asana", "trello", "figma", "miro", "clickup", "linear.app", "zoom", "webex", "spotify", "twitch", "reddit",
"paypal", "stripe", "shopify", "square", "xero", "quickbooks", "intuit", "docusign", "adobe", "mailchimp",
"twilio", "sendgrid", "zoho", "heroku", "vercel", "netlify", "cloudflare", "digitalocean", "oracle", "ibm",
// host names
"accounts.google.com", "oauth2.googleapis.com", "googleapis.com", "login.microsoftonline.com", "login.live.com",
"graph.microsoft.com", "appleid.apple.com", "github.com", "api.github.com", "gitlab.com", "bitbucket.org",
"slack.com", "discord.com", "facebook.com", "graph.facebook.com", "api.twitter.com", "x.com", "linkedin.com",
"okta.com", "oktapreview.com", "auth0.com", "amazoncognito.com", "amazonaws.com", "dropboxapi.com",
"login.salesforce.com", "atlassian.com", "auth.atlassian.com", "api.notion.com", "zoom.us", "yandex.ru",
"login.yahoo.com", "spotify.com", "id.twitch.tv",
}
// oauthReviewedParams are the authorize parameters a review accepted. A name
// added to the registry of the contract must be added here in the same
// reviewed change, with a generic reason: none may serve one service only.
var oauthReviewedParams = []string{
"prompt", // OpenID Connect: the consent or login prompt
"audience", // the API the token is for
"resource", // RFC 8707 resource indicator
"access_type", // asks for a refresh token (value offline only)
"include_granted_scopes", // incremental consent (value true only)
}
// oauthDeniedImports are import path prefixes of OAuth libraries and service
// SDKs. The host has its own small token client; a library would keep the
// response body in its errors, and an SDK would tie the host to a service.
var oauthDeniedImports = []string{
"golang.org/x/oauth2", "github.com/coreos/go-oidc", "github.com/zitadel/oidc", "github.com/markbates/goth",
"github.com/dghubble/", "cloud.google.com/", "google.golang.org/api", "github.com/aws/", "github.com/Azure/",
"github.com/AzureAD/", "github.com/microsoft/", "github.com/okta/", "github.com/auth0/", "github.com/google/go-github",
"github.com/slack-go/", "github.com/bwmarrin/discordgo", "github.com/huandu/facebook", "github.com/dropbox/",
"github.com/stripe/", "github.com/salesforce",
}
var oauthDeniedPattern = func() *regexp.Regexp {
quoted := make([]string, len(oauthDenied))
for i, d := range oauthDenied {
quoted[i] = regexp.QuoteMeta(d)
}
return regexp.MustCompile(`(?:^|[^a-z0-9-])(` + strings.Join(quoted, "|") + `)(?:$|[^a-z0-9-])`)
}()
// oauthNamesService returns the denied names the text holds.
func oauthNamesService(text string) []string {
var out []string
for _, m := range oauthDeniedPattern.FindAllStringSubmatch(strings.ToLower(text), -1) {
out = append(out, m[1])
}
return out
}
// oauthGoViolations checks one Go source: its string literals, its imports
// and, in the contract, the registry of authorize parameters.
func oauthGoViolations(path string, src []byte) []string {
f, err := parser.ParseFile(token.NewFileSet(), path, src, parser.SkipObjectResolution)
if err != nil {
return []string{path + ": does not parse: " + err.Error()}
}
var out []string
for _, imp := range f.Imports {
p, _ := strconv.Unquote(imp.Path.Value)
for _, denied := range oauthDeniedImports {
if strings.HasPrefix(p, denied) {
out = append(out, path+": imports "+p)
}
}
}
ast.Inspect(f, func(n ast.Node) bool {
if _, ok := n.(*ast.ImportSpec); ok {
// Import paths are checked above; the module path names a code host.
return false
}
if lit, ok := n.(*ast.BasicLit); ok && lit.Kind == token.STRING {
value, err := strconv.Unquote(lit.Value)
if err != nil {
value = lit.Value
}
for _, name := range oauthNamesService(value) {
out = append(out, path+": a string literal names "+name)
}
}
return true
})
if names, found := oauthParamRegistry(f); found {
for _, name := range names {
if !slices.Contains(oauthReviewedParams, name) {
out = append(out, path+": the authorize parameter "+name+" is not on the reviewed list")
}
}
}
return out
}
// oauthParamRegistry returns the names of the variable OAuthAuthorizeParams,
// and whether the file declares it.
func oauthParamRegistry(f *ast.File) ([]string, bool) {
var names []string
found := false
ast.Inspect(f, func(n ast.Node) bool {
spec, ok := n.(*ast.ValueSpec)
if !ok || len(spec.Names) != 1 || spec.Names[0].Name != "OAuthAuthorizeParams" || len(spec.Values) != 1 {
return true
}
found = true
list, ok := spec.Values[0].(*ast.CompositeLit)
if !ok {
names = append(names, "<not a literal list>")
return false
}
for _, elt := range list.Elts {
entry, ok := elt.(*ast.CompositeLit)
if !ok {
names = append(names, "<not a literal entry>")
continue
}
name := "<no literal name>"
for _, field := range entry.Elts {
kv, ok := field.(*ast.KeyValueExpr)
if key, isIdent := kv.Key.(*ast.Ident); !ok || !isIdent || key.Name != "Name" {
continue
}
if lit, ok := kv.Value.(*ast.BasicLit); ok && lit.Kind == token.STRING {
name, _ = strconv.Unquote(lit.Value)
}
}
names = append(names, name)
}
return false
})
return names, found
}
// oauthUIViolations checks one React source as text, comments included: JSX
// text is not a string literal, and a service named in a comment is a hint
// that the code serves it.
func oauthUIViolations(path string, src []byte) []string {
var out []string
for _, name := range oauthNamesService(string(src)) {
out = append(out, path+": names "+name)
}
return out
}
// oauthLocaleStrings returns the OAuth strings of one locale namespace by
// key path.
func oauthLocaleStrings(src []byte) (map[string]string, error) {
var doc map[string]any
if err := json.Unmarshal(src, &doc); err != nil {
return nil, err
}
out := map[string]string{}
var walk func(key string, v any, inOAuth bool)
walk = func(key string, v any, inOAuth bool) {
switch t := v.(type) {
case map[string]any:
for k, child := range t {
walk(key+"."+k, child, inOAuth || strings.Contains(strings.ToLower(k), "oauth"))
}
case string:
last := key[strings.LastIndex(key, ".")+1:]
if inOAuth || strings.HasPrefix(last, "reconnect") {
out[key] = t
}
}
}
walk("", doc, false)
return out, nil
}
// oauthLocaleViolations checks the OAuth strings of one locale namespace.
func oauthLocaleViolations(path string, src []byte) []string {
texts, err := oauthLocaleStrings(src)
if err != nil {
return []string{path + ": does not parse: " + err.Error()}
}
var out []string
for key, text := range texts {
for _, name := range oauthNamesService(text) {
out = append(out, path+": "+key+" names "+name)
}
}
return out
}
// oauthSurfaceViolations reads every file of the OAuth surface.
func oauthSurfaceViolations() []string {
var out []string
read := func(path string, check func(string, []byte) []string) {
src, err := os.ReadFile(path) // #nosec G304 -- the guard reads files of this repository
if err != nil {
out = append(out, path+": "+err.Error())
return
}
out = append(out, check(path, src)...)
}
goFiles, missing := oauthSurface(oauthGoGlobs)
uiFiles, missingUI := oauthSurface(oauthUIGlobs)
for _, g := range append(missing, missingUI...) {
out = append(out, g+": matches no file of the OAuth surface")
}
for _, path := range goFiles {
read(path, oauthGoViolations)
}
for _, path := range uiFiles {
read(path, oauthUIViolations)
}
locales, err := os.ReadDir("core/http/react-ui/public/locales")
if err != nil || len(locales) == 0 {
return append(out, "no locales to check")
}
for _, l := range locales {
for _, ns := range oauthLocaleFiles {
read(filepath.Join("core/http/react-ui/public/locales", l.Name(), ns), oauthLocaleViolations)
}
}
return out
}
var _ = Describe("OAuth logins that name no service", func() {
It("keeps service names and hosts, unreviewed authorize parameters and OAuth libraries out of the host", func() {
Expect(oauthSurfaceViolations()).To(BeEmpty())
src, err := os.ReadFile("pkg/addons/contract/oauth.go")
Expect(err).NotTo(HaveOccurred())
f, err := parser.ParseFile(token.NewFileSet(), "oauth.go", src, 0)
Expect(err).NotTo(HaveOccurred())
names, found := oauthParamRegistry(f)
Expect(found).To(BeTrue(), "the guard reads the registry of authorize parameters")
Expect(names).To(ConsistOf(oauthReviewedParams), "the registry and the reviewed list change together")
for _, ns := range oauthLocaleFiles {
src, err := os.ReadFile(filepath.Join("core/http/react-ui/public/locales/en", ns))
Expect(err).NotTo(HaveOccurred())
texts, err := oauthLocaleStrings(src)
Expect(err).NotTo(HaveOccurred())
Expect(len(texts)).To(BeNumerically(">=", 10), "the guard finds the OAuth strings of %s", ns)
}
})
It("finds the OAuth files by pattern, the application wiring and the Connectors page included, and fails on a pattern that matches nothing", func() {
goFiles, missing := oauthSurface(oauthGoGlobs)
Expect(missing).To(BeEmpty())
Expect(goFiles).To(ContainElements("core/application/studio_connectors.go", "core/application/addon_gateway.go",
"core/services/studio/connectorlogin.go", "core/services/studio/connectors/gorm_oauth.go", "pkg/addons/contract/oauth.go"))
Expect(goFiles).NotTo(ContainElement(HaveSuffix("_test.go")))
uiFiles, missing := oauthSurface(oauthUIGlobs)
Expect(missing).To(BeEmpty())
Expect(uiFiles).To(ContainElements("core/http/react-ui/src/pages/ChatConnectors.jsx", "core/http/react-ui/src/utils/connectorOAuth.js",
"core/http/react-ui/src/components/connectors/OAuthConnection.jsx", "core/http/react-ui/src/components/hostcontrols/OAuthClientSettings.jsx"))
_, missing = oauthSurface([]string{"core/services/studio/moved_oauth_*.go"})
Expect(missing).To(ConsistOf("core/services/studio/moved_oauth_*.go"))
})
It("fails on a service named in code, a locale or the UI, an unreviewed parameter and an OAuth library", func() {
goSrc := []byte(`package contract
import "golang.org/x/oauth2"
var OAuthAuthorizeParams = []OAuthParamSpec{{Name: "prompt"}, {Name: "hd"}}
const authorize = "https://accounts.google.com/o/oauth2/auth"
var _ = oauth2.Config{}
`)
Expect(oauthGoViolations("synthetic.go", goSrc)).To(ConsistOf(
"synthetic.go: imports golang.org/x/oauth2",
"synthetic.go: a string literal names accounts.google.com",
"synthetic.go: the authorize parameter hd is not on the reviewed list",
))
Expect(oauthGoViolations("generic.go", []byte("package x\n\n// Not GitHub: comments may explain.\nconst host = \"login.provider-a.example.test\"\nconst s = \"draws on the store\"\n"))).To(BeEmpty())
Expect(oauthUIViolations("synthetic.jsx", []byte(`export const Hint = () => <p>Sign in with GitHub</p>`))).To(ConsistOf("synthetic.jsx: names github"))
Expect(oauthLocaleViolations("synthetic.json", []byte(`{"tools":{"reconnect":"Reconnect to Okta"},"connectors":{"oauth":{"x":"Connected as {{account}}"},"title":"Telegram"}}`))).
To(ConsistOf("synthetic.json: .tools.reconnect names okta"))
})
})