Skip to content

Commit 0b42734

Browse files
committed
Fix inconsistent DecodeTCPFlags output
1 parent 1a5e2d1 commit 0b42734

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

pkg/pipeline/transform/transform_network.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ func (n *Network) Transform(inputEntry config.GenericMap) (config.GenericMap, bo
141141
case api.NetworkDecodeTCPFlags:
142142
if anyFlags, ok := outputEntry[rule.DecodeTCPFlags.Input]; ok && anyFlags != nil {
143143
if flags, err := util.ConvertToUint(anyFlags); err == nil {
144-
if flags := util.DecodeTCPFlags(flags); len(flags) > 0 {
145-
outputEntry[rule.DecodeTCPFlags.Output] = flags
144+
strFlags := util.DecodeTCPFlags(flags)
145+
// If input==output (ie. we're rewritting in place), always write the result even if empty/nil, to avoid having inconsistent output types
146+
if len(strFlags) > 0 || rule.DecodeTCPFlags.Output == rule.DecodeTCPFlags.Input {
147+
outputEntry[rule.DecodeTCPFlags.Output] = strFlags
146148
}
147149
}
148150
}

pkg/pipeline/transform/transform_network_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,29 @@ func Test_DecodeTCPFlagsDefaultValue(t *testing.T) {
624624
require.True(t, ok)
625625
require.Equal(t, config.GenericMap{"TcpFlags": uint(0), "TcpFlagsString": "unknown"}, flow)
626626
}
627+
628+
func Test_DecodeTCPFlagsConsistentOutput(t *testing.T) {
629+
dec, err := NewTransformNetwork(config.StageParam{
630+
Transform: &config.Transform{
631+
Network: &api.TransformNetwork{
632+
Rules: []api.NetworkTransformRule{
633+
{
634+
Type: "decode_tcp_flags",
635+
DecodeTCPFlags: &api.NetworkGenericRule{
636+
Input: "TcpFlags",
637+
Output: "TcpFlags",
638+
},
639+
},
640+
},
641+
},
642+
},
643+
}, nil)
644+
require.NoError(t, err)
645+
646+
var ok bool
647+
flow := config.GenericMap{"TcpFlags": uint(0)}
648+
flow, ok = dec.Transform(flow)
649+
require.True(t, ok)
650+
var nilValue []string
651+
require.Equal(t, config.GenericMap{"TcpFlags": nilValue}, flow)
652+
}

0 commit comments

Comments
 (0)