web3guard-cli 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.js +63 -10
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -41,6 +41,8 @@ const commander_1 = require("commander");
41
41
  const chalk_1 = __importDefault(require("chalk"));
42
42
  const ora_1 = __importDefault(require("ora"));
43
43
  const fs = __importStar(require("fs"));
44
+ const path = __importStar(require("path"));
45
+ const child_process_1 = require("child_process");
44
46
  const api_1 = require("./api");
45
47
  const config_1 = require("./config");
46
48
  const program = new commander_1.Command();
@@ -49,24 +51,42 @@ program
49
51
  .description('CLI for Web3 Guard - Intelligent Multi-Chain Auditing & Security Oracle')
50
52
  .version('1.0.0');
51
53
  program
52
- .command('scan <path>')
54
+ .command('scan [path]')
53
55
  .description('Scan a local smart contract file or directory for vulnerabilities')
54
56
  .option('--json', 'Output result in JSON format')
55
57
  .option('--out <file>', 'Save output to a file (JSON format)')
58
+ .option('--staged', 'Scan only staged files in Git')
59
+ .option('--strict', 'Exit with code 1 if vulnerabilities are found')
56
60
  .action(async (scanPath, options) => {
57
61
  let filesToScan = [];
58
- try {
59
- const stat = fs.statSync(scanPath);
60
- if (stat.isDirectory()) {
61
- filesToScan = (0, api_1.findContracts)(scanPath);
62
+ if (options.staged) {
63
+ try {
64
+ const output = (0, child_process_1.execSync)('git diff --cached --name-only --diff-filter=ACM').toString();
65
+ filesToScan = output.split('\n').filter(f => f.endsWith('.rs') || f.endsWith('.sol')).map(f => f.trim()).filter(f => f.length > 0);
62
66
  }
63
- else {
64
- filesToScan = [scanPath];
67
+ catch (e) {
68
+ console.error(chalk_1.default.red('Error running git command. Are you in a git repository?'));
69
+ process.exit(1);
65
70
  }
66
71
  }
67
- catch (e) {
68
- console.error(chalk_1.default.red(`Error accessing path: ${e.message}`));
69
- return;
72
+ else {
73
+ if (!scanPath) {
74
+ console.error(chalk_1.default.red('Error: path is required if --staged is not used.'));
75
+ process.exit(1);
76
+ }
77
+ try {
78
+ const stat = fs.statSync(scanPath);
79
+ if (stat.isDirectory()) {
80
+ filesToScan = (0, api_1.findContracts)(scanPath);
81
+ }
82
+ else {
83
+ filesToScan = [scanPath];
84
+ }
85
+ }
86
+ catch (e) {
87
+ console.error(chalk_1.default.red(`Error accessing path: ${e.message}`));
88
+ return;
89
+ }
70
90
  }
71
91
  if (filesToScan.length === 0) {
72
92
  console.log(chalk_1.default.yellow('No .rs or .sol files found to scan.'));
@@ -126,6 +146,12 @@ program
126
146
  }
127
147
  });
128
148
  }
149
+ if (options.strict) {
150
+ const hasVulnerabilities = allResults.some(r => r.result && r.result.vulnerabilities && r.result.vulnerabilities.length > 0);
151
+ if (hasVulnerabilities) {
152
+ process.exit(1);
153
+ }
154
+ }
129
155
  });
130
156
  program
131
157
  .command('score <address>')
@@ -181,4 +207,31 @@ program
181
207
  console.log(chalk_1.default.yellow('Usage: web3guard config set api-url <url>'));
182
208
  }
183
209
  });
210
+ program
211
+ .command('init-hook')
212
+ .description('Initialize a Git pre-commit hook for Web3 Guard')
213
+ .action(() => {
214
+ const hookDir = path.join(process.cwd(), '.git', 'hooks');
215
+ const hookPath = path.join(hookDir, 'pre-commit');
216
+ if (!fs.existsSync(hookDir)) {
217
+ console.error(chalk_1.default.red('Error: .git/hooks directory not found. Are you in a Git repository?'));
218
+ process.exit(1);
219
+ }
220
+ const hookContent = `#!/bin/sh
221
+ echo "🛡️ Running Web3 Guard Pre-Commit Scan..."
222
+ npx web3guard-cli scan --staged --strict
223
+ if [ $? -ne 0 ]; then
224
+ echo "❌ Security vulnerabilities found! Commit rejected."
225
+ exit 1
226
+ fi
227
+ `;
228
+ try {
229
+ fs.writeFileSync(hookPath, hookContent, { mode: 0o755 });
230
+ console.log(chalk_1.default.green('✅ Successfully installed Web3 Guard pre-commit hook at .git/hooks/pre-commit'));
231
+ }
232
+ catch (e) {
233
+ console.error(chalk_1.default.red(`Error writing hook file: ${e.message}`));
234
+ process.exit(1);
235
+ }
236
+ });
184
237
  program.parse(process.argv);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web3guard-cli",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Web3 Guard CLI and MCP Server",
5
5
  "main": "dist/index.js",
6
6
  "files": [