-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
permissions.go
592 lines (475 loc) · 14.1 KB
/
permissions.go
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
package core
import (
"bytes"
"errors"
"fmt"
"net/url"
"strings"
permkind "github.com/inoxlang/inox/internal/core/permkind"
)
var (
ErrImpossibleToVerifyPermissionForUrlHolderMutation = errors.New("impossible to verify permission for mutation of URL holder")
)
type PermissionKind = permkind.PermissionKind
// A Permission carries a kind and can include narrower permissions, for example
// (read http://**) includes (read https://example.com).
type Permission interface {
Kind() PermissionKind
InternalPermTypename() permkind.InternalPermissionTypename
Includes(Permission) bool
String() string
}
type NotAllowedError struct {
Permission Permission
Message string
}
func NewNotAllowedError(perm Permission) *NotAllowedError {
return &NotAllowedError{
Permission: perm,
Message: fmt.Sprintf("not allowed, missing permission: %s", perm.String()),
}
}
func (err NotAllowedError) Error() string {
return err.Message
}
func (err NotAllowedError) Is(target error) bool {
notAllowedErr, ok := target.(*NotAllowedError)
if !ok {
return false
}
return err.Permission.Includes(notAllowedErr.Permission) && notAllowedErr.Permission.Includes(err.Permission) &&
err.Message == notAllowedErr.Message
}
type GlobalVarPermission struct {
Kind_ PermissionKind
Name string //"*" means any
}
func (perm GlobalVarPermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm GlobalVarPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.GLOBAL_VAR_PERM_TYPENAME
}
func (perm GlobalVarPermission) Includes(otherPerm Permission) bool {
otherGlobVarPerm, ok := otherPerm.(GlobalVarPermission)
if !ok || !perm.Kind().Includes(otherGlobVarPerm.Kind()) {
return false
}
return perm.Name == "*" || perm.Name == otherGlobVarPerm.Name
}
func (perm GlobalVarPermission) String() string {
return fmt.Sprintf("[%s global(s) '%s']", perm.Kind_, perm.Name)
}
//
type EnvVarPermission struct {
Kind_ PermissionKind
Name string //"*" means any
}
func (perm EnvVarPermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm EnvVarPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.ENV_PERM_TYPENAME
}
func (perm EnvVarPermission) Includes(otherPerm Permission) bool {
otherEnvVarPerm, ok := otherPerm.(EnvVarPermission)
if !ok || !perm.Kind().Includes(otherEnvVarPerm.Kind()) {
return false
}
return perm.Name == "*" || perm.Name == otherEnvVarPerm.Name
}
func (perm EnvVarPermission) String() string {
return fmt.Sprintf("[%s env '%s']", perm.Kind_, perm.Name)
}
//
type LThreadPermission struct {
Kind_ PermissionKind
}
func (perm LThreadPermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm LThreadPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.LTHREAD_PERM_TYPENAME
}
func (perm LThreadPermission) Includes(otherPerm Permission) bool {
otherLThreadPerm, ok := otherPerm.(LThreadPermission)
return ok && perm.Kind_.Includes(otherLThreadPerm.Kind_)
}
func (perm LThreadPermission) String() string {
return fmt.Sprintf("[%s threads]", perm.Kind_)
}
type FilesystemPermission struct {
Kind_ PermissionKind
Entity WrappedString //Path, PathPattern ...
}
func CreateFsReadPerm(entity WrappedString) FilesystemPermission {
return FilesystemPermission{Kind_: permkind.Read, Entity: entity}
}
func (perm FilesystemPermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm FilesystemPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.FS_PERM_TYPENAME
}
func (perm FilesystemPermission) Includes(otherPerm Permission) bool {
otherFsPerm, ok := otherPerm.(FilesystemPermission)
if !ok || !perm.Kind_.Includes(otherFsPerm.Kind_) {
return false
}
switch e := perm.Entity.(type) {
case Path:
otherPath, ok := otherFsPerm.Entity.(Path)
return ok && e == otherPath
case PathPattern:
return e.Includes(nil, otherFsPerm.Entity)
}
return false
}
func (perm FilesystemPermission) String() string {
return fmt.Sprintf("[%s path(s) %s]", perm.Kind_, perm.Entity)
}
type CommandPermission struct {
CommandName WrappedString //string or Path or PathPattern
SubcommandNameChain []string //can be empty
}
func (perm CommandPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.LTHREAD_PERM_TYPENAME
}
func (perm CommandPermission) Kind() PermissionKind {
return permkind.Use
}
func (perm CommandPermission) Includes(otherPerm Permission) bool {
otherCmdPerm, ok := otherPerm.(CommandPermission)
if !ok || !perm.Kind().Includes(otherCmdPerm.Kind()) {
return false
}
switch cmdName := perm.CommandName.(type) {
case String:
otherCommandName, ok := otherCmdPerm.CommandName.(String)
if !ok || otherCommandName != cmdName {
return false
}
case Path:
otherCommandName, ok := otherCmdPerm.CommandName.(Path)
if !ok || otherCommandName != cmdName {
return false
}
case PathPattern:
switch otherCmdPerm.CommandName.(type) {
case Path, PathPattern:
if !cmdName.Includes(nil, otherCmdPerm.CommandName) {
return false
}
default:
return false
}
default:
return false
}
if len(otherCmdPerm.SubcommandNameChain) != len(perm.SubcommandNameChain) {
return false
}
for i, name := range perm.SubcommandNameChain {
if otherCmdPerm.SubcommandNameChain[i] != name {
return false
}
}
return true
}
func (perm CommandPermission) String() string {
b := bytes.NewBufferString("[exec command:")
b.WriteString(fmt.Sprint(perm.CommandName))
if len(perm.SubcommandNameChain) == 0 {
b.WriteString(" <no subcommand>")
}
for _, name := range perm.SubcommandNameChain {
b.WriteString(" ")
b.WriteString(name)
}
b.WriteString("]")
return b.String()
}
type HttpPermission struct {
Kind_ PermissionKind
Entity WrappedString //URL, URLPattern, HTTPHost, HTTPHostPattern ....
AnyEntity bool
}
func CreateHttpReadPerm(entity WrappedString) HttpPermission {
return HttpPermission{Kind_: permkind.Read, Entity: entity}
}
func (perm HttpPermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm HttpPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.HTTP_PERM_TYPENAME
}
func (perm HttpPermission) Includes(otherPerm Permission) bool {
otherHttpPerm, ok := otherPerm.(HttpPermission)
if !ok || !perm.Kind_.Includes(otherHttpPerm.Kind_) {
return false
}
if perm.AnyEntity {
return true
}
if otherHttpPerm.AnyEntity {
return false
}
switch e := perm.Entity.(type) {
case URL:
otherURL, ok := otherHttpPerm.Entity.(URL)
parsedURL, _ := url.Parse(string(e))
if parsedURL.RawQuery == "" {
parsedURL.ForceQuery = false
otherParsedURL, _ := url.Parse(string(otherURL))
otherParsedURL.RawQuery = ""
otherParsedURL.ForceQuery = false
return parsedURL.String() == otherParsedURL.String()
}
return ok && e == otherURL
case URLPattern:
otherURLPattern, ok := otherHttpPerm.Entity.(URLPattern)
if ok && e.IsPrefixPattern() && otherURLPattern.IsPrefixPattern() &&
strings.HasPrefix(strings.ReplaceAll(string(e), "/...", "/"), strings.ReplaceAll(string(otherURLPattern), "/...", "/")) {
return true
}
return e.Includes(nil, otherHttpPerm.Entity)
case Host:
host := e.WithoutScheme()
switch other := otherHttpPerm.Entity.(type) {
case URL:
otherURL, err := url.Parse(string(other))
if err != nil {
return false
}
return otherURL.Scheme == string(e.Scheme()) && otherURL.Host == host
case URLPattern:
otherURL, err := url.Parse(string(other))
if err != nil {
return false
}
return otherURL.Scheme == string(e.Scheme()) && otherURL.Host == host
case Host:
return e == other
}
case HostPattern:
return e.Includes(nil, otherHttpPerm.Entity)
}
return false
}
func (perm HttpPermission) String() string {
if perm.AnyEntity {
return fmt.Sprintf("[%s https://**:*]", perm.Kind_)
}
return fmt.Sprintf("[%s %s]", perm.Kind_, perm.Entity)
}
type DatabasePermission struct {
Kind_ PermissionKind
Entity WrappedString
}
func (perm DatabasePermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm DatabasePermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.FS_PERM_TYPENAME
}
func (perm DatabasePermission) Includes(otherPerm Permission) bool {
otherDbPerm, ok := otherPerm.(DatabasePermission)
if !ok || !perm.Kind_.Includes(otherDbPerm.Kind_) {
return false
}
switch e := perm.Entity.(type) {
case URL:
otherURL, ok := otherDbPerm.Entity.(URL)
parsedURL, _ := url.Parse(string(e))
if parsedURL.RawQuery != "" || otherURL.RawQuery() != "" {
panic(ErrUnreachable)
}
return ok && e == otherURL
case URLPattern:
return e.Includes(nil, otherDbPerm.Entity)
case Host:
host := e.WithoutScheme()
switch other := otherDbPerm.Entity.(type) {
case URL:
otherURL, err := url.Parse(string(other))
if err != nil {
return false
}
return otherURL.Scheme == string(e.Scheme()) && otherURL.Host == host
case URLPattern:
otherURL, err := url.Parse(string(other))
if err != nil {
return false
}
return otherURL.Scheme == string(e.Scheme()) && otherURL.Host == host
case Host:
return e == other
}
case HostPattern:
return e.Includes(nil, otherDbPerm.Entity)
}
return false
}
func (perm DatabasePermission) String() string {
return fmt.Sprintf("[%s %s]", perm.Kind_, perm.Entity)
}
type WebsocketPermission struct {
Kind_ PermissionKind
Endpoint ResourceName //ignored for some permission kinds
}
func (perm WebsocketPermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm WebsocketPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.WEBSOCKET_PERM_TYPENAME
}
func (perm WebsocketPermission) String() string {
if perm.Endpoint == nil {
return fmt.Sprintf("[websocket %s]", perm.Kind_)
}
return fmt.Sprintf("[websocket %s %s]", perm.Kind_, perm.Endpoint)
}
func (perm WebsocketPermission) Includes(otherPerm Permission) bool {
otherWsPerm, ok := otherPerm.(WebsocketPermission)
if !ok || !perm.Kind_.Includes(otherWsPerm.Kind_) {
return false
}
if perm.Kind_ == permkind.Provide {
return true
}
if perm.Endpoint == otherWsPerm.Endpoint {
return true
}
switch endpoint := perm.Endpoint.(type) {
case Host:
switch otherEndpoint := otherWsPerm.Endpoint.(type) {
case URL:
return otherEndpoint.Host() == endpoint
}
}
return false
}
type DNSPermission struct {
Kind_ PermissionKind
Domain WrappedString //Host | HostPattern
}
func (perm DNSPermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm DNSPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.DNS_PERM_TYPENAME
}
func (perm DNSPermission) String() string {
return fmt.Sprintf("[dns %s %s]", perm.Kind_, perm.Domain)
}
func (perm DNSPermission) Includes(otherPerm Permission) bool {
otherDnsPerm, ok := otherPerm.(DNSPermission)
if !ok || !perm.Kind_.Includes(otherDnsPerm.Kind_) {
return false
}
switch domain := perm.Domain.(type) {
case HostPattern:
switch otherDomain := otherDnsPerm.Domain.(type) {
case Host:
return domain.Test(nil, otherDomain)
case HostPattern:
return domain.includesPattern(otherDomain)
}
case Host:
switch otherDomain := otherDnsPerm.Domain.(type) {
case Host:
return domain == otherDomain
case HostPattern:
return false
}
}
return false
}
//----------------------------------------------------------------------
type RawTcpPermission struct {
Kind_ PermissionKind
Domain WrappedString //Host | HostPattern
}
func (perm RawTcpPermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm RawTcpPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.TCP_PERM_TYPENAME
}
func (perm RawTcpPermission) String() string {
return fmt.Sprintf("[tcp %s %s]", perm.Kind_, perm.Domain)
}
func (perm RawTcpPermission) Includes(otherPerm Permission) bool {
otherTcpPerm, ok := otherPerm.(RawTcpPermission)
if !ok || !perm.Kind().Includes(otherTcpPerm.Kind_) {
return false
}
switch domain := perm.Domain.(type) {
case HostPattern:
switch otherDomain := otherTcpPerm.Domain.(type) {
case Host:
return domain.Includes(nil, otherDomain)
case HostPattern:
return domain.includesPattern(otherDomain)
}
case Host:
switch otherDomain := otherTcpPerm.Domain.(type) {
case Host:
return domain == otherDomain
case HostPattern:
return false
}
}
return false
}
type ValueVisibilityPermission struct {
Pattern Pattern
}
func (perm ValueVisibilityPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.VALUE_VISIBILITY_PERM_TYPENAME
}
func (perm ValueVisibilityPermission) Kind() PermissionKind {
return permkind.See
}
func (perm ValueVisibilityPermission) String() string {
return fmt.Sprintf("[see value matching %s]", perm.Pattern)
}
func (perm ValueVisibilityPermission) Includes(otherPerm Permission) bool {
otherVisibilityPerm, ok := otherPerm.(ValueVisibilityPermission)
if !ok || !perm.Kind().Includes(otherPerm.Kind()) {
return false
}
//TODO: support all patterns
if exact, ok := otherVisibilityPerm.Pattern.(*ExactValuePattern); ok {
return perm.Pattern.Test(nil, exact.value)
}
return perm.Pattern.Equal(nil, otherVisibilityPerm.Pattern, map[uintptr]uintptr{}, 0)
}
type SystemGraphAccessPermission struct {
Kind_ PermissionKind
}
func (perm SystemGraphAccessPermission) InternalPermTypename() permkind.InternalPermissionTypename {
return permkind.SYSGRAPH_PERM_TYPENAME
}
func (perm SystemGraphAccessPermission) Kind() PermissionKind {
return perm.Kind_
}
func (perm SystemGraphAccessPermission) String() string {
return fmt.Sprintf("[%s system graph]", perm.Kind_.String())
}
func (perm SystemGraphAccessPermission) Includes(otherPerm Permission) bool {
otherSysGraphPerm, ok := otherPerm.(SystemGraphAccessPermission)
return ok && perm.Kind_.Includes(otherSysGraphPerm.Kind_)
}
func RemovePerms(grantedPerms, removedPerms []Permission) (remainingPerms []Permission) {
top:
for _, perm := range grantedPerms {
for _, removedPerm := range removedPerms {
if removedPerm.Includes(perm) {
continue top
}
}
remainingPerms = append(remainingPerms, perm)
}
return
}