/
validator.go
251 lines (227 loc) · 7.95 KB
/
validator.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Based on github.com/grpc-ecosystem/go-grpc-middleware/validator, which is
// Copyright 2016 Michal Witkowski and licensed under the Apache 2.0 License.
// Package validator implements a gRPC middleware that defines custom validators that are invoked when the service is called.
package validator
import (
"context"
"fmt"
"runtime/trace"
"sort"
"strings"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
var (
allowedFieldMaskPaths = make(map[string]map[string]struct{})
isZeroFieldMaskPaths = make(map[string][]string)
)
// RegisterAllowedFieldMaskPaths registers the allowed field mask paths for an
// RPC. Note that all allowed paths and sub-paths must be registered.
// This function is not safe for concurrent use.
func RegisterAllowedFieldMaskPaths(rpcFullMethod string, set bool, allPaths []string, allowedPaths ...string) {
if !sort.StringsAreSorted(allPaths) {
panic(fmt.Sprintf("paths for RPC '%s' are not sorted alphabetically: %v", rpcFullMethod, allPaths))
}
for _, p := range allowedPaths {
if i := sort.SearchStrings(allPaths, p); i == len(allPaths) || allPaths[i] != p {
panic(fmt.Sprintf("path '%s' is allowed for RPC '%s', but not present in all paths: %v", p, rpcFullMethod, allPaths))
}
}
allowed := make(map[string]struct{}, len(allowedPaths))
for _, p := range allowedPaths {
allowed[p] = struct{}{}
}
allowedFieldMaskPaths[rpcFullMethod] = allowed
if !set {
return
}
isZeroPaths := make([]string, 0, len(allPaths)-len(allowed))
for _, p := range allPaths {
_, ok := allowed[p]
if ok {
continue
}
isZeroPaths = append(isZeroPaths, p)
}
isZeroFieldMaskPaths[rpcFullMethod] = isZeroPaths
}
var errForbiddenFieldMaskPaths = errors.DefineInvalidArgument("field_mask_paths", "forbidden path(s) in field mask", "forbidden_paths")
func forbiddenPaths(requestedPaths []string, allowedPaths map[string]struct{}) []string {
var forbiddenPaths []string
nextRequestedPath:
for _, requestedPath := range requestedPaths {
if _, ok := allowedPaths[requestedPath]; ok {
continue nextRequestedPath
}
forbiddenPaths = append(forbiddenPaths, requestedPath)
}
return forbiddenPaths
}
func convertError(err error) error {
if ttnErr, ok := errors.From(err); ok {
return ttnErr
}
return status.Errorf(codes.InvalidArgument, err.Error())
}
var (
errNoValidator = errors.DefineUnimplemented("no_validator", "validator not defined")
errNonZeroPath = errors.DefineInvalidArgument("non_zero_path", "path `{path}` is not zero")
)
func validateMessage(ctx context.Context, fullMethod string, msg any) error {
var paths []string
switch v := msg.(type) {
case interface {
GetFieldMask() *fieldmaskpb.FieldMask
}:
paths = v.GetFieldMask().GetPaths()
}
if len(paths) > 0 {
region := trace.StartRegion(ctx, "validate field mask")
if forbiddenPaths := forbiddenPaths(paths, allowedFieldMaskPaths[fullMethod]); len(forbiddenPaths) > 0 {
region.End()
return errForbiddenFieldMaskPaths.WithAttributes("forbidden_paths", forbiddenPaths)
}
isZeroPaths, ok := isZeroFieldMaskPaths[fullMethod]
if ok && len(isZeroPaths) > 0 {
var isZero func(string) bool
outer:
for _, p := range paths {
prefix := p + "."
i := sort.Search(len(isZeroPaths), func(i int) bool {
return strings.HasPrefix(isZeroPaths[i], prefix)
})
if i == len(isZeroPaths) || !strings.HasPrefix(isZeroPaths[i], prefix) {
continue
}
tail := isZeroPaths[i+1:]
for _, sp := range isZeroPaths[i : i+1+sort.Search(len(tail), func(j int) bool {
return !strings.HasPrefix(tail[j], prefix)
})] {
if isZero == nil {
if v, ok := msg.(interface {
FieldIsZero(string) bool
}); ok {
isZero = v.FieldIsZero
} else {
break outer
}
}
if !isZero(sp) {
region.End()
return errNonZeroPath.WithAttributes("path", sp)
}
}
}
}
region.End()
}
switch v := msg.(type) {
case interface {
ValidateContext(context.Context) error
}:
defer trace.StartRegion(ctx, "validate with context").End()
if err := v.ValidateContext(ctx); err != nil {
return convertError(err)
}
return nil
case interface{ Validate() error }:
defer trace.StartRegion(ctx, "validate without context").End()
if err := v.Validate(); err != nil {
return convertError(err)
}
return nil
case interface {
ValidateFields(...string) error
}:
defer trace.StartRegion(ctx, "validate fields").End()
if err := v.ValidateFields(); err != nil {
return convertError(err)
}
return nil
case *emptypb.Empty:
return nil
default:
return errNoValidator.New()
}
}
// UnaryServerInterceptor returns a new unary server interceptor that validates
// incoming messages if those incoming messages implement:
//
// (A) ValidateContext(ctx context.Context) error
// (B) Validate() error
// (C) ValidateFields(...string) error
//
// If a message implements both, then (A) should call (B).
//
// Invalid messages will be rejected with the error returned from the validator,
// if that error is a TTN error, or with an `InvalidArgument` if it isn't.
//
// If the RPC's FullPath has a registered list of allowed field mask paths (see
// RegisterAllowedFieldMaskPaths) and the message implements GetFieldMask() fieldmaskpb.FieldMask
// then the field mask paths are validated according to the registered list.
func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if err := validateMessage(ctx, info.FullMethod, req); err != nil {
return nil, err
}
return handler(ctx, req)
}
}
type recvWrapper struct {
grpc.ServerStream
fullMethod string
}
func (s *recvWrapper) RecvMsg(msg any) error {
if err := s.ServerStream.RecvMsg(msg); err != nil {
return err
}
if err := validateMessage(s.Context(), s.fullMethod, msg); err != nil {
return err
}
return nil
}
// StreamServerInterceptor returns a new streaming server interceptor that validates
// incoming messages if those incoming messages implement:
//
// (A) ValidateContext(ctx context.Context) error
// (B) Validate() error
// (C) ValidateFields(...string) error
//
// If a message implements both, then (A) should call (B).
//
// Invalid messages will be rejected with the error returned from the validator,
// if that error is a TTN error, or with an `InvalidArgument` if it isn't.
//
// The stage at which invalid messages will be rejected with `InvalidArgument` varies
// based on the type of the RPC. For `ServerStream` (1:m) requests, it will happen
// before reaching any userspace handlers. For `ClientStream` (n:1) or `BidiStream` (n:m)
// RPCs, the messages will be rejected on calls to `stream.Recv()`.
//
// If the RPC's FullPath has a registered list of allowed field mask paths (see
// RegisterAllowedFieldMaskPaths) and the message implements GetFieldMask() fieldmaskpb.FieldMask
// then the field mask paths are validated according to the registered list.
func StreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return handler(srv, &recvWrapper{
ServerStream: stream,
fullMethod: info.FullMethod,
})
}
}