Skip to content

Commit 03c4651

Browse files
authored
Update go.mod file for v2 (#45)
And remove retract directive since we changed major --------- Signed-off-by: Eliott Bouhana <[email protected]>
1 parent 5296850 commit 03c4651

File tree

8 files changed

+99
-12
lines changed

8 files changed

+99
-12
lines changed

context.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,24 @@ func (context *Context) Run(addressData RunAddressData, timeout time.Duration) (
8585
context.totalOverallRuntimeNs.Add(uint64(dt.Nanoseconds()))
8686
}()
8787

88-
// We can ignore the encoding error because it can only tell us that the user values are incompatible with the WAF
89-
// which is not something we care about here.
88+
// At this point, the only error we can get is an error in case the top level object is a nil map, but this
89+
// behaviour is expected since either persistent or ephemeral addresses are allowed to be null one at a time.
90+
// In this case, EncodeAddresses will return nil contrary to Encode which will return an nil wafObject,
91+
// which is what we need to send to ddwaf_run to signal that the address data is empty.
92+
var persistentData *wafObject = nil
93+
var ephemeralData *wafObject = nil
9094
persistentEncoder := newLimitedEncoder()
91-
persistentData, _ := persistentEncoder.Encode(addressData.Persistent)
95+
ephemeralEncoder := newLimitedEncoder()
96+
if addressData.Persistent != nil {
97+
persistentData, _ = persistentEncoder.EncodeAddresses(addressData.Persistent)
98+
}
99+
100+
if addressData.Ephemeral != nil {
101+
ephemeralData, _ = ephemeralEncoder.EncodeAddresses(addressData.Ephemeral)
92102

103+
}
93104
// The WAF releases ephemeral address data at the end of each run call, so we need not keep the Go values live beyond
94105
// that in the same way we need for persistent data. We hence use a separate encoder.
95-
ephemeralEncoder := newLimitedEncoder()
96-
ephemeralData, _ := ephemeralEncoder.Encode(addressData.Ephemeral)
97106

98107
// ddwaf_run cannot run concurrently and the next append write on the context state so we need a mutex
99108
context.mutex.Lock()

encoder.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ func (encoder *encoder) Encode(data any) (*wafObject, error) {
6161
return wo, nil
6262
}
6363

64+
// EncodeAddresses takes a map of Go values and returns a wafObject pointer and an error.
65+
// The returned wafObject is the root of the tree of nested wafObjects representing the Go values.
66+
// This function is further optimized from Encode to take addresses as input and avoid further
67+
// errors in case the top-level map with addresses as keys is nil.
68+
// Since errors returned by Encode are not sent up between levels of the tree, this means that all errors come from the
69+
// top layer of encoding, which is the map of addresses. Hence, all errors should be developer errors since the map of
70+
// addresses is not user defined custom data.
71+
func (encoder *encoder) EncodeAddresses(addresses map[string]any) (*wafObject, error) {
72+
if addresses == nil {
73+
return nil, errUnsupportedValue
74+
}
75+
76+
return encoder.Encode(addresses)
77+
}
78+
6479
func encodeNative[T native](val T, t wafObjectType, obj *wafObject) {
6580
obj._type = t
6681
obj.value = (uintptr)(val)

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/DataDog/go-libddwaf
1+
module github.com/DataDog/go-libddwaf/v2
22

33
go 1.18
44

@@ -18,4 +18,5 @@ require (
1818
gopkg.in/yaml.v3 v3.0.1 // indirect
1919
)
2020

21-
retract v1.6.0 // Breaking version, published too soon
21+
// Version where import paths were not changed to go-libddwaf/v2
22+
retract v2.0.0

handle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"fmt"
1111
"sync"
1212

13-
"github.com/DataDog/go-libddwaf/internal/noopfree"
13+
"github.com/DataDog/go-libddwaf/v2/internal/noopfree"
1414
"go.uber.org/atomic"
1515
)
1616

waf_dl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"fmt"
1212
"os"
1313

14-
"github.com/DataDog/go-libddwaf/internal/lib"
14+
"github.com/DataDog/go-libddwaf/v2/internal/lib"
1515
"github.com/ebitengine/purego"
1616
)
1717

waf_dl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"runtime"
1616
"testing"
1717

18-
"github.com/DataDog/go-libddwaf/internal/lib"
18+
"github.com/DataDog/go-libddwaf/v2/internal/lib"
1919
"github.com/stretchr/testify/require"
2020
)
2121

waf_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"text/template"
2020
"time"
2121

22-
"github.com/DataDog/go-libddwaf/internal/lib"
22+
"github.com/DataDog/go-libddwaf/v2/internal/lib"
2323
"github.com/stretchr/testify/require"
2424
)
2525

@@ -511,6 +511,68 @@ func TestMatchingEphemeral(t *testing.T) {
511511
require.Nil(t, NewContext(waf))
512512
}
513513

514+
func TestMatchingEphemeralOnly(t *testing.T) {
515+
const (
516+
input1 = "my.input.1"
517+
input2 = "my.input.2"
518+
)
519+
520+
waf, err := newDefaultHandle(newArachniTestRulePair(ruleInput{Address: input1}, ruleInput{Address: input2}))
521+
require.NoError(t, err)
522+
require.NotNil(t, waf)
523+
524+
addrs := waf.Addresses()
525+
sort.Strings(addrs)
526+
require.Equal(t, []string{input1, input2}, addrs)
527+
528+
wafCtx := NewContext(waf)
529+
require.NotNil(t, wafCtx)
530+
531+
// Not matching because the address value doesn't match the rule
532+
runAddresses := RunAddressData{
533+
Ephemeral: map[string]interface{}{
534+
input1: "go client",
535+
},
536+
}
537+
res, err := wafCtx.Run(runAddresses, time.Second)
538+
require.NoError(t, err)
539+
require.Nil(t, res.Events)
540+
require.Nil(t, res.Actions)
541+
542+
// Not matching because the address is not used by the rule
543+
runAddresses = RunAddressData{
544+
Ephemeral: map[string]interface{}{
545+
"server.request.uri.raw": "something",
546+
},
547+
}
548+
res, err = wafCtx.Run(runAddresses, time.Second)
549+
require.NoError(t, err)
550+
require.Nil(t, res.Events)
551+
require.Nil(t, res.Actions)
552+
553+
// Not matching due to a timeout
554+
runAddresses = RunAddressData{
555+
Ephemeral: map[string]interface{}{
556+
input1: "Arachni-1",
557+
},
558+
}
559+
res, err = wafCtx.Run(runAddresses, 0)
560+
require.Equal(t, ErrTimeout, err)
561+
require.Nil(t, res.Events)
562+
require.Nil(t, res.Actions)
563+
564+
// Matching
565+
res, err = wafCtx.Run(runAddresses, time.Second)
566+
require.NoError(t, err)
567+
require.Len(t, res.Events, 1) // 1 ephemeral
568+
require.Nil(t, res.Actions)
569+
570+
wafCtx.Close()
571+
waf.Close()
572+
// Using the WAF instance after it was closed leads to a nil WAF context
573+
require.Nil(t, NewContext(waf))
574+
}
575+
514576
func TestActions(t *testing.T) {
515577
testActions := func(expectedActions []string) func(t *testing.T) {
516578
return func(t *testing.T) {

waf_unsupported_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"errors"
1313
"testing"
1414

15-
waf "github.com/DataDog/go-libddwaf"
15+
waf "github.com/DataDog/go-libddwaf/v2"
1616
"github.com/stretchr/testify/require"
1717
)
1818

0 commit comments

Comments
 (0)