Merge pull request #49 from micksmix/main

Fixed gitlab support
This commit is contained in:
Mick Grove 2025-07-23 19:41:12 -07:00 committed by GitHub
commit 884fa1cab9
4 changed files with 113 additions and 11 deletions

View file

@ -318,7 +318,27 @@ _If no token is provided Kingfisher still works for public repositories._
| 200 | Findings discovered |
| 205 | Validated findings discovered |
---
## Install a Pre-Commit Hook
Run the provided helper script to add a hook that scans staged files before each commit:
```bash
./install-precommit-hook.sh
```
This creates `.git/hooks/pre-commit` that scans the files staged for commit with `kingfisher scan --no-update-check` and blocks the commit if any secrets are found.
### Install a Pre-Receive Hook
To check incoming pushes on a server-side repository, install the pre-receive hook:
```bash
./install-prereceive-hook.sh
```
The resulting `.git/hooks/pre-receive` script scans the files in each pushed commit and rejects the push if any secrets are detected.
## Update Checks

32
install-precommit-hook.sh Normal file
View file

@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -euo pipefail
HOOK_DIR="$(git rev-parse --git-dir)/hooks"
HOOK_PATH="$HOOK_DIR/pre-commit"
if [ -e "$HOOK_PATH" ]; then
echo "Error: $HOOK_PATH already exists. Move or remove the existing hook to continue." >&2
exit 1
fi
cat > "$HOOK_PATH" <<'HOOK'
#!/usr/bin/env bash
# Pre-commit hook to run Kingfisher scan on staged changes
set -euo pipefail
if ! command -v kingfisher >/dev/null 2>&1; then
echo "kingfisher not found in PATH" >&2
exit 1
fi
git diff --cached --name-only -z | \
xargs -0 --no-run-if-empty kingfisher scan --no-update-check
status=$?
if [ "$status" -ne 0 ]; then
echo "Kingfisher detected secrets in staged files. Commit aborted." >&2
exit "$status"
fi
HOOK
chmod +x "$HOOK_PATH"
echo "Pre-commit hook installed to $HOOK_PATH"

View file

@ -0,0 +1,34 @@
#!/usr/bin/env bash
set -euo pipefail
HOOK_DIR="$(git rev-parse --git-dir)/hooks"
HOOK_PATH="$HOOK_DIR/pre-receive"
if [ -e "$HOOK_PATH" ]; then
echo "Error: $HOOK_PATH already exists. Move or remove the existing hook to continue." >&2
exit 1
fi
cat > "$HOOK_PATH" <<'HOOK'
#!/usr/bin/env bash
# Pre-receive hook to scan pushed commits with Kingfisher
set -euo pipefail
if ! command -v kingfisher >/dev/null 2>&1; then
echo "kingfisher not found in PATH" >&2
exit 1
fi
while read -r oldrev newrev refname; do
git diff-tree --no-commit-id --name-only -r "$oldrev" "$newrev" -z |
xargs -0 --no-run-if-empty kingfisher scan --no-update-check
status=$?
if [ "$status" -ne 0 ]; then
echo "Kingfisher detected secrets in push. Push rejected." >&2
exit "$status"
fi
done
HOOK
chmod +x "$HOOK_PATH"
echo "Pre-receive hook installed to $HOOK_PATH"

View file

@ -36,17 +36,33 @@ impl Git {
/// Create a new `Git` instance.
///
/// * `ignore_certs`: If `true`, disables SSL certificate verification for `git` operations.
pub fn new(ignore_certs: bool) -> Self {
let credentials = if std::env::var("KF_GITHUB_TOKEN").is_ok() {
vec![
"-c".into(),
r#"credential.helper="#.into(),
"-c".into(),
pub fn new(ignore_certs: bool) -> Self {
let mut credentials = Vec::new();
// If either GitHub or GitLab token is set, first clear existing credential.helpers
if std::env::var("KF_GITHUB_TOKEN").is_ok()
|| std::env::var("KF_GITLAB_TOKEN").is_ok()
{
credentials.push("-c".into());
credentials.push(r#"credential.helper="#.into());
}
// Inject GitHub token helper
if std::env::var("KF_GITHUB_TOKEN").is_ok() {
credentials.push("-c".into());
credentials.push(
r#"credential.helper=!_ghcreds() { echo username="kingfisher"; echo password="$KF_GITHUB_TOKEN"; }; _ghcreds"#.into(),
]
} else {
Vec::new()
};
);
}
// Inject GitLab token helper
if std::env::var("KF_GITLAB_TOKEN").is_ok() {
credentials.push("-c".into());
credentials.push(
r#"credential.helper=!_glcreds() { echo username="oauth2"; echo password="$KF_GITLAB_TOKEN"; }; _glcreds"#.into(),
);
}
Self { credentials, ignore_certs }
}