| 1 |
--- |
| 2 |
title: Configure security |
| 3 |
linkTitle: Security |
| 4 |
description: Configure security. |
| 5 |
categories: [] |
| 6 |
keywords: [] |
| 7 |
--- |
| 8 |
|
| 9 |
Hugo's built-in security policy, which restricts access to `os/exec`, remote communication, and similar operations, is configured via allowlists. By default, access is restricted. If a build attempts to use a feature not included in the allowlist, it will fail, providing a detailed message. |
| 10 |
|
| 11 |
This is the default security configuration: |
| 12 |
|
| 13 |
{{< code-toggle config=security />}} |
| 14 |
|
| 15 |
`allowContent` |
| 16 |
: {{< new-in 0.162.0 />}} |
| 17 |
: (`[]string`) A slice of [regular expressions](g) matching the [media type](g) of [content formats](g) allowed in the `content` directory. By default, Hugo denies the following content formats: |
| 18 |
|
| 19 |
- Emacs Org Mode (media type `text/org`): Hugo renders export blocks and `@@html:...@@` snippets verbatim, which can allow arbitrary JavaScript execution. See [details](https://orgmode.org/manual/Quoting-HTML-tags.html#Quoting-HTML-tags-1). |
| 20 |
- HTML (media type `text/html`): Hugo renders HTML file content verbatim, which can allow arbitrary JavaScript execution. |
| 21 |
|
| 22 |
See the [classification][] table for a mapping of content formats to media types. |
| 23 |
|
| 24 |
`enableInlineShortcodes` |
| 25 |
: (`bool`) Whether to enable [inline shortcodes][]. Default is `false`. |
| 26 |
|
| 27 |
`exec.allow` |
| 28 |
: (`[]string`) A slice of [regular expressions](g) matching the names of external executables that Hugo is allowed to run. |
| 29 |
|
| 30 |
`exec.osEnv` |
| 31 |
: (`[]string`) A slice of [regular expressions](g) matching the names of operating system environment variables that Hugo is allowed to access. |
| 32 |
|
| 33 |
`funcs.getenv` |
| 34 |
: (`[]string`) A slice of [regular expressions](g) matching the names of operating system environment variables that Hugo is allowed to access with the [`os.Getenv`][] function. |
| 35 |
|
| 36 |
`http.methods` |
| 37 |
: (`[]string`) A slice of [regular expressions](g) matching the HTTP methods that the [`resources.GetRemote`][] function is allowed to use. |
| 38 |
|
| 39 |
`http.mediaTypes` |
| 40 |
: (`[]string`) Applicable to the `resources.GetRemote` function, a slice of [regular expressions](g) matching the `Content-Type` in HTTP responses that Hugo trusts, bypassing file content analysis for media type detection. |
| 41 |
|
| 42 |
`http.proxyFromEnvironment` |
| 43 |
: {{< new-in 0.166.0 />}} |
| 44 |
: (`bool`) Whether the `resources.GetRemote` function honors the `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` environment variables. Default is `false`. When a proxy is used, Hugo connects to the proxy rather than the destination, so the resolved address validation described under `http.urls` does not apply. |
| 45 |
|
| 46 |
`http.urls` |
| 47 |
: (`[]string`) A slice of [regular expressions](g) matching the URLs that the `resources.GetRemote` function is allowed to access. |
| 48 |
|
| 49 |
The default allowlist denies URLs with an IP address or `localhost` as the host name. In addition, with the default allowlist, Hugo validates the resolved address when connecting and rejects connections to loopback, private, link-local, and other non-public addresses, e.g. a host name that resolves to a cloud metadata endpoint. This validation is disabled if you override `http.urls`, as the override may intentionally allow access to hosts on your local network, such as a development server. |
| 50 |
|
| 51 |
`node.permissions.disable` |
| 52 |
: {{< new-in 0.161.0 />}} |
| 53 |
: (`bool`) Whether to disable the Node.js [permission model][]. When `false`, Hugo runs Node.js tools with the `--permission` flag, restricting their file system and resource access to what is explicitly allowed below. Default is `false`. |
| 54 |
|
| 55 |
`node.permissions.allowAddons` |
| 56 |
: {{< new-in 0.161.0 />}} |
| 57 |
: (`[]string`) A slice of Node.js tool names permitted to load native addons (`--allow-addons`). |
| 58 |
|
| 59 |
`node.permissions.allowChildProcess` |
| 60 |
: {{< new-in 0.161.0 />}} |
| 61 |
: (`[]string`) A slice of Node.js tool names permitted to spawn child processes (`--allow-child-process`). |
| 62 |
|
| 63 |
`node.permissions.allowRead` |
| 64 |
: {{< new-in 0.161.0 />}} |
| 65 |
: (`[]string`) A slice of file system paths that Node.js tools are allowed to read (`--allow-fs-read`). Paths are relative to the working directory; `"."` means the working directory itself. Use `"*"` to allow all paths. |
| 66 |
|
| 67 |
The same paths bound what `js.Build`, `js.Batch`, `css.Build` and `css.Sass` may import from outside the `assets` directory, e.g. from `node_modules`. This check does not depend on `node.permissions.disable`. |
| 68 |
|
| 69 |
Node.js follows symbolic links even when they point outside the allowed paths. Hugo therefore fails the build if any allowed path contains a symbolic link whose target resolves outside the allowed set. To permit such a link, add its target to the list. The check runs once per build; if you also grant write access to an allowed path, a Node.js tool can create links at build time that escape this check. |
| 70 |
|
| 71 |
`node.permissions.allowWorker` |
| 72 |
: {{< new-in 0.161.0 />}} |
| 73 |
: (`[]string`) A slice of Node.js tool names permitted to spawn worker threads (`--allow-worker`). |
| 74 |
|
| 75 |
`node.permissions.allowWrite` |
| 76 |
: {{< new-in 0.161.0 />}} |
| 77 |
: (`[]string`) A slice of file system paths that Node.js tools are allowed to write (`--allow-fs-write`). Paths are relative to the working directory; `"."` means the working directory itself. Use `"*"` to allow all paths. |
| 78 |
|
| 79 |
## Negation rules |
| 80 |
|
| 81 |
{{< new-in 0.161.0 />}} |
| 82 |
|
| 83 |
Any pattern in an allowlist can be negated by prefixing it with an exclamation mark (`!`) and one space to turn it into a deny rule. Deny rules take precedence over allow rules. An allowlist composed entirely of deny rules implicitly allows everything it does not deny. An empty allowlist rejects everything. |
| 84 |
|
| 85 |
For example, to allow all URLs except those pointing to `evil.example.com`: |
| 86 |
|
| 87 |
```toml |
| 88 |
[security.http] |
| 89 |
urls = ['.*', '! ^https?://evil\.example\.com'] |
| 90 |
``` |
| 91 |
|
| 92 |
Setting an allowlist to the string `none` will completely disable the associated feature. |
| 93 |
|
| 94 |
## Environment variables |
| 95 |
|
| 96 |
You can also override your project configuration with environment variables. For example, to block `resources.GetRemote` from accessing any URL: |
| 97 |
|
| 98 |
```txt |
| 99 |
export HUGO_SECURITY_HTTP_URLS=none |
| 100 |
``` |
| 101 |
|
| 102 |
Learn more about [using environment variables][] to configure your site. |
| 103 |
|
| 104 |
[`os.Getenv`]: /functions/os/getenv/ |
| 105 |
[`resources.GetRemote`]: /functions/resources/getremote/ |
| 106 |
[classification]: /content-management/formats/#classification |
| 107 |
[inline shortcodes]: /content-management/shortcodes/#inline |
| 108 |
[permission model]: https://nodejs.org/api/permissions.html#permission-model |
| 109 |
[using environment variables]: /configuration/introduction/#environment-variables |