Skip to content

Commit 94bdd16

Browse files
authored
Scan repo2 (#6)
* standardize code formatting and detect local directory Signed-off-by: sonichen <[email protected]> * standardize code formatting and detect local directory Signed-off-by: sonichen <[email protected]> * delete compiled binary file Signed-off-by: sonichen <[email protected]> --------- Signed-off-by: sonichen <[email protected]>
1 parent 88efb99 commit 94bdd16

File tree

11 files changed

+1671
-625
lines changed

11 files changed

+1671
-625
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sensleak"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -19,4 +19,7 @@ chrono = "0.4"
1919
assert_cmd = "2.0.10"
2020
tempfile = "3.2.0"
2121
git2 = "0.17.1"
22-
mockito = "1.0.2"
22+
mockito = "1.0.2"
23+
csv = "1.1"
24+
log = "0.4"
25+
env_logger = "0.10.0"

README.md

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,96 +25,97 @@ Here are a few examples of how to use the tool in different scenarios:
2525

2626
- Running the tool in the command-line interface (CLI) to perform sensitive data checks.
2727

28-
**Note: This project is currently under development. The following features describe sensitive information search within a local folder.**
28+
**Note: This project is currently under development. **
2929

3030
```shell
31-
sensleaks-rs
32-
3331
Usage: sensleak.exe [OPTIONS] --repo <REPO>
3432

3533
Options:
36-
--repo <REPO> Target repository
37-
--config <CONFIG> Config path.. [default: gitleaks.toml]
38-
--report <REPORT> Path to write json leaks file [default: ]
39-
-v, --verbose Show verbose output from scan
40-
--pretty Pretty print json if leaks are present
41-
--commit <COMMIT> sha of commit to scan or "latest" to scan the last commit of the repository
42-
--commits <COMMITS> comma separated list of a commits to scan
43-
--commits-file <COMMITS_FILE> file of new line separated list of a commits to scan
44-
--commit-since <COMMIT_SINCE> Scan commits more recent than a specific date. Ex: '2006-01-02' or '2023-01-02T15:04:05-0700' format
45-
--commit-until <COMMIT_UNTIL> Scan commits older than a specific date. Ex: '2006-01-02' or '2006-10-02T15:04:05-0700' format
46-
--commit-from <COMMIT_FROM> Commit to start scan from
47-
--commit-to <COMMIT_TO> Commit to stop scan
48-
--branch <BRANCH> Branch to scan (comming soon)
49-
--uncommitted run gitleaks on uncommitted code (comming soon)
50-
--user <USER> user to scan (comming soon)
51-
-h, --help Print help (see more with '--help')
52-
-V, --version Print version
53-
54-
34+
--repo <REPO> Target repository
35+
--config <CONFIG> Config path [default: gitleaks.toml]
36+
--report <REPORT> Path to write json leaks file [default: ]
37+
--report-format <REPORT_FORMAT> json, csv, sarif [default: json]
38+
-v, --verbose Show verbose output from scan
39+
--pretty Pretty print json if leaks are present
40+
--commit <COMMIT> sha of commit to scan or "latest" to scan the last commit of the repository
41+
--commits <COMMITS> comma separated list of a commits to scan
42+
--commits-file <COMMITS_FILE> file of new line separated list of a commits to scan
43+
--commit-since <COMMIT_SINCE> Scan commits more recent than a specific date. Ex: '2006-01-02' or '2023-01-02T15:04:05-0700' format
44+
--commit-until <COMMIT_UNTIL> Scan commits older than a specific date. Ex: '2006-01-02' or '2006-10-02T15:04:05-0700' format
45+
--commit-from <COMMIT_FROM> Commit to start scan from
46+
--commit-to <COMMIT_TO> Commit to stop scan
47+
--branch <BRANCH> Branch to scan
48+
--uncommitted <UNCOMMITTED> Run sensleak on uncommitted code [possible values: true, false]
49+
--user <USER> Set user to scan [default: ]
50+
--repo-config Load config from target repo. Config file must be ".gitleaks.toml" or "gitleaks.toml"
51+
--debug log debug messages
52+
--disk <DISK> Clones repo(s) to disk
53+
-h, --help Print help (see more with '--help')
54+
-V, --version Print version
5555

5656
Repository: https://github.com/open-rust-initiative/sensleak-rs
5757
```
5858
59-
Examples: (Test repo: https://github.com/sonichen/TestGitOperation)
59+
Examples:
6060
6161
```shell
62-
sensleak --repo="D:/Workplace/Git/TestGitOperation" --commit="8bdca802af0514ce29947e20c6be1719974ad866" -v --pretty
62+
sensleak --repo="https://github.com/sonichen/TestGitOperation.git" -v --pretty --commit="140cef166cd8ba98201d9cad80289a75cd590cec"
6363
```
6464
6565
Output:
6666
6767
```shell
68-
[INFO][2023-05-26 11:51:04] Open repo ...
68+
[INFO][2023-06-01 09:16:02] Clone repo ...
6969
[
7070
Leak {
7171
line: "twilio_api_key = SK12345678901234567890123456789012",
7272
line_number: 6,
73-
secret: "api_key = SK12345678901234567890123456789012",
74-
entropy: "3.5",
75-
commit: "8bdca802af0514ce29947e20c6be1719974ad866",
73+
offender: "api_key = SK12345678901234567890123456789012",
74+
commit: "140cef166cd8ba98201d9cad80289a75cd590cec",
7675
repo: "TestGitOperation",
7776
rule: "Generic API Key",
78-
commit_message: "test\n",
77+
commit_message: "Merge pull request #1 from sonichen/secret\n\nSecret",
7978
author: "sonichen",
80-
email: "1606673007@qq.com",
79+
email: "[email protected].com",
8180
file: "/src/key.java",
82-
date: "2023-05-23 23:55:12 -08:00",
81+
date: "2023-05-27 04:28:55 -08:00",
8382
tags: "",
8483
operation: "addition",
8584
},
86-
...
8785
Leak {
88-
line: "twilio_api_key = SK12345678901234567890123456789012",
89-
line_number: 2,
90-
secret: "SK12345678901234567890123456789012",
91-
entropy: "",
92-
commit: "8bdca802af0514ce29947e20c6be1719974ad866",
86+
line: "Vault Service Token = hvs.abcdefghijklmn1234567890opqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ",
87+
line_number: 8,
88+
offender: "Token = hvs.abcdefghijklmn1234567890opqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ",
89+
commit: "140cef166cd8ba98201d9cad80289a75cd590cec",
9390
repo: "TestGitOperation",
94-
rule: "Twilio API Key",
95-
commit_message: "test\n",
91+
rule: "Generic API Key",
92+
commit_message: "Merge pull request #1 from sonichen/secret\n\nSecret",
9693
author: "sonichen",
97-
email: "1606673007@qq.com",
98-
file: "/src/mykey.java",
99-
date: "2023-05-23 23:55:12 -08:00",
94+
email: "[email protected].com",
95+
file: "/src/key.java",
96+
date: "2023-05-27 04:28:55 -08:00",
10097
tags: "",
10198
operation: "addition",
10299
},
100+
Leak {
101+
line: " 网址 = https://hooks.slack.com/workflows/B01234567/T01234567/abcdefghijklmnopqrstuvwx",
102+
line_number: 7,
103+
offender: "https://hooks.slack.com/workflows/B01234567/T01234567/abcdefghijklmnopqrstuvwx",
104+
commit: "140cef166cd8ba98201d9cad80289a75cd590cec",
105+
repo: "TestGitOperation",
106+
rule: "Slack Webhook",
107+
commit_message: "Merge pull request #1 from sonichen/secret\n\nSecret",
108+
author: "sonichen",
109+
110+
file: "/src/key.java",
111+
date: "2023-05-27 04:28:55 -08:00",
112+
tags: "",
113+
operation: "addition",
114+
},
115+
...
103116
]
104-
[WARN][2023-05-26 11:51:05]10 leaks detected. 1 commits scanned in 1.7318395s
105-
106-
```
107-
108-
117+
[WARN][2023-06-01 09:16:03]10 leaks detected. 1 commits scanned in 1.6758691s
109118

110-
More examples:
111-
112-
```shell
113-
cargo run -- --repo="D:/Workplace/Git/TestGitOperation" --commit="8bdca802af0514ce29947e20c6be1719974ad866" -v --pretty
114-
cargo run -- --repo="D:/Workplace/Git/TestGitOperation" --commits="4362fc4df48df74a46b56368d7fff1b02d01be72,8bdca802af0514ce29947e20c6be1719974ad866" -v --pretty
115-
cargo run -- --repo="D:/Workplace/Git/TestGitOperation" --commits-file="tests/files/commits.txt" -v --pretty
116-
cargo run -- --repo="D:/Workplace/Git/TestGitOperation" --commit-since="2023-05-20" --commit-until="2023-05-26" -v --pretty
117-
cargo run -- --repo="D:/Workplace/Git/TestGitOperation" --commit-to="4362fc4df48df74a46b56368d7fff1b02d01be72" --commit-from="8bdca802af0514ce29947e20c6be1719974ad866" -v --pretty
118119
```
119120
120121

src/custom_error.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::error::Error;
2+
use std::fmt;
3+
4+
/// CustomError represents custom errors that can occur in the application.
5+
#[derive(Debug)]
6+
pub enum CustomError {
7+
FailDeleteDir,
8+
FailCreateDir,
9+
FailLoadRepo,
10+
FailCloneRepo,
11+
InvalidRepoName,
12+
ObjectNotFound,
13+
RepoInternalError,
14+
ObjectNotAccess,
15+
ObjectConvertFail,
16+
AccessWalkerError,
17+
RepoCommitError,
18+
WalkerSortError,
19+
PushWalkerHeadError,
20+
InvalidDateFormat,
21+
InvalidTimeFormat,
22+
InvalidTomlFile,
23+
}
24+
25+
impl fmt::Display for CustomError {
26+
/// Formats the error message for display.
27+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28+
let error_message = match *self {
29+
CustomError::FailDeleteDir => "Failed to delete directory",
30+
CustomError::FailCreateDir => "Failed to create directory",
31+
CustomError::FailLoadRepo => "Failed to load repository",
32+
CustomError::FailCloneRepo => "Failed to clone repository",
33+
CustomError::InvalidRepoName => "Invalid repository name",
34+
CustomError::RepoInternalError => "Internal error within the repository",
35+
CustomError::ObjectNotFound => "Failure to find a blob or tree object in the repository",
36+
CustomError::ObjectNotAccess => "Failed to access the repository's object database",
37+
CustomError::ObjectConvertFail => "Failed to convert object to commit",
38+
CustomError::AccessWalkerError => "Failure to create or access the revision walker",
39+
CustomError::RepoCommitError => "Failed to find a commit in the repository",
40+
CustomError::WalkerSortError => "Failed to set the sorting order of the revision walker",
41+
CustomError::PushWalkerHeadError => "Failed to push the HEAD reference to the revision walker",
42+
CustomError::InvalidDateFormat => "Invalid date format",
43+
CustomError::InvalidTimeFormat => "Invalid time format",
44+
CustomError::InvalidTomlFile => "Invalid TOML file",
45+
};
46+
write!(f, "{}", error_message)
47+
}
48+
}
49+
50+
impl Error for CustomError {}

0 commit comments

Comments
 (0)