@@ -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+
514576func TestActions (t * testing.T ) {
515577 testActions := func (expectedActions []string ) func (t * testing.T ) {
516578 return func (t * testing.T ) {
0 commit comments