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
//! BASH002: Missing `set -o pipefail` in Pipelines
//!
//! **Rule**: Detect pipelines without `set -o pipefail`
//!
//! **Why this matters**:
//! Without `pipefail`, pipelines only check the last command's exit code:
//! - `failing_cmd | succeeding_cmd` returns 0 (hides failure!)
//! - Errors in early pipeline stages go unnoticed
//! - Silent data corruption or incomplete processing
//! - Critical for production script reliability
//!
//! **Examples**:
//!
//! ❌ **DANGEROUS** (missing pipefail):
//! ```bash
//! #!/bin/bash
//! set -e # Only catches last command in pipeline!
//! curl https://example.com/data.json | jq '.items[]'
//! # If curl fails, jq succeeds with empty input → exit 0 (wrong!)
//! ```
//!
//! ✅ **SAFE** (with pipefail):
//! ```bash
//! #!/bin/bash
//! set -eo pipefail # Catches failures anywhere in pipeline
//! curl https://example.com/data.json | jq '.items[]'
//! # If curl fails, script exits with error (correct!)
//! ```
//!
//! ## Detection Logic
//!
//! This rule detects scripts that:
//! 1. Contain pipelines (commands with `|`)
//! 2. Do NOT have `set -o pipefail` or `set -euo pipefail`
//!
//! ## Auto-fix
//!
//! Suggests adding `set -o pipefail` after shebang:
//! ```bash
//! #!/bin/bash
//! set -eo pipefail # Recommended: combine with set -e
//! ```
use crate::linter::LintResult;
use crate::linter::{Diagnostic, Severity, Span};
/// Check for missing `set -o pipefail` in scripts with pipelines
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
let mut has_pipefail = false;
let mut has_pipeline = false;
let mut first_pipeline_line = 0;
for (line_num, line) in source.lines().enumerate() {
let trimmed = line.trim();
// Strip comments
let code_only = if let Some(pos) = trimmed.find('#') {
&trimmed[..pos]
} else {
trimmed
};
let code_only = code_only.trim();
// Check for pipefail setting
if code_only.contains("set") && code_only.contains("pipefail") {
has_pipefail = true;
}
// Check for pipeline (but not in comments, strings, or certain contexts)
if code_only.contains('|') &&
!code_only.contains("||") && // Not logical OR
!is_in_string_or_regex(code_only)
{
if !has_pipeline {
first_pipeline_line = line_num;
}
has_pipeline = true;
}
}
// If script has pipelines but no pipefail, warn
if has_pipeline && !has_pipefail {
let span = Span::new(
first_pipeline_line + 1,
1,
first_pipeline_line + 1,
source.lines().nth(first_pipeline_line).unwrap_or("").len(),
);
let diag = Diagnostic::new(
"BASH002",
Severity::Warning,
"Script uses pipelines without 'set -o pipefail' - pipeline failures may be hidden (only last command's exit code is checked)",
span,
);
result.add(diag);
}
result
}
/// Check if pipe character is in a string or regex
fn is_in_string_or_regex(line: &str) -> bool {
// Simple heuristic: if line contains quotes and pipe is between them
// This is a simplified check - full parsing would be more accurate
let single_quote_count = line.matches('\'').count();
let double_quote_count = line.matches('"').count();
// If odd number of quotes, pipe might be in string
!single_quote_count.is_multiple_of(2) || !double_quote_count.is_multiple_of(2)
}
#[cfg(test)]
mod tests {
use super::*;
// RED Phase: Write failing tests first (EXTREME TDD)
/// RED TEST 1: Detect pipeline without pipefail
#[test]
fn test_BASH002_detects_pipeline_without_pipefail() {
let script = r#"#!/bin/bash
set -e
curl https://example.com/data.json | jq '.items[]'
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "BASH002");
assert_eq!(diag.severity, Severity::Warning);
assert!(diag.message.contains("pipefail"));
assert!(diag.message.contains("pipeline"));
}
/// RED TEST 2: Pass when pipefail is set
#[test]
fn test_BASH002_passes_with_pipefail() {
let script = r#"#!/bin/bash
set -eo pipefail
curl https://example.com/data.json | jq '.items[]'
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0, "Should pass with pipefail");
}
/// RED TEST 3: Pass when set -o pipefail is used (alternative syntax)
#[test]
fn test_BASH002_passes_with_set_o_pipefail() {
let script = r#"#!/bin/bash
set -e
set -o pipefail
cat file.txt | grep pattern | sort
"#;
let result = check(script);
assert_eq!(
result.diagnostics.len(),
0,
"Should pass with set -o pipefail"
);
}
/// RED TEST 4: Pass when no pipelines exist
#[test]
fn test_BASH002_passes_without_pipelines() {
let script = r#"#!/bin/bash
set -e
echo "Hello"
curl https://example.com/data.json
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0, "Should pass without pipelines");
}
/// RED TEST 5: Detect multiple pipelines without pipefail
#[test]
fn test_BASH002_detects_multiple_pipelines() {
let script = r#"#!/bin/bash
cat file1.txt | grep foo
cat file2.txt | grep bar | sort
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "BASH002");
}
/// RED TEST 6: Ignore logical OR (||)
#[test]
fn test_BASH002_ignores_logical_or() {
let script = r#"#!/bin/bash
set -e
command1 || command2
[ -f file ] || exit 1
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0, "Should ignore logical OR");
}
/// RED TEST 7: Detect pipeline in function
#[test]
fn test_BASH002_detects_pipeline_in_function() {
let script = r#"#!/bin/bash
process_data() {
cat data.json | jq '.items[]'
}
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "BASH002");
}
/// RED TEST 8: Pass with set -euo pipefail (combined flags)
#[test]
fn test_BASH002_passes_with_combined_flags() {
let script = r#"#!/bin/bash
set -euo pipefail
find . -name "*.txt" | xargs grep pattern
"#;
let result = check(script);
assert_eq!(
result.diagnostics.len(),
0,
"Should pass with combined flags"
);
}
}
#[cfg(test)]
mod property_tests {
use super::*;
use proptest::prelude::*;
proptest! {
#![proptest_config(proptest::test_runner::Config::with_cases(10))]
/// PROPERTY TEST 1: Never panics on any input
#[test]
fn prop_bash002_never_panics(s in ".*") {
let _ = check(&s);
}
/// PROPERTY TEST 2: Always detects pipeline without pipefail
#[test]
fn prop_bash002_detects_pipeline(
cmd1 in "[a-z]{3,10}",
cmd2 in "[a-z]{3,10}",
) {
let script = format!("#!/bin/bash\n{} | {}", cmd1, cmd2);
let result = check(&script);
prop_assert_eq!(result.diagnostics.len(), 1);
prop_assert_eq!(result.diagnostics[0].code.as_str(), "BASH002");
}
/// PROPERTY TEST 3: Passes when pipefail is set
#[test]
fn prop_bash002_passes_with_pipefail(
cmd1 in "[a-z]{3,10}",
cmd2 in "[a-z]{3,10}",
) {
let script = format!("#!/bin/bash\nset -o pipefail\n{} | {}", cmd1, cmd2);
let result = check(&script);
prop_assert_eq!(result.diagnostics.len(), 0);
}
}
}