2021-12-12 06:11:11 -05:00
|
|
|
// Copyright 2018 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 security
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/herrors"
|
|
|
|
"github.com/gohugoio/hugo/common/types"
|
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/parser"
|
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
const securityConfigKey = "security"
|
|
|
|
|
|
|
|
// DefaultConfig holds the default security policy.
|
|
|
|
var DefaultConfig = Config{
|
|
|
|
Exec: Exec{
|
|
|
|
Allow: NewWhitelist(
|
|
|
|
"^dart-sass-embedded$",
|
|
|
|
"^go$", // for Go Modules
|
|
|
|
"^npx$", // used by all Node tools (Babel, PostCSS).
|
|
|
|
"^postcss$",
|
|
|
|
),
|
|
|
|
// These have been tested to work with Hugo's external programs
|
|
|
|
// on Windows, Linux and MacOS.
|
2023-01-17 04:52:51 -05:00
|
|
|
OsEnv: NewWhitelist(`(?i)^((HTTPS?|NO)_PROXY|PATH(EXT)?|APPDATA|TE?MP|TERM|GO\w+)$`),
|
2021-12-12 06:11:11 -05:00
|
|
|
},
|
|
|
|
Funcs: Funcs{
|
2022-12-02 06:13:34 -05:00
|
|
|
Getenv: NewWhitelist("^HUGO_", "^CI$"),
|
2021-12-12 06:11:11 -05:00
|
|
|
},
|
|
|
|
HTTP: HTTP{
|
|
|
|
URLs: NewWhitelist(".*"),
|
|
|
|
Methods: NewWhitelist("(?i)GET|POST"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the top level security config.
|
2023-01-04 12:24:36 -05:00
|
|
|
// <docsmeta>{"name": "security", "description": "This section holds the top level security config.", "newIn": "0.91.0" }</docsmeta>
|
2021-12-12 06:11:11 -05:00
|
|
|
type Config struct {
|
2023-01-04 12:24:36 -05:00
|
|
|
// Restricts access to os.Exec....
|
|
|
|
// <docsmeta>{ "newIn": "0.91.0" }</docsmeta>
|
2021-12-12 06:11:11 -05:00
|
|
|
Exec Exec `json:"exec"`
|
|
|
|
|
|
|
|
// Restricts access to certain template funcs.
|
|
|
|
Funcs Funcs `json:"funcs"`
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// Restricts access to resources.GetRemote, getJSON, getCSV.
|
2021-12-12 06:11:11 -05:00
|
|
|
HTTP HTTP `json:"http"`
|
|
|
|
|
|
|
|
// Allow inline shortcodes
|
|
|
|
EnableInlineShortcodes bool `json:"enableInlineShortcodes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec holds os/exec policies.
|
|
|
|
type Exec struct {
|
|
|
|
Allow Whitelist `json:"allow"`
|
|
|
|
OsEnv Whitelist `json:"osEnv"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funcs holds template funcs policies.
|
|
|
|
type Funcs struct {
|
|
|
|
// OS env keys allowed to query in os.Getenv.
|
|
|
|
Getenv Whitelist `json:"getenv"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTP struct {
|
|
|
|
// URLs to allow in remote HTTP (resources.Get, getJSON, getCSV).
|
|
|
|
URLs Whitelist `json:"urls"`
|
|
|
|
|
|
|
|
// HTTP methods to allow.
|
|
|
|
Methods Whitelist `json:"methods"`
|
2023-05-20 11:37:04 -04:00
|
|
|
|
|
|
|
// Media types where the Content-Type in the response is used instead of resolving from the file content.
|
|
|
|
MediaTypes Whitelist `json:"mediaTypes"`
|
2021-12-12 06:11:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToTOML converts c to TOML with [security] as the root.
|
|
|
|
func (c Config) ToTOML() string {
|
|
|
|
sec := c.ToSecurityMap()
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
if err := parser.InterfaceToConfig(sec, metadecoders.TOML, &b); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(b.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) CheckAllowedExec(name string) error {
|
|
|
|
if !c.Exec.Allow.Accept(name) {
|
|
|
|
return &AccessDeniedError{
|
|
|
|
name: name,
|
|
|
|
path: "security.exec.allow",
|
|
|
|
policies: c.ToTOML(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-12-23 06:46:04 -05:00
|
|
|
|
2021-12-12 06:11:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) CheckAllowedGetEnv(name string) error {
|
|
|
|
if !c.Funcs.Getenv.Accept(name) {
|
|
|
|
return &AccessDeniedError{
|
|
|
|
name: name,
|
|
|
|
path: "security.funcs.getenv",
|
|
|
|
policies: c.ToTOML(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) CheckAllowedHTTPURL(url string) error {
|
|
|
|
if !c.HTTP.URLs.Accept(url) {
|
|
|
|
return &AccessDeniedError{
|
|
|
|
name: url,
|
|
|
|
path: "security.http.urls",
|
|
|
|
policies: c.ToTOML(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) CheckAllowedHTTPMethod(method string) error {
|
|
|
|
if !c.HTTP.Methods.Accept(method) {
|
|
|
|
return &AccessDeniedError{
|
|
|
|
name: method,
|
|
|
|
path: "security.http.method",
|
|
|
|
policies: c.ToTOML(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSecurityMap converts c to a map with 'security' as the root key.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (c Config) ToSecurityMap() map[string]any {
|
2021-12-12 06:11:11 -05:00
|
|
|
// Take it to JSON and back to get proper casing etc.
|
|
|
|
asJson, err := json.Marshal(c)
|
|
|
|
herrors.Must(err)
|
2022-03-17 17:03:27 -04:00
|
|
|
m := make(map[string]any)
|
2021-12-12 06:11:11 -05:00
|
|
|
herrors.Must(json.Unmarshal(asJson, &m))
|
|
|
|
|
|
|
|
// Add the root
|
2022-03-17 17:03:27 -04:00
|
|
|
sec := map[string]any{
|
2021-12-12 06:11:11 -05:00
|
|
|
"security": m,
|
|
|
|
}
|
|
|
|
return sec
|
2021-12-23 06:46:04 -05:00
|
|
|
|
2021-12-12 06:11:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeConfig creates a privacy Config from a given Hugo configuration.
|
|
|
|
func DecodeConfig(cfg config.Provider) (Config, error) {
|
|
|
|
sc := DefaultConfig
|
|
|
|
if cfg.IsSet(securityConfigKey) {
|
|
|
|
m := cfg.GetStringMap(securityConfigKey)
|
|
|
|
dec, err := mapstructure.NewDecoder(
|
|
|
|
&mapstructure.DecoderConfig{
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: &sc,
|
|
|
|
DecodeHook: stringSliceToWhitelistHook(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return sc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = dec.Decode(m); err != nil {
|
|
|
|
return sc, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sc.EnableInlineShortcodes {
|
|
|
|
// Legacy
|
|
|
|
sc.EnableInlineShortcodes = cfg.GetBool("enableInlineShortcodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc, nil
|
2021-12-23 06:46:04 -05:00
|
|
|
|
2021-12-12 06:11:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func stringSliceToWhitelistHook() mapstructure.DecodeHookFuncType {
|
|
|
|
return func(
|
|
|
|
f reflect.Type,
|
|
|
|
t reflect.Type,
|
2022-03-17 17:03:27 -04:00
|
|
|
data any) (any, error) {
|
2021-12-12 06:11:11 -05:00
|
|
|
|
|
|
|
if t != reflect.TypeOf(Whitelist{}) {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
wl := types.ToStringSlicePreserveString(data)
|
|
|
|
|
|
|
|
return NewWhitelist(wl...), nil
|
2021-12-23 06:46:04 -05:00
|
|
|
|
2021-12-12 06:11:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccessDeniedError represents a security policy conflict.
|
|
|
|
type AccessDeniedError struct {
|
|
|
|
path string
|
|
|
|
name string
|
|
|
|
policies string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *AccessDeniedError) Error() string {
|
|
|
|
return fmt.Sprintf("access denied: %q is not whitelisted in policy %q; the current security configuration is:\n\n%s\n\n", e.name, e.path, e.policies)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAccessDenied reports whether err is an AccessDeniedError
|
|
|
|
func IsAccessDenied(err error) bool {
|
|
|
|
var notFoundErr *AccessDeniedError
|
|
|
|
return errors.As(err, ¬FoundErr)
|
|
|
|
}
|