|
1 | 1 | # CodeGuardian — Sensitive Data Scanner |
2 | 2 |
|
3 | | -A small Node.js CLI that scans your repository for commonly leaked secrets before you push. |
| 3 | +Lightweight CLI to scan repositories for accidentally committed secrets (API keys, tokens, private keys). This short guide starts with how to use CodeGuardian in your project, how to integrate it into CI, and then explains the feature set and configuration. |
4 | 4 |
|
5 | | -Features |
6 | | -- CLI scanner using configurable regex rules |
7 | | -- Sample Husky pre-commit hook |
8 | | -- Optional CI mode (exit non-zero when findings are present) |
| 5 | +## How developers use CodeGuardian. |
9 | 6 |
|
10 | | -Quick start |
11 | | -1. Install dependencies: |
| 7 | +--- |
12 | 8 |
|
13 | | -```bash |
14 | | -npm install |
15 | | -``` |
16 | | - |
17 | | -2. (Optional) Install husky hooks (manual steps) |
| 9 | +Installation (two quick ways): |
18 | 10 |
|
19 | | -Note: this project no longer runs `husky install` automatically during `npm install` — the `prepare` script was removed to avoid install-time failures on machines that don't have Husky installed. If you want local pre-commit hooks, install and enable Husky manually: |
| 11 | +- Run directly with npx (no install required): |
20 | 12 |
|
21 | 13 | ```bash |
22 | | -# install husky as a dev dependency |
23 | | -npm install --save-dev husky |
| 14 | +npx @shivarm/codeguardian |
| 15 | +``` |
24 | 16 |
|
25 | | -# install husky hooks into .husky/ |
26 | | -npx husky install |
| 17 | +- Install as a dev dependency (recommended for team projects): |
27 | 18 |
|
28 | | -# add a pre-commit hook that runs CodeGuardian on staged files and fails the commit |
29 | | -npx husky add .husky/pre-commit "npx codeguardian --staged --ci" |
| 19 | +```bash |
| 20 | +npm install --save-dev @shivarm/codeguardian |
30 | 21 | ``` |
31 | 22 |
|
32 | | -3. Run the scanner on the repo: |
| 23 | +Basic commands: |
| 24 | + |
| 25 | +- Scan entire repository: |
33 | 26 |
|
34 | 27 | ```bash |
35 | 28 | npx codeguardian |
36 | 29 | ``` |
37 | 30 |
|
38 | | -Config |
39 | | -Drop a `.codeguardianrc.json` in the repo with the following shape: |
| 31 | +- Scan only staged files (fast; good for pre-commit hooks): |
40 | 32 |
|
41 | | -```json |
42 | | -{ |
43 | | - "rules": [ |
44 | | - { "name": "Example", "pattern": "AKIA[0-9A-Z]{16}", "flags": "g" } |
45 | | - ] |
46 | | -} |
| 33 | +```bash |
| 34 | +npx codeguardian --staged |
47 | 35 | ``` |
48 | 36 |
|
49 | | -You can also add an `ignoreFiles` array of globs or paths to skip scanning noisy files (for example lockfiles or build outputs). Example `.codeguardianrc.json`: |
| 37 | +- CI mode (exit non-zero on findings): |
50 | 38 |
|
51 | | -```json |
52 | | -{ |
53 | | - "ignoreFiles": [ |
54 | | - "package-lock.json", |
55 | | - "yarn.lock", |
56 | | - "pnpm-lock.yaml", |
57 | | - "dist/**", |
58 | | - "node_modules/**" |
59 | | - ], |
60 | | - "rules": [ |
61 | | - { "name": "AWS Access Key ID", "pattern": "AKIA[0-9A-Z]{16}", "flags": "g" }, |
62 | | - { "name": "Simple API key assignment", "pattern": "api_key\\s*[=:\\s]\\s*([A-Za-z0-9_\\-]{8,})", "flags": "gi" } |
63 | | - ] |
64 | | -} |
| 39 | +```bash |
| 40 | +npx codeguardian --ci |
65 | 41 | ``` |
66 | 42 |
|
67 | | -To run the scanner with a custom config file use `--config`: |
| 43 | +Custom config (optional): |
68 | 44 |
|
69 | 45 | ```bash |
70 | 46 | npx codeguardian --config .codeguardianrc.json |
71 | 47 | ``` |
72 | 48 |
|
73 | | -CI Integration |
74 | | -Run `npx codeguardian --ci` in your CI pipeline and fail the build if any findings are present. |
| 49 | +## How to integrate with CI (GitHub Actions). |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +Use the built-in workflow `.github/workflows/codeguardian.yml` or add a step to your pipeline to run the scanner in CI mode. Example snippet: |
| 54 | + |
| 55 | +```yaml |
| 56 | +on: |
| 57 | + push: |
| 58 | + branches: [main] |
| 59 | + pull_request: |
| 60 | + branches: [main] |
| 61 | + |
| 62 | +jobs: |
| 63 | + scan: |
| 64 | + name: Run CodeGuardian |
| 65 | + runs-on: ubuntu-latest |
| 66 | + steps: |
| 67 | + - name: Checkout repository |
| 68 | + uses: actions/checkout@v5 |
| 69 | + |
| 70 | + - name: Setup Node.js |
| 71 | + uses: actions/setup-node@v5 |
| 72 | + with: |
| 73 | + node-version: "22" |
| 74 | + cache: "npm" |
| 75 | + |
| 76 | + - name: Install dependencies |
| 77 | + run: npm install |
| 78 | + |
| 79 | + - name: Run CodeGuardian scanner (CI mode) |
| 80 | + run: npx codeguardian --ci |
| 81 | +``` |
| 82 | +
|
| 83 | +When run with `--ci` the CLI exits with a non-zero code if any findings are detected — this will fail the job and block merges until issues are resolved. |
| 84 | + |
| 85 | +## What CodeGuardian offers |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +- Rule-based scanning: configure regex rules (name, pattern, flags) to detect secrets. |
| 90 | +- `ignoreFiles`: glob list to skip noisy files (lockfiles, build artifacts). |
| 91 | +- Staged-file scanning: run only what will be committed (fast pre-commit checks). |
| 92 | +- Husky integration: optional pre-commit hooks to block commits locally. |
| 93 | +- CI-ready: `--ci` mode for failing pipelines on findings. |
| 94 | + |
| 95 | +## Developer guide & advanced configuration |
75 | 96 |
|
76 | | -Notes |
77 | | -Notes |
78 | | -- The default ruleset (in `default-config.json`) is a starting point — tune it for your project to reduce false positives. |
79 | | -- The scanner respects `.gitignore` and ignores `node_modules` and `.git` by default. Additionally, CodeGuardian now ignores common lockfiles by default (e.g. `package-lock.json`, `yarn.lock`, `pnpm-lock.yaml`) to reduce noisy matches from integrity/hash lines. |
| 97 | +--- |
| 98 | + |
| 99 | +## CLI options |
| 100 | + |
| 101 | +- `-c, --config <path>` — path to JSON config file (default: `.codeguardianrc.json`) |
| 102 | +- `-s, --staged` — only scan staged files |
| 103 | +- `--ci` — CI mode: exit non-zero when findings exist |
| 104 | +- `-v, --verbose` — verbose output |
| 105 | + |
| 106 | +## Config file (`.codeguardianrc.json`) |
| 107 | + |
| 108 | +Minimal shape: |
| 109 | + |
| 110 | +```json |
| 111 | +{ |
| 112 | + "ignoreFiles": ["package-lock.json", "yarn.lock", "dist/**"], |
| 113 | + "rules": [{ "name": "AWS Access Key ID", "pattern": "AKIA[0-9A-Z]{16}", "flags": "g" }] |
| 114 | +} |
| 115 | +``` |
80 | 116 |
|
81 | | -If you want to ignore additional files from scanning (beyond `.gitignore` and the default lockfiles), add them to your `.codeguardianrc.json`. |
| 117 | +Rules are JavaScript regular expressions expressed as strings. `flags` is optional (for example `gi`). The scanner will try to compile each rule. invalid patterns are skipped. |
0 commit comments