-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.sh
More file actions
executable file
·186 lines (158 loc) · 5.12 KB
/
Copy pathvalidators.sh
File metadata and controls
executable file
·186 lines (158 loc) · 5.12 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
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# Input validators for the OpenGrep composite action.
#
# Each function returns 0 on success and 1 on failure (never exits) so the
# same code path is exercised by bats unit tests and by action.yml's
# composite steps. Call sites in action.yml convert a nonzero return into
# `exit 1` to abort the step.
#
# Keep error message prefixes stable; the security test suite and downstream
# consumers may match on them.
# Reject path traversal, overlong paths, and ASCII control characters.
validate_path() {
local path="$1"
local max_length=500
# Length check
if [ ${#path} -gt $max_length ]; then
printf 'Error: Path too long (max %s chars)\n' "$max_length"
return 1
fi
# Traversal protection: reject any ".." segment or percent-encoded variants
if [[ "$path" =~ (^|/)\.\.(/|$) ]] \
|| [[ "${path,,}" == *%2e%2e* ]] \
|| [[ "${path,,}" == *..%2f* ]] \
|| [[ "${path,,}" == *%2e.* ]] \
|| [[ "${path,,}" == *.%2e* ]]; then
printf 'Error: Path traversal detected in: %s\n' "$path"
return 1
fi
# Bash variables cannot contain NUL bytes; reject the other ASCII control
# bytes under a byte-oriented locale so UTF-8 input is not split with printf.
local LC_ALL=C
if [[ "$path" == *[[:cntrl:]]* ]]; then
printf 'Error: Invalid characters in path: %s\n' "$path"
return 1
fi
return 0
}
# Include pattern validator: length cap, control-char rejection, charset allowlist.
validate_include_pattern() {
local p="$1"
local max_length=500
# Length cap (same defense-in-depth cap as path-like inputs)
if [ ${#p} -gt $max_length ]; then
printf 'Error: include pattern too long (>%s chars): %s\n' "$max_length" "$p" >&2
return 1
fi
# Bash variables cannot contain NUL bytes; reject the other ASCII control
# bytes under a byte-oriented locale.
local LC_ALL=C
if [[ "$p" == *[[:cntrl:]]* ]]; then
printf 'Error: include pattern contains control characters: %s\n' "$p" >&2
return 1
fi
# Whitelist allowed characters (common glob/pattern set)
# Character class order matters in bash regex: ] must come first to be
# literal, [ is literal inside a class, - must be last to avoid being a
# range. Escaping with \] breaks the class entirely (rejects everything).
if ! [[ "$p" =~ ^[]A-Za-z0-9._*/+?={}:,@%^~[-]+$ ]]; then
printf 'Error: include pattern has invalid characters: %s\n' "$p" >&2
return 1
fi
return 0
}
# Numeric validator with per-input upper bounds. Empty values are treated
# as "input not provided" and accepted (matches prior behaviour where the
# outer `if [ -n ... ]` gated the check).
validate_numeric() {
local name="$1"
local value="$2"
[ -n "$value" ] || return 0
if ! [[ "$value" =~ ^[0-9]+$ ]]; then
printf 'Error: %s must be a positive integer\n' "$name"
return 1
fi
case "$name" in
"max-target-bytes")
if [ "$value" -gt 1073741824 ]; then # 1GB limit
printf 'Error: max-target-bytes too large (max 1GB)\n'
return 1
fi
;;
"timeout")
if [ "$value" -gt 3600 ]; then # 1 hour limit
printf 'Error: timeout too large (max 3600 seconds)\n'
return 1
fi
;;
"jobs")
if [ "$value" -gt 16 ]; then # Reasonable limit
printf 'Error: jobs too large (max 16)\n'
return 1
fi
;;
esac
return 0
}
# Boolean validator: accept only the literal strings "true" and "false".
# Empty input is accepted (treated as not provided), matching the
# original `if [ -n "$input_value" ]` gating in action.yml.
validate_boolean() {
local name="$1"
local value="$2"
[ -n "$value" ] || return 0
case "$value" in
"true"|"false") return 0 ;;
*)
printf 'Error: %s must be '\''true'\'' or '\''false'\''\n' "$name"
return 1
;;
esac
}
# OpenGrep version validator. Accepts "latest" as an explicit opt-in or a
# release tag in the form 1.2.3 / v1.2.3, with optional prerelease suffixes.
validate_opengrep_version() {
local value="$1"
[ -n "$value" ] || return 0
if [ "$value" = "latest" ]; then
return 0
fi
if [[ "$value" =~ ^v?[0-9]+[.][0-9]+[.][0-9]+([-][A-Za-z0-9][A-Za-z0-9.-]*)?$ ]]; then
return 0
fi
printf 'Error: Invalid OpenGrep version\n'
return 1
}
# SHA256 validator for user-supplied OpenGrep release checksums.
validate_sha256() {
local name="$1"
local value="$2"
[ -n "$value" ] || return 0
if [[ "$value" =~ ^[A-Fa-f0-9]{64}$ ]]; then
return 0
fi
printf 'Error: %s must be a 64-character SHA256 checksum\n' "$name"
return 1
}
# Enum validator. Usage: validate_enum <name> <value> <allowed...>
# Error messages are preserved verbatim for the two call sites in
# action.yml (output-format, severity) and fall back to a generic message
# for any future caller.
validate_enum() {
local name="$1"
local value="$2"
shift 2
local allowed
for allowed in "$@"; do
if [ "$value" = "$allowed" ]; then
return 0
fi
done
case "$name" in
"output-format") printf 'Error: Invalid output format\n' ;;
"severity") printf 'Error: Invalid severity level\n' ;;
*) printf 'Error: Invalid %s\n' "$name" ;;
esac
return 1
}