Repository navigation
Expand file tree
/
Copy pathvalidate.go
More file actions
329 lines (303 loc) · 9.88 KB
/
Copy pathvalidate.go
File metadata and controls
329 lines (303 loc) · 9.88 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package verify
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"unicode/utf8"
)
const maxDisplayBytes = 128
const maxIdentifierBytes = 64
type admittedPlan struct {
id string
profiles []Profile
}
func admit(root string, plan Plan) (admittedPlan, error) {
return admitWithCancellation(root, plan, func() error { return nil })
}
func admitWithCancellation(root string, plan Plan, cancelled func() error) (admittedPlan, error) {
if !validPlanHeader(root, plan) {
return admittedPlan{}, ErrInvalidPlan
}
if !validPlanAggregate(plan, MaxPlanControls, MaxPlanBytes) {
return admittedPlan{}, ErrInvalidPlan
}
root = filepath.Clean(root)
information, err := os.Stat(root)
if err != nil || information == nil || !information.IsDir() {
return admittedPlan{}, errors.Join(ErrInvalidPlan, err)
}
result := admittedPlan{id: plan.ID, profiles: make([]Profile, len(plan.Profiles))}
seen := make(map[string]bool, len(plan.Profiles))
executables := make(map[string]string)
directories := map[string]string{"": root}
for index, profile := range plan.Profiles {
if err = cancelled(); err != nil {
return admittedPlan{}, err
}
admitted, err := admitProfile(root, profile, seen, executables, directories, cancelled)
if err != nil {
return admittedPlan{}, err
}
result.profiles[index] = admitted
}
return result, nil
}
func validPlanHeader(root string, plan Plan) bool {
return filepath.IsAbs(root) && len(root) <= MaxPathBytes && validIdentifier(plan.ID) &&
len(plan.Profiles) > 0 && len(plan.Profiles) <= MaxProfiles
}
func validPlanAggregate(plan Plan, maximumControls, maximumBytes int) bool {
if maximumControls <= 0 || maximumBytes <= 0 {
return false
}
controls := 0
bytes := len(plan.ID)
for _, profile := range plan.Profiles {
if len(profile.ID) > maximumBytes-bytes {
return false
}
bytes += len(profile.ID)
if len(profile.Controls) > maximumControls-controls {
return false
}
controls += len(profile.Controls)
for _, control := range profile.Controls {
command := control.Command
if len(command.Arguments) > MaxArguments || len(command.Environment) > MaxEnvironment {
return false
}
material := len(control.ID) + len(control.Name) + len(command.Executable) + len(command.Directory) +
len(command.Input) + len(command.ExpectedStdout) + len(command.ExpectedStderr)
for _, value := range command.Arguments {
material += len(value)
}
for _, value := range command.Environment {
material += len(value)
}
if material > maximumBytes-bytes {
return false
}
bytes += material
}
}
return controls != 0
}
func admitProfile(
root string,
profile Profile,
profiles map[string]bool,
executables map[string]string,
directories map[string]string,
cancelled func() error,
) (Profile, error) {
if !validIdentifier(profile.ID) || profiles[profile.ID] || len(profile.Controls) == 0 || len(profile.Controls) > MaxControls {
return Profile{}, ErrInvalidPlan
}
profiles[profile.ID] = true
result := Profile{ID: profile.ID, Controls: make([]Control, len(profile.Controls))}
seen := make(map[string]bool, len(profile.Controls))
for index, control := range profile.Controls {
if err := cancelled(); err != nil {
return Profile{}, err
}
admitted, err := admitControl(root, control, seen, executables, directories)
if err != nil {
return Profile{}, err
}
result.Controls[index] = admitted
}
return result, nil
}
func admitControl(root string, control Control, controls map[string]bool, executables map[string]string, directories map[string]string) (Control, error) {
if !validIdentifier(control.ID) || controls[control.ID] || !displayText(control.Name) {
return Control{}, ErrInvalidPlan
}
controls[control.ID] = true
command, err := admitCommand(root, control.Command, executables, directories)
if err != nil {
return Control{}, err
}
return Control{ID: control.ID, Name: control.Name, Command: command}, nil
}
func admitCommand(root string, command Command, executables map[string]string, directories map[string]string) (Command, error) {
if !validCommandBounds(command) {
return Command{}, ErrInvalidPlan
}
executable, found := executables[command.Executable]
if !found {
var err error
executable, err = canonicalExecutable(command.Executable)
if err != nil {
return Command{}, errors.Join(ErrInvalidPlan, err)
}
executables[command.Executable] = executable
}
directory, err := admittedDirectoryCached(root, command.Directory, directories)
if err != nil {
return Command{}, err
}
arguments, err := boundedStrings(command.Arguments, false)
if err != nil {
return Command{}, err
}
environment, err := boundedStrings(command.Environment, true)
if err != nil {
return Command{}, err
}
return Command{
Executable: executable, Arguments: arguments, Directory: directory, Environment: environment,
Input: append([]byte(nil), command.Input...), Timeout: command.Timeout, OutputLimit: command.OutputLimit,
ShowSuccessOutput: command.ShowSuccessOutput,
ExpectedStdout: cloneBytes(command.ExpectedStdout), ExpectedStderr: cloneBytes(command.ExpectedStderr),
}, nil
}
func validCommandBounds(command Command) bool {
return validCommandPath(command) && validCommandResources(command) && validCommandExpectedOutput(command)
}
func validCommandPath(command Command) bool {
return filepath.IsAbs(command.Executable) && command.Executable == filepath.Clean(command.Executable) && len(command.Executable) <= MaxPathBytes
}
func validCommandResources(command Command) bool {
return command.Timeout > 0 && command.Timeout <= MaxTimeout && command.OutputLimit > 0 && command.OutputLimit <= MaxOutputBytes &&
len(command.Arguments) <= MaxArguments && len(command.Environment) <= MaxEnvironment && len(command.Input) <= MaxInputBytes
}
func validCommandExpectedOutput(command Command) bool {
return len(command.ExpectedStdout) <= command.OutputLimit && len(command.ExpectedStderr) <= command.OutputLimit &&
len(command.ExpectedStderr) <= MaxExpectedBytes && len(command.ExpectedStdout) <= MaxExpectedBytes-len(command.ExpectedStderr)
}
func cloneBytes(value []byte) []byte {
if value == nil {
return nil
}
return append([]byte{}, value...)
}
func canonicalExecutable(path string) (string, error) {
return canonicalExecutablePlatform(path)
}
func canonicalExecutableWith(path string, evaluate func(string) (string, error), stat func(string) (os.FileInfo, error)) (string, error) {
resolved, err := evaluate(path)
if err != nil {
return "", err
}
resolved = filepath.Clean(resolved)
information, err := stat(resolved)
if err != nil {
return "", err
}
if !information.Mode().IsRegular() || !filepath.IsAbs(resolved) || len(resolved) > MaxPathBytes {
return "", errors.New("executable is not a regular file")
}
return resolved, nil
}
func admittedDirectoryCached(root, directory string, admitted map[string]string) (string, error) {
if result, found := admitted[directory]; found {
return result, nil
}
native := filepath.FromSlash(directory)
if filepath.IsAbs(native) || native != filepath.Clean(native) || native == "." || native == ".." || strings.HasPrefix(native, ".."+string(filepath.Separator)) || len(native) > MaxPathBytes {
return "", ErrInvalidPlan
}
opened, err := os.OpenRoot(root)
if err != nil {
return "", errors.Join(ErrInvalidPlan, err)
}
information, statErr := opened.Stat(native)
if err := errors.Join(statErr, opened.Close()); err != nil || information == nil || !information.IsDir() {
return "", errors.Join(ErrInvalidPlan, err)
}
result := filepath.Join(root, native)
admitted[directory] = result
return result, nil
}
func boundedStrings(values []string, environment bool) ([]string, error) {
result := make([]string, len(values))
var environmentNames [MaxEnvironment]string
environmentCount := 0
maximum := MaxArgumentBytes
if environment {
maximum = MaxEnvironmentBytes
}
total := 0
for index, value := range values {
if !utf8.ValidString(value) || strings.IndexByte(value, 0) >= 0 {
return nil, ErrInvalidPlan
}
if len(value) > maximum-total {
return nil, ErrInvalidPlan
}
total += len(value)
if environment {
name, _, found := strings.Cut(value, "=")
if !found || !validEnvironmentName(name) || duplicateEnvironmentName(environmentNames[:environmentCount], name) {
return nil, ErrInvalidPlan
}
environmentNames[environmentCount] = name
environmentCount++
}
result[index] = value
}
return result, nil
}
func validIdentifier(value string) bool {
if len(value) == 0 || len(value) > maxIdentifierBytes || value[0] < 'a' || value[0] > 'z' {
return false
}
for index := 1; index < len(value); index++ {
character := value[index]
if character < 'a' || character > 'z' {
if character < '0' || character > '9' {
if character != '_' && character != '-' {
return false
}
}
}
}
return true
}
func validEnvironmentName(value string) bool {
if value == "" || !environmentNameStart(value[0]) {
return false
}
for index := 1; index < len(value); index++ {
if !environmentNamePart(value[index]) {
return false
}
}
return true
}
func environmentNameStart(value byte) bool {
return value == '_' || value >= 'A' && value <= 'Z' || value >= 'a' && value <= 'z'
}
func environmentNamePart(value byte) bool {
return environmentNameStart(value) || value >= '0' && value <= '9'
}
func duplicateEnvironmentName(previous []string, name string) bool {
for _, value := range previous {
if strings.EqualFold(value, name) {
return true
}
}
return false
}
func displayText(value string) bool {
if value == "" || len(value) > maxDisplayBytes || !utf8.ValidString(value) {
return false
}
for _, character := range value {
if !strconv.IsPrint(character) {
return false
}
}
return true
}
func selectedProfile(plan admittedPlan, id string) (Profile, error) {
for _, profile := range plan.profiles {
if profile.ID == id {
return profile, nil
}
}
return Profile{}, fmt.Errorf("%w: unknown profile %q", ErrInvalidPlan, id)
}