diff --git a/experimental/transaction.go b/experimental/transaction.go new file mode 100644 index 000000000..d3e38e4f5 --- /dev/null +++ b/experimental/transaction.go @@ -0,0 +1,18 @@ +// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors +// SPDX-License-Identifier: Apache-2.0 + +package experimental + +import ( + "context" + + "github.com/corazawaf/coraza/v3/types" +) + +type Transaction interface { + types.Transaction + // UnixTimestamp returns the Unix timestamp of the transaction + UnixTimestamp() int64 + // Context returns the context of the transaction + Context() context.Context +} diff --git a/experimental/transaction_test.go b/experimental/transaction_test.go new file mode 100644 index 000000000..8e6ee5649 --- /dev/null +++ b/experimental/transaction_test.go @@ -0,0 +1,29 @@ +// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors +// SPDX-License-Identifier: Apache-2.0 + +package experimental_test + +import ( + "testing" + + "github.com/corazawaf/coraza/v3" + "github.com/corazawaf/coraza/v3/experimental" +) + +func TestTxTimestamp(t *testing.T) { + waf, err := coraza.NewWAF(coraza.NewWAFConfig()) + if err != nil { + panic(err) + } + tx := waf.NewTransaction() + tx2, ok := tx.(experimental.Transaction) + if !ok { + t.Error("Transaction does not implement experimental.Transaction") + } + if tx2.UnixTimestamp() == 0 { + t.Error("Timestamp should not be 0") + } + if tx2.Context() == nil { + t.Error("Context should not be nil") + } +} diff --git a/internal/corazawaf/transaction.go b/internal/corazawaf/transaction.go index 1a4785b90..7db8cf2af 100644 --- a/internal/corazawaf/transaction.go +++ b/internal/corazawaf/transaction.go @@ -1576,6 +1576,14 @@ func (tx *Transaction) String() string { return res.String() } +func (tx *Transaction) UnixTimestamp() int64 { + return tx.Timestamp +} + +func (tx *Transaction) Context() context.Context { + return tx.context +} + // generateRequestBodyError generates all the error variables for the request body parser func (tx *Transaction) generateRequestBodyError(err error) { tx.variables.reqbodyError.Set("1")