Repository navigation
Expand file tree
/
Copy pathscope_validation.go
More file actions
74 lines (69 loc) · 2.27 KB
/
Copy pathscope_validation.go
File metadata and controls
74 lines (69 loc) · 2.27 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
package cortex
import (
"context"
"errors"
"fmt"
"strings"
"unicode/utf8"
)
var (
// ErrInvalidScope identifies an ambiguous or unsupported scope identity.
ErrInvalidScope = errors.New("cortex: invalid scope")
// ErrUnsupportedScopeDepth means storage cannot enforce every level.
ErrUnsupportedScopeDepth = errors.New("cortex: scope exceeds three supported levels")
)
// ValidateScope checks the identity the built-in stores can enforce. Keys are
// unique and ordered. Keys and values must be valid UTF-8; no Unicode
// normalization is applied. The canonical codec reserves slash and equals.
func ValidateScope(s Scope) error {
if s.IsZero() {
return ErrNoScope
}
if len(s.Levels) > maxScopeLevels {
return fmt.Errorf("%w: %w", ErrInvalidScope, ErrUnsupportedScopeDepth)
}
seen := make(map[string]bool, len(s.Levels))
for _, level := range s.Levels {
if !utf8.ValidString(level.Key) || !utf8.ValidString(level.Value) || strings.TrimSpace(level.Key) == "" || strings.TrimSpace(level.Value) == "" || strings.ContainsAny(level.Key+level.Value, "/=") || seen[level.Key] {
return ErrInvalidScope
}
seen[level.Key] = true
}
return nil
}
// RequireScope returns detached, validated request identity. Invalid attached
// scopes mask their parent and retain a typed error for boundary adapters.
func RequireScope(ctx context.Context) (Scope, error) {
s, ok := ctx.Value(scopeKey).(Scope)
if !ok {
return Scope{}, ErrNoScope
}
if err := ValidateScope(s); err != nil {
return Scope{}, err
}
return Scope{Levels: append([]Level(nil), s.Levels...)}, nil
}
// DecodeStoredScope checks that canonical and indexed identity agree. Overflow
// rows must be inventoried and migrated by an operator, never truncated on read.
func DecodeStoredScope(canonical string, indexed [3]string, hasExtra bool) (Scope, error) {
scope, err := ParseCanonical(canonical)
if err != nil {
return Scope{}, err
}
if err := ValidateScope(scope); err != nil {
return Scope{}, err
}
if hasExtra {
return Scope{}, fmt.Errorf("%w: %w", ErrInvalidScope, ErrUnsupportedScopeDepth)
}
for i, column := range indexed {
want := ""
if i < len(scope.Levels) {
want = scope.Levels[i].Key + "=" + scope.Levels[i].Value
}
if column != want {
return Scope{}, ErrInvalidScope
}
}
return scope, nil
}