-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtest.rs
396 lines (320 loc) · 14.3 KB
/
test.rs
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#![allow(deprecated)]
fn sink<T>(_: T) { }
// --- tests ---
fn test_env_vars() {
sink(std::env::var("HOME")); // $ Alert[rust/summary/taint-sources] hasTaintFlow="HOME"
sink(std::env::var_os("PATH")); // $ Alert[rust/summary/taint-sources] hasTaintFlow="PATH"
let var1 = std::env::var("HOME").expect("HOME not set"); // $ Alert[rust/summary/taint-sources]
let var2 = std::env::var_os("PATH").unwrap(); // $ Alert[rust/summary/taint-sources]
sink(var1); // $ hasTaintFlow="HOME"
sink(var2); // $ hasTaintFlow="PATH"
for (key, value) in std::env::vars() { // $ Alert[rust/summary/taint-sources]
sink(key); // $ MISSING: hasTaintFlow
sink(value); // $ MISSING: hasTaintFlow
}
for (key, value) in std::env::vars_os() { // $ Alert[rust/summary/taint-sources]
sink(key); // $ MISSING: hasTaintFlow
sink(value); // $ MISSING: hasTaintFlow
}
}
fn test_env_args() {
let args: Vec<String> = std::env::args().collect(); // $ Alert[rust/summary/taint-sources]
let my_path = &args[0];
let arg1 = &args[1];
let arg2 = std::env::args().nth(2).unwrap(); // $ Alert[rust/summary/taint-sources]
let arg3 = std::env::args_os().nth(3).unwrap(); // $ Alert[rust/summary/taint-sources]
let arg4 = std::env::args().nth(4).unwrap().parse::<usize>().unwrap(); // $ Alert[rust/summary/taint-sources]
sink(my_path); // $ hasTaintFlow
sink(arg1); // $ hasTaintFlow
sink(arg2); // $ hasTaintFlow
sink(arg3); // $ hasTaintFlow
sink(arg4); // $ hasTaintFlow
for arg in std::env::args() { // $ Alert[rust/summary/taint-sources]
sink(arg); // $ hasTaintFlow
}
for arg in std::env::args_os() { // $ Alert[rust/summary/taint-sources]
sink(arg); // $ hasTaintFlow
}
}
fn test_env_dirs() {
let dir = std::env::current_dir().expect("FAILED"); // $ Alert[rust/summary/taint-sources]
let exe = std::env::current_exe().expect("FAILED"); // $ Alert[rust/summary/taint-sources]
let home = std::env::home_dir().expect("FAILED"); // $ Alert[rust/summary/taint-sources]
sink(dir); // $ hasTaintFlow
sink(exe); // $ hasTaintFlow
sink(home); // $ hasTaintFlow
}
async fn test_reqwest() -> Result<(), reqwest::Error> {
let remote_string1 = reqwest::blocking::get("example.com")?.text()?; // $ Alert[rust/summary/taint-sources]
sink(remote_string1); // $ hasTaintFlow="example.com"
let remote_string2 = reqwest::blocking::get("example.com").unwrap().text().unwrap(); // $ Alert[rust/summary/taint-sources]
sink(remote_string2); // $ hasTaintFlow="example.com"
let remote_string3 = reqwest::blocking::get("example.com").unwrap().text_with_charset("utf-8").unwrap(); // $ Alert[rust/summary/taint-sources]
sink(remote_string3); // $ hasTaintFlow="example.com"
let remote_string4 = reqwest::blocking::get("example.com").unwrap().bytes().unwrap(); // $ Alert[rust/summary/taint-sources]
sink(remote_string4); // $ hasTaintFlow="example.com"
let remote_string5 = reqwest::get("example.com").await?.text().await?; // $ Alert[rust/summary/taint-sources]
sink(remote_string5); // $ MISSING: hasTaintFlow
let remote_string6 = reqwest::get("example.com").await?.bytes().await?; // $ Alert[rust/summary/taint-sources]
sink(remote_string6); // $ MISSING: hasTaintFlow
let mut request1 = reqwest::get("example.com").await?; // $ Alert[rust/summary/taint-sources]
while let Some(chunk) = request1.chunk().await? {
sink(chunk); // $ MISSING: hasTaintFlow
}
Ok(())
}
use std::io::Write;
use http_body_util::BodyExt;
async fn test_hyper_http(case: i64) -> Result<(), Box<dyn std::error::Error>> {
// using http + hyper libs to fetch a web page
let address = "example.com:80";
let url = "http://example.com/";
// create the connection
println!("connecting to {}...", address);
let stream = tokio::net::TcpStream::connect(address).await?;
let io = hyper_util::rt::TokioIo::new(stream);
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
// drive the HTTP connection
tokio::task::spawn(async move {
conn.await.expect("connection failed");
});
// make the request
println!("sending request...");
if (case == 0) {
// simple flow case
let request = http::Request::builder().uri(url).body(String::from(""))?;
let mut response = sender.send_request(request).await?; // $ Alert[rust/summary/taint-sources]
sink(&response); // $ hasTaintFlow=request
sink(response); // $ hasTaintFlow=request
return Ok(())
}
// more realistic uses of results...
let request = http::Request::builder().uri(url).body(String::from(""))?;
let mut response = sender.send_request(request).await?; // $ Alert[rust/summary/taint-sources]
sink(&response); // $ hasTaintFlow=request
if !response.status().is_success() {
return Err("request failed".into())
}
match case {
1 => {
sink(response.body()); // $ MISSING: hasTaintFlow
sink(response.body_mut()); // $ MISSING: hasTaintFlow
let body = response.into_body();
sink(&body); // $ MISSING: hasTaintFlow
println!("awaiting response...");
let data = body.collect().await?;
sink(&data); // $ MISSING: hasTaintFlow
let bytes = data.to_bytes();
println!("bytes = {:?}", &bytes);
sink(bytes); // $ MISSING: hasTaintFlow
}
2 => {
println!("streaming response...");
while let Some(frame) = response.frame().await {
if let Some(data) = frame?.data_ref() {
std::io::stdout().write_all(data)?;
sink(data); // $ MISSING: hasTaintFlow
sink(data[0]); // $ MISSING: hasTaintFlow
for byte in data {
sink(byte); // $ MISSING: hasTaintFlow
}
}
}
}
3 => {
let headers = response.headers();
if headers.contains_key(http::header::CONTENT_TYPE) {
println!("CONTENT_TYPE = {}", response.headers()[http::header::CONTENT_TYPE].to_str().unwrap());
sink(&headers[http::header::CONTENT_TYPE]); // $ MISSING: hasTaintFlow
sink(headers[http::header::CONTENT_TYPE].to_str().unwrap()); // $ MISSING: hasTaintFlow
sink(headers[http::header::CONTENT_TYPE].as_bytes()); // $ MISSING: hasTaintFlow
sink(headers.get(http::header::CONTENT_TYPE).unwrap()); // $ MISSING: hasTaintFlow
}
if headers.contains_key("Content-type") {
println!("Content-type = {}", response.headers().get("Content-type").unwrap().to_str().unwrap());
sink(headers.get("Content-type").unwrap()); // $ MISSING: hasTaintFlow
sink(headers.get("Content-type").unwrap().to_str().unwrap()); // $ MISSING: hasTaintFlow
sink(headers.get("Content-type").unwrap().as_bytes()); // $ MISSING: hasTaintFlow
sink(&headers["Content-type"]); // $ MISSING: hasTaintFlow
}
if headers.contains_key(http::header::COOKIE) {
sink(response.headers().get(http::header::COOKIE)); // $ MISSING: hasTaintFlow
for cookie in headers.get_all(http::header::COOKIE) {
println!("cookie = {}", cookie.to_str().unwrap());
sink(cookie); // $ MISSING: hasTaintFlow
sink(cookie.to_str().unwrap()); // $ MISSING: hasTaintFlow
}
}
let (parts, body) = response.into_parts();
if parts.headers.contains_key(http::header::CONTENT_TYPE) {
println!("CONTENT_TYPE = {}", parts.headers[http::header::CONTENT_TYPE].to_str().unwrap());
sink(&parts.headers[http::header::CONTENT_TYPE]); // $ MISSING: hasTaintFlow
sink(parts.headers[http::header::CONTENT_TYPE].to_str().unwrap()); // $ MISSING: hasTaintFlow
sink(parts.headers[http::header::CONTENT_TYPE].as_bytes()); // $ MISSING: hasTaintFlow
sink(parts.headers.get(http::header::CONTENT_TYPE).unwrap()); // $ MISSING: hasTaintFlow
}
sink(body); // $ MISSING: hasTaintFlow
}
_ => {}
}
Ok(())
}
use std::fs;
fn test_fs() -> Result<(), Box<dyn std::error::Error>> {
{
let buffer: Vec<u8> = std::fs::read("file.bin")?; // $ Alert[rust/summary/taint-sources]
sink(buffer); // $ hasTaintFlow="file.bin"
}
{
let buffer: Vec<u8> = fs::read("file.bin")?; // $ Alert[rust/summary/taint-sources]
sink(buffer); // $ hasTaintFlow="file.bin"
}
{
let buffer = fs::read_to_string("file.txt")?; // $ Alert[rust/summary/taint-sources]
sink(buffer); // $ hasTaintFlow="file.txt"
}
for entry in fs::read_dir("directory")? {
let e = entry?;
let path = e.path(); // $ Alert[rust/summary/taint-sources]
let file_name = e.file_name(); // $ Alert[rust/summary/taint-sources]
sink(path); // $ hasTaintFlow
sink(file_name); // $ hasTaintFlow
}
{
let target = fs::read_link("symlink.txt")?; // $ Alert[rust/summary/taint-sources]
sink(target); // $ hasTaintFlow="symlink.txt"
}
Ok(())
}
use std::io::Read;
use std::io::BufRead;
fn test_io_fs() -> std::io::Result<()> {
// --- stdin ---
{
let mut buffer = [0u8; 100];
let _bytes = std::io::stdin().read(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
sink(&buffer); // $ hasTaintFlow
}
{
let mut buffer = Vec::<u8>::new();
let _bytes = std::io::stdin().read_to_end(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
sink(&buffer); // $ MISSING: hasTaintFlow
}
{
let mut buffer = String::new();
let _bytes = std::io::stdin().read_to_string(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
sink(&buffer); // $ hasTaintFlow
}
{
let mut buffer = [0; 100];
std::io::stdin().read_exact(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
sink(&buffer); // $ hasTaintFlow
}
for byte in std::io::stdin().bytes() { // $ Alert[rust/summary/taint-sources]
sink(byte); // $ hasTaintFlow
}
// --- file ---
let mut file = std::fs::File::open("file.txt")?; // $ Alert[rust/summary/taint-sources]
{
let mut buffer = [0u8; 100];
let _bytes = file.read(&mut buffer)?;
sink(&buffer); // $ hasTaintFlow="file.txt"
}
{
let mut buffer = Vec::<u8>::new();
let _bytes = file.read_to_end(&mut buffer)?;
sink(&buffer); // $ hasTaintFlow="file.txt"
}
{
let mut buffer = String::new();
let _bytes = file.read_to_string(&mut buffer)?;
sink(&buffer); // $ hasTaintFlow="file.txt"
}
{
let mut buffer = [0; 100];
file.read_exact(&mut buffer)?;
sink(&buffer); // $ hasTaintFlow="file.txt"
}
for byte in file.bytes() {
sink(byte); // $ hasTaintFlow="file.txt"
}
// --- BufReader ---
{
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
let data = reader.fill_buf()?;
sink(&data); // $ hasTaintFlow
}
{
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
let data = reader.buffer();
sink(&data); // $ hasTaintFlow
}
{
let mut buffer = String::new();
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
reader.read_line(&mut buffer)?;
sink(&buffer); // $ hasTaintFlow
}
{
let mut buffer = Vec::<u8>::new();
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
reader.read_until(b',', &mut buffer)?;
sink(&buffer); // $ hasTaintFlow
}
{
let mut buffer = Vec::<u8>::new();
let mut reader_split = std::io::BufReader::new(std::io::stdin()).split(b','); // $ Alert[rust/summary/taint-sources]
while let Some(chunk) = reader_split.next() {
sink(chunk.unwrap()); // $ MISSING: hasTaintFlow
}
}
{
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
for line in reader.lines() {
sink(line); // $ hasTaintFlow
}
}
{
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
let line = reader.lines().nth(1).unwrap();
sink(line.unwrap().clone()); // $ MISSING: hasTaintFlow
}
{
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
let lines: Vec<_> = reader.lines().collect();
sink(lines[1].as_ref().unwrap().clone()); // $ MISSING: hasTaintFlow
}
// --- misc operations ---
{
let mut buffer = String::new();
let mut file1 = std::fs::File::open("file.txt")?; // $ Alert[rust/summary/taint-sources]
let mut file2 = std::fs::File::open("another_file.txt")?; // $ Alert[rust/summary/taint-sources]
let mut reader = file1.chain(file2);
reader.read_to_string(&mut buffer)?;
sink(&buffer); // $ hasTaintFlow="file.txt" hasTaintFlow="another_file.txt"
}
{
let mut buffer = String::new();
let mut file1 = std::fs::File::open("file.txt")?; // $ Alert[rust/summary/taint-sources]
let mut reader = file1.take(100);
reader.read_to_string(&mut buffer)?;
sink(&buffer); // $ hasTaintFlow="file.txt"
}
{
let mut buffer = String::new();
let _bytes = std::io::stdin().lock().read_to_string(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
sink(&buffer); // $ hasTaintFlow
}
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let case = std::env::args().nth(1).unwrap_or(String::from("1")).parse::<i64>().unwrap(); // $ Alert[rust/summary/taint-sources]
println!("test_hyper_http...");
match futures::executor::block_on(test_hyper_http(case)) {
Ok(_) => println!("complete"),
Err(e) => println!("error: {}", e),
}
println!("");
Ok(())
}