diff --git a/Makefile b/Makefile index f64ff6a1..2785c129 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,7 @@ CHROMIUM_ALLOW_FILE_ACCESS_FROM_FILES=false CHROMIUM_PROXY_SERVER= CHROMIUM_ALLOW_LIST= CHROMIUM_DENY_LIST="^file:///[^tmp].*" +CHROMIUM_DISABLE_JAVASCRIPT=false CHROMIUM_DISABLE_ROUTES=false LIBREOFFICE_DISABLES_ROUTES=false LOG_LEVEL=info @@ -83,6 +84,7 @@ run: ## Start a Gotenberg container --chromium-proxy-server=$(CHROMIUM_PROXY_SERVER) \ --chromium-allow-list=$(CHROMIUM_ALLOW_LIST) \ --chromium-deny-list=$(CHROMIUM_DENY_LIST) \ + --chromium-disable-javascript=$(CHROMIUM_DISABLE_JAVASCRIPT) \ --chromium-disable-routes=$(CHROMIUM_DISABLE_ROUTES) \ --libreoffice-disable-routes=$(LIBREOFFICE_DISABLES_ROUTES) \ --log-level=$(LOG_LEVEL) \ diff --git a/pkg/modules/chromium/chromium.go b/pkg/modules/chromium/chromium.go index 485004e8..c8d39bf9 100644 --- a/pkg/modules/chromium/chromium.go +++ b/pkg/modules/chromium/chromium.go @@ -65,6 +65,7 @@ type Chromium struct { proxyServer string allowList *regexp.Regexp denyList *regexp.Regexp + disableJavaScript bool disableRoutes bool } @@ -218,6 +219,7 @@ func (mod Chromium) Descriptor() gotenberg.ModuleDescriptor { fs.String("chromium-proxy-server", "", "Set the outbound proxy server; this switch only affects HTTP and HTTPS requests") fs.String("chromium-allow-list", "", "Set the allowed URLs for Chromium using a regular expression") fs.String("chromium-deny-list", "^file:///[^tmp].*", "Set the denied URLs for Chromium using a regular expression") + fs.Bool("chromium-disable-javascript", false, "Disable JavaScript") fs.Bool("chromium-disable-routes", false, "Disable the routes") err := fs.MarkDeprecated("chromium-user-agent", "use the userAgent form field instead") @@ -241,6 +243,7 @@ func (mod *Chromium) Provision(ctx *gotenberg.Context) error { mod.proxyServer = flags.MustString("chromium-proxy-server") mod.allowList = flags.MustRegexp("chromium-allow-list") mod.denyList = flags.MustRegexp("chromium-deny-list") + mod.disableJavaScript = flags.MustBool("chromium-disable-javascript") mod.disableRoutes = flags.MustBool("chromium-disable-routes") binPath, ok := os.LookupEnv("CHROMIUM_BIN_PATH") @@ -389,6 +392,23 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath return chromedp.Tasks{ network.Enable(), fetch.Enable(), + chromedp.ActionFunc(func(ctx context.Context) error { + // See https://github.com/gotenberg/gotenberg/issues/175. + if !mod.disableJavaScript { + logger.Debug("JavaScript not disabled") + + return nil + } + + logger.Debug("disable JavaScript") + + err := emulation.SetScriptExecutionDisabled(true).Do(ctx) + if err == nil { + return nil + } + + return fmt.Errorf("disable JavaScript: %w", err) + }), chromedp.ActionFunc(func(ctx context.Context) error { if len(options.ExtraHTTPHeaders) == 0 { logger.Debug("no extra HTTP headers") @@ -408,6 +428,8 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath return nil } + emulation.SetScriptExecutionDisabled(true) + return fmt.Errorf("set extra HTTP headers: %w", err) }), chromedp.ActionFunc(func(ctx context.Context) error { diff --git a/pkg/modules/chromium/chromium_test.go b/pkg/modules/chromium/chromium_test.go index c9adcb16..bde82849 100644 --- a/pkg/modules/chromium/chromium_test.go +++ b/pkg/modules/chromium/chromium_test.go @@ -238,6 +238,7 @@ func TestChromium_PDF(t *testing.T) { proxyServer string allowList *regexp.Regexp denyList *regexp.Regexp + disableJavaScript bool expectErr bool }{ { @@ -256,6 +257,10 @@ func TestChromium_PDF(t *testing.T) { UserAgent: "foo", }, }, + { + URL: "file:///tests/test/testdata/chromium/html/sample9/index.html", + disableJavaScript: true, + }, { URL: "file:///tests/test/testdata/chromium/html/sample4/index.html", options: Options{ @@ -412,6 +417,7 @@ func TestChromium_PDF(t *testing.T) { mod.allowList = tc.allowList mod.denyList = tc.denyList + mod.disableJavaScript = tc.disableJavaScript outputDir, err := gotenberg.MkdirAll() if err != nil { diff --git a/test/testdata/chromium/html/sample9/index.html b/test/testdata/chromium/html/sample9/index.html new file mode 100644 index 00000000..a98f7842 --- /dev/null +++ b/test/testdata/chromium/html/sample9/index.html @@ -0,0 +1,17 @@ + + +
+ ++ JavaScript disabled. +
+ + + + \ No newline at end of file