pi-lens 3.6.2 → 3.6.4
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.
- package/CHANGELOG.md +10 -2
- package/package.json +4 -4
- package/tsconfig.json +1 -1
- package/clients/__tests__/file-time.test.js +0 -216
- package/clients/__tests__/file-time.test.ts +0 -276
- package/clients/__tests__/format-service.test.js +0 -245
- package/clients/__tests__/format-service.test.ts +0 -339
- package/clients/__tests__/formatters.test.js +0 -271
- package/clients/__tests__/formatters.test.ts +0 -401
- package/clients/agent-behavior-client.js +0 -110
- package/clients/agent-behavior-client.test.js +0 -94
- package/clients/agent-behavior-client.test.ts +0 -116
- package/clients/amain-types.js +0 -164
- package/clients/architect-client.js +0 -291
- package/clients/ast-grep-client.js +0 -253
- package/clients/ast-grep-parser.js +0 -84
- package/clients/ast-grep-rule-manager.js +0 -89
- package/clients/ast-grep-types.js +0 -9
- package/clients/auto-loop.js +0 -131
- package/clients/biome-client.js +0 -420
- package/clients/biome-client.test.js +0 -144
- package/clients/biome-client.test.ts +0 -163
- package/clients/cache/rule-cache.js +0 -72
- package/clients/cache-manager.js +0 -245
- package/clients/cache-manager.test.js +0 -197
- package/clients/cache-manager.test.ts +0 -299
- package/clients/complexity-client.js +0 -675
- package/clients/complexity-client.test.js +0 -234
- package/clients/complexity-client.test.ts +0 -255
- package/clients/config-validator.js +0 -465
- package/clients/dependency-checker.js +0 -325
- package/clients/dependency-checker.test.js +0 -60
- package/clients/dependency-checker.test.ts +0 -71
- package/clients/dispatch/__tests__/autofix-integration.test.js +0 -245
- package/clients/dispatch/__tests__/autofix-integration.test.ts +0 -300
- package/clients/dispatch/__tests__/runner-registration.test.js +0 -234
- package/clients/dispatch/__tests__/runner-registration.test.ts +0 -286
- package/clients/dispatch/debug.log +0 -1
- package/clients/dispatch/dispatcher.edge.test.js +0 -82
- package/clients/dispatch/dispatcher.edge.test.ts +0 -100
- package/clients/dispatch/dispatcher.format.test.js +0 -46
- package/clients/dispatch/dispatcher.format.test.ts +0 -58
- package/clients/dispatch/dispatcher.inline.test.js +0 -74
- package/clients/dispatch/dispatcher.inline.test.ts +0 -93
- package/clients/dispatch/dispatcher.js +0 -381
- package/clients/dispatch/dispatcher.test.js +0 -116
- package/clients/dispatch/dispatcher.test.ts +0 -149
- package/clients/dispatch/integration.js +0 -108
- package/clients/dispatch/plan.js +0 -183
- package/clients/dispatch/runners/architect.js +0 -83
- package/clients/dispatch/runners/architect.test.js +0 -138
- package/clients/dispatch/runners/architect.test.ts +0 -162
- package/clients/dispatch/runners/ast-grep-napi.js +0 -405
- package/clients/dispatch/runners/ast-grep-napi.test.js +0 -107
- package/clients/dispatch/runners/ast-grep-napi.test.ts +0 -129
- package/clients/dispatch/runners/ast-grep.js +0 -157
- package/clients/dispatch/runners/biome.js +0 -55
- package/clients/dispatch/runners/config-validation.js +0 -67
- package/clients/dispatch/runners/go-vet.js +0 -48
- package/clients/dispatch/runners/index.js +0 -47
- package/clients/dispatch/runners/lsp.js +0 -102
- package/clients/dispatch/runners/oxlint.js +0 -67
- package/clients/dispatch/runners/oxlint.test.js +0 -230
- package/clients/dispatch/runners/oxlint.test.ts +0 -303
- package/clients/dispatch/runners/pyright.js +0 -100
- package/clients/dispatch/runners/pyright.test.js +0 -98
- package/clients/dispatch/runners/pyright.test.ts +0 -121
- package/clients/dispatch/runners/python-slop.js +0 -97
- package/clients/dispatch/runners/python-slop.test.js +0 -203
- package/clients/dispatch/runners/python-slop.test.ts +0 -298
- package/clients/dispatch/runners/ruff.js +0 -48
- package/clients/dispatch/runners/rust-clippy.js +0 -102
- package/clients/dispatch/runners/scan_codebase.test.js +0 -89
- package/clients/dispatch/runners/scan_codebase.test.ts +0 -105
- package/clients/dispatch/runners/shellcheck.js +0 -147
- package/clients/dispatch/runners/shellcheck.test.js +0 -98
- package/clients/dispatch/runners/shellcheck.test.ts +0 -129
- package/clients/dispatch/runners/similarity.js +0 -230
- package/clients/dispatch/runners/spellcheck.js +0 -106
- package/clients/dispatch/runners/spellcheck.test.js +0 -158
- package/clients/dispatch/runners/spellcheck.test.ts +0 -214
- package/clients/dispatch/runners/tree-sitter.js +0 -246
- package/clients/dispatch/runners/ts-lsp.js +0 -125
- package/clients/dispatch/runners/ts-slop.js +0 -113
- package/clients/dispatch/runners/type-safety.js +0 -142
- package/clients/dispatch/runners/utils/diagnostic-parsers.js +0 -134
- package/clients/dispatch/runners/utils/runner-helpers.js +0 -115
- package/clients/dispatch/runners/utils.js +0 -51
- package/clients/dispatch/runners/yaml-rule-parser.js +0 -360
- package/clients/dispatch/types.js +0 -16
- package/clients/dispatch/utils/format-utils.js +0 -44
- package/clients/dogfood.test.js +0 -201
- package/clients/dogfood.test.ts +0 -269
- package/clients/file-kinds.js +0 -177
- package/clients/file-kinds.test.js +0 -169
- package/clients/file-kinds.test.ts +0 -210
- package/clients/file-time.js +0 -152
- package/clients/file-utils.js +0 -40
- package/clients/fix-scanners.js +0 -204
- package/clients/format-service.js +0 -184
- package/clients/formatters.js +0 -488
- package/clients/go-client.js +0 -203
- package/clients/go-client.test.js +0 -127
- package/clients/go-client.test.ts +0 -143
- package/clients/installer/index.js +0 -403
- package/clients/interviewer-templates.js +0 -75
- package/clients/interviewer.js +0 -173
- package/clients/jscpd-client.js +0 -196
- package/clients/jscpd-client.test.js +0 -127
- package/clients/jscpd-client.test.ts +0 -145
- package/clients/knip-client.js +0 -239
- package/clients/knip-client.test.js +0 -112
- package/clients/knip-client.test.ts +0 -128
- package/clients/latency-logger.js +0 -40
- package/clients/lsp/__tests__/client.test.js +0 -310
- package/clients/lsp/__tests__/client.test.ts +0 -412
- package/clients/lsp/__tests__/config.test.js +0 -167
- package/clients/lsp/__tests__/config.test.ts +0 -217
- package/clients/lsp/__tests__/error-recovery.test.js +0 -213
- package/clients/lsp/__tests__/error-recovery.test.ts +0 -279
- package/clients/lsp/__tests__/integration.test.js +0 -127
- package/clients/lsp/__tests__/integration.test.ts +0 -160
- package/clients/lsp/__tests__/launch.test.js +0 -313
- package/clients/lsp/__tests__/launch.test.ts +0 -394
- package/clients/lsp/__tests__/server.test.js +0 -259
- package/clients/lsp/__tests__/server.test.ts +0 -332
- package/clients/lsp/__tests__/service.test.js +0 -438
- package/clients/lsp/__tests__/service.test.ts +0 -530
- package/clients/lsp/client.js +0 -350
- package/clients/lsp/config.js +0 -112
- package/clients/lsp/index.js +0 -318
- package/clients/lsp/installer/index.js +0 -391
- package/clients/lsp/interactive-install.js +0 -221
- package/clients/lsp/language.js +0 -170
- package/clients/lsp/launch.js +0 -329
- package/clients/lsp/lsp/launch.js +0 -116
- package/clients/lsp/lsp/server.js +0 -532
- package/clients/lsp/lsp-index.js +0 -10
- package/clients/lsp/path-utils.js +0 -5
- package/clients/lsp/server.js +0 -725
- package/clients/lsp/test-py-spawn/requirements.txt +0 -1
- package/clients/lsp/test-py-spawn/test.py +0 -3
- package/clients/lsp/test-py-svc/requirements.txt +0 -1
- package/clients/lsp/test-py-svc/test.py +0 -3
- package/clients/lsp/test-python-project/requirements.txt +0 -1
- package/clients/lsp/test-python-project/test.py +0 -5
- package/clients/metrics-client.js +0 -107
- package/clients/metrics-client.test.js +0 -128
- package/clients/metrics-client.test.ts +0 -163
- package/clients/metrics-history.js +0 -367
- package/clients/path-utils.js +0 -142
- package/clients/pipeline.js +0 -272
- package/clients/production-readiness.js +0 -522
- package/clients/project-index.js +0 -255
- package/clients/project-metadata.js +0 -531
- package/clients/ruff-client.js +0 -325
- package/clients/ruff-client.test.js +0 -132
- package/clients/ruff-client.test.ts +0 -153
- package/clients/rules-scanner.js +0 -97
- package/clients/runner-tracker.js +0 -152
- package/clients/rust-client.js +0 -205
- package/clients/rust-client.test.js +0 -108
- package/clients/rust-client.test.ts +0 -130
- package/clients/safe-spawn-async.js +0 -163
- package/clients/safe-spawn.js +0 -241
- package/clients/sanitize.js +0 -291
- package/clients/sanitize.test.js +0 -177
- package/clients/sanitize.test.ts +0 -223
- package/clients/scan-architectural-debt.js +0 -167
- package/clients/scan-utils.js +0 -83
- package/clients/secrets-scanner.js +0 -119
- package/clients/secrets-scanner.test.js +0 -100
- package/clients/secrets-scanner.test.ts +0 -113
- package/clients/sg-runner.js +0 -292
- package/clients/state-matrix.js +0 -160
- package/clients/subprocess-client.js +0 -65
- package/clients/symbol-types.js +0 -5
- package/clients/test-runner-client.js +0 -523
- package/clients/test-runner-client.test.js +0 -192
- package/clients/test-runner-client.test.ts +0 -253
- package/clients/test-utils.js +0 -27
- package/clients/test-utils.ts +0 -36
- package/clients/todo-scanner.js +0 -200
- package/clients/todo-scanner.test.js +0 -301
- package/clients/todo-scanner.test.ts +0 -352
- package/clients/tool-availability.js +0 -207
- package/clients/tree-sitter-client.js +0 -601
- package/clients/tree-sitter-query-loader.js +0 -355
- package/clients/tree-sitter-symbol-extractor.js +0 -289
- package/clients/ts-service.js +0 -129
- package/clients/type-coverage-client.js +0 -127
- package/clients/type-coverage-client.test.js +0 -105
- package/clients/type-coverage-client.test.ts +0 -125
- package/clients/type-safety-client.js +0 -138
- package/clients/types.js +0 -11
- package/clients/typescript-client.codefix.test.js +0 -157
- package/clients/typescript-client.codefix.test.ts +0 -186
- package/clients/typescript-client.js +0 -509
- package/clients/typescript-client.test.js +0 -105
- package/clients/typescript-client.test.ts +0 -126
- package/commands/booboo.js +0 -1007
- package/commands/fix-from-booboo.js +0 -398
- package/commands/fix-simplified.js +0 -618
- package/commands/rate.js +0 -281
- package/commands/rate.test.js +0 -119
- package/commands/rate.test.ts +0 -131
- package/commands/refactor.js +0 -130
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Content-level secrets scanner
|
|
3
|
-
*
|
|
4
|
-
* Scans file content for potential secret patterns before write.
|
|
5
|
-
* Works on all file types via regex matching.
|
|
6
|
-
*
|
|
7
|
-
* Detected patterns:
|
|
8
|
-
* - Stripe/OpenAI keys (sk-*)
|
|
9
|
-
* - GitHub tokens (ghp_*, gho_*, github_pat_*)
|
|
10
|
-
* - AWS keys (AKIA*)
|
|
11
|
-
* - Slack tokens (xoxp-*, xoxb-*)
|
|
12
|
-
* - Private keys (BEGIN PRIVATE KEY)
|
|
13
|
-
* - Generic API key/password patterns
|
|
14
|
-
*/
|
|
15
|
-
import { isTestFile } from "./file-utils.js";
|
|
16
|
-
// Patterns ordered by specificity - first match wins per line
|
|
17
|
-
const SECRET_PATTERNS = [
|
|
18
|
-
// High-confidence: specific key prefixes
|
|
19
|
-
{
|
|
20
|
-
pattern: /sk-[a-zA-Z0-9-]{20,}/g,
|
|
21
|
-
name: "stripe-openai-key",
|
|
22
|
-
message: "Possible Stripe or OpenAI API key (sk-*)",
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
pattern: /ghp_[a-zA-Z0-9]{36}/g,
|
|
26
|
-
name: "github-personal-token",
|
|
27
|
-
message: "GitHub personal access token (ghp_*)",
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
pattern: /gho_[a-zA-Z0-9]{36}/g,
|
|
31
|
-
name: "github-oauth-token",
|
|
32
|
-
message: "GitHub OAuth token (gho_*)",
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
pattern: /github_pat_[a-zA-Z_]{82}/g,
|
|
36
|
-
name: "github-fine-grained-pat",
|
|
37
|
-
message: "GitHub fine-grained PAT (github_pat_*)",
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
pattern: /AKIA[0-9A-Z]{16}/g,
|
|
41
|
-
name: "aws-access-key",
|
|
42
|
-
message: "AWS access key ID (AKIA*)",
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
pattern: /xox[bp]-[a-zA-Z0-9]{10,}/g,
|
|
46
|
-
name: "slack-token",
|
|
47
|
-
message: "Slack token (xoxb-*/xoxp-*)",
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
pattern: /-----BEGIN\s+(RSA\s+)?PRIVATE KEY-----/g,
|
|
51
|
-
name: "private-key",
|
|
52
|
-
message: "Private key material detected",
|
|
53
|
-
},
|
|
54
|
-
// Medium-confidence: quoted credentials
|
|
55
|
-
{
|
|
56
|
-
pattern: /password\s*[:=]\s*["'][^"']{4,}["']/gi,
|
|
57
|
-
name: "hardcoded-password",
|
|
58
|
-
message: "Possible hardcoded password",
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
pattern: /(?:secret|api_?key|token|access_?key)\s*[:=]\s*["'][a-zA-Z0-9_\-/.]{8,}["']/gi,
|
|
62
|
-
name: "hardcoded-secret",
|
|
63
|
-
message: "Possible hardcoded secret or API key",
|
|
64
|
-
},
|
|
65
|
-
// .env format: KEY=VALUE (no quotes)
|
|
66
|
-
{
|
|
67
|
-
pattern: /^(?:API_?KEY|SECRET|TOKEN|PASSWORD|AWS_?ACCESS_?KEY)\s*=\s*\S{8,}/gim,
|
|
68
|
-
name: "env-file-secret",
|
|
69
|
-
message: "Possible secret in .env format",
|
|
70
|
-
},
|
|
71
|
-
];
|
|
72
|
-
/**
|
|
73
|
-
* Scan content for potential secrets
|
|
74
|
-
* Returns findings with line numbers.
|
|
75
|
-
* Skips test files to avoid false positives.
|
|
76
|
-
*/
|
|
77
|
-
export function scanForSecrets(content, filePath) {
|
|
78
|
-
// Skip test files — secrets in tests are usually fake/test values
|
|
79
|
-
if (filePath && isTestFile(filePath)) {
|
|
80
|
-
return [];
|
|
81
|
-
}
|
|
82
|
-
const findings = [];
|
|
83
|
-
const lines = content.split("\n");
|
|
84
|
-
for (let i = 0; i < lines.length; i++) {
|
|
85
|
-
const line = lines[i];
|
|
86
|
-
let _matched = false;
|
|
87
|
-
for (const pattern of SECRET_PATTERNS) {
|
|
88
|
-
// Reset lastIndex before each test (important for global regex)
|
|
89
|
-
const regex = new RegExp(pattern.pattern.source, pattern.pattern.flags);
|
|
90
|
-
if (regex.test(line)) {
|
|
91
|
-
findings.push({
|
|
92
|
-
line: i + 1,
|
|
93
|
-
message: pattern.message,
|
|
94
|
-
});
|
|
95
|
-
_matched = true;
|
|
96
|
-
break; // One finding per line
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return findings;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Format secrets findings for terminal output
|
|
104
|
-
*/
|
|
105
|
-
export function formatSecrets(findings, filePath) {
|
|
106
|
-
if (findings.length === 0)
|
|
107
|
-
return "";
|
|
108
|
-
const lines = [
|
|
109
|
-
`🔴 STOP — ${findings.length} potential secret(s) in ${filePath}:`,
|
|
110
|
-
];
|
|
111
|
-
for (const f of findings.slice(0, 5)) {
|
|
112
|
-
lines.push(` L${f.line}: ${f.message}`);
|
|
113
|
-
}
|
|
114
|
-
if (findings.length > 5) {
|
|
115
|
-
lines.push(` ... and ${findings.length - 5} more`);
|
|
116
|
-
}
|
|
117
|
-
lines.push(" → Remove before continuing. Use env vars instead.");
|
|
118
|
-
return lines.join("\n");
|
|
119
|
-
}
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { formatSecrets, scanForSecrets } from "./secrets-scanner.js";
|
|
3
|
-
describe("scanForSecrets", () => {
|
|
4
|
-
it("should detect Stripe/OpenAI keys (sk-*)", () => {
|
|
5
|
-
const content = `const apiKey = "sk-live-1234567890abcdefghij";`;
|
|
6
|
-
const findings = scanForSecrets(content);
|
|
7
|
-
expect(findings.length).toBe(1);
|
|
8
|
-
expect(findings[0].message).toContain("Stripe or OpenAI");
|
|
9
|
-
});
|
|
10
|
-
it("should detect GitHub personal tokens (ghp_*)", () => {
|
|
11
|
-
const content = `token = "ghp_1234567890abcdefghijklmnopqrstuvwxyz";`;
|
|
12
|
-
const findings = scanForSecrets(content);
|
|
13
|
-
expect(findings.length).toBe(1);
|
|
14
|
-
expect(findings[0].message).toContain("GitHub personal");
|
|
15
|
-
});
|
|
16
|
-
it("should detect AWS access keys (AKIA*)", () => {
|
|
17
|
-
const content = `const AWS_KEY = "AKIAIOSFODNN7EXAMPLE";`;
|
|
18
|
-
const findings = scanForSecrets(content);
|
|
19
|
-
expect(findings.length).toBe(1);
|
|
20
|
-
expect(findings[0].message).toContain("AWS access key");
|
|
21
|
-
});
|
|
22
|
-
it("should detect private key material", () => {
|
|
23
|
-
const content = `-----BEGIN RSA PRIVATE KEY-----
|
|
24
|
-
MIIEpAIBAAKCAQEA...`;
|
|
25
|
-
const findings = scanForSecrets(content);
|
|
26
|
-
expect(findings.length).toBe(1);
|
|
27
|
-
expect(findings[0].message).toContain("Private key");
|
|
28
|
-
});
|
|
29
|
-
it("should detect hardcoded passwords", () => {
|
|
30
|
-
const content = `const config = { password: "hunter2" };`;
|
|
31
|
-
const findings = scanForSecrets(content);
|
|
32
|
-
expect(findings.length).toBe(1);
|
|
33
|
-
expect(findings[0].message).toContain("password");
|
|
34
|
-
});
|
|
35
|
-
it("should detect secrets in .env format", () => {
|
|
36
|
-
const content = `API_KEY=sk-live-1234567890abcdefghij
|
|
37
|
-
DATABASE_URL=postgres://localhost`;
|
|
38
|
-
const findings = scanForSecrets(content);
|
|
39
|
-
expect(findings.length).toBe(1);
|
|
40
|
-
// sk-* pattern catches this first (more specific)
|
|
41
|
-
expect(findings[0].message).toContain("Stripe or OpenAI");
|
|
42
|
-
});
|
|
43
|
-
it("should NOT flag safe content", () => {
|
|
44
|
-
const content = `
|
|
45
|
-
const name = "test";
|
|
46
|
-
const url = "https://example.com";
|
|
47
|
-
const port = 3000;
|
|
48
|
-
const message = "Hello world";
|
|
49
|
-
`;
|
|
50
|
-
const findings = scanForSecrets(content);
|
|
51
|
-
expect(findings.length).toBe(0);
|
|
52
|
-
});
|
|
53
|
-
it("should NOT flag env var references", () => {
|
|
54
|
-
const content = `const key = process.env.API_KEY;`;
|
|
55
|
-
const findings = scanForSecrets(content);
|
|
56
|
-
expect(findings.length).toBe(0);
|
|
57
|
-
});
|
|
58
|
-
it("should detect multiple secrets", () => {
|
|
59
|
-
const content = `
|
|
60
|
-
const sk = "sk-live-1234567890abcdefghij";
|
|
61
|
-
const gh = "ghp_1234567890abcdefghijklmnopqrstuvwxyz";
|
|
62
|
-
`;
|
|
63
|
-
const findings = scanForSecrets(content);
|
|
64
|
-
expect(findings.length).toBe(2);
|
|
65
|
-
});
|
|
66
|
-
it("should report correct line numbers", () => {
|
|
67
|
-
const content = `line 1
|
|
68
|
-
line 2
|
|
69
|
-
const secret = "sk-live-1234567890abcdefghij";
|
|
70
|
-
line 4`;
|
|
71
|
-
const findings = scanForSecrets(content);
|
|
72
|
-
expect(findings.length).toBe(1);
|
|
73
|
-
expect(findings[0].line).toBe(3);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
describe("formatSecrets", () => {
|
|
77
|
-
it("should format findings for terminal output", () => {
|
|
78
|
-
const findings = [
|
|
79
|
-
{ line: 5, message: "Possible Stripe or OpenAI API key (sk-*)" },
|
|
80
|
-
];
|
|
81
|
-
const output = formatSecrets(findings, "src/config.ts");
|
|
82
|
-
expect(output).toContain("STOP");
|
|
83
|
-
expect(output).toContain("1 potential secret(s)");
|
|
84
|
-
expect(output).toContain("L5");
|
|
85
|
-
expect(output).toContain("src/config.ts");
|
|
86
|
-
});
|
|
87
|
-
it("should return empty string for no findings", () => {
|
|
88
|
-
const output = formatSecrets([], "src/config.ts");
|
|
89
|
-
expect(output).toBe("");
|
|
90
|
-
});
|
|
91
|
-
it("should truncate at 5 findings", () => {
|
|
92
|
-
const findings = Array.from({ length: 10 }, (_, i) => ({
|
|
93
|
-
line: i + 1,
|
|
94
|
-
message: "Test secret",
|
|
95
|
-
}));
|
|
96
|
-
const output = formatSecrets(findings, "src/config.ts");
|
|
97
|
-
expect(output).toContain("10 potential secret(s)");
|
|
98
|
-
expect(output).toContain("... and 5 more");
|
|
99
|
-
});
|
|
100
|
-
});
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { formatSecrets, scanForSecrets } from "./secrets-scanner.js";
|
|
3
|
-
|
|
4
|
-
describe("scanForSecrets", () => {
|
|
5
|
-
it("should detect Stripe/OpenAI keys (sk-*)", () => {
|
|
6
|
-
const content = `const apiKey = "sk-live-1234567890abcdefghij";`;
|
|
7
|
-
const findings = scanForSecrets(content);
|
|
8
|
-
expect(findings.length).toBe(1);
|
|
9
|
-
expect(findings[0].message).toContain("Stripe or OpenAI");
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it("should detect GitHub personal tokens (ghp_*)", () => {
|
|
13
|
-
const content = `token = "ghp_1234567890abcdefghijklmnopqrstuvwxyz";`;
|
|
14
|
-
const findings = scanForSecrets(content);
|
|
15
|
-
expect(findings.length).toBe(1);
|
|
16
|
-
expect(findings[0].message).toContain("GitHub personal");
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("should detect AWS access keys (AKIA*)", () => {
|
|
20
|
-
const content = `const AWS_KEY = "AKIAIOSFODNN7EXAMPLE";`;
|
|
21
|
-
const findings = scanForSecrets(content);
|
|
22
|
-
expect(findings.length).toBe(1);
|
|
23
|
-
expect(findings[0].message).toContain("AWS access key");
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("should detect private key material", () => {
|
|
27
|
-
const content = `-----BEGIN RSA PRIVATE KEY-----
|
|
28
|
-
MIIEpAIBAAKCAQEA...`;
|
|
29
|
-
const findings = scanForSecrets(content);
|
|
30
|
-
expect(findings.length).toBe(1);
|
|
31
|
-
expect(findings[0].message).toContain("Private key");
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("should detect hardcoded passwords", () => {
|
|
35
|
-
const content = `const config = { password: "hunter2" };`;
|
|
36
|
-
const findings = scanForSecrets(content);
|
|
37
|
-
expect(findings.length).toBe(1);
|
|
38
|
-
expect(findings[0].message).toContain("password");
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("should detect secrets in .env format", () => {
|
|
42
|
-
const content = `API_KEY=sk-live-1234567890abcdefghij
|
|
43
|
-
DATABASE_URL=postgres://localhost`;
|
|
44
|
-
const findings = scanForSecrets(content);
|
|
45
|
-
expect(findings.length).toBe(1);
|
|
46
|
-
// sk-* pattern catches this first (more specific)
|
|
47
|
-
expect(findings[0].message).toContain("Stripe or OpenAI");
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("should NOT flag safe content", () => {
|
|
51
|
-
const content = `
|
|
52
|
-
const name = "test";
|
|
53
|
-
const url = "https://example.com";
|
|
54
|
-
const port = 3000;
|
|
55
|
-
const message = "Hello world";
|
|
56
|
-
`;
|
|
57
|
-
const findings = scanForSecrets(content);
|
|
58
|
-
expect(findings.length).toBe(0);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("should NOT flag env var references", () => {
|
|
62
|
-
const content = `const key = process.env.API_KEY;`;
|
|
63
|
-
const findings = scanForSecrets(content);
|
|
64
|
-
expect(findings.length).toBe(0);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("should detect multiple secrets", () => {
|
|
68
|
-
const content = `
|
|
69
|
-
const sk = "sk-live-1234567890abcdefghij";
|
|
70
|
-
const gh = "ghp_1234567890abcdefghijklmnopqrstuvwxyz";
|
|
71
|
-
`;
|
|
72
|
-
const findings = scanForSecrets(content);
|
|
73
|
-
expect(findings.length).toBe(2);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it("should report correct line numbers", () => {
|
|
77
|
-
const content = `line 1
|
|
78
|
-
line 2
|
|
79
|
-
const secret = "sk-live-1234567890abcdefghij";
|
|
80
|
-
line 4`;
|
|
81
|
-
const findings = scanForSecrets(content);
|
|
82
|
-
expect(findings.length).toBe(1);
|
|
83
|
-
expect(findings[0].line).toBe(3);
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe("formatSecrets", () => {
|
|
88
|
-
it("should format findings for terminal output", () => {
|
|
89
|
-
const findings = [
|
|
90
|
-
{ line: 5, message: "Possible Stripe or OpenAI API key (sk-*)" },
|
|
91
|
-
];
|
|
92
|
-
const output = formatSecrets(findings, "src/config.ts");
|
|
93
|
-
expect(output).toContain("STOP");
|
|
94
|
-
expect(output).toContain("1 potential secret(s)");
|
|
95
|
-
expect(output).toContain("L5");
|
|
96
|
-
expect(output).toContain("src/config.ts");
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it("should return empty string for no findings", () => {
|
|
100
|
-
const output = formatSecrets([], "src/config.ts");
|
|
101
|
-
expect(output).toBe("");
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("should truncate at 5 findings", () => {
|
|
105
|
-
const findings = Array.from({ length: 10 }, (_, i) => ({
|
|
106
|
-
line: i + 1,
|
|
107
|
-
message: "Test secret",
|
|
108
|
-
}));
|
|
109
|
-
const output = formatSecrets(findings, "src/config.ts");
|
|
110
|
-
expect(output).toContain("10 potential secret(s)");
|
|
111
|
-
expect(output).toContain("... and 5 more");
|
|
112
|
-
});
|
|
113
|
-
});
|
package/clients/sg-runner.js
DELETED
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SgRunner - encapsulates ast-grep subprocess management
|
|
3
|
-
*
|
|
4
|
-
* Extracted from AstGrepClient to simplify the main client.
|
|
5
|
-
* Handles: spawn, spawnSync, temp dir management, JSON parsing.
|
|
6
|
-
*/
|
|
7
|
-
import { spawn } from "node:child_process";
|
|
8
|
-
import * as fs from "node:fs";
|
|
9
|
-
import * as os from "node:os";
|
|
10
|
-
import * as path from "node:path";
|
|
11
|
-
import { safeSpawn } from "./safe-spawn.js";
|
|
12
|
-
/**
|
|
13
|
-
* Escape an argument for Windows cmd.exe shell execution.
|
|
14
|
-
* Handles spaces, quotes, and special characters.
|
|
15
|
-
*/
|
|
16
|
-
function escapeWindowsArg(arg) {
|
|
17
|
-
// If no special characters, return as-is
|
|
18
|
-
if (!/[\s"]/.test(arg))
|
|
19
|
-
return arg;
|
|
20
|
-
// Escape quotes by doubling them
|
|
21
|
-
return `"${arg.replace(/"/g, '""')}"`;
|
|
22
|
-
}
|
|
23
|
-
export class SgRunner {
|
|
24
|
-
constructor(verbose = false) {
|
|
25
|
-
this.sgPath = null;
|
|
26
|
-
this.available = null;
|
|
27
|
-
this.log = verbose
|
|
28
|
-
? (msg) => console.error(`[sg-runner] ${msg}`)
|
|
29
|
-
: () => { };
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Check if ast-grep CLI is available, auto-install if not
|
|
33
|
-
*/
|
|
34
|
-
async ensureAvailable() {
|
|
35
|
-
// Fast path: already checked
|
|
36
|
-
if (this.available !== null)
|
|
37
|
-
return this.available;
|
|
38
|
-
// Check if available in PATH (fast)
|
|
39
|
-
const pathResult = safeSpawn("sg", ["--version"], {
|
|
40
|
-
timeout: 5000,
|
|
41
|
-
});
|
|
42
|
-
if (!pathResult.error && pathResult.status === 0) {
|
|
43
|
-
this.sgPath = "sg";
|
|
44
|
-
this.available = true;
|
|
45
|
-
this.log("ast-grep found in PATH");
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
// Auto-install via pi-lens installer
|
|
49
|
-
this.log("ast-grep not found, attempting auto-install...");
|
|
50
|
-
const { ensureTool } = await import("./installer/index.js");
|
|
51
|
-
const installedPath = await ensureTool("ast-grep");
|
|
52
|
-
if (installedPath) {
|
|
53
|
-
this.sgPath = installedPath;
|
|
54
|
-
this.available = true;
|
|
55
|
-
this.log(`ast-grep auto-installed: ${installedPath}`);
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
this.available = false;
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Check if ast-grep CLI is available (legacy sync method)
|
|
63
|
-
* Prefer ensureAvailable() for auto-install behavior
|
|
64
|
-
*/
|
|
65
|
-
isAvailable() {
|
|
66
|
-
if (this.available !== null)
|
|
67
|
-
return this.available;
|
|
68
|
-
const result = safeSpawn("npx", ["sg", "--version"], {
|
|
69
|
-
timeout: 10000,
|
|
70
|
-
});
|
|
71
|
-
this.available = !result.error && result.status === 0;
|
|
72
|
-
return this.available;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Get the sg command to use (local binary or "sg" from PATH)
|
|
76
|
-
*/
|
|
77
|
-
getSgCommand() {
|
|
78
|
-
return this.sgPath || "sg";
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Run ast-grep asynchronously, return parsed matches
|
|
82
|
-
*/
|
|
83
|
-
async exec(args) {
|
|
84
|
-
return new Promise((resolve) => {
|
|
85
|
-
// On Windows with Git Bash/MSYS2, we need to use bash to properly
|
|
86
|
-
// handle $variables in patterns (prevent shell expansion)
|
|
87
|
-
const isWindows = process.platform === "win32";
|
|
88
|
-
const hasBash = process.env.MSYSTEM || process.env.GIT_SHELL;
|
|
89
|
-
let proc;
|
|
90
|
-
if (isWindows && hasBash) {
|
|
91
|
-
// Use bash -c with properly escaped command
|
|
92
|
-
// In bash, use single quotes around arguments containing $ to prevent expansion
|
|
93
|
-
const escapedArgs = args.map((arg) => {
|
|
94
|
-
// For bash, wrap $-containing args in single quotes
|
|
95
|
-
if (arg.includes("$")) {
|
|
96
|
-
return `'${arg.replace(/'/g, "'\\''")}'`;
|
|
97
|
-
}
|
|
98
|
-
// For other args with spaces/special chars, use double quotes
|
|
99
|
-
if (/[\s"]/.test(arg)) {
|
|
100
|
-
return `"${arg.replace(/"/g, '\\"')}"`;
|
|
101
|
-
}
|
|
102
|
-
return arg;
|
|
103
|
-
});
|
|
104
|
-
const bashCommand = `${this.getSgCommand()} ${escapedArgs.join(" ")}`;
|
|
105
|
-
proc = spawn("bash", ["-c", bashCommand], {
|
|
106
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
107
|
-
windowsHide: true,
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
else if (isWindows) {
|
|
111
|
-
// Fallback: use cmd.exe with standard escaping
|
|
112
|
-
const fullCommand = `${this.getSgCommand()} ${args.map(escapeWindowsArg).join(" ")}`;
|
|
113
|
-
proc = spawn(fullCommand, {
|
|
114
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
115
|
-
shell: true,
|
|
116
|
-
windowsHide: true,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
// Unix: normal spawn without shell
|
|
121
|
-
proc = spawn(this.getSgCommand(), args, {
|
|
122
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
let stdout = "";
|
|
126
|
-
let stderr = "";
|
|
127
|
-
proc.stdout.on("data", (data) => (stdout += data.toString()));
|
|
128
|
-
proc.stderr.on("data", (data) => (stderr += data.toString()));
|
|
129
|
-
proc.on("error", (err) => {
|
|
130
|
-
if (err.message.includes("ENOENT")) {
|
|
131
|
-
resolve({
|
|
132
|
-
matches: [],
|
|
133
|
-
error: "ast-grep CLI not found. Install: npm i -D @ast-grep/cli",
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
resolve({ matches: [], error: err.message });
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
proc.on("close", (code) => {
|
|
141
|
-
if (code !== 0 && !stdout.trim()) {
|
|
142
|
-
// Enhanced error messages for common pattern issues
|
|
143
|
-
let errorMsg = stderr.trim() || `Exit code ${code}`;
|
|
144
|
-
if (stderr.includes("Multiple AST nodes are detected")) {
|
|
145
|
-
errorMsg =
|
|
146
|
-
`Invalid AST pattern: The pattern appears to contain multiple AST nodes or is malformed.\n` +
|
|
147
|
-
`Common causes:\n` +
|
|
148
|
-
` 1. Missing parentheses: use it($TEST) not it"test"\n` +
|
|
149
|
-
` 2. Raw text without structure: use console.log($MSG) not just "console.log"\n` +
|
|
150
|
-
` 3. Unclosed quotes or brackets\n\n` +
|
|
151
|
-
`Original error: ${errorMsg}`;
|
|
152
|
-
}
|
|
153
|
-
else if (stderr.includes("Cannot parse query")) {
|
|
154
|
-
errorMsg =
|
|
155
|
-
`Pattern syntax error: The pattern could not be parsed as valid code.\n` +
|
|
156
|
-
`Tips:\n` +
|
|
157
|
-
` - Patterns must be valid ${args.includes("--lang") ? args[args.indexOf("--lang") + 1] : "language"} syntax\n` +
|
|
158
|
-
` - Use metavariables like $NAME, $ARGS for variable parts\n` +
|
|
159
|
-
` - Example: 'function $NAME($$$PARAMS) { $$$BODY }'\n\n` +
|
|
160
|
-
`Original error: ${errorMsg}`;
|
|
161
|
-
}
|
|
162
|
-
resolve({
|
|
163
|
-
matches: [],
|
|
164
|
-
error: stderr.includes("No files found") ? undefined : errorMsg,
|
|
165
|
-
});
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
if (!stdout.trim()) {
|
|
169
|
-
resolve({ matches: [] });
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
try {
|
|
173
|
-
const parsed = JSON.parse(stdout);
|
|
174
|
-
const matches = Array.isArray(parsed) ? parsed : [parsed];
|
|
175
|
-
resolve({ matches });
|
|
176
|
-
}
|
|
177
|
-
catch {
|
|
178
|
-
resolve({ matches: [], error: "Failed to parse output" });
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Run ast-grep synchronously (for simple scans)
|
|
185
|
-
*/
|
|
186
|
-
execSync(args) {
|
|
187
|
-
const result = safeSpawn("npx", ["sg", ...args], {
|
|
188
|
-
timeout: 30000,
|
|
189
|
-
});
|
|
190
|
-
if (result.error) {
|
|
191
|
-
return { output: "", error: result.error.message };
|
|
192
|
-
}
|
|
193
|
-
const output = result.stdout || result.stderr || "";
|
|
194
|
-
return { output };
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Run a temporary rule scan (creates temp dir with rule file)
|
|
198
|
-
*/
|
|
199
|
-
tempScan(dir, ruleId, ruleYaml, timeout = 30000) {
|
|
200
|
-
const tmpDir = os.tmpdir();
|
|
201
|
-
const ts = Date.now();
|
|
202
|
-
const sessionDir = path.join(tmpDir, `pi-lens-temp-${ruleId}-${ts}`);
|
|
203
|
-
const rulesSubdir = path.join(sessionDir, "rules");
|
|
204
|
-
const ruleFile = path.join(rulesSubdir, `${ruleId}.yml`);
|
|
205
|
-
const configFile = path.join(sessionDir, ".sgconfig.yml");
|
|
206
|
-
try {
|
|
207
|
-
fs.mkdirSync(rulesSubdir, { recursive: true });
|
|
208
|
-
fs.writeFileSync(configFile, `ruleDirs:\n - ./rules\n`);
|
|
209
|
-
fs.writeFileSync(ruleFile, ruleYaml);
|
|
210
|
-
const result = safeSpawn("npx", ["sg", "scan", "--config", configFile, "--json", dir], { timeout });
|
|
211
|
-
const output = result.stdout || result.stderr || "";
|
|
212
|
-
if (!output.trim())
|
|
213
|
-
return [];
|
|
214
|
-
const items = JSON.parse(output);
|
|
215
|
-
return Array.isArray(items) ? items : [items];
|
|
216
|
-
}
|
|
217
|
-
catch {
|
|
218
|
-
return [];
|
|
219
|
-
}
|
|
220
|
-
finally {
|
|
221
|
-
try {
|
|
222
|
-
fs.rmSync(sessionDir, { recursive: true, force: true });
|
|
223
|
-
}
|
|
224
|
-
catch (err) {
|
|
225
|
-
this.log(`Cleanup failed: ${err.message}`);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Run a rule file scan (temporary config approach) - alias for tempScan
|
|
231
|
-
*/
|
|
232
|
-
scanWithRule(ruleYaml, dir, timeout = 30000) {
|
|
233
|
-
const sessionDir = path.join(os.tmpdir(), `sg-scan-${Date.now()}`);
|
|
234
|
-
const rulesSubdir = path.join(sessionDir, "rules");
|
|
235
|
-
const configFile = path.join(sessionDir, ".sgconfig.yml");
|
|
236
|
-
const ruleFile = path.join(rulesSubdir, "rule.yml");
|
|
237
|
-
try {
|
|
238
|
-
fs.mkdirSync(rulesSubdir, { recursive: true });
|
|
239
|
-
fs.writeFileSync(configFile, `ruleDirs:\n - ./rules\n`);
|
|
240
|
-
fs.writeFileSync(ruleFile, ruleYaml);
|
|
241
|
-
const result = safeSpawn("npx", ["sg", "scan", "--config", configFile, "--json", dir], { timeout });
|
|
242
|
-
const output = result.stdout || result.stderr || "";
|
|
243
|
-
if (!output.trim())
|
|
244
|
-
return [];
|
|
245
|
-
const items = JSON.parse(output);
|
|
246
|
-
return Array.isArray(items) ? items : [items];
|
|
247
|
-
}
|
|
248
|
-
catch {
|
|
249
|
-
return [];
|
|
250
|
-
}
|
|
251
|
-
finally {
|
|
252
|
-
try {
|
|
253
|
-
fs.rmSync(sessionDir, { recursive: true, force: true });
|
|
254
|
-
}
|
|
255
|
-
catch (err) {
|
|
256
|
-
this.log(`Cleanup failed: ${err.message}`);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Format matches for display
|
|
262
|
-
*/
|
|
263
|
-
formatMatches(matches, isDryRun = false, maxItems = 50, showModeIndicator = false) {
|
|
264
|
-
if (matches.length === 0) {
|
|
265
|
-
if (showModeIndicator) {
|
|
266
|
-
return isDryRun
|
|
267
|
-
? "[DRY-RUN] No matches found."
|
|
268
|
-
: "[APPLIED] No changes made (no matches found).";
|
|
269
|
-
}
|
|
270
|
-
return "No matches found";
|
|
271
|
-
}
|
|
272
|
-
const shown = matches.slice(0, maxItems);
|
|
273
|
-
const lines = shown.map((m) => {
|
|
274
|
-
const loc = `${m.file}:${m.range.start.line + 1}:${m.range.start.column + 1}`;
|
|
275
|
-
const text = m.text.length > 100 ? `${m.text.slice(0, 100)}...` : m.text;
|
|
276
|
-
return isDryRun && m.replacement
|
|
277
|
-
? `${loc}\n - ${text}\n + ${m.replacement}`
|
|
278
|
-
: `${loc}: ${text}`;
|
|
279
|
-
});
|
|
280
|
-
if (matches.length > maxItems) {
|
|
281
|
-
lines.unshift(`Found ${matches.length} matches (showing first ${maxItems}):`);
|
|
282
|
-
}
|
|
283
|
-
if (showModeIndicator) {
|
|
284
|
-
const prefix = isDryRun ? "[DRY-RUN]" : "[APPLIED]";
|
|
285
|
-
const suffix = isDryRun
|
|
286
|
-
? "\n\n(Dry run — use apply=true to apply changes)"
|
|
287
|
-
: "";
|
|
288
|
-
return `${prefix} ${matches.length} replacement(s):\n\n${lines.join("\n")}${suffix}`;
|
|
289
|
-
}
|
|
290
|
-
return lines.join("\n");
|
|
291
|
-
}
|
|
292
|
-
}
|