Skip to content

Commit 7f3062e

Browse files
author
Hela Bot
committed
Added whitelist secret feature
1 parent 6ce7e4d commit 7f3062e

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

src/main.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use utils::pipeline;
99

1010
async fn execute_scan(
1111
scan_type: &str,
12+
mongo_uri: &str,
1213
path: &str,
1314
base_branch: Option<&str>,
1415
pr_branch: Option<&str>,
@@ -28,6 +29,7 @@ async fn execute_scan(
2829

2930
scanner
3031
.execute_scan(
32+
mongo_uri,
3133
scan_type,
3234
path,
3335
base_branch,
@@ -188,6 +190,7 @@ async fn main() {
188190
if is_sast {
189191
execute_scan(
190192
"sast",
193+
&mongo_uri,
191194
&path,
192195
Some(&base_branch),
193196
pr_branch_option,
@@ -204,6 +207,7 @@ async fn main() {
204207
if is_sca {
205208
execute_scan(
206209
"sca",
210+
&mongo_uri,
207211
&path,
208212
Some(&base_branch),
209213
pr_branch_option,
@@ -220,6 +224,7 @@ async fn main() {
220224
if is_secret {
221225
execute_scan(
222226
"secret",
227+
&mongo_uri,
223228
&path,
224229
Some(&base_branch),
225230
pr_branch_option,
@@ -236,6 +241,7 @@ async fn main() {
236241
if is_license_compliance {
237242
execute_scan(
238243
"license-compliance",
244+
&mongo_uri,
239245
&path,
240246
Some(&base_branch),
241247
pr_branch_option,

src/scans/scanner.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl ScanRunner {
2626

2727
pub async fn execute_scan(
2828
&self,
29+
mongo_uri: &str,
2930
scan_type: &str,
3031
path: &str,
3132
branch: Option<&str>,
@@ -53,7 +54,7 @@ impl ScanRunner {
5354
}
5455
"secret" => {
5556
self.secret_tool
56-
.run_scan(path, branch, pr_branch, verbose)
57+
.run_scan(path, branch, pr_branch, mongo_uri, verbose)
5758
.await
5859
}
5960
"license-compliance" => {

src/scans/tools/secret_tool.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::{fs, time::Instant};
22

33
use serde_json::{json, Value};
44

5-
use crate::utils::common::{checkout, count_env_variables, execute_command, print_error};
5+
use crate::utils::common::{
6+
checkout, count_env_variables, execute_command, list_whitelisted_secrets, print_error,
7+
};
68

79
pub struct SecretTool;
810

@@ -16,6 +18,7 @@ impl SecretTool {
1618
_path: &str,
1719
_branch: Option<&str>,
1820
pr_branch: Option<&str>,
21+
mongo_uri: &str,
1922
verbose: bool,
2023
) {
2124
let start_time = Instant::now();
@@ -61,7 +64,9 @@ impl SecretTool {
6164

6265
let cmd = format!("trufflehog filesystem --no-update {} --json --exclude-detectors=FLOAT,SIGNABLE,YANDEX,OANDA,CIRCLE,PARSEUR,URI,SENTRYTOKEN,SIRV,ETSYAPIKEY,UNIFYID,MIRO,FRESHDESK,ALIBABA,YELP,FLATIO,GETRESPONSE,ATERA,GITTER,SONARCLOUD,AZURESEARCHADMINKEY", _path);
6366
let output_data = execute_command(&cmd, true).await;
67+
6468
let mut results: Vec<Value> = Vec::new();
69+
6570
for line in output_data.lines() {
6671
let json_output: serde_json::Value =
6772
serde_json::from_str(&line).expect("Error parsing JSON");
@@ -100,6 +105,26 @@ impl SecretTool {
100105
continue;
101106
}
102107
}
108+
// Check if the detected secret is whitelisted
109+
if !mongo_uri.is_empty() {
110+
// Fetch whitelisted secrets from MongoDB
111+
let whitelisted_secrets = match list_whitelisted_secrets(mongo_uri).await {
112+
Ok(secrets) => secrets,
113+
Err(e) => {
114+
eprintln!("Error fetching whitelisted secrets: {}", e);
115+
continue; // You might want to handle the error differently
116+
}
117+
};
118+
119+
// Check if the detected secret is in the whitelisted secrets
120+
if let Some(raw_value) = result["Raw"].as_str() {
121+
if whitelisted_secrets.contains(&raw_value.to_string()) {
122+
println!("[+] Skipping because {} is whitelisted...", raw_value);
123+
continue;
124+
}
125+
}
126+
}
127+
103128
new_results.push(result.clone());
104129
}
105130
results = new_results;

src/utils/common.rs

+24
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ pub async fn bulk_check_hash_exists(
102102
Ok(existing_hashes)
103103
}
104104

105+
pub async fn list_whitelisted_secrets(
106+
mongo_uri: &str,
107+
) -> Result<HashSet<String>, Box<dyn std::error::Error>> {
108+
let client = connect_to_mongodb(mongo_uri, "code-security-open-source").await?;
109+
let collection: Collection<Document> = client
110+
.database("code-security-open-source")
111+
.collection("secrets");
112+
113+
// Create the filter to match the secret
114+
let mut cursor = collection.find(None, None).await?;
115+
let mut secrets_list: HashSet<String> = HashSet::new(); // Make this mutable
116+
while let Some(doc) = cursor.next().await {
117+
match doc {
118+
Ok(document) => {
119+
if let Some(secret) = document.get_str("secret").ok() {
120+
secrets_list.insert(secret.to_string());
121+
}
122+
}
123+
Err(e) => return Err(e.into()),
124+
}
125+
}
126+
Ok(secrets_list) // Return the secrets_list
127+
}
128+
105129
pub async fn register_hash(message: &str, mongo_uri: &str) {
106130
let hashed_message = hash_text(message);
107131
match connect_to_mongodb(mongo_uri, "code-security-open-source").await {

src/utils/pipeline.rs

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub async fn pipeline_failure(
2424
product_name: String,
2525
engagement_name: String,
2626
) {
27-
// if code_path contains ghp_* thend redact that value because its token
2827
let redacted_code_path = redact_github_token(&code_path);
2928
// generate report in sarif format sast_result_sarif.json sca_result_sarif.json secret_result_sarif.json
3029
let mut total_issues = 0;

0 commit comments

Comments
 (0)