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
//! SEC019: Injection Safety - Unquoted Variables
//!
//! **Rule**: Detect unquoted variable expansions that may lead to injection attacks
//!
//! **Why this matters**:
//! Unquoted variables can be exploited for command injection attacks. When user-controlled
//! data is expanded without quotes, attackers can inject shell metacharacters (`;`, `|`, `&`, etc.)
//! to execute arbitrary commands.
//!
//! **Auto-fix**: Add quotes around variable expansions
//!
//! ## Examples
//!
//! ❌ **UNSAFE** (Injection Risk):
//! ```bash
//! echo $user_input # Attacker: user_input="; rm -rf /"
//! rm -rf $dir # Attacker: dir="/; cat /etc/passwd"
//! eval $command # Critical: arbitrary code execution
//! ```
//!
//! ✅ **SAFE**:
//! ```bash
//! echo "$user_input" # Quoted: safe from injection
//! rm -rf "$dir" # Quoted: safe from injection
//! # Avoid eval entirely, or use arrays
//! ```
//!
//! ## Security Properties (Taint Tracking)
//!
//! This rule implements basic taint tracking:
//! - Variables from external sources are considered **Tainted**
//! - Unquoted tainted variables → **Injection Risk**
//! - Quoted variables → **Sanitized** (safe)
//!
//! ## Exceptions
//!
//! - Variables in `[[ ]]` test contexts (safe by design)
//! - Arithmetic expressions `$(( ))` (safe by design)
//! - Some control flow constructs where quoting not needed
use crate::linter::{Diagnostic, LintResult, Severity, Span};
/// Check for unquoted variable expansions (injection risk)
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
for (line_num, line) in source.lines().enumerate() {
check_line(line, line_num, &mut result);
}
result
}
fn check_line(line: &str, line_num: usize, result: &mut LintResult) {
let chars: Vec<char> = line.chars().collect();
let mut i = 0;
while i < chars.len() {
// Look for $VAR or ${VAR}
if chars[i] == '$' && i + 1 < chars.len() {
let start_col = i;
// Skip $$ (process ID) - special case, not user data
if chars[i + 1] == '$' {
i += 2;
continue;
}
// Skip $? $# $@ $* (special variables)
if matches!(chars[i + 1], '?' | '#' | '@' | '*' | '!' | '-') {
i += 2;
continue;
}
// Skip $(( (arithmetic expansion - safe)
if i + 2 < chars.len() && chars[i + 1] == '(' && chars[i + 2] == '(' {
i = skip_arithmetic(&chars, i);
continue;
}
// Skip $( (command substitution - check inside)
if chars[i + 1] == '(' {
i = skip_command_substitution(&chars, i);
continue;
}
// Check if variable is quoted
let is_quoted = is_variable_quoted(&chars, start_col);
// Check if in safe context
let in_safe_context = is_in_safe_context(line, start_col);
if !is_quoted && !in_safe_context {
// Unquoted variable expansion - potential injection
let var_end = find_variable_end(&chars, i);
let var_name = extract_variable_name(&chars, i, var_end);
let span = Span::new(line_num + 1, start_col + 1, line_num + 1, var_end + 1);
let message = format!(
"Unquoted variable expansion ${} - injection risk. Use \"${}\" instead",
var_name, var_name
);
let diag = Diagnostic::new("SEC019", Severity::Warning, &message, span);
result.add(diag);
}
i = find_variable_end(&chars, i);
} else {
i += 1;
}
}
}
/// Check if variable is quoted (inside "..." or '...')
fn is_variable_quoted(chars: &[char], var_pos: usize) -> bool {
let mut in_double_quotes = false;
let mut in_single_quotes = false;
for ch in chars.iter().take(var_pos) {
match ch {
'"' if !in_single_quotes => in_double_quotes = !in_double_quotes,
'\'' if !in_double_quotes => in_single_quotes = !in_single_quotes,
_ => {}
}
}
// Double quotes protect against injection, single quotes don't expand vars
in_double_quotes || in_single_quotes
}
/// Check if variable is in a safe context (e.g., [[ ]], arithmetic)
fn is_in_safe_context(line: &str, _var_pos: usize) -> bool {
// Check for [[ ]] test context (safe by design)
if line.contains("[[") && line.contains("]]") {
return true;
}
false
}
/// Find the end position of a variable expansion
fn find_variable_end(chars: &[char], start: usize) -> usize {
if start + 1 >= chars.len() {
return start + 1;
}
let mut i = start + 1; // Skip $
if chars[i] == '{' {
// ${VAR} form
i += 1;
while i < chars.len() && chars[i] != '}' {
i += 1;
}
if i < chars.len() {
i += 1; // Include closing }
}
} else {
// $VAR form
while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
i += 1;
}
}
i
}
/// Extract variable name from expansion
fn extract_variable_name(chars: &[char], start: usize, end: usize) -> String {
let mut name_start = start + 1; // Skip $
if name_start < chars.len() && chars[name_start] == '{' {
name_start += 1; // Skip {
}
let mut name_end = end;
if name_end > name_start && chars.get(name_end - 1) == Some(&'}') {
name_end -= 1; // Exclude }
}
chars[name_start..name_end.min(chars.len())]
.iter()
.collect()
}
/// Skip arithmetic expansion $(( ... ))
fn skip_arithmetic(chars: &[char], start: usize) -> usize {
let mut i = start + 3; // Skip $((
let mut depth = 1;
while i + 1 < chars.len() && depth > 0 {
if chars[i] == '(' && chars[i + 1] == '(' {
depth += 1;
i += 2;
} else if chars[i] == ')' && chars[i + 1] == ')' {
depth -= 1;
i += 2;
} else {
i += 1;
}
}
i
}
/// Skip command substitution $( ... )
fn skip_command_substitution(chars: &[char], start: usize) -> usize {
let mut i = start + 2; // Skip $(
let mut depth = 1;
while i < chars.len() && depth > 0 {
if chars[i] == '$' && i + 1 < chars.len() && chars[i + 1] == '(' {
depth += 1;
i += 2;
} else if chars[i] == ')' {
depth -= 1;
i += 1;
} else {
i += 1;
}
}
i
}
#[cfg(test)]
#[path = "sec019_tests_sec019_001.rs"]
mod tests_extracted;