From 36d6e9b8c6c03c89f24fe2d6520e69f47d31a3a4 Mon Sep 17 00:00:00 2001 From: Marton Rasek Date: Tue, 1 Dec 2026 22:41:51 +0100 Subject: [PATCH] server: reject a next-hop action with more than one option The API NexthopAction has four fields: address, self, unchanged and peer_address. table.NexthopAction holds only one of them, as the set-next-hop configuration (oc.BgpNextHopType) does. newNexthopActionFromApiStruct picks self, then unchanged, then peer-address, then the address, and drops the others without an error. A caller that sets more than one gets a next hop that it may not expect, and ListStatement returns only the one that is applied. Return an error instead, as newPrefixFromApiStruct does for ip-prefix and rtc-prefix. Assisted-by: Claude Opus 5.5 Signed-off-by: Marton Rasek --- pkg/server/grpc_server.go | 9 +++++++++ pkg/server/grpc_server_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/pkg/server/grpc_server.go b/pkg/server/grpc_server.go index 85476700cf..14aedba4f3 100644 --- a/pkg/server/grpc_server.go +++ b/pkg/server/grpc_server.go @@ -1848,6 +1848,15 @@ func newNexthopActionFromApiStruct(a *api.NexthopAction) (*table.NexthopAction, if a == nil { return nil, nil } + n := 0 + for _, set := range []bool{a.Self, a.Unchanged, a.PeerAddress, a.Address != ""} { + if set { + n++ + } + } + if n > 1 { + return nil, fmt.Errorf("next-hop self, unchanged, peer-address and address are mutually exclusive") + } return table.NewNexthopAction(oc.BgpNextHopType( func() string { switch { diff --git a/pkg/server/grpc_server_test.go b/pkg/server/grpc_server_test.go index 339fad7547..f3f14fd71f 100644 --- a/pkg/server/grpc_server_test.go +++ b/pkg/server/grpc_server_test.go @@ -856,6 +856,38 @@ func TestNewPrefixFromApiStructRTC(t *testing.T) { assert.True(t, p.Prefix.Contains(full.Prefix.Addr())) } +func TestNewNexthopActionFromApiStruct(t *testing.T) { + tests := []struct { + in *api.NexthopAction + want string + }{ + {&api.NexthopAction{Self: true}, "self"}, + {&api.NexthopAction{Unchanged: true}, "unchanged"}, + {&api.NexthopAction{PeerAddress: true}, "peer-address"}, + {&api.NexthopAction{Address: "192.0.2.1"}, "192.0.2.1"}, + {&api.NexthopAction{Address: "self"}, "self"}, + } + for _, tt := range tests { + a, err := newNexthopActionFromApiStruct(tt.in) + require.NoError(t, err) + assert.Equal(t, tt.want, a.String()) + } + + // Only one option can be applied, so a combination is rejected + // instead of silently dropping all but one of them. + for _, in := range []*api.NexthopAction{ + {Self: true, Unchanged: true}, + {Self: true, PeerAddress: true}, + {Unchanged: true, PeerAddress: true}, + {Self: true, Address: "192.0.2.1"}, + {Unchanged: true, Address: "192.0.2.1"}, + {PeerAddress: true, Address: "192.0.2.1"}, + } { + _, err := newNexthopActionFromApiStruct(in) + assert.Error(t, err, "%v", in) + } +} + func TestNewNeighborSetFromApiStructZone(t *testing.T) { ds, err := newDefinedSetFromApiStruct(&api.DefinedSet{ DefinedType: api.DefinedType_DEFINED_TYPE_NEIGHBOR,