From 369c874ebe8bd5714a13634529d633ddbed45218 Mon Sep 17 00:00:00 2001 From: Kert Laansalu <94850466+kert114@users.noreply.github.com> Date: Mon, 19 Oct 2026 10:52:12 +0100 Subject: [PATCH] ci(security): scan credentials with shared gitleaks rules --- .github/workflows/gitleaks.yml | 112 ++++++++++++++++++++++++++++++ .github/workflows/secret-scan.yml | 14 ++++ README.md | 31 +++++++++ tests/test_secret_scan.py | 70 +++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 .github/workflows/gitleaks.yml create mode 100644 .github/workflows/secret-scan.yml create mode 100644 tests/test_secret_scan.py diff --git a/.github/workflows/gitleaks.yml b/.github/workflows/gitleaks.yml new file mode 100644 index 0000000..42fe813 --- /dev/null +++ b/.github/workflows/gitleaks.yml @@ -0,0 +1,112 @@ +name: Gitleaks +on: + workflow_call: + inputs: + full_history: + type: boolean + default: false +permissions: + contents: read +jobs: + scan: + name: gitleaks + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 + with: + fetch-depth: 0 + persist-credentials: false + ref: ${{ github.event.pull_request.head.sha || github.sha }} + - name: Install pinned scanner + shell: bash + run: | + set -euo pipefail + cd "$RUNNER_TEMP" + curl --fail --silent --show-error --location --retry 3 \ + https://github.com/gitleaks/gitleaks/releases/download/v8.30.1/gitleaks_8.30.1_linux_x64.tar.gz \ + --output gitleaks.tar.gz + echo '551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb gitleaks.tar.gz' | sha256sum --check --status + tar -xzf gitleaks.tar.gz gitleaks + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" + - name: Scan with central credential rules + id: scan + shell: bash + env: + FULL_HISTORY: ${{ inputs.full_history }} + BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }} + run: | + set -euo pipefail + config="$RUNNER_TEMP/60x-gitleaks.toml" + cat > "$config" <<'TOML' + title = "60x credential detection" + [extend] + useDefault = true + [[rules]] + id = "60x-api-key" + description = "AI Brain API key generated by invitations.generate_api_key" + regex = '''\bsk_live_[A-Za-z0-9_-]{43}\b''' + keywords = ["sk_live_"] + [[rules]] + id = "gcp-service-account-private-key" + description = "Private key field in GCP service-account JSON" + regex = '''(?i)"private_key"\s*:\s*"(-----BEGIN (?:RSA )?PRIVATE KEY-----[^"\r\n]+)''' + secretGroup = 1 + keywords = ["private_key"] + [[rules]] + id = "60x-oauth-refresh-token" + description = "AI Brain MCP OAuth refresh token, 48 URL-safe random bytes" + regex = '''(?i)(?:mcp_refresh_token|refresh_token)["']?\s*[:=]\s*["']([A-Za-z0-9_-]{64})["']''' + secretGroup = 1 + keywords = ["refresh_token"] + [[rules]] + id = "60x-oauth-client-secret" + description = "MCP client secret and agent-mail browser or access token, 32 random bytes" + regex = '''(?i)(?:mcp_client_secret|client_secret|browser_token|agent_mail_token)["']?\s*[:=]\s*["']([A-Za-z0-9_-]{43})["']''' + secretGroup = 1 + keywords = ["client_secret", "browser_token", "agent_mail_token"] + TOML + log_opts='--all' + if [[ "$FULL_HISTORY" != true ]]; then + [[ "$BASE_SHA" =~ ^[a-f0-9]{40}$ && "$HEAD_SHA" =~ ^[a-f0-9]{40}$ ]] || { echo 'Missing valid scan range'; exit 2; } + log_opts="$BASE_SHA..$HEAD_SHA" + fi + mkdir -p "$RUNNER_TEMP/empty-gitleaks-ignore" + raw="$RUNNER_TEMP/gitleaks-redacted.json" + trap 'rm -f "$raw"' EXIT + set +e + gitleaks git . --config "$config" --log-opts="$log_opts" \ + --gitleaks-ignore-path "$RUNNER_TEMP/empty-gitleaks-ignore" \ + --ignore-gitleaks-allow --redact=100 --no-banner --no-color \ + --exit-code 10 --timeout 900 --report-format json --report-path "$raw" >/dev/null 2>&1 + result=$? + set -e + if [[ "$result" != 0 && "$result" != 10 ]]; then + echo 'Scanner error. No raw scanner output is published.' + exit 2 + fi + python3 - "$raw" <<'PY' + import json, os, pathlib, sys + findings = json.loads(pathlib.Path(sys.argv[1]).read_text()) + safe = sorted({(x["File"], x["RuleID"]) for x in findings}) + report = [{"path": p, "rule_id": r} for p, r in safe] + pathlib.Path(os.environ["RUNNER_TEMP"], "gitleaks-findings.json").write_text(json.dumps(report, indent=2) + "\n") + with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as out: + out.write(f"Gitleaks found {len(report)} unique path/rule pairs. The artifact contains paths and rule IDs only.\n") + PY + echo "result=$result" >> "$GITHUB_OUTPUT" + - name: Save paths and rule IDs only + if: always() && steps.scan.outputs.result != '' + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: gitleaks-findings + path: ${{ runner.temp }}/gitleaks-findings.json + retention-days: 30 + if-no-files-found: error + - name: Enforce scan result + if: steps.scan.outputs.result != '0' + shell: bash + run: | + echo 'Gitleaks blocked this change. Review the path/rule report; route real credentials to F05 rotation.' + exit 1 diff --git a/.github/workflows/secret-scan.yml b/.github/workflows/secret-scan.yml new file mode 100644 index 0000000..1a97422 --- /dev/null +++ b/.github/workflows/secret-scan.yml @@ -0,0 +1,14 @@ +name: Secret scanning +on: + pull_request: + merge_group: + schedule: + - cron: '17 4 * * 1' + workflow_dispatch: +permissions: + contents: read +jobs: + secrets: + uses: ./.github/workflows/gitleaks.yml + with: + full_history: ${{ github.event_name != 'pull_request' && github.event_name != 'merge_group' }} diff --git a/README.md b/README.md index 6c641b5..5e9a001 100644 --- a/README.md +++ b/README.md @@ -1 +1,32 @@ [60x.ai](https://60x.ai) + +# Shared organization workflows + +`.github/workflows/gitleaks.yml` runs the free gitleaks CLI. Call it from a private repository with `contents: read`; no license or inherited secrets are needed. Pin the reusable workflow to a reviewed commit SHA. + +```yaml +name: Secret scanning +on: + pull_request: + merge_group: + schedule: + - cron: '17 4 * * 1' + workflow_dispatch: +permissions: + contents: read +jobs: + secrets: + uses: 60xAI/.github/.github/workflows/gitleaks.yml@REVIEWED_COMMIT_SHA + with: + full_history: ${{ github.event_name != 'pull_request' && github.event_name != 'merge_group' }} +``` + +The job named `secrets / gitleaks` must pass on pull requests. PR scans cover commits between the base and head, including credentials added and subsequently removed in that range. Scheduled and manual runs scan every fetched branch and tag, including deleted files in reachable history. Unreachable or server-only objects are outside the scan. + +The central config extends the pinned gitleaks default rules with AI Brain `sk_live_` API keys, GCP JSON private keys, MCP refresh tokens and client secrets, and contextual agent-mail browser/access tokens. Unprefixed tokens need a recognizable assignment name. The default rules also detect private keys, provider tokens, and JSON Web Tokens. These rules cannot identify every arbitrary random string. + +Caller `.gitleaks.toml`, `.gitleaksignore`, and inline `gitleaks:allow` comments cannot waive this scan. The upstream rules include their normal file and synthetic-value exclusions. Reports retain only file paths and rule IDs. Raw scan output, matches, secret values, and code snippets are never published. A scanner error fails the job. + +When a scan fails, inspect the reported path privately. Assign genuine exposures to the owner of the credential and rotate under SOC2 F05. Remove the credential from current code; do not rewrite shared history. A weekly scan continues to fail for historical exposures until an explicit, reviewed exception policy exists. This workflow does not automatically waive old findings or rotate keys. + +Run `PATH=":$PATH" python3 tests/test_secret_scan.py` to exercise the actual workflow scan step against synthetic credentials in a temporary Git repository. The gitleaks version must match the workflow. Validate workflow syntax with `actionlint`. diff --git a/tests/test_secret_scan.py b/tests/test_secret_scan.py new file mode 100644 index 0000000..4a1a5e8 --- /dev/null +++ b/tests/test_secret_scan.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +"""Exercise the workflow's scan step with synthetic credentials in a real Git repo.""" +import json +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +WORKFLOW = ROOT / '.github/workflows/gitleaks.yml' + +def run(*args, cwd, env=None): + return subprocess.run(args, cwd=cwd, env=env, text=True, capture_output=True) + +class SecretScanTest(unittest.TestCase): + def test_custom_credentials_block_and_reports_omit_values(self): + workflow = WORKFLOW.read_text() + step = workflow.split(' - name: Scan with central credential rules\n', 1)[1] + block = step.split(' run: |\n', 1)[1].split(' - name: Save paths', 1)[0] + script = '\n'.join(line[10:] if line.startswith(' ') else line for line in block.splitlines()) + with tempfile.TemporaryDirectory() as tmp: + repo = Path(tmp, 'repo'); repo.mkdir() + scratch = Path(tmp, 'runner'); scratch.mkdir() + for args in [('init', '-q'), ('config', 'user.name', 'Security Test'), ('config', 'user.email', 'test@example.invalid')]: + self.assertEqual(run('git', *args, cwd=repo).returncode, 0) + (repo / 'README.md').write_text('Synthetic secret-scanner test.\n') + self.commit(repo, 'Clean base') + base = run('git', 'rev-parse', 'HEAD', cwd=repo).stdout.strip() + api_key = 'sk_' + 'live_' + 'F' * 43 + (repo / 'fake.txt').write_text('Obviously fake scanner fixture: ' + api_key + '\n') + self.commit(repo, 'Synthetic API credential') + baseline = run('gitleaks', 'git', '.', '--log-opts=--all', '--redact=100', '--no-banner', cwd=repo) + self.assertEqual(baseline.returncode, 0, 'Default rules unexpectedly caught the fixture; baseline must be re-evaluated') + (repo / 'other-fakes.txt').write_text( + 'refresh_token="' + 'FAKE0000' * 8 + '"\n' + 'client_secret="' + ('FAKE0000' * 6)[:43] + '"\n' + '{"private_key":"-----BEGIN PRIVATE KEY-----\\nOBVIOUSLY_FAKE_NOT_A_KEY\\n-----END PRIVATE KEY-----\\n"}\n') + self.commit(repo, 'Other synthetic credentials') + env = os.environ | {'RUNNER_TEMP': str(scratch), 'GITHUB_STEP_SUMMARY': str(scratch/'summary'), + 'GITHUB_OUTPUT': str(scratch/'output'), 'FULL_HISTORY': 'true', 'BASE_SHA': base, 'HEAD_SHA': ''} + result = run('bash', '-c', script, cwd=repo, env=env) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertIn('result=10', (scratch/'output').read_text()) + report = json.loads((scratch/'gitleaks-findings.json').read_text()) + rules = {x['rule_id'] for x in report} + self.assertTrue({'60x-api-key', '60x-oauth-refresh-token', '60x-oauth-client-secret', 'gcp-service-account-private-key'} <= rules, sorted(rules)) + self.assertTrue(all(set(x) == {'path','rule_id'} for x in report)) + self.assertNotIn(api_key, json.dumps(report)) + self.assertFalse((scratch/'gitleaks-redacted.json').exists()) + (repo / 'fake.txt').unlink(); (repo / 'other-fakes.txt').unlink() + self.commit(repo, 'Remove synthetic fixtures') + clean_base = run('git', 'rev-parse', 'HEAD', cwd=repo).stdout.strip() + (repo / 'README.md').write_text('A clean follow-up change.\n') + self.commit(repo, 'Clean follow-up') + head = run('git', 'rev-parse', 'HEAD', cwd=repo).stdout.strip() + env.update(FULL_HISTORY='false', BASE_SHA=clean_base, HEAD_SHA=head) + (scratch/'output').write_text('') + clean = run('bash', '-c', script, cwd=repo, env=env) + self.assertEqual(clean.returncode, 0, clean.stdout + clean.stderr) + self.assertIn('result=0', (scratch/'output').read_text()) + self.assertEqual(json.loads((scratch/'gitleaks-findings.json').read_text()), []) + print('Baseline: default rules miss the fake API key. Custom rules: all four block. Clean PR range: passes despite historical fixtures.') + + def commit(self, repo, message): + self.assertEqual(run('git', 'add', '.', cwd=repo).returncode, 0) + self.assertEqual(run('git', 'commit', '-qm', message, cwd=repo).returncode, 0) + +if __name__ == '__main__': + unittest.main()