mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 00:22:14 +01:00
chore: using error middleware to handle common errors and lighten codebase
This commit is contained in:
@@ -13,7 +13,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/dlclark/regexp2"
|
||||
"github.com/hashicorp/go-retryablehttp"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
@@ -52,31 +51,12 @@ func webhookMiddleware(w *Webhook) api.Middleware {
|
||||
|
||||
// Let's check if the webhook URLs are acceptable according to our
|
||||
// allowed/denied lists.
|
||||
filter := func(url, header string, allowList, denyList *regexp2.Regexp, deadline time.Time) error {
|
||||
err := gotenberg.FilterDeadline(allowList, denyList, url, deadline)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if errors.Is(err, gotenberg.ErrFiltered) {
|
||||
return api.WrapError(
|
||||
err,
|
||||
api.NewSentinelHttpError(
|
||||
http.StatusForbidden,
|
||||
fmt.Sprintf("Invalid '%s' header value: '%s' does not match the authorized URL", header, url),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
err := filter(webhookUrl, "Gotenberg-Webhook-Url", w.allowList, w.denyList, deadline)
|
||||
err := gotenberg.FilterDeadline(w.allowList, w.denyList, webhookUrl, deadline)
|
||||
if err != nil {
|
||||
return fmt.Errorf("filter webhook URL: %w", err)
|
||||
}
|
||||
|
||||
err = filter(webhookErrorUrl, "Gotenberg-Webhook-Error-Url", w.errorAllowList, w.errorDenyList, deadline)
|
||||
err = gotenberg.FilterDeadline(w.errorAllowList, w.errorDenyList, webhookErrorUrl, deadline)
|
||||
if err != nil {
|
||||
return fmt.Errorf("filter webhook error URL: %w", err)
|
||||
}
|
||||
|
||||
@@ -119,10 +119,8 @@ func TestWebhookMiddlewareGuards(t *testing.T) {
|
||||
mod.allowList = regexp2.MustCompile("bar", 0)
|
||||
return mod
|
||||
}(),
|
||||
noDeadline: false,
|
||||
expectError: true,
|
||||
expectHttpError: true,
|
||||
expectHttpStatus: http.StatusForbidden,
|
||||
noDeadline: false,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
scenario: "webhook URL is denied",
|
||||
@@ -137,10 +135,8 @@ func TestWebhookMiddlewareGuards(t *testing.T) {
|
||||
mod.denyList = regexp2.MustCompile("foo", 0)
|
||||
return mod
|
||||
}(),
|
||||
noDeadline: false,
|
||||
expectError: true,
|
||||
expectHttpError: true,
|
||||
expectHttpStatus: http.StatusForbidden,
|
||||
noDeadline: false,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
scenario: "webhook error URL is not allowed",
|
||||
@@ -155,10 +151,8 @@ func TestWebhookMiddlewareGuards(t *testing.T) {
|
||||
mod.errorAllowList = regexp2.MustCompile("foo", 0)
|
||||
return mod
|
||||
}(),
|
||||
noDeadline: false,
|
||||
expectError: true,
|
||||
expectHttpError: true,
|
||||
expectHttpStatus: http.StatusForbidden,
|
||||
noDeadline: false,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
scenario: "webhook error URL is denied",
|
||||
@@ -173,10 +167,8 @@ func TestWebhookMiddlewareGuards(t *testing.T) {
|
||||
mod.errorDenyList = regexp2.MustCompile("bar", 0)
|
||||
return mod
|
||||
}(),
|
||||
noDeadline: false,
|
||||
expectError: true,
|
||||
expectHttpError: true,
|
||||
expectHttpStatus: http.StatusForbidden,
|
||||
noDeadline: false,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
scenario: "invalid webhook method (GET)",
|
||||
|
||||
Reference in New Issue
Block a user