From 14f94b156656f360682c330c9ba1275593a95456 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Wed, 24 Nov 2021 16:32:17 +0100 Subject: [PATCH] feat(chromium): add validation of 'emulatedMediaType' form field at the handler level --- pkg/modules/chromium/routes.go | 26 +++++++------ pkg/modules/chromium/routes_test.go | 59 +++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/pkg/modules/chromium/routes.go b/pkg/modules/chromium/routes.go index 7e988911..5c87b882 100644 --- a/pkg/modules/chromium/routes.go +++ b/pkg/modules/chromium/routes.go @@ -62,7 +62,21 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) { return nil }). - String("emulatedMediaType", &emulatedMediaType, defaultOptions.EmulatedMediaType). + Custom("emulatedMediaType", func(value string) error { + if value == "" { + emulatedMediaType = defaultOptions.EmulatedMediaType + + return nil + } + + if value != "screen" && value != "print" { + return fmt.Errorf("wrong value, expected either 'screen', 'print' or empty") + } + + emulatedMediaType = value + + return nil + }). Bool("landscape", &landscape, defaultOptions.Landscape). Bool("printBackground", &printBackground, defaultOptions.PrintBackground). Float64("scale", &scale, defaultOptions.Scale). @@ -493,16 +507,6 @@ func convertURL(ctx *api.Context, chromium API, engine gotenberg.PDFEngine, URL, ) } - if errors.Is(err, ErrInvalidEmulatedMediaType) { - return api.WrapError( - fmt.Errorf("convert to PDF: %w", err), - api.NewSentinelHTTPError( - http.StatusBadRequest, - fmt.Sprintf("The media type '%s' (emulatedMediaType) is invalid: allowed values are 'screen' or 'print'", options.EmulatedMediaType), - ), - ) - } - 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 aaee28a5..000ef6e0 100644 --- a/pkg/modules/chromium/routes_test.go +++ b/pkg/modules/chromium/routes_test.go @@ -54,6 +54,50 @@ func TestFormDataChromiumPDFOptions(t *testing.T) { "foo": "bar", } + return options + }(), + }, + { + ctx: func() *api.MockContext { + ctx := &api.MockContext{Context: &api.Context{}} + ctx.SetValues(map[string][]string{ + "extraHttpHeaders": { + "foo", + }, + }) + + return ctx + }(), + options: DefaultOptions(), + }, + { + ctx: func() *api.MockContext { + ctx := &api.MockContext{Context: &api.Context{}} + ctx.SetValues(map[string][]string{ + "emulatedMediaType": { + "foo", + }, + }) + + return ctx + }(), + options: DefaultOptions(), + }, + { + ctx: func() *api.MockContext { + ctx := &api.MockContext{Context: &api.Context{}} + ctx.SetValues(map[string][]string{ + "emulatedMediaType": { + "screen", + }, + }) + + return ctx + }(), + options: func() Options { + options := DefaultOptions() + options.EmulatedMediaType = "screen" + return options }(), }, @@ -765,21 +809,6 @@ func TestConvertURL(t *testing.T) { expectHTTPErr: true, expectHTTPStatus: http.StatusForbidden, }, - { - ctx: &api.MockContext{Context: &api.Context{}}, - api: func() API { - chromiumAPI := struct{ ProtoAPI }{} - chromiumAPI.pdf = func(_ context.Context, _ *zap.Logger, _, _ string, _ Options) error { - return ErrInvalidEmulatedMediaType - } - - return chromiumAPI - }(), - options: DefaultOptions(), - expectErr: true, - expectHTTPErr: true, - expectHTTPStatus: http.StatusBadRequest, - }, { ctx: &api.MockContext{Context: &api.Context{}}, api: func() API {