Files
substrate/cmd/ateapi/internal/controlapi/validate.go
T
Luiz Oliveira 790ee35229 Reject object updates containing unknown fields (#1174)
https://github.com/agent-substrate/substrate/issues/1011

An update containing unknown fields during a RMW indicates there's a
version skew between client and server.

So, the server enforces a policy to fail explicitly when it receives an
unknown proto field: so that it does not silently drop fields that the
user intended to set but the current server version cannot process

This also helps to avoid behaviour drift during rolling upgrades: During
rolling upgrades, different components (like the data plane and control
plane) can be on different versions. If an older server replica . If an
older server replica processes a request coming from a newer client, it
will reject it (it doesn't know how to validate/handle it) and it's the
client's responsibility to retry.

- [x] Tests pass
- [x] Appropriate changes to documentation are included in the PR
2026-08-25 08:21:25 -07:00

74 lines
2.4 KiB
Go

// Copyright 2026 Google LLC
//
// 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.
package controlapi
import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protopath"
"google.golang.org/protobuf/reflect/protorange"
"google.golang.org/protobuf/reflect/protoreflect"
"k8s.io/apimachinery/pkg/util/validation/field"
)
func toGRPCStatusError(errs field.ErrorList) error {
return status.Error(codes.InvalidArgument, errs.ToAggregate().Error())
}
// validateNoUnknownFields reports an error for every unknown field in m, at
// every level of nesting.
//
// A client newer than this binary can set fields this binary has no descriptor
// for. protobuf keeps those bytes on the parsed message, so an Update — which
// replaces the whole object — would persist them. The server cannot validate
// such a field.
func validateNoUnknownFields(m proto.Message, fldPath *field.Path) field.ErrorList {
r := m.ProtoReflect()
if !r.IsValid() {
return nil
}
var errs field.ErrorList
if err := protorange.Range(r, func(p protopath.Values) error {
msg, ok := p.Index(-1).Value.Interface().(protoreflect.Message)
if !ok || len(msg.GetUnknown()) == 0 {
return nil
}
// Report the path of the unknwown field
errs = append(errs, field.Invalid(toFieldPath(p.Path, fldPath), field.OmitValueType{}, ""))
return nil
}); err != nil {
errs = append(errs, field.InternalError(fldPath, err))
}
return errs
}
// toFieldPath renders a protopath as a field.Path rooted at root.
func toFieldPath(p protopath.Path, root *field.Path) *field.Path {
out := root
for _, step := range p {
switch step.Kind() {
case protopath.FieldAccessStep:
out = out.Child(step.FieldDescriptor().TextName())
case protopath.ListIndexStep:
out = out.Index(step.ListIndex())
case protopath.MapIndexStep:
out = out.Key(step.MapIndex().String())
}
}
return out
}