ship-safe 4.3.0 → 5.0.1
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/README.md +83 -23
- package/cli/__tests__/agents.test.js +579 -0
- package/cli/agents/agentic-security-agent.js +261 -0
- package/cli/agents/base-agent.js +11 -1
- package/cli/agents/deep-analyzer.js +333 -0
- package/cli/agents/index.js +16 -1
- package/cli/agents/injection-tester.js +45 -0
- package/cli/agents/mcp-security-agent.js +358 -0
- package/cli/agents/mobile-scanner.js +6 -0
- package/cli/agents/orchestrator.js +67 -8
- package/cli/agents/pii-compliance-agent.js +301 -0
- package/cli/agents/rag-security-agent.js +204 -0
- package/cli/agents/sbom-generator.js +100 -11
- package/cli/agents/scoring-engine.js +4 -0
- package/cli/agents/supabase-rls-agent.js +6 -0
- package/cli/agents/supply-chain-agent.js +152 -1
- package/cli/agents/verifier-agent.js +292 -0
- package/cli/bin/ship-safe.js +32 -6
- package/cli/commands/agent.js +2 -0
- package/cli/commands/audit.js +103 -7
- package/cli/commands/baseline.js +2 -1
- package/cli/commands/ci.js +262 -0
- package/cli/commands/fix.js +218 -216
- package/cli/commands/mcp.js +304 -303
- package/cli/commands/red-team.js +8 -2
- package/cli/commands/remediate.js +2 -0
- package/cli/commands/scan.js +567 -565
- package/cli/commands/score.js +2 -0
- package/cli/commands/watch.js +161 -160
- package/cli/index.js +1 -1
- package/cli/utils/patterns.js +1118 -1104
- package/cli/utils/secrets-verifier.js +247 -0
- package/package.json +2 -2
|
@@ -720,3 +720,582 @@ describe('Code Context in Findings', () => {
|
|
|
720
720
|
} finally { cleanup(dir); }
|
|
721
721
|
});
|
|
722
722
|
});
|
|
723
|
+
|
|
724
|
+
// =============================================================================
|
|
725
|
+
// MCP SECURITY AGENT (v5.0)
|
|
726
|
+
// =============================================================================
|
|
727
|
+
|
|
728
|
+
describe('MCPSecurityAgent', async () => {
|
|
729
|
+
const { MCPSecurityAgent } = await import('../agents/mcp-security-agent.js');
|
|
730
|
+
const agent = new MCPSecurityAgent();
|
|
731
|
+
|
|
732
|
+
it('detects MCP tool with shell execution', async () => {
|
|
733
|
+
const { dir, file } = writeTempFile('server.tool("run_cmd", async (a) => { return execSync(a.cmd); });');
|
|
734
|
+
try {
|
|
735
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
736
|
+
assert.ok(findings.some(f => f.rule === 'MCP_TOOL_SHELL_EXEC'), 'Should detect shell exec in MCP tool');
|
|
737
|
+
} finally { cleanup(dir); }
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
it('detects MCP tool with file system write', async () => {
|
|
741
|
+
const { dir, file } = writeTempFile('server.tool("write", async (a) => { fs.writeFileSync(a.path, a.data); });');
|
|
742
|
+
try {
|
|
743
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
744
|
+
assert.ok(findings.some(f => f.rule === 'MCP_TOOL_FS_WRITE'), 'Should detect fs write in MCP tool');
|
|
745
|
+
} finally { cleanup(dir); }
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it('detects MCP tool arguments passed to eval', async () => {
|
|
749
|
+
const { dir, file } = writeTempFile('server.tool("exec", async (a) => { return eval(a.code); });');
|
|
750
|
+
try {
|
|
751
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
752
|
+
assert.ok(findings.some(f => f.rule === 'MCP_TOOL_ARGS_TO_EVAL'), 'Should detect eval in MCP tool');
|
|
753
|
+
} finally { cleanup(dir); }
|
|
754
|
+
});
|
|
755
|
+
|
|
756
|
+
it('detects HTTP without TLS for remote MCP', async () => {
|
|
757
|
+
const { dir, file } = writeTempFile(`
|
|
758
|
+
const transport = new SSEServerTransport("http://remote-server.com:8080/mcp");
|
|
759
|
+
`);
|
|
760
|
+
try {
|
|
761
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
762
|
+
assert.ok(findings.some(f => f.rule === 'MCP_HTTP_NO_TLS'), 'Should detect HTTP without TLS');
|
|
763
|
+
} finally { cleanup(dir); }
|
|
764
|
+
});
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
// =============================================================================
|
|
768
|
+
// AGENTIC SECURITY AGENT (v5.0)
|
|
769
|
+
// =============================================================================
|
|
770
|
+
|
|
771
|
+
describe('AgenticSecurityAgent', async () => {
|
|
772
|
+
const { AgenticSecurityAgent } = await import('../agents/agentic-security-agent.js');
|
|
773
|
+
const agent = new AgenticSecurityAgent();
|
|
774
|
+
|
|
775
|
+
it('detects auto-execute without confirmation', async () => {
|
|
776
|
+
const { dir, file } = writeTempFile(`
|
|
777
|
+
const config = {
|
|
778
|
+
auto_approve: true,
|
|
779
|
+
requireConfirmation: false,
|
|
780
|
+
};
|
|
781
|
+
`);
|
|
782
|
+
try {
|
|
783
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
784
|
+
assert.ok(findings.some(f => f.rule === 'AGENT_TOOL_NO_CONFIRMATION'), 'Should detect auto-approve');
|
|
785
|
+
} finally { cleanup(dir); }
|
|
786
|
+
});
|
|
787
|
+
|
|
788
|
+
it('detects user input in agent memory', async () => {
|
|
789
|
+
const { dir, file } = writeTempFile(`
|
|
790
|
+
memory.push(userMessage);
|
|
791
|
+
context.add(input);
|
|
792
|
+
`);
|
|
793
|
+
try {
|
|
794
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
795
|
+
assert.ok(findings.some(f => f.rule === 'AGENT_MEMORY_USER_WRITE'), 'Should detect memory poisoning');
|
|
796
|
+
} finally { cleanup(dir); }
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
it('detects agent with shell tool access', async () => {
|
|
800
|
+
const { dir, file } = writeTempFile(`
|
|
801
|
+
const tools = [searchTool, child_process.exec];
|
|
802
|
+
const functions = [subprocess.run];
|
|
803
|
+
`);
|
|
804
|
+
try {
|
|
805
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
806
|
+
assert.ok(findings.some(f => f.rule === 'AGENT_TOOL_SHELL_ACCESS'), 'Should detect shell access in tools');
|
|
807
|
+
} finally { cleanup(dir); }
|
|
808
|
+
});
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
// =============================================================================
|
|
812
|
+
// RAG SECURITY AGENT (v5.0)
|
|
813
|
+
// =============================================================================
|
|
814
|
+
|
|
815
|
+
describe('RAGSecurityAgent', async () => {
|
|
816
|
+
const { RAGSecurityAgent } = await import('../agents/rag-security-agent.js');
|
|
817
|
+
const agent = new RAGSecurityAgent();
|
|
818
|
+
|
|
819
|
+
it('detects user upload to vector store', async () => {
|
|
820
|
+
const { dir, file } = writeTempFile('const docs = multer().single("file"); await vectorStore.addDocuments(docs);');
|
|
821
|
+
try {
|
|
822
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
823
|
+
assert.ok(findings.some(f => f.rule === 'RAG_USER_UPLOAD_TO_VECTORDB'), 'Should detect user upload to vector DB');
|
|
824
|
+
} finally { cleanup(dir); }
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
it('detects trust_remote_code=True', async () => {
|
|
828
|
+
const { dir, file } = writeTempFile(`
|
|
829
|
+
model = AutoModel.from_pretrained("user/model", trust_remote_code=True)
|
|
830
|
+
`, '.py');
|
|
831
|
+
try {
|
|
832
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
833
|
+
assert.ok(findings.some(f => f.rule === 'RAG_TRUST_REMOTE_CODE'), 'Should detect trust_remote_code');
|
|
834
|
+
} finally { cleanup(dir); }
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
it('detects pickle model loading', async () => {
|
|
838
|
+
const { dir, file } = writeTempFile(`
|
|
839
|
+
model = torch.load("model.pkl")
|
|
840
|
+
`, '.py');
|
|
841
|
+
try {
|
|
842
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
843
|
+
assert.ok(findings.some(f => f.rule === 'RAG_PICKLE_EMBEDDING_MODEL'), 'Should detect pickle load');
|
|
844
|
+
} finally { cleanup(dir); }
|
|
845
|
+
});
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
// =============================================================================
|
|
849
|
+
// PII COMPLIANCE AGENT (v5.0)
|
|
850
|
+
// =============================================================================
|
|
851
|
+
|
|
852
|
+
describe('PIIComplianceAgent', async () => {
|
|
853
|
+
const { PIIComplianceAgent } = await import('../agents/pii-compliance-agent.js');
|
|
854
|
+
const agent = new PIIComplianceAgent();
|
|
855
|
+
|
|
856
|
+
it('detects PII in console.log', async () => {
|
|
857
|
+
const { dir, file } = writeTempFile('console.log("User email:", user.email);');
|
|
858
|
+
try {
|
|
859
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
860
|
+
assert.ok(findings.some(f => f.rule === 'PII_IN_CONSOLE_LOG'), 'Should detect PII in console.log');
|
|
861
|
+
} finally { cleanup(dir); }
|
|
862
|
+
});
|
|
863
|
+
|
|
864
|
+
it('detects PII sent to analytics', async () => {
|
|
865
|
+
const { dir, file } = writeTempFile(`
|
|
866
|
+
analytics.track("signup", { email: user.email, phone: user.phone });
|
|
867
|
+
`);
|
|
868
|
+
try {
|
|
869
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
870
|
+
assert.ok(findings.some(f => f.rule === 'PII_TO_ANALYTICS'), 'Should detect PII to analytics');
|
|
871
|
+
} finally { cleanup(dir); }
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
it('detects SSN pattern in source code', async () => {
|
|
875
|
+
const { dir, file } = writeTempFile('const testSSN = "123-45-6789";');
|
|
876
|
+
try {
|
|
877
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
878
|
+
assert.ok(findings.some(f => f.rule === 'PII_SSN_IN_CODE'), 'Should detect SSN pattern');
|
|
879
|
+
} finally { cleanup(dir); }
|
|
880
|
+
});
|
|
881
|
+
});
|
|
882
|
+
|
|
883
|
+
// =============================================================================
|
|
884
|
+
// VIBE CODE DETECTION (v5.0)
|
|
885
|
+
// =============================================================================
|
|
886
|
+
|
|
887
|
+
describe('Vibe Code Detection', async () => {
|
|
888
|
+
const { InjectionTester } = await import('../agents/injection-tester.js');
|
|
889
|
+
const agent = new InjectionTester();
|
|
890
|
+
|
|
891
|
+
it('detects TODO to add authentication', async () => {
|
|
892
|
+
const { dir, file } = writeTempFile('// TODO: add authentication\napp.post("/api/admin", handler);');
|
|
893
|
+
try {
|
|
894
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
895
|
+
assert.ok(findings.some(f => f.rule === 'VIBE_TODO_AUTH'), 'Should detect TODO auth');
|
|
896
|
+
} finally { cleanup(dir); }
|
|
897
|
+
});
|
|
898
|
+
|
|
899
|
+
it('detects placeholder secrets', async () => {
|
|
900
|
+
const { dir, file } = writeTempFile('const apiKey = "your-api-key-here";');
|
|
901
|
+
try {
|
|
902
|
+
const findings = await agent.analyze({ rootPath: dir, files: [file], recon: {}, options: {} });
|
|
903
|
+
assert.ok(findings.some(f => f.rule === 'VIBE_PLACEHOLDER_SECRET'), 'Should detect placeholder secret');
|
|
904
|
+
} finally { cleanup(dir); }
|
|
905
|
+
});
|
|
906
|
+
});
|
|
907
|
+
|
|
908
|
+
// =============================================================================
|
|
909
|
+
// VERIFIER AGENT (v5.0)
|
|
910
|
+
// =============================================================================
|
|
911
|
+
|
|
912
|
+
describe('VerifierAgent', async () => {
|
|
913
|
+
const { VerifierAgent } = await import('../agents/verifier-agent.js');
|
|
914
|
+
const verifier = new VerifierAgent();
|
|
915
|
+
|
|
916
|
+
it('confirms finding with user input and no sanitization', async () => {
|
|
917
|
+
const code = 'app.post("/api", (req, res) => {\n const name = req.body.name;\n db.query(`SELECT * FROM users WHERE name = ${name}`);\n res.send("ok");\n});';
|
|
918
|
+
const { dir, file } = writeTempFile(code);
|
|
919
|
+
try {
|
|
920
|
+
const findings = [{
|
|
921
|
+
file, line: 3, severity: 'critical', confidence: 'high',
|
|
922
|
+
rule: 'SQL_INJECTION', matched: '`SELECT * FROM users',
|
|
923
|
+
}];
|
|
924
|
+
const verified = verifier.verify(findings);
|
|
925
|
+
assert.strictEqual(verified[0].verified, true, 'Should verify finding with user input');
|
|
926
|
+
assert.strictEqual(verified[0].confidence, 'high', 'Should keep high confidence');
|
|
927
|
+
} finally { cleanup(dir); }
|
|
928
|
+
});
|
|
929
|
+
|
|
930
|
+
it('downgrades finding with sanitization upstream', async () => {
|
|
931
|
+
const code = 'app.post("/api", (req, res) => {\n const name = sanitize(req.body.name);\n const validated = validator.escape(name);\n db.query(`SELECT * FROM users WHERE name = ${validated}`);\n res.send("ok");\n});';
|
|
932
|
+
const { dir, file } = writeTempFile(code);
|
|
933
|
+
try {
|
|
934
|
+
const findings = [{
|
|
935
|
+
file, line: 4, severity: 'critical', confidence: 'high',
|
|
936
|
+
rule: 'SQL_INJECTION', matched: '`SELECT * FROM users',
|
|
937
|
+
}];
|
|
938
|
+
const verified = verifier.verify(findings);
|
|
939
|
+
assert.strictEqual(verified[0].verified, false, 'Should not verify sanitized finding');
|
|
940
|
+
assert.strictEqual(verified[0].confidence, 'medium', 'Should downgrade confidence');
|
|
941
|
+
} finally { cleanup(dir); }
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
it('skips verification for medium/low severity', async () => {
|
|
945
|
+
const findings = [{
|
|
946
|
+
file: '/fake/path.js', line: 1, severity: 'medium', confidence: 'high',
|
|
947
|
+
rule: 'SOME_RULE', matched: 'something',
|
|
948
|
+
}];
|
|
949
|
+
const verified = verifier.verify(findings);
|
|
950
|
+
assert.strictEqual(verified[0].verified, null, 'Should skip medium severity');
|
|
951
|
+
});
|
|
952
|
+
});
|
|
953
|
+
|
|
954
|
+
// =============================================================================
|
|
955
|
+
// DEEP ANALYZER
|
|
956
|
+
// =============================================================================
|
|
957
|
+
|
|
958
|
+
describe('DeepAnalyzer', async () => {
|
|
959
|
+
const { DeepAnalyzer } = await import('../agents/deep-analyzer.js');
|
|
960
|
+
|
|
961
|
+
it('returns findings unchanged when no provider is set', async () => {
|
|
962
|
+
const analyzer = new DeepAnalyzer({ provider: null });
|
|
963
|
+
const findings = [
|
|
964
|
+
{ file: '/test.js', line: 1, severity: 'critical', rule: 'SQL_INJECTION', confidence: 'high' },
|
|
965
|
+
];
|
|
966
|
+
const result = await analyzer.analyze(findings);
|
|
967
|
+
assert.strictEqual(result.length, 1);
|
|
968
|
+
assert.strictEqual(result[0].deepAnalysis, undefined, 'No deep analysis without provider');
|
|
969
|
+
});
|
|
970
|
+
|
|
971
|
+
it('only selects critical/high findings for analysis', async () => {
|
|
972
|
+
// Mock provider that records what it receives
|
|
973
|
+
let receivedPrompt = '';
|
|
974
|
+
const mockProvider = {
|
|
975
|
+
name: 'MockLLM',
|
|
976
|
+
async complete(sys, prompt) {
|
|
977
|
+
receivedPrompt = prompt;
|
|
978
|
+
return '[]';
|
|
979
|
+
},
|
|
980
|
+
};
|
|
981
|
+
|
|
982
|
+
const analyzer = new DeepAnalyzer({ provider: mockProvider, budgetCents: 100 });
|
|
983
|
+
const findings = [
|
|
984
|
+
{ file: '/a.js', line: 1, severity: 'critical', rule: 'RULE_A', confidence: 'high', title: 'A', description: 'A' },
|
|
985
|
+
{ file: '/b.js', line: 2, severity: 'medium', rule: 'RULE_B', confidence: 'high', title: 'B', description: 'B' },
|
|
986
|
+
{ file: '/c.js', line: 3, severity: 'low', rule: 'RULE_C', confidence: 'high', title: 'C', description: 'C' },
|
|
987
|
+
{ file: '/d.js', line: 4, severity: 'high', rule: 'RULE_D', confidence: 'high', title: 'D', description: 'D' },
|
|
988
|
+
];
|
|
989
|
+
await analyzer.analyze(findings);
|
|
990
|
+
// Only critical and high should be in the prompt
|
|
991
|
+
assert.ok(receivedPrompt.includes('RULE_A'), 'Should include critical finding');
|
|
992
|
+
assert.ok(receivedPrompt.includes('RULE_D'), 'Should include high finding');
|
|
993
|
+
assert.ok(!receivedPrompt.includes('RULE_B'), 'Should exclude medium finding');
|
|
994
|
+
assert.ok(!receivedPrompt.includes('RULE_C'), 'Should exclude low finding');
|
|
995
|
+
});
|
|
996
|
+
|
|
997
|
+
it('attaches deep analysis from LLM response', async () => {
|
|
998
|
+
const mockProvider = {
|
|
999
|
+
name: 'MockLLM',
|
|
1000
|
+
async complete() {
|
|
1001
|
+
return JSON.stringify([{
|
|
1002
|
+
findingId: 'test.js:5:XSS_DANGEROUS',
|
|
1003
|
+
tainted: true,
|
|
1004
|
+
sanitized: false,
|
|
1005
|
+
exploitability: 'confirmed',
|
|
1006
|
+
reasoning: 'User input flows directly to innerHTML without sanitization.',
|
|
1007
|
+
}]);
|
|
1008
|
+
},
|
|
1009
|
+
};
|
|
1010
|
+
|
|
1011
|
+
const analyzer = new DeepAnalyzer({ provider: mockProvider, budgetCents: 100 });
|
|
1012
|
+
const findings = [{
|
|
1013
|
+
file: '/some/path/test.js', line: 5, severity: 'critical',
|
|
1014
|
+
rule: 'XSS_DANGEROUS', confidence: 'medium', title: 'XSS', description: 'XSS via innerHTML',
|
|
1015
|
+
}];
|
|
1016
|
+
|
|
1017
|
+
const result = await analyzer.analyze(findings);
|
|
1018
|
+
assert.ok(result[0].deepAnalysis, 'Should have deepAnalysis attached');
|
|
1019
|
+
assert.strictEqual(result[0].deepAnalysis.tainted, true);
|
|
1020
|
+
assert.strictEqual(result[0].deepAnalysis.sanitized, false);
|
|
1021
|
+
assert.strictEqual(result[0].deepAnalysis.exploitability, 'confirmed');
|
|
1022
|
+
assert.strictEqual(result[0].confidence, 'high', 'Confirmed finding should have high confidence');
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
it('downgrades confidence for false_positive analysis', async () => {
|
|
1026
|
+
const mockProvider = {
|
|
1027
|
+
name: 'MockLLM',
|
|
1028
|
+
async complete() {
|
|
1029
|
+
return JSON.stringify([{
|
|
1030
|
+
findingId: 'app.js:10:EVAL_CALL',
|
|
1031
|
+
tainted: false,
|
|
1032
|
+
sanitized: false,
|
|
1033
|
+
exploitability: 'false_positive',
|
|
1034
|
+
reasoning: 'Static string passed to eval, no user input path.',
|
|
1035
|
+
}]);
|
|
1036
|
+
},
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
const analyzer = new DeepAnalyzer({ provider: mockProvider, budgetCents: 100 });
|
|
1040
|
+
const findings = [{
|
|
1041
|
+
file: '/code/app.js', line: 10, severity: 'high',
|
|
1042
|
+
rule: 'EVAL_CALL', confidence: 'high', title: 'Eval', description: 'eval() usage',
|
|
1043
|
+
}];
|
|
1044
|
+
|
|
1045
|
+
const result = await analyzer.analyze(findings);
|
|
1046
|
+
assert.strictEqual(result[0].confidence, 'low', 'False positive should downgrade to low confidence');
|
|
1047
|
+
assert.strictEqual(result[0].deepAnalysis.exploitability, 'false_positive');
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
it('respects budget limit', async () => {
|
|
1051
|
+
let callCount = 0;
|
|
1052
|
+
const mockProvider = {
|
|
1053
|
+
name: 'MockLLM',
|
|
1054
|
+
async complete() {
|
|
1055
|
+
callCount++;
|
|
1056
|
+
// Return a very long response to burn budget
|
|
1057
|
+
return '[]';
|
|
1058
|
+
},
|
|
1059
|
+
};
|
|
1060
|
+
|
|
1061
|
+
const analyzer = new DeepAnalyzer({ provider: mockProvider, budgetCents: 0 });
|
|
1062
|
+
const findings = Array.from({ length: 10 }, (_, i) => ({
|
|
1063
|
+
file: `/f${i}.js`, line: 1, severity: 'critical', rule: `RULE_${i}`,
|
|
1064
|
+
confidence: 'high', title: `Rule ${i}`, description: `Desc ${i}`,
|
|
1065
|
+
}));
|
|
1066
|
+
|
|
1067
|
+
await analyzer.analyze(findings);
|
|
1068
|
+
// With 0 budget, should not make any calls (budget check happens before first batch)
|
|
1069
|
+
assert.strictEqual(callCount, 0, 'Should not call LLM when budget is 0');
|
|
1070
|
+
});
|
|
1071
|
+
|
|
1072
|
+
it('handles LLM errors gracefully', async () => {
|
|
1073
|
+
const mockProvider = {
|
|
1074
|
+
name: 'MockLLM',
|
|
1075
|
+
async complete() {
|
|
1076
|
+
throw new Error('API rate limit exceeded');
|
|
1077
|
+
},
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
const analyzer = new DeepAnalyzer({ provider: mockProvider, budgetCents: 100 });
|
|
1081
|
+
const findings = [{
|
|
1082
|
+
file: '/err.js', line: 1, severity: 'critical', rule: 'SOME_RULE',
|
|
1083
|
+
confidence: 'high', title: 'Rule', description: 'Desc',
|
|
1084
|
+
}];
|
|
1085
|
+
|
|
1086
|
+
// Should not throw
|
|
1087
|
+
const result = await analyzer.analyze(findings);
|
|
1088
|
+
assert.strictEqual(result.length, 1);
|
|
1089
|
+
assert.strictEqual(result[0].deepAnalysis, undefined, 'Failed analysis should not attach result');
|
|
1090
|
+
});
|
|
1091
|
+
|
|
1092
|
+
it('parses malformed LLM response without crashing', async () => {
|
|
1093
|
+
const mockProvider = {
|
|
1094
|
+
name: 'MockLLM',
|
|
1095
|
+
async complete() {
|
|
1096
|
+
return 'This is not valid JSON at all';
|
|
1097
|
+
},
|
|
1098
|
+
};
|
|
1099
|
+
|
|
1100
|
+
const analyzer = new DeepAnalyzer({ provider: mockProvider, budgetCents: 100 });
|
|
1101
|
+
const findings = [{
|
|
1102
|
+
file: '/bad.js', line: 1, severity: 'high', rule: 'BAD_RULE',
|
|
1103
|
+
confidence: 'high', title: 'Bad', description: 'Bad response test',
|
|
1104
|
+
}];
|
|
1105
|
+
|
|
1106
|
+
const result = await analyzer.analyze(findings);
|
|
1107
|
+
assert.strictEqual(result.length, 1);
|
|
1108
|
+
assert.strictEqual(result[0].deepAnalysis, undefined, 'Malformed response should not attach result');
|
|
1109
|
+
});
|
|
1110
|
+
|
|
1111
|
+
it('static create() returns null when no provider available', () => {
|
|
1112
|
+
// Remove all API key env vars temporarily
|
|
1113
|
+
const saved = {};
|
|
1114
|
+
for (const key of ['ANTHROPIC_API_KEY', 'OPENAI_API_KEY', 'GOOGLE_API_KEY', 'GEMINI_API_KEY']) {
|
|
1115
|
+
saved[key] = process.env[key];
|
|
1116
|
+
delete process.env[key];
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
try {
|
|
1120
|
+
const analyzer = DeepAnalyzer.create('/nonexistent/path');
|
|
1121
|
+
assert.strictEqual(analyzer, null, 'Should return null when no provider is available');
|
|
1122
|
+
} finally {
|
|
1123
|
+
// Restore
|
|
1124
|
+
for (const [key, val] of Object.entries(saved)) {
|
|
1125
|
+
if (val !== undefined) process.env[key] = val;
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
});
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
// =============================================================================
|
|
1132
|
+
// CROSS-AGENT AWARENESS & FRAMEWORK-AWARE shouldRun
|
|
1133
|
+
// =============================================================================
|
|
1134
|
+
|
|
1135
|
+
describe('Framework-aware shouldRun', async () => {
|
|
1136
|
+
const { MobileScanner } = await import('../agents/mobile-scanner.js');
|
|
1137
|
+
const { SupabaseRLSAgent } = await import('../agents/supabase-rls-agent.js');
|
|
1138
|
+
const { InjectionTester } = await import('../agents/injection-tester.js');
|
|
1139
|
+
|
|
1140
|
+
it('MobileScanner skips when no mobile framework detected', () => {
|
|
1141
|
+
const agent = new MobileScanner();
|
|
1142
|
+
const recon = { frameworks: ['express', 'nextjs'], databases: ['postgres'] };
|
|
1143
|
+
assert.strictEqual(agent.shouldRun(recon), false, 'Should skip for non-mobile projects');
|
|
1144
|
+
});
|
|
1145
|
+
|
|
1146
|
+
it('MobileScanner runs when react-native detected', () => {
|
|
1147
|
+
const agent = new MobileScanner();
|
|
1148
|
+
const recon = { frameworks: ['react-native'] };
|
|
1149
|
+
assert.strictEqual(agent.shouldRun(recon), true, 'Should run for React Native projects');
|
|
1150
|
+
});
|
|
1151
|
+
|
|
1152
|
+
it('SupabaseRLSAgent skips when no Supabase detected', () => {
|
|
1153
|
+
const agent = new SupabaseRLSAgent();
|
|
1154
|
+
const recon = { frameworks: ['express'], databases: ['postgres'], authPatterns: ['jwt'] };
|
|
1155
|
+
assert.strictEqual(agent.shouldRun(recon), false, 'Should skip for non-Supabase projects');
|
|
1156
|
+
});
|
|
1157
|
+
|
|
1158
|
+
it('SupabaseRLSAgent runs when Supabase detected', () => {
|
|
1159
|
+
const agent = new SupabaseRLSAgent();
|
|
1160
|
+
const recon = { databases: ['supabase'], authPatterns: ['supabase-auth'] };
|
|
1161
|
+
assert.strictEqual(agent.shouldRun(recon), true, 'Should run for Supabase projects');
|
|
1162
|
+
});
|
|
1163
|
+
|
|
1164
|
+
it('InjectionTester always runs (default shouldRun)', () => {
|
|
1165
|
+
const agent = new InjectionTester();
|
|
1166
|
+
const recon = { frameworks: ['express'] };
|
|
1167
|
+
assert.strictEqual(agent.shouldRun(recon), true, 'Universal agents always run');
|
|
1168
|
+
});
|
|
1169
|
+
});
|
|
1170
|
+
|
|
1171
|
+
// =============================================================================
|
|
1172
|
+
// SECRETS VERIFIER
|
|
1173
|
+
// =============================================================================
|
|
1174
|
+
|
|
1175
|
+
describe('SecretsVerifier', async () => {
|
|
1176
|
+
const { SecretsVerifier } = await import('../utils/secrets-verifier.js');
|
|
1177
|
+
|
|
1178
|
+
it('skips non-secret findings', async () => {
|
|
1179
|
+
const verifier = new SecretsVerifier();
|
|
1180
|
+
const findings = [
|
|
1181
|
+
{ file: '/a.js', line: 1, severity: 'high', category: 'injection', rule: 'SQL_INJECTION', matched: 'SELECT *' },
|
|
1182
|
+
];
|
|
1183
|
+
const results = await verifier.verify(findings);
|
|
1184
|
+
assert.strictEqual(results.length, 0, 'Should skip non-secret findings');
|
|
1185
|
+
});
|
|
1186
|
+
|
|
1187
|
+
it('extracts secret value from quoted match', () => {
|
|
1188
|
+
const verifier = new SecretsVerifier();
|
|
1189
|
+
const secret = verifier._extractSecret('API_KEY="sk_live_abc123def456"');
|
|
1190
|
+
assert.strictEqual(secret, 'sk_live_abc123def456');
|
|
1191
|
+
});
|
|
1192
|
+
|
|
1193
|
+
it('extracts secret value from assignment', () => {
|
|
1194
|
+
const verifier = new SecretsVerifier();
|
|
1195
|
+
const secret = verifier._extractSecret('token=ghp_abcdefghijklmnop');
|
|
1196
|
+
assert.strictEqual(secret, 'ghp_abcdefghijklmnop');
|
|
1197
|
+
});
|
|
1198
|
+
|
|
1199
|
+
it('returns null for short/empty matches', () => {
|
|
1200
|
+
const verifier = new SecretsVerifier();
|
|
1201
|
+
assert.strictEqual(verifier._extractSecret(''), null);
|
|
1202
|
+
assert.strictEqual(verifier._extractSecret(null), null);
|
|
1203
|
+
});
|
|
1204
|
+
|
|
1205
|
+
it('finds probe for known rule names', () => {
|
|
1206
|
+
const verifier = new SecretsVerifier();
|
|
1207
|
+
assert.ok(verifier._findProbe('GITHUB_TOKEN'), 'Should find GitHub probe');
|
|
1208
|
+
assert.ok(verifier._findProbe('OPENAI_API_KEY'), 'Should find OpenAI probe');
|
|
1209
|
+
assert.ok(verifier._findProbe('STRIPE_LIVE_KEY'), 'Should find Stripe probe');
|
|
1210
|
+
assert.strictEqual(verifier._findProbe('UNKNOWN_PATTERN_XYZ'), null, 'Should return null for unknown');
|
|
1211
|
+
});
|
|
1212
|
+
});
|
|
1213
|
+
|
|
1214
|
+
// =============================================================================
|
|
1215
|
+
// SBOM GENERATOR (CRA Enhancement)
|
|
1216
|
+
// =============================================================================
|
|
1217
|
+
|
|
1218
|
+
describe('SBOMGenerator CRA', async () => {
|
|
1219
|
+
const { SBOMGenerator } = await import('../agents/sbom-generator.js');
|
|
1220
|
+
|
|
1221
|
+
it('generates SBOM with CRA-required fields', () => {
|
|
1222
|
+
const sbom = new SBOMGenerator();
|
|
1223
|
+
const bom = sbom.generate(process.cwd());
|
|
1224
|
+
|
|
1225
|
+
// CRA fields
|
|
1226
|
+
assert.ok(bom.metadata.supplier, 'Should have supplier field');
|
|
1227
|
+
assert.ok(bom.metadata.lifecycles, 'Should have lifecycles field');
|
|
1228
|
+
assert.strictEqual(bom.metadata.lifecycles[0].phase, 'build');
|
|
1229
|
+
assert.ok(Array.isArray(bom.vulnerabilities), 'Should have vulnerabilities array');
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1232
|
+
it('attachVulnerabilities adds CVEs to SBOM', () => {
|
|
1233
|
+
const sbom = new SBOMGenerator();
|
|
1234
|
+
const bom = sbom.generate(process.cwd());
|
|
1235
|
+
|
|
1236
|
+
const vulns = [
|
|
1237
|
+
{ id: 'CVE-2024-1234', package: 'lodash@4.17.20', severity: 'high', description: 'Prototype pollution' },
|
|
1238
|
+
];
|
|
1239
|
+
sbom.attachVulnerabilities(bom, vulns);
|
|
1240
|
+
|
|
1241
|
+
assert.strictEqual(bom.vulnerabilities.length, 1);
|
|
1242
|
+
assert.strictEqual(bom.vulnerabilities[0].id, 'CVE-2024-1234');
|
|
1243
|
+
assert.strictEqual(bom.vulnerabilities[0].ratings[0].severity, 'high');
|
|
1244
|
+
});
|
|
1245
|
+
|
|
1246
|
+
it('detects licenses from node_modules', () => {
|
|
1247
|
+
const sbom = new SBOMGenerator();
|
|
1248
|
+
const licenses = sbom._detectLicenses(process.cwd());
|
|
1249
|
+
// Our project uses chalk, commander, etc. — should find some licenses
|
|
1250
|
+
if (Object.keys(licenses).length > 0) {
|
|
1251
|
+
const firstLicense = Object.values(licenses)[0];
|
|
1252
|
+
assert.ok(typeof firstLicense === 'string', 'License should be a string');
|
|
1253
|
+
}
|
|
1254
|
+
});
|
|
1255
|
+
});
|
|
1256
|
+
|
|
1257
|
+
// =============================================================================
|
|
1258
|
+
// ORCHESTRATOR — CROSS-AGENT SHARED FINDINGS
|
|
1259
|
+
// =============================================================================
|
|
1260
|
+
|
|
1261
|
+
describe('Orchestrator cross-agent awareness', async () => {
|
|
1262
|
+
const { Orchestrator } = await import('../agents/orchestrator.js');
|
|
1263
|
+
|
|
1264
|
+
it('passes sharedFindings in context to agents', async () => {
|
|
1265
|
+
const orchestrator = new Orchestrator();
|
|
1266
|
+
let receivedSharedFindings = null;
|
|
1267
|
+
|
|
1268
|
+
// Mock agent that captures context
|
|
1269
|
+
const mockAgent = {
|
|
1270
|
+
name: 'MockAgent',
|
|
1271
|
+
category: 'test',
|
|
1272
|
+
shouldRun: () => true,
|
|
1273
|
+
async analyze(context) {
|
|
1274
|
+
receivedSharedFindings = context.sharedFindings;
|
|
1275
|
+
return [];
|
|
1276
|
+
},
|
|
1277
|
+
};
|
|
1278
|
+
|
|
1279
|
+
orchestrator.register(mockAgent);
|
|
1280
|
+
await orchestrator.runAll(process.cwd(), { quiet: true, skipVerifier: true });
|
|
1281
|
+
|
|
1282
|
+
assert.ok(Array.isArray(receivedSharedFindings), 'sharedFindings should be an array in context');
|
|
1283
|
+
});
|
|
1284
|
+
|
|
1285
|
+
it('skips agents where shouldRun returns false', async () => {
|
|
1286
|
+
const orchestrator = new Orchestrator();
|
|
1287
|
+
let ran = false;
|
|
1288
|
+
|
|
1289
|
+
const skipAgent = {
|
|
1290
|
+
name: 'SkipMe',
|
|
1291
|
+
category: 'test',
|
|
1292
|
+
shouldRun: () => false,
|
|
1293
|
+
async analyze() { ran = true; return []; },
|
|
1294
|
+
};
|
|
1295
|
+
|
|
1296
|
+
orchestrator.register(skipAgent);
|
|
1297
|
+
await orchestrator.runAll(process.cwd(), { quiet: true, skipVerifier: true });
|
|
1298
|
+
|
|
1299
|
+
assert.strictEqual(ran, false, 'Agent with shouldRun=false should not execute');
|
|
1300
|
+
});
|
|
1301
|
+
});
|