feat(chromium): add --chromium-disable-javascript property (fixes #175)

This commit is contained in:
Julien Neuhart
2021-11-22 18:41:21 +01:00
parent dd1e11f102
commit ee0debe14a
4 changed files with 47 additions and 0 deletions

View File

@@ -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) \

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Gutenberg</title>
</head>
<body>
<p id="javascript">
JavaScript disabled.
</p>
<script type="application/javascript">
document.getElementById('javascript').innerText = 'JavaScript not disabled.'
</script>
</body>
</html>