Skip to content

Commit

Permalink
add custom error templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jptosso committed Feb 7, 2024
1 parent 7d69e57 commit 9784368
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions experimental/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,33 @@
package experimental

import (
"embed"
"net/http"

"github.com/corazawaf/coraza/v3"
)

//go:embed error_template.html error_template.html
var embededTemplates embed.FS

var (
errorTemplate []byte

Check failure on line 17 in experimental/middleware/middleware.go

View workflow job for this annotation

GitHub Actions / lint

var `errorTemplate` is unused (unused)
interruptionTemplate []byte

Check failure on line 18 in experimental/middleware/middleware.go

View workflow job for this annotation

GitHub Actions / lint

var `interruptionTemplate` is unused (unused)
)

func init() {
var err error
errorTemplate, err = embededTemplates.ReadFile("error_template.html")
if err != nil {
panic(err)
}
interruptionTemplate, err = embededTemplates.ReadFile("interruption_template.html")
if err != nil {
panic(err)
}

}

// Options represents the options for the experimental middleware
type Options struct {
// EnforceBlocking enables the blocking of requests that are interrupted
Expand Down Expand Up @@ -41,6 +63,21 @@ type Options struct {
// If the rate is 0, the middleware will not sample
// If the rate is 100, the middleware will sample all requests
SamplingRate int

// CustomInterruptionTemplate represents the custom interruption template
// If the interruption is not processed, the middleware will use a default
// Interruption template supports variables in macro expansion format: %{var}
// Variables are:
// - transaction_id
CustomInterruptionTemplate []byte

// CustomErrorTemplate represents the custom error template
// If the middleware fails to process the request, it will use a default
// Error template supports variables in macro expansion format: %{var}
// Variables are:
// - transaction_id
// - error
CustomErrorTemplate []byte
}

// DefaultOptions returns the default options for the middleware
Expand All @@ -64,6 +101,11 @@ func DefaultOptions(waf coraza.WAF) Options {
// Keys are:
// - coraza_transaction: types.Transaction
// - coraza_interruption: types.Interruption
// - coraza_error: error
//
// If Coraza fails to process the request, the middleware will return a generic error.
// The next handler will not be executed and coraza_error will be available under
// the request context.
//
// The middleware will flush the request body and it will consume
// the response in case ProcessResponse Option is enabled.
Expand Down

0 comments on commit 9784368

Please sign in to comment.