mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-22 21:42:50 +02:00
Add some basic security policies with sensible defaults
This ommmit contains some security hardening measures for the Hugo build runtime. There are some rarely used features in Hugo that would be good to have disabled by default. One example would be the "external helpers". For `asciidoctor` and some others we use Go's `os/exec` package to start a new process. These are a predefined set of binary names, all loaded from `PATH` and with a predefined set of arguments. Still, if you don't use `asciidoctor` in your project, you might as well have it turned off. You can configure your own in the new `security` configuration section, but the defaults are configured to create a minimal amount of site breakage. And if that do happen, you will get clear instructions in the loa about what to do. The default configuration is listed below. Note that almost all of these options are regular expression _whitelists_ (a string or a slice); the value `none` will block all. ```toml [security] enableInlineShortcodes = false [security.exec] allow = ['^dart-sass-embedded$', '^go$', '^npx$', '^postcss$'] osEnv = ['(?i)^(PATH|PATHEXT|APPDATA|TMP|TEMP|TERM)$'] [security.funcs] getenv = ['^HUGO_'] [security.http] methods = ['(?i)GET|POST'] urls = ['.*'] ```
This commit is contained in:
@@ -21,10 +21,9 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gohugoio/hugo/common/hexec"
|
||||
"github.com/gohugoio/hugo/htesting"
|
||||
|
||||
"github.com/cli/safeexec"
|
||||
|
||||
"github.com/gohugoio/hugo/identity"
|
||||
"github.com/gohugoio/hugo/markup/asciidocext/asciidocext_config"
|
||||
"github.com/gohugoio/hugo/markup/converter"
|
||||
@@ -67,7 +66,11 @@ type asciidocConverter struct {
|
||||
}
|
||||
|
||||
func (a *asciidocConverter) Convert(ctx converter.RenderContext) (converter.Result, error) {
|
||||
content, toc, err := a.extractTOC(a.getAsciidocContent(ctx.Src, a.ctx))
|
||||
b, err := a.getAsciidocContent(ctx.Src, a.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content, toc, err := a.extractTOC(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -83,20 +86,19 @@ func (a *asciidocConverter) Supports(_ identity.Identity) bool {
|
||||
|
||||
// getAsciidocContent calls asciidoctor as an external helper
|
||||
// to convert AsciiDoc content to HTML.
|
||||
func (a *asciidocConverter) getAsciidocContent(src []byte, ctx converter.DocumentContext) []byte {
|
||||
path := getAsciidoctorExecPath()
|
||||
if path == "" {
|
||||
func (a *asciidocConverter) getAsciidocContent(src []byte, ctx converter.DocumentContext) ([]byte, error) {
|
||||
if !hasAsciiDoc() {
|
||||
a.cfg.Logger.Errorln("asciidoctor not found in $PATH: Please install.\n",
|
||||
" Leaving AsciiDoc content unrendered.")
|
||||
return src
|
||||
return src, nil
|
||||
}
|
||||
|
||||
args := a.parseArgs(ctx)
|
||||
args = append(args, "-")
|
||||
|
||||
a.cfg.Logger.Infoln("Rendering", ctx.DocumentName, "with", path, "using asciidoctor args", args, "...")
|
||||
a.cfg.Logger.Infoln("Rendering", ctx.DocumentName, " using asciidoctor args", args, "...")
|
||||
|
||||
return internal.ExternallyRenderContent(a.cfg, ctx, src, path, args)
|
||||
return internal.ExternallyRenderContent(a.cfg, ctx, src, asciiDocBinaryName, args)
|
||||
}
|
||||
|
||||
func (a *asciidocConverter) parseArgs(ctx converter.DocumentContext) []string {
|
||||
@@ -195,12 +197,10 @@ func (a *asciidocConverter) appendArg(args []string, option, value, defaultValue
|
||||
return args
|
||||
}
|
||||
|
||||
func getAsciidoctorExecPath() string {
|
||||
path, err := safeexec.LookPath("asciidoctor")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return path
|
||||
const asciiDocBinaryName = "asciidoctor"
|
||||
|
||||
func hasAsciiDoc() bool {
|
||||
return hexec.InPath(asciiDocBinaryName)
|
||||
}
|
||||
|
||||
// extractTOC extracts the toc from the given src html.
|
||||
@@ -311,8 +311,12 @@ func nodeContent(node *html.Node) string {
|
||||
|
||||
// Supports returns whether Asciidoctor is installed on this computer.
|
||||
func Supports() bool {
|
||||
hasBin := hasAsciiDoc()
|
||||
if htesting.SupportsAll() {
|
||||
if !hasBin {
|
||||
panic("asciidoctor not installed")
|
||||
}
|
||||
return true
|
||||
}
|
||||
return getAsciidoctorExecPath() != ""
|
||||
return hasBin
|
||||
}
|
||||
|
@@ -21,8 +21,10 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/common/hexec"
|
||||
"github.com/gohugoio/hugo/common/loggers"
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/config/security"
|
||||
"github.com/gohugoio/hugo/markup/converter"
|
||||
"github.com/gohugoio/hugo/markup/markup_config"
|
||||
"github.com/gohugoio/hugo/markup/tableofcontents"
|
||||
@@ -280,20 +282,28 @@ func TestAsciidoctorAttributes(t *testing.T) {
|
||||
c.Assert(args[4], qt.Equals, "--no-header-footer")
|
||||
}
|
||||
|
||||
func getProvider(c *qt.C, mconf markup_config.Config) converter.Provider {
|
||||
sc := security.DefaultConfig
|
||||
sc.Exec.Allow = security.NewWhitelist("asciidoctor")
|
||||
|
||||
p, err := Provider.New(
|
||||
converter.ProviderConfig{
|
||||
MarkupConfig: mconf,
|
||||
Logger: loggers.NewErrorLogger(),
|
||||
Exec: hexec.New(sc),
|
||||
},
|
||||
)
|
||||
c.Assert(err, qt.IsNil)
|
||||
return p
|
||||
}
|
||||
|
||||
func TestConvert(t *testing.T) {
|
||||
if !Supports() {
|
||||
t.Skip("asciidoctor not installed")
|
||||
}
|
||||
c := qt.New(t)
|
||||
|
||||
mconf := markup_config.Default
|
||||
p, err := Provider.New(
|
||||
converter.ProviderConfig{
|
||||
MarkupConfig: mconf,
|
||||
Logger: loggers.NewErrorLogger(),
|
||||
},
|
||||
)
|
||||
c.Assert(err, qt.IsNil)
|
||||
p := getProvider(c, markup_config.Default)
|
||||
|
||||
conv, err := p.New(converter.DocumentContext{})
|
||||
c.Assert(err, qt.IsNil)
|
||||
@@ -308,14 +318,8 @@ func TestTableOfContents(t *testing.T) {
|
||||
t.Skip("asciidoctor not installed")
|
||||
}
|
||||
c := qt.New(t)
|
||||
mconf := markup_config.Default
|
||||
p, err := Provider.New(
|
||||
converter.ProviderConfig{
|
||||
MarkupConfig: mconf,
|
||||
Logger: loggers.NewErrorLogger(),
|
||||
},
|
||||
)
|
||||
c.Assert(err, qt.IsNil)
|
||||
p := getProvider(c, markup_config.Default)
|
||||
|
||||
conv, err := p.New(converter.DocumentContext{})
|
||||
c.Assert(err, qt.IsNil)
|
||||
r, err := conv.Convert(converter.RenderContext{Src: []byte(`:toc: macro
|
||||
@@ -390,14 +394,7 @@ func TestTableOfContentsWithCode(t *testing.T) {
|
||||
t.Skip("asciidoctor not installed")
|
||||
}
|
||||
c := qt.New(t)
|
||||
mconf := markup_config.Default
|
||||
p, err := Provider.New(
|
||||
converter.ProviderConfig{
|
||||
MarkupConfig: mconf,
|
||||
Logger: loggers.NewErrorLogger(),
|
||||
},
|
||||
)
|
||||
c.Assert(err, qt.IsNil)
|
||||
p := getProvider(c, markup_config.Default)
|
||||
conv, err := p.New(converter.DocumentContext{})
|
||||
c.Assert(err, qt.IsNil)
|
||||
r, err := conv.Convert(converter.RenderContext{Src: []byte(`:toc: auto
|
||||
@@ -433,13 +430,8 @@ func TestTableOfContentsPreserveTOC(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
mconf := markup_config.Default
|
||||
mconf.AsciidocExt.PreserveTOC = true
|
||||
p, err := Provider.New(
|
||||
converter.ProviderConfig{
|
||||
MarkupConfig: mconf,
|
||||
Logger: loggers.NewErrorLogger(),
|
||||
},
|
||||
)
|
||||
c.Assert(err, qt.IsNil)
|
||||
p := getProvider(c, mconf)
|
||||
|
||||
conv, err := p.New(converter.DocumentContext{})
|
||||
c.Assert(err, qt.IsNil)
|
||||
r, err := conv.Convert(converter.RenderContext{Src: []byte(`:toc:
|
||||
|
Reference in New Issue
Block a user