-
Notifications
You must be signed in to change notification settings - Fork 8
SK-3018-gitleaks-detection-fix-added-automated-script-to-fix-generate… #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
skyflow-himanshupal
merged 4 commits into
main
from
himanshupal/SK-3018-gitleaks-detection-multiple-secret-exposures-identified-across-repository-skyflow-node
Sep 24, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6d417f8
SK-3018-gitleaks-detection-fix-added-automated-script-to-fix-generate…
skyflow-himanshupal 096a98a
SK-3156-gitleaks-detection-fix-added-script-to-fix-generated-files-fa…
skyflow-himanshupal 9f3bbda
SK-3156-addressed-review-comments
skyflow-himanshupal f550f3a
SK-3156-added-CI-checks
skyflow-himanshupal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| #!/usr/bin/env bash | ||
| # Two-tier secret-leak guard, run on every local commit. | ||
| # Tier 1: auto-redact gitleaks findings inside src/_generated_ (driven live | ||
| # by Rule/gitleaks.toml via scripts/patch-generated-secrets.js, not | ||
| # a hand-maintained list) and re-stage the files it touched. Never | ||
| # blocks the commit by itself - it either fixes generated code or | ||
| # leaves it untouched for tier 2 to catch. | ||
| # Tier 2: run the real gitleaks scan against the staged diff and block on | ||
| # any finding tier 1 didn't (or couldn't) resolve. | ||
| # Installed via the repo's own "prepare" npm script - see package.json. | ||
| set -uo pipefail | ||
|
|
||
| repo_root="$(git rev-parse --show-toplevel)" | ||
| cd "$repo_root" | ||
|
|
||
| generated_dir="src/ _generated_" | ||
|
|
||
| # Snapshot what's already staged in the generated dir before tier 1 runs, so | ||
| # an intentionally-unstaged, in-progress change there (e.g. mid-way through | ||
| # testing a Fern regen) isn't unconditionally swept into this commit. Tier 1 | ||
| # only edits working-tree files, never the index, so this snapshot stays | ||
| # accurate regardless of what it does next. | ||
| already_staged_in_generated="$(git diff --cached --name-only -- "$generated_dir" || true)" | ||
|
|
||
| git_dir="$(git rev-parse --git-dir)" | ||
| touched_file_list="$git_dir/leak-guard-touched-files.txt" | ||
| # A list from a previous commit's tier 1 run must never be reused here - if | ||
| # tier 1 doesn't run this time (node missing, below), stale entries from | ||
| # that earlier run would otherwise get staged again. | ||
| rm -f "$touched_file_list" | ||
|
|
||
| # git invokes hooks with a leaner PATH than your interactive shell, so a | ||
| # Node install managed by nvm/volta/fnm or bundled with an IDE (rather than | ||
| # a system package) is often invisible here even though `node` works fine | ||
| # in your terminal. If that's you, run this once and commit again: | ||
| # git config leakguard.nodepath "$(dirname "$(command -v node)")" | ||
| # It's a local git config value (not committed), so it won't affect anyone | ||
| # else's machine. | ||
| if ! command -v node >/dev/null 2>&1; then | ||
| custom_node_dir="$(git config --get leakguard.nodepath || true)" | ||
| [ -n "$custom_node_dir" ] && PATH="$custom_node_dir:$PATH" && export PATH | ||
| fi | ||
|
|
||
| if ! command -v node >/dev/null 2>&1; then | ||
| echo "[leak-guard] tier 1 skipped: 'node' not found on PATH inside the git hook environment." | ||
| echo "[leak-guard] If 'node' works in your terminal, git hooks are likely just seeing a different PATH." | ||
| echo "[leak-guard] Fix: git config leakguard.nodepath \"\$(dirname \"\$(command -v node)\")\", then commit again." | ||
| echo "[leak-guard] continuing to tier 2; this alone will not block the commit." | ||
| else | ||
| echo "[leak-guard] tier 1: auto-redacting gitleaks findings in generated code..." | ||
| if ! node scripts/patch-generated-secrets.js; then | ||
| echo "[leak-guard] tier 1 couldn't fully resolve generated code - see the message above." | ||
| echo "[leak-guard] continuing to tier 2; this alone will not block the commit." | ||
| fi | ||
| fi | ||
|
|
||
| # Stages only what tier 1 actually touched this run (recorded to | ||
| # $touched_file_list - see recordTouchedFiles in the script) plus whatever | ||
| # was already staged above. A file sitting in src/_generated_ that tier 1 | ||
| # didn't touch and the caller hadn't staged is left alone rather than | ||
| # force-added - see the med-severity review finding on the old | ||
| # unconditional `git add` of the whole directory. | ||
| to_stage=() | ||
| if [ -f "$touched_file_list" ]; then | ||
| while IFS= read -r f; do | ||
| [ -n "$f" ] && to_stage+=("$f") | ||
| done < "$touched_file_list" | ||
| fi | ||
| while IFS= read -r f; do | ||
| [ -n "$f" ] && to_stage+=("$f") | ||
| done <<< "$already_staged_in_generated" | ||
|
|
||
| if [ "${#to_stage[@]}" -gt 0 ]; then | ||
| git add -- "${to_stage[@]}" | ||
| fi | ||
|
|
||
| if ! command -v gitleaks >/dev/null 2>&1; then | ||
| echo "[leak-guard] tier 2 skipped: 'gitleaks' binary not found locally." | ||
| echo "[leak-guard] CI will still block this PR on any finding (see Gitleaks.yml) - install gitleaks locally to catch issues before pushing instead of waiting on CI." | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "[leak-guard] tier 2: scanning staged changes with gitleaks..." | ||
| if ! gitleaks protect --staged --config="Rule/gitleaks.toml" --redact; then | ||
| echo "" | ||
| echo "[leak-guard] commit blocked - gitleaks found something in your staged changes." | ||
| echo "[leak-guard] known false positive in generated code -> re-run tier 1 (node scripts/patch-generated-secrets.js) and check its output." | ||
| echo "[leak-guard] false positive elsewhere -> add an allowlist entry to Rule/gitleaks.toml." | ||
| echo "[leak-guard] real secret -> remove it and rotate the credential before committing." | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "[leak-guard] clean - proceeding with commit." | ||
| exit 0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| name: Gitleaks Auto-Redact | ||
|
|
||
| # Runs scripts/patch-generated-secrets.js on every push to a PR and, if it | ||
| # finds anything to redact in the Fern-generated code, commits and pushes | ||
| # the fix straight to the PR branch - the same "run in CI, commit the | ||
| # result back" pattern common-release.yml uses for its version bump. This | ||
| # closes the gap where a contributor without gitleaks or node installed | ||
| # locally never gets tier 1's local auto-redaction (see | ||
| # .githooks/pre-commit): CI now does it for them instead of only warning. | ||
| # | ||
| # This is additive to Gitleaks.yml, not a replacement for it - it doesn't | ||
| # change that workflow's scan or its exit behavior. | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
| branches: | ||
| - main | ||
|
|
||
| permissions: | ||
| contents: write | ||
|
|
||
| jobs: | ||
| auto-redact: | ||
| runs-on: ubuntu-latest | ||
| # A fork PR's checkout token can't push back to someone else's fork - | ||
| # GitHub blocks that regardless of what token this job holds - so skip | ||
| # cleanly rather than fail noisily. Gitleaks.yml still scans fork PRs | ||
| # as usual; this job only ever adds a redaction commit for same-repo | ||
| # branches. | ||
| if: github.event.pull_request.head.repo.full_name == github.repository | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| token: ${{ secrets.PAT_ACTIONS }} | ||
| ref: ${{ github.head_ref }} | ||
|
|
||
| - uses: actions/setup-node@v3 | ||
| with: | ||
| node-version: '20.x' | ||
|
|
||
| - name: Install packages | ||
| run: npm install --ignore-scripts | ||
|
|
||
| - name: Install gitleaks | ||
| run: | | ||
| GITLEAKS_VERSION="$(curl -fsSL https://api.github.com/repos/gitleaks/gitleaks/releases/latest | grep -m1 '"tag_name"' | cut -d '"' -f4 | sed 's/^v//')" | ||
| curl -fsSL "https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz" -o gitleaks.tar.gz | ||
| tar -xzf gitleaks.tar.gz gitleaks | ||
| sudo install -m 0755 gitleaks /usr/local/bin/gitleaks | ||
| rm -f gitleaks.tar.gz gitleaks | ||
| gitleaks version | ||
|
|
||
| - name: Auto-redact generated code | ||
| run: node scripts/patch-generated-secrets.js | ||
|
|
||
| - name: Commit and push redaction, if any | ||
| run: | | ||
| git config user.name "${{ github.actor }}" | ||
| git config user.email "${{ github.actor }}@users.noreply.github.com" | ||
| git checkout "${{ github.head_ref }}" | ||
| if git diff --quiet -- "src/ _generated_"; then | ||
| echo "No fake secrets found to redact." | ||
| else | ||
| git add -- "src/ _generated_" | ||
| git commit -m "[AUTOMATED] redact fake secrets in generated code" | ||
| git push origin "HEAD:${{ github.head_ref }}" | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.