diff options
author | Bjørn Erik Pedersen <[email protected]> | 2021-12-12 12:11:11 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2021-12-16 09:40:22 +0100 |
commit | f4389e48ce0a70807362772d66c12ab5cd9e15f8 (patch) | |
tree | 1334516a199dcdf4133758e3664348287e73e88b /deps | |
parent | 803f572e66c5e22213ddcc994c41b3e80e9c1f35 (diff) | |
download | hugo-f4389e48ce0a70807362772d66c12ab5cd9e15f8.tar.gz hugo-f4389e48ce0a70807362772d66c12ab5cd9e15f8.zip |
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 = ['.*']
```
Diffstat (limited to 'deps')
-rw-r--r-- | deps/deps.go | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/deps/deps.go b/deps/deps.go index 6b9da21fe..191193b9b 100644 --- a/deps/deps.go +++ b/deps/deps.go @@ -8,8 +8,10 @@ import ( "github.com/pkg/errors" "github.com/gohugoio/hugo/cache/filecache" + "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/helpers" "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/langs" @@ -36,6 +38,8 @@ type Deps struct { // Used to log errors that may repeat itself many times. LogDistinct loggers.Logger + ExecHelper *hexec.Exec + // The templates to use. This will usually implement the full tpl.TemplateManager. tmpl tpl.TemplateHandler @@ -230,6 +234,12 @@ func New(cfg DepsCfg) (*Deps, error) { cfg.OutputFormats = output.DefaultFormats } + securityConfig, err := security.DecodeConfig(cfg.Cfg) + if err != nil { + return nil, errors.WithMessage(err, "failed to create security config from configuration") + } + execHelper := hexec.New(securityConfig) + ps, err := helpers.NewPathSpec(fs, cfg.Language, logger) if err != nil { return nil, errors.Wrap(err, "create PathSpec") @@ -243,12 +253,12 @@ func New(cfg DepsCfg) (*Deps, error) { errorHandler := &globalErrHandler{} buildState := &BuildState{} - resourceSpec, err := resources.NewSpec(ps, fileCaches, buildState, logger, errorHandler, cfg.OutputFormats, cfg.MediaTypes) + resourceSpec, err := resources.NewSpec(ps, fileCaches, buildState, logger, errorHandler, execHelper, cfg.OutputFormats, cfg.MediaTypes) if err != nil { return nil, err } - contentSpec, err := helpers.NewContentSpec(cfg.Language, logger, ps.BaseFs.Content.Fs) + contentSpec, err := helpers.NewContentSpec(cfg.Language, logger, ps.BaseFs.Content.Fs, execHelper) if err != nil { return nil, err } @@ -269,6 +279,7 @@ func New(cfg DepsCfg) (*Deps, error) { Fs: fs, Log: ignorableLogger, LogDistinct: logDistinct, + ExecHelper: execHelper, templateProvider: cfg.TemplateProvider, translationProvider: cfg.TranslationProvider, WithTemplate: cfg.WithTemplate, @@ -311,7 +322,7 @@ func (d Deps) ForLanguage(cfg DepsCfg, onCreated func(d *Deps) error) (*Deps, er return nil, err } - d.ContentSpec, err = helpers.NewContentSpec(l, d.Log, d.BaseFs.Content.Fs) + d.ContentSpec, err = helpers.NewContentSpec(l, d.Log, d.BaseFs.Content.Fs, d.ExecHelper) if err != nil { return nil, err } @@ -322,7 +333,7 @@ func (d Deps) ForLanguage(cfg DepsCfg, onCreated func(d *Deps) error) (*Deps, er // TODO(bep) clean up these inits. resourceCache := d.ResourceSpec.ResourceCache postBuildAssets := d.ResourceSpec.PostBuildAssets - d.ResourceSpec, err = resources.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.BuildState, d.Log, d.globalErrHandler, cfg.OutputFormats, cfg.MediaTypes) + d.ResourceSpec, err = resources.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.BuildState, d.Log, d.globalErrHandler, d.ExecHelper, cfg.OutputFormats, cfg.MediaTypes) if err != nil { return nil, err } |