diff --git a/pkg/modules/api/middlewares.go b/pkg/modules/api/middlewares.go index c5cd6f5f..ad24060f 100644 --- a/pkg/modules/api/middlewares.go +++ b/pkg/modules/api/middlewares.go @@ -32,6 +32,22 @@ func ParseError(err error) (int, string) { return http.StatusServiceUnavailable, http.StatusText(http.StatusServiceUnavailable) } + if errors.Is(err, gotenberg.ErrFiltered) { + return http.StatusForbidden, http.StatusText(http.StatusForbidden) + } + + if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { + return http.StatusTooManyRequests, http.StatusText(http.StatusTooManyRequests) + } + + if errors.Is(err, gotenberg.ErrPdfEngineMethodNotSupported) { + return http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented) + } + + if errors.Is(err, gotenberg.ErrPdfFormatNotSupported) { + return http.StatusBadRequest, "A least one PDF engine does not handle one of the requested PDF format, while other have failed to convert for other reasons" + } + var httpErr HttpError if errors.As(err, &httpErr) { return httpErr.HttpError() diff --git a/pkg/modules/api/middlewares_test.go b/pkg/modules/api/middlewares_test.go index 146817a2..e921f454 100644 --- a/pkg/modules/api/middlewares_test.go +++ b/pkg/modules/api/middlewares_test.go @@ -29,9 +29,24 @@ func TestParseError(t *testing.T) { expectMessage: http.StatusText(http.StatusInternalServerError), }, { - err: context.DeadlineExceeded, - expectStatus: http.StatusServiceUnavailable, - expectMessage: http.StatusText(http.StatusServiceUnavailable), + err: gotenberg.ErrFiltered, + expectStatus: http.StatusForbidden, + expectMessage: http.StatusText(http.StatusForbidden), + }, + { + err: gotenberg.ErrMaximumQueueSizeExceeded, + expectStatus: http.StatusTooManyRequests, + expectMessage: http.StatusText(http.StatusTooManyRequests), + }, + { + err: gotenberg.ErrPdfEngineMethodNotSupported, + expectStatus: http.StatusNotImplemented, + expectMessage: http.StatusText(http.StatusNotImplemented), + }, + { + err: gotenberg.ErrPdfFormatNotSupported, + expectStatus: http.StatusBadRequest, + expectMessage: "A least one PDF engine does not handle one of the requested PDF format, while other have failed to convert for other reasons", }, { err: WrapError( diff --git a/pkg/modules/chromium/routes.go b/pkg/modules/chromium/routes.go index 8859b9ab..e43400a7 100644 --- a/pkg/modules/chromium/routes.go +++ b/pkg/modules/chromium/routes.go @@ -508,7 +508,7 @@ func convertUrl(ctx *api.Context, chromium Api, engine gotenberg.PdfEngine, url outputPath := ctx.GeneratePath("", ".pdf") err := chromium.Pdf(ctx, ctx.Log(), url, outputPath, options) - err = handleChromiumError(err, url, options.Options) + err = handleChromiumError(err, options.Options) if err != nil { if errors.Is(err, ErrOmitBackgroundWithoutPrintBackground) { return api.WrapError( @@ -553,26 +553,6 @@ func convertUrl(ctx *api.Context, chromium Api, engine gotenberg.PdfEngine, url err = engine.Convert(ctx, ctx.Log(), pdfFormats, convertInputPath, convertOutputPath) if err != nil { - if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusTooManyRequests, - "The maximum queue size has been reached", - ), - ) - } - - if errors.Is(err, gotenberg.ErrPdfFormatNotSupported) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusBadRequest, - fmt.Sprintf("At least one PDF engine does not handle one of the PDF format in '%+v', while other have failed to convert for other reasons", pdfFormats), - ), - ) - } - return fmt.Errorf("convert PDF: %w", err) } @@ -593,7 +573,7 @@ func screenshotUrl(ctx *api.Context, chromium Api, url string, options Screensho outputPath := ctx.GeneratePath("", ext) err := chromium.Screenshot(ctx, ctx.Log(), url, outputPath, options) - err = handleChromiumError(err, url, options.Options) + err = handleChromiumError(err, options.Options) if err != nil { return fmt.Errorf("screenshot: %w", err) } @@ -606,31 +586,11 @@ func screenshotUrl(ctx *api.Context, chromium Api, url string, options Screensho return nil } -func handleChromiumError(err error, url string, options Options) error { +func handleChromiumError(err error, options Options) error { if err == nil { return nil } - if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { - return api.WrapError( - err, - api.NewSentinelHttpError( - http.StatusTooManyRequests, - "The maximum queue size has been reached", - ), - ) - } - - if errors.Is(err, gotenberg.ErrFiltered) { - return api.WrapError( - err, - api.NewSentinelHttpError( - http.StatusForbidden, - fmt.Sprintf("'%s' does not match the authorized URLs", url), - ), - ) - } - if errors.Is(err, ErrInvalidEvaluationExpression) { if options.WaitForExpression == "" { // We do not expect the 'waitWindowStatus' form field to return diff --git a/pkg/modules/chromium/routes_test.go b/pkg/modules/chromium/routes_test.go index 269944f0..3ae9cf78 100644 --- a/pkg/modules/chromium/routes_test.go +++ b/pkg/modules/chromium/routes_test.go @@ -1230,30 +1230,6 @@ func TestConvertUrl(t *testing.T) { expectHttpStatus int expectOutputPathsCount int }{ - { - scenario: "ErrMaximumQueueSizeExceeded", - ctx: &api.ContextMock{Context: new(api.Context)}, - api: &ApiMock{PdfMock: func(ctx context.Context, logger *zap.Logger, url, outputPath string, options PdfOptions) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }}, - options: DefaultPdfOptions(), - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, - { - scenario: "ErrFiltered", - ctx: &api.ContextMock{Context: new(api.Context)}, - api: &ApiMock{PdfMock: func(ctx context.Context, logger *zap.Logger, url, outputPath string, options PdfOptions) error { - return gotenberg.ErrFiltered - }}, - options: DefaultPdfOptions(), - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusForbidden, - expectOutputPathsCount: 0, - }, { scenario: "ErrOmitBackgroundWithoutPrintBackground", ctx: &api.ContextMock{Context: new(api.Context)}, @@ -1353,38 +1329,6 @@ func TestConvertUrl(t *testing.T) { expectHttpError: false, expectOutputPathsCount: 0, }, - { - scenario: "ErrMaximumQueueSizeExceeded (PDF engine)", - ctx: &api.ContextMock{Context: new(api.Context)}, - api: &ApiMock{PdfMock: func(ctx context.Context, logger *zap.Logger, url, outputPath string, options PdfOptions) error { - return nil - }}, - engine: &gotenberg.PdfEngineMock{ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }}, - pdfFormats: gotenberg.PdfFormats{PdfA: "foo"}, - options: DefaultPdfOptions(), - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, - { - scenario: "ErrPdfFormatNotSupported", - ctx: &api.ContextMock{Context: new(api.Context)}, - api: &ApiMock{PdfMock: func(ctx context.Context, logger *zap.Logger, url, outputPath string, options PdfOptions) error { - return nil - }}, - engine: &gotenberg.PdfEngineMock{ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrPdfFormatNotSupported - }}, - pdfFormats: gotenberg.PdfFormats{PdfA: "foo"}, - options: DefaultPdfOptions(), - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusBadRequest, - expectOutputPathsCount: 0, - }, { scenario: "error from PDF engine", ctx: &api.ContextMock{Context: new(api.Context)}, @@ -1490,30 +1434,6 @@ func TestScreenshotUrl(t *testing.T) { expectHttpStatus int expectOutputPathsCount int }{ - { - scenario: "ErrMaximumQueueSizeExceeded", - ctx: &api.ContextMock{Context: new(api.Context)}, - api: &ApiMock{ScreenshotMock: func(ctx context.Context, logger *zap.Logger, url, outputPath string, options ScreenshotOptions) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }}, - options: DefaultScreenshotOptions(), - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, - { - scenario: "ErrFiltered", - ctx: &api.ContextMock{Context: new(api.Context)}, - api: &ApiMock{ScreenshotMock: func(ctx context.Context, logger *zap.Logger, url, outputPath string, options ScreenshotOptions) error { - return gotenberg.ErrFiltered - }}, - options: DefaultScreenshotOptions(), - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusForbidden, - expectOutputPathsCount: 0, - }, { scenario: "ErrInvalidEvaluationExpression (without waitForExpression form field)", ctx: &api.ContextMock{Context: new(api.Context)}, diff --git a/pkg/modules/libreoffice/routes.go b/pkg/modules/libreoffice/routes.go index eec44354..485eb623 100644 --- a/pkg/modules/libreoffice/routes.go +++ b/pkg/modules/libreoffice/routes.go @@ -68,16 +68,6 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap err = libreOffice.Pdf(ctx, ctx.Log(), inputPath, outputPaths[i], options) if err != nil { - if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { - return api.WrapError( - fmt.Errorf("convert to PDF: %w", err), - api.NewSentinelHttpError( - http.StatusTooManyRequests, - "The maximum queue size has been reached", - ), - ) - } - if errors.Is(err, libreofficeapi.ErrInvalidPdfFormats) { return api.WrapError( fmt.Errorf("convert to PDF: %w", err), @@ -107,16 +97,6 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap err = engine.Merge(ctx, ctx.Log(), outputPaths, outputPath) if err != nil { - if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { - return api.WrapError( - fmt.Errorf("merge PDFs: %w", err), - api.NewSentinelHttpError( - http.StatusTooManyRequests, - "The maximum queue size has been reached", - ), - ) - } - return fmt.Errorf("merge PDFs: %w", err) } @@ -129,26 +109,6 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap err = engine.Convert(ctx, ctx.Log(), pdfFormats, convertInputPath, convertOutputPath) if err != nil { - if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusTooManyRequests, - "The maximum queue size has been reached", - ), - ) - } - - if errors.Is(err, gotenberg.ErrPdfFormatNotSupported) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusBadRequest, - fmt.Sprintf("At least one PDF engine does not handle one of the PDF format in '%+v', while other have failed to convert for other reasons", pdfFormats), - ), - ) - } - return fmt.Errorf("convert PDF: %w", err) } @@ -180,26 +140,6 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap err = engine.Convert(ctx, ctx.Log(), pdfFormats, convertInputPath, convertOutputPaths[i]) if err != nil { - if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusTooManyRequests, - "The maximum queue size has been reached", - ), - ) - } - - if errors.Is(err, gotenberg.ErrPdfFormatNotSupported) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusBadRequest, - fmt.Sprintf("At least one PDF engine does not handle one of the PDF format in '%+v', while other have failed to convert for other reasons", pdfFormats), - ), - ) - } - return fmt.Errorf("convert PDF: %w", err) } diff --git a/pkg/modules/libreoffice/routes_test.go b/pkg/modules/libreoffice/routes_test.go index aa39df96..734da2ff 100644 --- a/pkg/modules/libreoffice/routes_test.go +++ b/pkg/modules/libreoffice/routes_test.go @@ -39,28 +39,6 @@ func TestConvertRoute(t *testing.T) { expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, - { - scenario: "ErrMaximumQueueSizeExceeded", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "document.docx": "/document.docx", - }) - return ctx - }(), - libreOffice: &libreofficeapi.ApiMock{ - PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options libreofficeapi.Options) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }, - ExtensionsMock: func() []string { - return []string{".docx"} - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, { scenario: "ErrPdfFormatNotSupported (nativePdfFormats)", ctx: func() *api.ContextMock { @@ -131,76 +109,6 @@ func TestConvertRoute(t *testing.T) { expectHttpError: false, expectOutputPathsCount: 0, }, - { - scenario: "ErrMaximumQueueSizeExceeded (single file)", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "document.docx": "/document.docx", - }) - ctx.SetValues(map[string][]string{ - "pdfa": { - gotenberg.PdfA1b, - }, - "nativePdfFormats": { - "false", - }, - }) - return ctx - }(), - libreOffice: &libreofficeapi.ApiMock{ - PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options libreofficeapi.Options) error { - return nil - }, - ExtensionsMock: func() []string { - return []string{".docx"} - }, - }, - engine: &gotenberg.PdfEngineMock{ - ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, - { - scenario: "ErrPdfFormatNotSupported (single file)", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "document.docx": "/document.docx", - }) - ctx.SetValues(map[string][]string{ - "pdfa": { - "foo", - }, - "nativePdfFormats": { - "false", - }, - }) - return ctx - }(), - libreOffice: &libreofficeapi.ApiMock{ - PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options libreofficeapi.Options) error { - return nil - }, - ExtensionsMock: func() []string { - return []string{".docx"} - }, - }, - engine: &gotenberg.PdfEngineMock{ - ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrPdfFormatNotSupported - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusBadRequest, - expectOutputPathsCount: 0, - }, { scenario: "PDF engine convert error (single file)", ctx: func() *api.ContextMock { @@ -378,39 +286,6 @@ func TestConvertRoute(t *testing.T) { expectOutputPathsCount: 2, expectOutputPaths: []string{"/document.docx.pdf", "/document2.docx.pdf"}, }, - { - scenario: "ErrMaximumQueueSizeExceeded (merge)", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "document.docx": "/document.docx", - "document2.docx": "/document2.docx", - }) - ctx.SetValues(map[string][]string{ - "merge": { - "true", - }, - }) - return ctx - }(), - libreOffice: &libreofficeapi.ApiMock{ - PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options libreofficeapi.Options) error { - return nil - }, - ExtensionsMock: func() []string { - return []string{".docx"} - }, - }, - engine: &gotenberg.PdfEngineMock{ - MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, { scenario: "merge error", ctx: func() *api.ContextMock { @@ -443,90 +318,6 @@ func TestConvertRoute(t *testing.T) { expectHttpError: false, expectOutputPathsCount: 0, }, - { - scenario: "ErrMaximumQueueSizeExceeded (convert post-merge)", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "document.docx": "/document.docx", - "document2.docx": "/document2.docx", - }) - ctx.SetValues(map[string][]string{ - "merge": { - "true", - }, - "pdfa": { - gotenberg.PdfA1b, - }, - "nativePdfFormats": { - "false", - }, - }) - return ctx - }(), - libreOffice: &libreofficeapi.ApiMock{ - PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options libreofficeapi.Options) error { - return nil - }, - ExtensionsMock: func() []string { - return []string{".docx"} - }, - }, - engine: &gotenberg.PdfEngineMock{ - MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { - return nil - }, - ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, - { - scenario: "ErrPdfFormatNotSupported (merge)", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "document.docx": "/document.docx", - "document2.docx": "/document2.docx", - }) - ctx.SetValues(map[string][]string{ - "merge": { - "true", - }, - "pdfa": { - "foo", - }, - "nativePdfFormats": { - "false", - }, - }) - return ctx - }(), - libreOffice: &libreofficeapi.ApiMock{ - PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options libreofficeapi.Options) error { - return nil - }, - ExtensionsMock: func() []string { - return []string{".docx"} - }, - }, - engine: &gotenberg.PdfEngineMock{ - MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { - return nil - }, - ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrPdfFormatNotSupported - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusBadRequest, - expectOutputPathsCount: 0, - }, { scenario: "PDF engine convert error (merge)", ctx: func() *api.ContextMock { diff --git a/pkg/modules/pdfengines/routes.go b/pkg/modules/pdfengines/routes.go index 5baabed9..59dddca3 100644 --- a/pkg/modules/pdfengines/routes.go +++ b/pkg/modules/pdfengines/routes.go @@ -49,16 +49,6 @@ func mergeRoute(engine gotenberg.PdfEngine) api.Route { err = engine.Merge(ctx, ctx.Log(), inputPaths, outputPath) if err != nil { - if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { - return api.WrapError( - fmt.Errorf("merge PDFs: %w", err), - api.NewSentinelHttpError( - http.StatusTooManyRequests, - "The maximum queue size has been reached", - ), - ) - } - return fmt.Errorf("merge PDFs: %w", err) } @@ -72,26 +62,6 @@ func mergeRoute(engine gotenberg.PdfEngine) api.Route { err = engine.Convert(ctx, ctx.Log(), pdfFormats, convertInputPath, convertOutputPath) if err != nil { - if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusTooManyRequests, - "The maximum queue size has been reached", - ), - ) - } - - if errors.Is(err, gotenberg.ErrPdfFormatNotSupported) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusBadRequest, - fmt.Sprintf("At least one PDF engine does not handle one of the PDF format in '%+v', while other have failed to convert for other reasons", pdfFormats), - ), - ) - } - return fmt.Errorf("convert PDF: %w", err) } @@ -167,26 +137,6 @@ func convertRoute(engine gotenberg.PdfEngine) api.Route { err = engine.Convert(ctx, ctx.Log(), pdfFormats, inputPath, outputPaths[i]) if err != nil { - if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusTooManyRequests, - "The maximum queue size has been reached", - ), - ) - } - - if errors.Is(err, gotenberg.ErrPdfFormatNotSupported) { - return api.WrapError( - fmt.Errorf("convert PDF: %w", err), - api.NewSentinelHttpError( - http.StatusBadRequest, - fmt.Sprintf("At least one PDF engine does not handle one of the PDF format in '%+v', while other have failed to convert for other reasons", pdfFormats), - ), - ) - } - return fmt.Errorf("convert PDF: %w", err) } } diff --git a/pkg/modules/pdfengines/routes_test.go b/pkg/modules/pdfengines/routes_test.go index 46b5d8b0..e8eb915c 100644 --- a/pkg/modules/pdfengines/routes_test.go +++ b/pkg/modules/pdfengines/routes_test.go @@ -32,27 +32,6 @@ func TestMergeHandler(t *testing.T) { expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, - { - scenario: "ErrMaximumQueueSizeExceeded (merge)", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "file.pdf": "/file.pdf", - "file2.pdf": "/file2.pdf", - }) - - return ctx - }(), - engine: &gotenberg.PdfEngineMock{ - MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, { scenario: "error from PDF engine", ctx: func() *api.ContextMock { @@ -111,62 +90,6 @@ func TestMergeHandler(t *testing.T) { expectHttpError: false, expectOutputPathsCount: 1, }, - { - scenario: "ErrMaximumQueueSizeExceeded (convert)", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "file.pdf": "/file.pdf", - "file2.pdf": "/file2.pdf", - }) - ctx.SetValues(map[string][]string{ - "pdfa": { - gotenberg.PdfA1b, - }, - }) - return ctx - }(), - engine: &gotenberg.PdfEngineMock{ - MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { - return nil - }, - ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, - { - scenario: "ErrPdfFormatNotSupported", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "file.pdf": "/file.pdf", - "file2.pdf": "/file2.pdf", - }) - ctx.SetValues(map[string][]string{ - "pdfa": { - "foo", - }, - }) - return ctx - }(), - engine: &gotenberg.PdfEngineMock{ - MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { - return nil - }, - ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrPdfFormatNotSupported - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusBadRequest, - expectOutputPathsCount: 0, - }, { scenario: "error from PDF engine (convert)", ctx: func() *api.ContextMock { @@ -298,54 +221,6 @@ func TestConvertHandler(t *testing.T) { expectHttpStatus: http.StatusBadRequest, expectOutputPathsCount: 0, }, - { - scenario: "ErrMaximumQueueSizeExceeded", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "file.pdf": "/file.pdf", - }) - ctx.SetValues(map[string][]string{ - "pdfa": { - gotenberg.PdfA1b, - }, - }) - return ctx - }(), - engine: &gotenberg.PdfEngineMock{ - ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrMaximumQueueSizeExceeded - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusTooManyRequests, - expectOutputPathsCount: 0, - }, - { - scenario: "ErrPdfFormatNotSupported", - ctx: func() *api.ContextMock { - ctx := &api.ContextMock{Context: new(api.Context)} - ctx.SetFiles(map[string]string{ - "file.pdf": "/file.pdf", - }) - ctx.SetValues(map[string][]string{ - "pdfa": { - "foo", - }, - }) - return ctx - }(), - engine: &gotenberg.PdfEngineMock{ - ConvertMock: func(ctx context.Context, logger *zap.Logger, formats gotenberg.PdfFormats, inputPath, outputPath string) error { - return gotenberg.ErrPdfFormatNotSupported - }, - }, - expectError: true, - expectHttpError: true, - expectHttpStatus: http.StatusBadRequest, - expectOutputPathsCount: 0, - }, { scenario: "error from PDF engine", ctx: func() *api.ContextMock { diff --git a/pkg/modules/webhook/middleware.go b/pkg/modules/webhook/middleware.go index 29ffc78b..d7e8ad8a 100644 --- a/pkg/modules/webhook/middleware.go +++ b/pkg/modules/webhook/middleware.go @@ -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) } diff --git a/pkg/modules/webhook/middleware_test.go b/pkg/modules/webhook/middleware_test.go index 89e13739..9fc1f8fe 100644 --- a/pkg/modules/webhook/middleware_test.go +++ b/pkg/modules/webhook/middleware_test.go @@ -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)",