secretscout 3.1.0

Blazingly fast Rust CLI for detecting secrets, passwords, API keys, and tokens in git repositories
Documentation
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Event routing and processing module
//!
//! This module handles GitHub event parsing and routing for all supported
//! event types: push, pull_request, workflow_dispatch, and schedule.

use crate::config::Config;
use crate::error::{EventError, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::str::FromStr;

/// Supported GitHub event types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EventType {
    Push,
    PullRequest,
    WorkflowDispatch,
    Schedule,
}

/// Complete event context with all necessary information for scanning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventContext {
    pub event_type: EventType,
    pub repository: Repository,
    pub base_ref: String,
    pub head_ref: String,
    pub commits: Vec<Commit>,
    pub pull_request: Option<PullRequest>,
}

/// Repository information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Repository {
    pub owner: String,
    pub name: String,
    pub full_name: String,
    pub html_url: String,
}

/// Commit information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Commit {
    pub sha: String,
    pub author: Author,
    pub message: String,
}

/// Author information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Author {
    pub name: String,
    pub email: String,
}

/// Pull request information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PullRequest {
    pub number: i64,
    pub base: GitReference,
    pub head: GitReference,
}

/// Git reference (branch or tag)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitReference {
    pub sha: String,
    pub ref_name: String,
}

impl std::str::FromStr for EventType {
    type Err = crate::error::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "push" => Ok(EventType::Push),
            "pull_request" => Ok(EventType::PullRequest),
            "workflow_dispatch" => Ok(EventType::WorkflowDispatch),
            "schedule" => Ok(EventType::Schedule),
            _ => Err(EventError::UnsupportedEvent(s.to_string()).into()),
        }
    }
}

/// Parse event context from configuration
#[cfg(feature = "native")]
pub async fn parse_event_context(config: &Config) -> Result<EventContext> {
    // Parse event type
    let event_type = EventType::from_str(&config.event_name)?;

    // Read event JSON file
    let event_json = read_event_file(&config.event_path)?;

    // Parse repository information
    let repository = parse_repository(&event_json, config)?;

    // Route to event-specific parser
    match event_type {
        EventType::Push => parse_push_event(&event_json, repository, config).await,
        EventType::PullRequest => parse_pull_request_event(&event_json, repository, config).await,
        EventType::WorkflowDispatch => parse_workflow_dispatch_event(repository),
        EventType::Schedule => parse_schedule_event(repository),
    }
}

/// Read event JSON file
fn read_event_file(path: &Path) -> Result<serde_json::Value> {
    let contents = std::fs::read_to_string(path)
        .map_err(|e| EventError::InvalidEventJson(format!("Failed to read event file: {}", e)))?;

    serde_json::from_str(&contents)
        .map_err(|e| EventError::InvalidEventJson(format!("Failed to parse JSON: {}", e)).into())
}

/// Parse repository from event JSON
fn parse_repository(event_json: &serde_json::Value, config: &Config) -> Result<Repository> {
    // Try to extract repository from event
    if let Some(repo_obj) = event_json.get("repository") {
        let owner = repo_obj["owner"]["login"]
            .as_str()
            .ok_or_else(|| EventError::MissingField("repository.owner.login".to_string()))?
            .to_string();

        let name = repo_obj["name"]
            .as_str()
            .ok_or_else(|| EventError::MissingField("repository.name".to_string()))?
            .to_string();

        let full_name = repo_obj["full_name"]
            .as_str()
            .ok_or_else(|| EventError::MissingField("repository.full_name".to_string()))?
            .to_string();

        let html_url = repo_obj["html_url"]
            .as_str()
            .ok_or_else(|| EventError::MissingField("repository.html_url".to_string()))?
            .to_string();

        Ok(Repository {
            owner,
            name,
            full_name,
            html_url,
        })
    } else {
        // Fallback for schedule events where repository may be undefined
        let full_name = config.repository.clone();
        let owner = config.repository_owner.clone();
        let name = full_name
            .trim_start_matches(&format!("{}/", owner))
            .to_string();
        let html_url = format!("https://github.com/{}", full_name);

        Ok(Repository {
            owner,
            name,
            full_name,
            html_url,
        })
    }
}

/// Parse push event
#[cfg(feature = "native")]
async fn parse_push_event(
    event_json: &serde_json::Value,
    repository: Repository,
    config: &Config,
) -> Result<EventContext> {
    let commits_array = event_json["commits"]
        .as_array()
        .ok_or_else(|| EventError::MissingField("commits".to_string()))?;

    if commits_array.is_empty() {
        return Err(EventError::NoCommits.into());
    }

    let commits: Vec<Commit> = commits_array.iter().filter_map(parse_commit).collect();

    if commits.is_empty() {
        return Err(EventError::NoCommits.into());
    }

    // Determine base and head refs
    let base_ref = config
        .base_ref
        .clone()
        .unwrap_or_else(|| commits[0].sha.clone());
    let head_ref = commits.last().unwrap().sha.clone();

    Ok(EventContext {
        event_type: EventType::Push,
        repository,
        base_ref,
        head_ref,
        commits,
        pull_request: None,
    })
}

/// Parse pull request event
#[cfg(feature = "native")]
async fn parse_pull_request_event(
    event_json: &serde_json::Value,
    repository: Repository,
    config: &Config,
) -> Result<EventContext> {
    let pr_obj = event_json["pull_request"]
        .as_object()
        .ok_or_else(|| EventError::MissingField("pull_request".to_string()))?;

    let pr_number = pr_obj["number"]
        .as_i64()
        .ok_or_else(|| EventError::MissingField("pull_request.number".to_string()))?;

    let base_sha = pr_obj["base"]["sha"]
        .as_str()
        .ok_or_else(|| EventError::MissingField("pull_request.base.sha".to_string()))?
        .to_string();

    let base_ref_name = pr_obj["base"]["ref"]
        .as_str()
        .ok_or_else(|| EventError::MissingField("pull_request.base.ref".to_string()))?
        .to_string();

    let head_sha = pr_obj["head"]["sha"]
        .as_str()
        .ok_or_else(|| EventError::MissingField("pull_request.head.sha".to_string()))?
        .to_string();

    let head_ref_name = pr_obj["head"]["ref"]
        .as_str()
        .ok_or_else(|| EventError::MissingField("pull_request.head.ref".to_string()))?
        .to_string();

    let pull_request = PullRequest {
        number: pr_number,
        base: GitReference {
            sha: base_sha.clone(),
            ref_name: base_ref_name,
        },
        head: GitReference {
            sha: head_sha.clone(),
            ref_name: head_ref_name,
        },
    };

    // Fetch PR commits to determine exact scan range
    let pr_commits = crate::github::fetch_pr_commits(config, &repository, pr_number).await?;

    if pr_commits.is_empty() {
        return Err(EventError::NoCommits.into());
    }

    let base_ref = config
        .base_ref
        .clone()
        .unwrap_or_else(|| pr_commits[0].sha.clone());
    let head_ref = pr_commits.last().unwrap().sha.clone();

    Ok(EventContext {
        event_type: EventType::PullRequest,
        repository,
        base_ref,
        head_ref,
        commits: pr_commits,
        pull_request: Some(pull_request),
    })
}

/// Parse workflow dispatch event
fn parse_workflow_dispatch_event(repository: Repository) -> Result<EventContext> {
    Ok(EventContext {
        event_type: EventType::WorkflowDispatch,
        repository,
        base_ref: String::new(),
        head_ref: String::new(),
        commits: Vec::new(),
        pull_request: None,
    })
}

/// Parse schedule event
fn parse_schedule_event(repository: Repository) -> Result<EventContext> {
    Ok(EventContext {
        event_type: EventType::Schedule,
        repository,
        base_ref: String::new(),
        head_ref: String::new(),
        commits: Vec::new(),
        pull_request: None,
    })
}

/// Parse a single commit from JSON
fn parse_commit(commit_json: &serde_json::Value) -> Option<Commit> {
    Some(Commit {
        sha: commit_json["id"].as_str()?.to_string(),
        author: Author {
            name: commit_json["author"]["name"].as_str()?.to_string(),
            email: commit_json["author"]["email"].as_str()?.to_string(),
        },
        message: commit_json["message"].as_str()?.to_string(),
    })
}

/// Build log-opts for gitleaks based on event context
pub fn build_log_opts(context: &EventContext) -> String {
    match context.event_type {
        EventType::Push => {
            if context.base_ref == context.head_ref {
                // Single commit
                "-1".to_string()
            } else {
                // Range scan
                format!(
                    "--no-merges --first-parent {}^..{}",
                    context.base_ref, context.head_ref
                )
            }
        }
        EventType::PullRequest => {
            // Always range scan for PRs
            format!(
                "--no-merges --first-parent {}^..{}",
                context.base_ref, context.head_ref
            )
        }
        EventType::WorkflowDispatch | EventType::Schedule => {
            // Full repository scan - no log-opts
            String::new()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_event_type_from_str() {
        assert_eq!(EventType::from_str("push").unwrap(), EventType::Push);
        assert_eq!(
            EventType::from_str("pull_request").unwrap(),
            EventType::PullRequest
        );
        assert_eq!(
            EventType::from_str("workflow_dispatch").unwrap(),
            EventType::WorkflowDispatch
        );
        assert_eq!(
            EventType::from_str("schedule").unwrap(),
            EventType::Schedule
        );
        assert!(EventType::from_str("invalid").is_err());
    }

    #[test]
    fn test_build_log_opts() {
        let context = EventContext {
            event_type: EventType::Push,
            repository: Repository {
                owner: "owner".to_string(),
                name: "repo".to_string(),
                full_name: "owner/repo".to_string(),
                html_url: "https://github.com/owner/repo".to_string(),
            },
            base_ref: "abc123".to_string(),
            head_ref: "def456".to_string(),
            commits: Vec::new(),
            pull_request: None,
        };

        assert_eq!(
            build_log_opts(&context),
            "--no-merges --first-parent abc123^..def456"
        );

        // Single commit
        let context = EventContext {
            event_type: EventType::Push,
            repository: Repository {
                owner: "owner".to_string(),
                name: "repo".to_string(),
                full_name: "owner/repo".to_string(),
                html_url: "https://github.com/owner/repo".to_string(),
            },
            base_ref: "abc123".to_string(),
            head_ref: "abc123".to_string(),
            commits: Vec::new(),
            pull_request: None,
        };

        assert_eq!(build_log_opts(&context), "-1");

        // Full scan
        let context = EventContext {
            event_type: EventType::WorkflowDispatch,
            repository: Repository {
                owner: "owner".to_string(),
                name: "repo".to_string(),
                full_name: "owner/repo".to_string(),
                html_url: "https://github.com/owner/repo".to_string(),
            },
            base_ref: String::new(),
            head_ref: String::new(),
            commits: Vec::new(),
            pull_request: None,
        };

        assert_eq!(build_log_opts(&context), "");
    }
}