mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
f4389e48ce
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 = ['.*'] ```
120 lines
3.4 KiB
Go
120 lines
3.4 KiB
Go
// Copyright 2020 The Hugo Authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// Package godartsass integrates with the Dass Sass Embedded protocol to transpile
|
|
// SCSS/SASS.
|
|
package dartsass
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
"github.com/gohugoio/hugo/hugolib/filesystems"
|
|
"github.com/gohugoio/hugo/resources"
|
|
"github.com/gohugoio/hugo/resources/resource"
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/bep/godartsass"
|
|
"github.com/mitchellh/mapstructure"
|
|
)
|
|
|
|
// used as part of the cache key.
|
|
const transformationName = "tocss-dart"
|
|
|
|
func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error) {
|
|
if !Supports() {
|
|
return &Client{dartSassNotAvailable: true}, nil
|
|
}
|
|
|
|
if err := rs.ExecHelper.Sec().CheckAllowedExec(dartSassEmbeddedBinaryName); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
transpiler, err := godartsass.Start(godartsass.Options{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Client{sfs: fs, workFs: rs.BaseFs.Work, rs: rs, transpiler: transpiler}, nil
|
|
}
|
|
|
|
type Client struct {
|
|
dartSassNotAvailable bool
|
|
rs *resources.Spec
|
|
sfs *filesystems.SourceFilesystem
|
|
workFs afero.Fs
|
|
transpiler *godartsass.Transpiler
|
|
}
|
|
|
|
func (c *Client) ToCSS(res resources.ResourceTransformer, args map[string]interface{}) (resource.Resource, error) {
|
|
if c.dartSassNotAvailable {
|
|
return res.Transform(resources.NewFeatureNotAvailableTransformer(transformationName, args))
|
|
}
|
|
return res.Transform(&transform{c: c, optsm: args})
|
|
}
|
|
|
|
func (c *Client) Close() error {
|
|
if c.transpiler == nil {
|
|
return nil
|
|
}
|
|
return c.transpiler.Close()
|
|
}
|
|
|
|
func (c *Client) toCSS(args godartsass.Args, src io.Reader) (godartsass.Result, error) {
|
|
var res godartsass.Result
|
|
|
|
in := helpers.ReaderToString(src)
|
|
args.Source = in
|
|
|
|
res, err := c.transpiler.Execute(args)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
return res, err
|
|
}
|
|
|
|
type Options struct {
|
|
|
|
// Hugo, will by default, just replace the extension of the source
|
|
// to .css, e.g. "scss/main.scss" becomes "scss/main.css". You can
|
|
// control this by setting this, e.g. "styles/main.css" will create
|
|
// a Resource with that as a base for RelPermalink etc.
|
|
TargetPath string
|
|
|
|
// Hugo automatically adds the entry directories (where the main.scss lives)
|
|
// for project and themes to the list of include paths sent to LibSASS.
|
|
// Any paths set in this setting will be appended. Note that these will be
|
|
// treated as relative to the working dir, i.e. no include paths outside the
|
|
// project/themes.
|
|
IncludePaths []string
|
|
|
|
// Default is nested.
|
|
// One of nested, expanded, compact, compressed.
|
|
OutputStyle string
|
|
|
|
// When enabled, Hugo will generate a source map.
|
|
EnableSourceMap bool
|
|
}
|
|
|
|
func decodeOptions(m map[string]interface{}) (opts Options, err error) {
|
|
if m == nil {
|
|
return
|
|
}
|
|
err = mapstructure.WeakDecode(m, &opts)
|
|
|
|
if opts.TargetPath != "" {
|
|
opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath)
|
|
}
|
|
|
|
return
|
|
}
|