Skip to content

Commit fc73969

Browse files
authored
feat(encoder): coerce aliases of byte slices as byte slices (#70)
One of the sources of false positives are partial parsing using `json.RawMessage` for json parsing for example. Ignoring these kind of values during encoding makes the WAF more reliable and faster in case different types of byte slice aliases are present Signed-off-by: Eliott Bouhana <[email protected]>
1 parent f547dc1 commit fc73969

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

encoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (encoder *encoder) encode(value reflect.Value, obj *wafObject, depth int) e
165165
case kind == reflect.String: // string type
166166
encoder.encodeString(value.String(), obj)
167167

168-
case value.Type() == reflect.TypeOf([]byte(nil)):
168+
case (kind == reflect.Array || kind == reflect.Slice) && value.Type().Elem().Kind() == reflect.Uint8:
169169
// Byte Arrays are skipped voluntarily because they are often used
170170
// to do partial parsing which leads to false positives
171171
return nil

encoder_decoder_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package waf
99

1010
import (
1111
"context"
12+
"encoding/json"
1213
"reflect"
1314
"sort"
1415
"testing"
@@ -132,6 +133,11 @@ func TestEncodeDecode(t *testing.T) {
132133
Input: []byte("hello, waf"),
133134
DecodeError: errUnsupportedValue,
134135
},
136+
{
137+
Name: "json-raw",
138+
Input: json.RawMessage("hello, waf"),
139+
DecodeError: errUnsupportedValue,
140+
},
135141
{
136142
Name: "nil-byte-slice",
137143
Input: []byte(nil),
@@ -260,11 +266,13 @@ func TestEncodeDecode(t *testing.T) {
260266
private string
261267
a string
262268
A string
269+
partial json.RawMessage
263270
}{
264271
Public: "Public",
265272
private: "private",
266273
a: "a",
267274
A: "A",
275+
partial: json.RawMessage("test"),
268276
},
269277
Output: map[string]any{
270278
"A": "A",

0 commit comments

Comments
 (0)