1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
# safetext
**This is not an officially supported Google product.**
Safe-by-construction libraries for producing formats like YAML, to replace
syntax-unaware libraries like `text/template` and `sprintf` that are at risk of
injection vulnerabilities.
## Example use-case
Since `text/template` is not syntax-aware of the formats it produces, it does
not offer any protection against injection vulnerabilities.
Consider the following `produceConfig` function which uses `text/template` to
generate YAML:
```
package main
import (
"bytes"
"fmt"
"text/template"
)
func produceConfig(params any) (error, string) {
tmpl, _ := template.New("test").Parse("{ hello: {{ .addressee }} }")
var buf bytes.Buffer
err := tmpl.Execute(&buf, params)
if err != nil {
return err, ""
}
return nil, buf.String()
}
func main() {
goodReplacements := map[string]interface{}{
"addressee": "safe",
}
err, config := produceConfig(goodReplacements)
if err == nil {
fmt.Println(config)
} else {
fmt.Printf("Error: %v\n", err)
}
badReplacements := map[string]interface{}{
"addressee": "world, oops: true",
}
err, config = produceConfig(badReplacements)
if err == nil {
fmt.Println(config)
} else {
fmt.Printf("Error: %v\n", err)
}
}
```
This program demonstrates how a malicious `addressee` input can cause injection
of new YAML keys in the template execution result.
With `text/template`, no errors will be encountered when this happens, and the
program output will be:
```
{ hello: safe }
{ hello: world, oops: true }
```
By instead switching from `text/template`to `safetext/yamltemplate`, the
injection would have been prevented, with the output instead being:
```
{ hello: safe }
Error: YAML Injection Detected
```
## Instructions for `text/template` replacements
Injection detection is automatically applied when accessing input data fields.
- It can also be manually enabled on the result of any function call:
```
{{ RetrieveUntrustedData | ApplyInjectionDetection }}
```
- The injection logic can be disabled on certain fields by applying the
`StructuralData` annotation:
```
{{ (StructuralData .x) }}
```
- The `StructuralData` annotation is also needed when passing an input to a
function where the input should not be mutated, such as a performing some
kind of lookup:
```
name: {{ readFile (StructuralData .pathToName) | ApplyInjectionDetection }}
```
- It is recommended to make full use of `text/template` features like
conditional expressions, range loops, etc to avoid the `StructuralData`
annotation where possible. For example, instead of:
```
properties:
{{ (StructuralData .PropertiesYaml) }}
```
Consider:
```
properties:{{ range .Properties }}
- {{ . }}{{ end }}
```
### `yamltemplate`
The intention of `yamltemplate` is to ensure that by-default none of the strings
in the input data affect the structure of the resultant YAML (just the values).
- For example, the below template would be compatible with yamltemplate as-is,
whilst automatically preventing any injections from the `Name` input:
```
name: {{.Name}}
```
- However, any template nodes that *are* expected to change the resultant YAML
structure, such as inserting arbitrary YAML config, would need to be
annotated explicitly as `StructuralData`:
```
config: {{ (StructuralData .Config) }}
```
- Another case of needing the `StructuralData` annotation would be where you
need to include a complete map into the yaml structure. Using
`StructuralData` alone may let injections pass through via the key so we
need an extra layer of validation here:
~~~
labels:
{{- range $key, $value := .Labels }}
{{ (StructuralData $key | MapKey) }}: {{ $value }}
{{- end }}
```
The corresponding golang side could look like this:
~~~
func mapKeyFunc(data any) (string, error) { if v, ok := data.(string); ok {
matched, err := regexp.MatchString(`^[a-zA-Z0-9/\-.]+$`, v) if err != nil {
return "", err } if !matched { return "", fmt.Errorf("invalid characters in
the key: %v", v) } return v, nil }
```
return "", errors.New("invalid input")
```
} ...
tmp:= template.New("something") tmp.Funcs(map[string]any{"MapKey":
mapKeyFunc}) tmpl := template.Must(tmp.Parse(yamlTemplate)) ```
- You can combine `yamltemplate` with `shprintf`. Consider the following
cloud-init yaml template:
```
---
write_files:
- path: /etc/nginx/refresh.sh
owner: root:root
permissions: 0755 # Don't forget the 0 (you are probably using octal...)
content: |
#!/bin/bash
set -euo pipefail
{{ shprintf `curl %s > /tmp/something` .userInput }}
```
Evaluating this template with safetext/yamltemplate, both shell command and
YAML injections will be prevented.
To do this, you need to setup the golang side like this:
```
tmp:= addons.WithShsprintf(template.New("something"))
tmpl := template.Must(tmp.Parse(yamlTemplate))
```
#### Unsupported use cases for `yamltemplate`
- YAML with duplicate keys. Duplicate keys are non-standard YAML, and not
supported by this library. Please refactor your YAML template to remove
duplicate keys. For example:
```
- project:
members: member-a
members: member-b
```
To:
```
- project:
members: member-b
```
### `shtemplate`
`shtemplate` is designed to allow you to generate shell scripts with the
guarantee that none of the input data strings will be able to inject new
commands or flags, without explicit annotation.
- For example, a template script designed to just print one string will fail
to render if that string injects a new command `` `./evil` ``:
```
echo "{{ .addressee }}"
```
- To explicitly allow an input string to contain new commands not from the
template string, the `StructuralData` annotation can be used:
```
{{ (StructuralData .commands) }}
```
- Flags (arguments starting with `-`) are also forbidden by-default. For
example, the below template will fail to render if `Filename` is
`--interactive`:
```
git add {{ .Filename }}
```
- To explicitly allow an input string passed as a command argument to be a
flag, the `AllowFlags` annotation can be used:
```
git add {{ (AllowFlags .FilenameOrGitAddFlag) }}
```
- Multiple arguments from a single input string is also forbidden by-default.
This construct should instead be implemented using an array and `range`
expression:
```
ls {{ range .Paths }}{{.}} {{end}}
```
### Unsupported use-cases for `text/template` replacements
- Escaping logic outside of the templating system. Instead, you should
annotate the escaping logic into your template (EG: `.UntrustedField |
escape`).
- Partial formats. The libraries are designed to be used for generating
complete files. If you generate segments and then concatenate them together,
you should instead move this logic into the templating system itself (using
constructs like `if` or `range`).
- Functions with side effects. The libraries work by performing multiple
template executions, so if you register functions that have side effects,
this could cause unexpected behaviour (EG: `id: {{ AllocateID }}`).
## `shsprintf`
`shsprintf` is designed to allow you to generate shell scripts with the
guarantee that none of the input data strings will be able to inject new
commands or flags regardless of potentially incorrect escaping. See the below
example, which will return the error `shsprintf.ErrShInjection` instead of the
script with an injected command:
```
message := "`whoami`"
result, err := shsprintf.Sprintf("git commit -m %s", message)
```
`shsprintf.Sprintf` adds an error return value compared to `fmt.Sprintf`, but
the API is otherwise the same. `shsprintf.MustSprintf` is available for cases
where panic is acceptable.
`shsprintf` comes with an escaping function that is recommended for use:
```
message := "`whoami`"
result := shsprintf.MustSprintf("git commit -m %s", shsprintf.EscapeDefaultContext(message))
```
Unlike with `text/template` there are no special annotations. If you need to
pass multiple arguments for example, this should be done by altering the format
string:
```
files := []any{ "file1", "file2", "file3" }
result, err := shsprintf.Sprintf("cat" + strings.Repeat(" %s", len(files)), files...)
```
|