octocode-cli 1.2.6 → 1.2.8
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/LICENSE +21 -63
- package/README.md +85 -142
- package/out/octocode-cli.js +7063 -6934
- package/package.json +8 -6
- package/skills/README.md +97 -120
- package/skills/octocode-code-engineer/.claude/settings.local.json +18 -0
- package/skills/octocode-code-engineer/.octocode/rfc/RFC-code-engineer-weakness-fixes.md +255 -0
- package/skills/octocode-code-engineer/.plan/VALIDATED_PLAN.md +223 -0
- package/skills/octocode-code-engineer/README.md +178 -0
- package/skills/octocode-code-engineer/SKILL.md +418 -0
- package/skills/octocode-code-engineer/coverage/architecture.ts.html +7828 -0
- package/skills/octocode-code-engineer/coverage/ast-helpers.ts.html +211 -0
- package/skills/octocode-code-engineer/coverage/ast-search.ts.html +1795 -0
- package/skills/octocode-code-engineer/coverage/base.css +224 -0
- package/skills/octocode-code-engineer/coverage/block-navigation.js +87 -0
- package/skills/octocode-code-engineer/coverage/cache.ts.html +376 -0
- package/skills/octocode-code-engineer/coverage/cli.ts.html +982 -0
- package/skills/octocode-code-engineer/coverage/clover.xml +3217 -0
- package/skills/octocode-code-engineer/coverage/collect-effects.ts.html +664 -0
- package/skills/octocode-code-engineer/coverage/collect-input-sources.ts.html +577 -0
- package/skills/octocode-code-engineer/coverage/collect-performance.ts.html +331 -0
- package/skills/octocode-code-engineer/coverage/collect-prototype-pollution.ts.html +421 -0
- package/skills/octocode-code-engineer/coverage/collect-security.ts.html +604 -0
- package/skills/octocode-code-engineer/coverage/collect-test-profile.ts.html +589 -0
- package/skills/octocode-code-engineer/coverage/coverage-final.json +30 -0
- package/skills/octocode-code-engineer/coverage/dependencies.ts.html +997 -0
- package/skills/octocode-code-engineer/coverage/dependency-summary.ts.html +688 -0
- package/skills/octocode-code-engineer/coverage/discovery.ts.html +322 -0
- package/skills/octocode-code-engineer/coverage/favicon.png +0 -0
- package/skills/octocode-code-engineer/coverage/graph-analytics.ts.html +1510 -0
- package/skills/octocode-code-engineer/coverage/index.html +536 -0
- package/skills/octocode-code-engineer/coverage/index.ts.html +826 -0
- package/skills/octocode-code-engineer/coverage/metrics.ts.html +553 -0
- package/skills/octocode-code-engineer/coverage/pipeline.ts.html +2044 -0
- package/skills/octocode-code-engineer/coverage/prettify.css +1 -0
- package/skills/octocode-code-engineer/coverage/prettify.js +2 -0
- package/skills/octocode-code-engineer/coverage/report-analysis.ts.html +1570 -0
- package/skills/octocode-code-engineer/coverage/report-writer.ts.html +1102 -0
- package/skills/octocode-code-engineer/coverage/security-detectors.ts.html +1747 -0
- package/skills/octocode-code-engineer/coverage/semantic-detectors.ts.html +2152 -0
- package/skills/octocode-code-engineer/coverage/semantic.ts.html +1897 -0
- package/skills/octocode-code-engineer/coverage/sort-arrow-sprite.png +0 -0
- package/skills/octocode-code-engineer/coverage/sorter.js +210 -0
- package/skills/octocode-code-engineer/coverage/summary-md.ts.html +1222 -0
- package/skills/octocode-code-engineer/coverage/test-quality-detectors.ts.html +1039 -0
- package/skills/octocode-code-engineer/coverage/tree-sitter-analyzer.ts.html +955 -0
- package/skills/octocode-code-engineer/coverage/ts-analyzer.ts.html +1213 -0
- package/skills/octocode-code-engineer/coverage/types.ts.html +2473 -0
- package/skills/octocode-code-engineer/coverage/utils.ts.html +820 -0
- package/skills/octocode-code-engineer/eslint.config.mjs +54 -0
- package/skills/octocode-code-engineer/minify-scripts.mjs +32 -0
- package/skills/octocode-code-engineer/package.json +54 -0
- package/skills/octocode-code-engineer/references/agent-ast-reading-rfc.md +95 -0
- package/skills/octocode-code-engineer/references/architecture-techniques.md +121 -0
- package/skills/octocode-code-engineer/references/ast-search.md +210 -0
- package/skills/octocode-code-engineer/references/ast-tree-search.md +151 -0
- package/skills/octocode-code-engineer/references/cli-reference.md +167 -0
- package/skills/octocode-code-engineer/references/concepts.md +107 -0
- package/skills/octocode-code-engineer/references/finding-categories.md +128 -0
- package/skills/octocode-code-engineer/references/improvement-roadmap.md +304 -0
- package/skills/octocode-code-engineer/references/output-files.md +144 -0
- package/skills/octocode-code-engineer/references/playbooks.md +204 -0
- package/skills/octocode-code-engineer/references/present-results.md +136 -0
- package/skills/octocode-code-engineer/references/tool-workflows.md +566 -0
- package/skills/octocode-code-engineer/references/validate-investigate.md +225 -0
- package/skills/octocode-code-engineer/scripts/analysis/dependencies.js +1 -0
- package/skills/octocode-code-engineer/scripts/analysis/dependency-summary.js +1 -0
- package/skills/octocode-code-engineer/scripts/analysis/discovery.js +1 -0
- package/skills/octocode-code-engineer/scripts/analysis/graph-analytics.js +1 -0
- package/skills/octocode-code-engineer/scripts/analysis/semantic.js +1 -0
- package/skills/octocode-code-engineer/scripts/ast/helpers.js +1 -0
- package/skills/octocode-code-engineer/scripts/ast/metrics.js +1 -0
- package/skills/octocode-code-engineer/scripts/ast/search.js +2 -0
- package/skills/octocode-code-engineer/scripts/ast/tree-search.js +2 -0
- package/skills/octocode-code-engineer/scripts/ast/tree-sitter.js +1 -0
- package/skills/octocode-code-engineer/scripts/ast/ts-analyzer.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/chains.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/effects.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/input-sources.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/performance.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/prototype-pollution.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/security.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/test-profile.js +1 -0
- package/skills/octocode-code-engineer/scripts/common/is-direct-run.js +1 -0
- package/skills/octocode-code-engineer/scripts/common/utils.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/code-quality.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/cohesion.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/coupling.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/cycle.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/dead-code.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/import-style.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/index.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/security.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/semantic.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/shared.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/test-quality.js +1 -0
- package/skills/octocode-code-engineer/scripts/index.js +1 -0
- package/skills/octocode-code-engineer/scripts/pipeline/cache.js +1 -0
- package/skills/octocode-code-engineer/scripts/pipeline/cli.js +1 -0
- package/skills/octocode-code-engineer/scripts/pipeline/main.js +2 -0
- package/skills/octocode-code-engineer/scripts/reporting/analysis.js +1 -0
- package/skills/octocode-code-engineer/scripts/reporting/summary-md.js +1 -0
- package/skills/octocode-code-engineer/scripts/reporting/writer.js +1 -0
- package/skills/octocode-code-engineer/scripts/types/constants.js +1 -0
- package/skills/octocode-code-engineer/scripts/types/index.js +1 -0
- package/skills/octocode-code-engineer/scripts/types/interfaces.js +1 -0
- package/skills/octocode-code-engineer/src/analysis/dependencies.test.ts +545 -0
- package/skills/octocode-code-engineer/src/analysis/dependencies.ts +406 -0
- package/skills/octocode-code-engineer/src/analysis/dependency-summary.test.ts +566 -0
- package/skills/octocode-code-engineer/src/analysis/dependency-summary.ts +257 -0
- package/skills/octocode-code-engineer/src/analysis/discovery.test.ts +420 -0
- package/skills/octocode-code-engineer/src/analysis/discovery.ts +87 -0
- package/skills/octocode-code-engineer/src/analysis/graph-analytics.test.ts +449 -0
- package/skills/octocode-code-engineer/src/analysis/graph-analytics.ts +534 -0
- package/skills/octocode-code-engineer/src/analysis/semantic.test.ts +1533 -0
- package/skills/octocode-code-engineer/src/analysis/semantic.ts +830 -0
- package/skills/octocode-code-engineer/src/ast/helpers.test.ts +185 -0
- package/skills/octocode-code-engineer/src/ast/helpers.ts +62 -0
- package/skills/octocode-code-engineer/src/ast/metrics.test.ts +304 -0
- package/skills/octocode-code-engineer/src/ast/metrics.ts +204 -0
- package/skills/octocode-code-engineer/src/ast/search.test.ts +647 -0
- package/skills/octocode-code-engineer/src/ast/search.ts +648 -0
- package/skills/octocode-code-engineer/src/ast/tree-search.test.ts +199 -0
- package/skills/octocode-code-engineer/src/ast/tree-search.ts +392 -0
- package/skills/octocode-code-engineer/src/ast/tree-sitter.test.ts +407 -0
- package/skills/octocode-code-engineer/src/ast/tree-sitter.ts +402 -0
- package/skills/octocode-code-engineer/src/ast/ts-analyzer.test.ts +1864 -0
- package/skills/octocode-code-engineer/src/ast/ts-analyzer.ts +509 -0
- package/skills/octocode-code-engineer/src/collectors/chains.ts +74 -0
- package/skills/octocode-code-engineer/src/collectors/effects.test.ts +490 -0
- package/skills/octocode-code-engineer/src/collectors/effects.ts +332 -0
- package/skills/octocode-code-engineer/src/collectors/input-sources.test.ts +144 -0
- package/skills/octocode-code-engineer/src/collectors/input-sources.ts +196 -0
- package/skills/octocode-code-engineer/src/collectors/performance.test.ts +82 -0
- package/skills/octocode-code-engineer/src/collectors/performance.ts +141 -0
- package/skills/octocode-code-engineer/src/collectors/prototype-pollution.test.ts +55 -0
- package/skills/octocode-code-engineer/src/collectors/prototype-pollution.ts +162 -0
- package/skills/octocode-code-engineer/src/collectors/security.test.ts +124 -0
- package/skills/octocode-code-engineer/src/collectors/security.ts +309 -0
- package/skills/octocode-code-engineer/src/collectors/test-profile.test.ts +97 -0
- package/skills/octocode-code-engineer/src/collectors/test-profile.ts +269 -0
- package/skills/octocode-code-engineer/src/common/is-direct-run.test.ts +32 -0
- package/skills/octocode-code-engineer/src/common/is-direct-run.ts +13 -0
- package/skills/octocode-code-engineer/src/common/utils.test.ts +463 -0
- package/skills/octocode-code-engineer/src/common/utils.ts +304 -0
- package/skills/octocode-code-engineer/src/detectors/code-quality.ts +966 -0
- package/skills/octocode-code-engineer/src/detectors/cohesion.ts +539 -0
- package/skills/octocode-code-engineer/src/detectors/coupling.ts +323 -0
- package/skills/octocode-code-engineer/src/detectors/cycle.ts +349 -0
- package/skills/octocode-code-engineer/src/detectors/dead-code.ts +320 -0
- package/skills/octocode-code-engineer/src/detectors/import-style.ts +376 -0
- package/skills/octocode-code-engineer/src/detectors/index.test.ts +3061 -0
- package/skills/octocode-code-engineer/src/detectors/index.ts +88 -0
- package/skills/octocode-code-engineer/src/detectors/security.test.ts +882 -0
- package/skills/octocode-code-engineer/src/detectors/security.ts +821 -0
- package/skills/octocode-code-engineer/src/detectors/semantic.ts +758 -0
- package/skills/octocode-code-engineer/src/detectors/shared.ts +49 -0
- package/skills/octocode-code-engineer/src/detectors/test-quality.test.ts +388 -0
- package/skills/octocode-code-engineer/src/detectors/test-quality.ts +367 -0
- package/skills/octocode-code-engineer/src/index.test.ts +4425 -0
- package/skills/octocode-code-engineer/src/index.ts +403 -0
- package/skills/octocode-code-engineer/src/pipeline/cache.test.ts +199 -0
- package/skills/octocode-code-engineer/src/pipeline/cache.ts +130 -0
- package/skills/octocode-code-engineer/src/pipeline/cli.test.ts +493 -0
- package/skills/octocode-code-engineer/src/pipeline/cli.ts +344 -0
- package/skills/octocode-code-engineer/src/pipeline/main.test.ts +174 -0
- package/skills/octocode-code-engineer/src/pipeline/main.ts +1074 -0
- package/skills/octocode-code-engineer/src/pipeline.test.ts +84 -0
- package/skills/octocode-code-engineer/src/reporting/analysis.test.ts +782 -0
- package/skills/octocode-code-engineer/src/reporting/analysis.ts +688 -0
- package/skills/octocode-code-engineer/src/reporting/output-contract.test.ts +463 -0
- package/skills/octocode-code-engineer/src/reporting/summary-md.test.ts +421 -0
- package/skills/octocode-code-engineer/src/reporting/summary-md.ts +714 -0
- package/skills/octocode-code-engineer/src/reporting/writer.ts +430 -0
- package/skills/octocode-code-engineer/src/sanity.test.ts +47 -0
- package/skills/octocode-code-engineer/src/types/constants.ts +248 -0
- package/skills/octocode-code-engineer/src/types/index.ts +80 -0
- package/skills/octocode-code-engineer/src/types/interfaces.ts +682 -0
- package/skills/octocode-code-engineer/tsconfig.json +17 -0
- package/skills/octocode-code-engineer/vitest.config.ts +8 -0
- package/skills/octocode-documentation-writer/README.md +113 -0
- package/skills/octocode-documentation-writer/SKILL.md +886 -0
- package/skills/octocode-documentation-writer/references/agent-discovery-analysis.md +453 -0
- package/skills/octocode-documentation-writer/references/agent-documentation-writer.md +255 -0
- package/skills/octocode-documentation-writer/references/agent-engineer-questions.md +247 -0
- package/skills/octocode-documentation-writer/references/agent-orchestrator.md +370 -0
- package/skills/octocode-documentation-writer/references/agent-qa-validator.md +227 -0
- package/skills/octocode-documentation-writer/references/agent-researcher.md +250 -0
- package/skills/octocode-documentation-writer/schemas/analysis-schema.json +886 -0
- package/skills/octocode-documentation-writer/schemas/discovery-tasks.json +96 -0
- package/skills/octocode-documentation-writer/schemas/documentation-structure.json +373 -0
- package/skills/octocode-documentation-writer/schemas/partial-discovery-schema.json +102 -0
- package/skills/octocode-documentation-writer/schemas/partial-research-schema.json +98 -0
- package/skills/octocode-documentation-writer/schemas/qa-results-schema.json +113 -0
- package/skills/octocode-documentation-writer/schemas/questions-schema.json +228 -0
- package/skills/octocode-documentation-writer/schemas/research-schema.json +104 -0
- package/skills/octocode-documentation-writer/schemas/state-schema.json +222 -0
- package/skills/octocode-documentation-writer/schemas/work-assignments-schema.json +74 -0
- package/skills/octocode-plan/SKILL.md +122 -116
- package/skills/octocode-prompt-optimizer/SKILL.md +617 -0
- package/skills/octocode-pull-request-reviewer/README.md +249 -0
- package/skills/octocode-pull-request-reviewer/SKILL.md +479 -0
- package/skills/octocode-pull-request-reviewer/references/dependency-check.md +74 -0
- package/skills/octocode-pull-request-reviewer/references/domain-reviewers.md +24 -0
- package/skills/octocode-pull-request-reviewer/references/execution-lifecycle.md +441 -0
- package/skills/octocode-pull-request-reviewer/references/flow-analysis-protocol.md +64 -0
- package/skills/octocode-pull-request-reviewer/references/output-template.md +174 -0
- package/skills/octocode-pull-request-reviewer/references/parallel-agent-protocol.md +182 -0
- package/skills/octocode-pull-request-reviewer/references/review-guidelines.md +26 -0
- package/skills/octocode-pull-request-reviewer/references/verification-checklist.md +40 -0
- package/skills/octocode-research/.claude/settings.local.json +46 -0
- package/skills/octocode-research/.octocode/plan/code-review-fixes/plan.md +312 -0
- package/skills/octocode-research/.octocode/plan/code-review-fixes/research.md +212 -0
- package/skills/octocode-research/.octocode/plans/NODE_SERVER_START_PLAN.md +755 -0
- package/skills/octocode-research/.octocode/research/code-review/research.md +371 -0
- package/skills/octocode-research/.octocode/review/IMPROVEMENTS.md +391 -0
- package/skills/octocode-research/.octocode/review/REVIEW_PLAN.md +289 -0
- package/skills/octocode-research/.octocode/review/REVIEW_REPORT.md +356 -0
- package/skills/octocode-research/AGENTS.md +349 -0
- package/skills/octocode-research/README.md +494 -0
- package/skills/octocode-research/SKILL.md +652 -274
- package/skills/octocode-research/docs/API_REFERENCE.md +562 -0
- package/skills/octocode-research/docs/ARCHITECTURE.md +554 -0
- package/skills/octocode-research/docs/FLOWS.md +577 -0
- package/skills/octocode-research/docs/OVERVIEW.md +564 -0
- package/skills/octocode-research/docs/SERVER_FLOWS.md +631 -0
- package/skills/octocode-research/ecosystem.config.cjs +88 -0
- package/skills/octocode-research/eslint.config.mjs +27 -0
- package/skills/octocode-research/package.json +84 -0
- package/skills/octocode-research/references/GUARDRAILS.md +40 -0
- package/skills/octocode-research/references/PARALLEL_AGENT_PROTOCOL.md +178 -0
- package/skills/octocode-research/references/roast-prompt.md +149 -0
- package/skills/octocode-research/scripts/server-init.d.ts +2 -0
- package/skills/octocode-research/scripts/server-init.js +2 -0
- package/skills/octocode-research/scripts/server.d.ts +8 -0
- package/skills/octocode-research/scripts/server.js +445 -0
- package/skills/octocode-research/src/__tests__/integration/circuitBreaker.test.ts +205 -0
- package/skills/octocode-research/src/__tests__/integration/routes.test.ts +374 -0
- package/skills/octocode-research/src/__tests__/unit/circuitBreaker.test.ts +245 -0
- package/skills/octocode-research/src/__tests__/unit/errorHandler.test.ts +183 -0
- package/skills/octocode-research/src/__tests__/unit/httpPreprocess.test.ts +157 -0
- package/skills/octocode-research/src/__tests__/unit/logger.test.ts +143 -0
- package/skills/octocode-research/src/__tests__/unit/queryParser.test.ts +130 -0
- package/skills/octocode-research/src/__tests__/unit/responseBuilder.test.ts +469 -0
- package/skills/octocode-research/src/__tests__/unit/retry.test.ts +205 -0
- package/skills/octocode-research/src/index.ts +186 -0
- package/skills/octocode-research/src/mcpCache.ts +49 -0
- package/skills/octocode-research/src/middleware/errorHandler.ts +65 -0
- package/skills/octocode-research/src/middleware/logger.ts +61 -0
- package/skills/octocode-research/src/middleware/queryParser.ts +115 -0
- package/skills/octocode-research/src/middleware/readiness.ts +17 -0
- package/skills/octocode-research/src/routes/github.ts +197 -0
- package/skills/octocode-research/src/routes/local.ts +175 -0
- package/skills/octocode-research/src/routes/lsp.ts +177 -0
- package/skills/octocode-research/src/routes/package.ts +127 -0
- package/skills/octocode-research/src/routes/prompts.ts +138 -0
- package/skills/octocode-research/src/routes/tools.ts +677 -0
- package/skills/octocode-research/src/server-init.ts +363 -0
- package/skills/octocode-research/src/server.ts +285 -0
- package/skills/octocode-research/src/types/errorGuards.ts +151 -0
- package/skills/octocode-research/src/types/express.d.ts +76 -0
- package/skills/octocode-research/src/types/guards.ts +98 -0
- package/skills/octocode-research/src/types/mcp.ts +119 -0
- package/skills/octocode-research/src/types/responses.ts +199 -0
- package/skills/octocode-research/src/types/toolTypes.ts +33 -0
- package/skills/octocode-research/src/utils/asyncTimeout.ts +116 -0
- package/skills/octocode-research/src/utils/circuitBreaker.ts +492 -0
- package/skills/octocode-research/src/utils/colors.ts +53 -0
- package/skills/octocode-research/src/utils/errorQueue.ts +71 -0
- package/skills/octocode-research/src/utils/logEmoji.ts +103 -0
- package/skills/octocode-research/src/utils/logger.ts +413 -0
- package/skills/octocode-research/src/utils/resilience.ts +169 -0
- package/skills/octocode-research/src/utils/responseBuilder.ts +495 -0
- package/skills/octocode-research/src/utils/responseFactory.ts +100 -0
- package/skills/octocode-research/src/utils/responseParser.ts +272 -0
- package/skills/octocode-research/src/utils/retry.ts +280 -0
- package/skills/octocode-research/src/utils/routeFactory.ts +117 -0
- package/skills/octocode-research/src/utils/url.ts +20 -0
- package/skills/octocode-research/src/validation/httpPreprocess.ts +155 -0
- package/skills/octocode-research/src/validation/index.ts +2 -0
- package/skills/octocode-research/src/validation/schemas.ts +578 -0
- package/skills/octocode-research/src/validation/toolCallSchema.ts +132 -0
- package/skills/octocode-research/tsconfig.json +21 -0
- package/skills/octocode-research/tsdown.config.ts +42 -0
- package/skills/octocode-research/vitest.config.ts +20 -0
- package/skills/octocode-researcher/SKILL.md +461 -0
- package/skills/octocode-researcher/references/fallbacks.md +120 -0
- package/skills/{octocode-local-search → octocode-researcher}/references/tool-reference.md +132 -49
- package/skills/{octocode-local-search → octocode-researcher}/references/workflow-patterns.md +204 -4
- package/skills/octocode-rfc-generator/SKILL.md +223 -0
- package/skills/octocode-rfc-generator/references/rfc-template.md +193 -0
- package/skills/octocode-roast/SKILL.md +63 -21
- package/skills/octocode-implement/SKILL.md +0 -293
- package/skills/octocode-implement/references/execution-phases.md +0 -317
- package/skills/octocode-implement/references/tool-reference.md +0 -403
- package/skills/octocode-implement/references/workflow-patterns.md +0 -385
- package/skills/octocode-local-search/SKILL.md +0 -449
- package/skills/octocode-pr-review/SKILL.md +0 -391
- package/skills/octocode-pr-review/references/domain-reviewers.md +0 -105
- package/skills/octocode-pr-review/references/execution-lifecycle.md +0 -116
- package/skills/octocode-pr-review/references/research-flows.md +0 -75
- package/skills/octocode-research/references/tool-reference.md +0 -304
- package/skills/octocode-research/references/workflow-patterns.md +0 -325
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { collectPerformanceData } from './performance.js';
|
|
5
|
+
|
|
6
|
+
import type { FileEntry } from '../types/index.js';
|
|
7
|
+
|
|
8
|
+
function parse(code: string, fileName = '/repo/src/test.ts'): ts.SourceFile {
|
|
9
|
+
return ts.createSourceFile(fileName, code, ts.ScriptTarget.ESNext, true);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function emptyFileEntry(): FileEntry {
|
|
13
|
+
return {
|
|
14
|
+
package: 'test',
|
|
15
|
+
file: 'test.ts',
|
|
16
|
+
parseEngine: 'typescript',
|
|
17
|
+
nodeCount: 0,
|
|
18
|
+
kindCounts: {},
|
|
19
|
+
functions: [],
|
|
20
|
+
flows: [],
|
|
21
|
+
dependencyProfile: {
|
|
22
|
+
internalDependencies: [],
|
|
23
|
+
externalDependencies: [],
|
|
24
|
+
unresolvedDependencies: [],
|
|
25
|
+
declaredExports: [],
|
|
26
|
+
importedSymbols: [],
|
|
27
|
+
reExports: [],
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('collectPerformanceData', () => {
|
|
33
|
+
it('collects await-in-loop locations', () => {
|
|
34
|
+
const sourceFile = parse(`
|
|
35
|
+
async function run(items: string[]) {
|
|
36
|
+
for (const item of items) {
|
|
37
|
+
await fetch(item);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
`);
|
|
41
|
+
const fileEntry = emptyFileEntry();
|
|
42
|
+
collectPerformanceData(sourceFile, 'test.ts', fileEntry);
|
|
43
|
+
expect(fileEntry.awaitInLoopLocations?.length).toBeGreaterThan(0);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('collects sync io calls', () => {
|
|
47
|
+
const sourceFile = parse(
|
|
48
|
+
`function read() { fs.readFileSync('/tmp/a.txt'); }`
|
|
49
|
+
);
|
|
50
|
+
const fileEntry = emptyFileEntry();
|
|
51
|
+
collectPerformanceData(sourceFile, 'test.ts', fileEntry);
|
|
52
|
+
expect(fileEntry.syncIoCalls?.some(c => c.name === 'readFileSync')).toBe(
|
|
53
|
+
true
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('collects timer calls and marks cleanup when clearTimeout exists', () => {
|
|
58
|
+
const sourceFile = parse(`
|
|
59
|
+
function run() {
|
|
60
|
+
setTimeout(() => {}, 5);
|
|
61
|
+
clearTimeout(1 as unknown as NodeJS.Timeout);
|
|
62
|
+
}
|
|
63
|
+
`);
|
|
64
|
+
const fileEntry = emptyFileEntry();
|
|
65
|
+
collectPerformanceData(sourceFile, 'test.ts', fileEntry);
|
|
66
|
+
expect(fileEntry.timerCalls?.length).toBe(1);
|
|
67
|
+
expect(fileEntry.timerCalls?.[0].kind).toBe('setTimeout');
|
|
68
|
+
expect(fileEntry.timerCalls?.[0].hasCleanup).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('collects listener registrations and removals', () => {
|
|
72
|
+
const sourceFile = parse(`
|
|
73
|
+
const emitter = new EventEmitter();
|
|
74
|
+
emitter.on('data', () => {});
|
|
75
|
+
emitter.off('data', () => {});
|
|
76
|
+
`);
|
|
77
|
+
const fileEntry = emptyFileEntry();
|
|
78
|
+
collectPerformanceData(sourceFile, 'test.ts', fileEntry);
|
|
79
|
+
expect(fileEntry.listenerRegistrations?.length).toBeGreaterThan(0);
|
|
80
|
+
expect(fileEntry.listenerRemovals?.length).toBeGreaterThan(0);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
|
|
3
|
+
import { blockContainsCall, findParentBlock } from './effects.js';
|
|
4
|
+
import { isFunctionLike } from '../ast/helpers.js';
|
|
5
|
+
import { getLineAndCharacter } from '../common/utils.js';
|
|
6
|
+
|
|
7
|
+
import type { CodeLocation, FileEntry, TimerCall } from '../types/index.js';
|
|
8
|
+
|
|
9
|
+
const SYNC_IO_METHODS = new Set([
|
|
10
|
+
'readFileSync',
|
|
11
|
+
'writeFileSync',
|
|
12
|
+
'existsSync',
|
|
13
|
+
'mkdirSync',
|
|
14
|
+
'readdirSync',
|
|
15
|
+
'statSync',
|
|
16
|
+
'lstatSync',
|
|
17
|
+
'unlinkSync',
|
|
18
|
+
'rmdirSync',
|
|
19
|
+
'renameSync',
|
|
20
|
+
'copyFileSync',
|
|
21
|
+
'accessSync',
|
|
22
|
+
'appendFileSync',
|
|
23
|
+
'chmodSync',
|
|
24
|
+
'chownSync',
|
|
25
|
+
'openSync',
|
|
26
|
+
'closeSync',
|
|
27
|
+
'execSync',
|
|
28
|
+
'execFileSync',
|
|
29
|
+
'spawnSync',
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
export function collectPerformanceData(
|
|
33
|
+
sourceFile: ts.SourceFile,
|
|
34
|
+
fileRelative: string,
|
|
35
|
+
fileEntry: FileEntry
|
|
36
|
+
): void {
|
|
37
|
+
const awaitInLoopLocations: CodeLocation[] = [];
|
|
38
|
+
const syncIoCalls: Array<{
|
|
39
|
+
name: string;
|
|
40
|
+
lineStart: number;
|
|
41
|
+
lineEnd: number;
|
|
42
|
+
}> = [];
|
|
43
|
+
const timerCalls: TimerCall[] = [];
|
|
44
|
+
const listenerRegistrations: CodeLocation[] = [];
|
|
45
|
+
const listenerRemovals: CodeLocation[] = [];
|
|
46
|
+
|
|
47
|
+
const isInsideLoop = (node: ts.Node): boolean => {
|
|
48
|
+
let current = node.parent;
|
|
49
|
+
while (current) {
|
|
50
|
+
if (
|
|
51
|
+
ts.isForStatement(current) ||
|
|
52
|
+
ts.isWhileStatement(current) ||
|
|
53
|
+
ts.isDoStatement(current) ||
|
|
54
|
+
ts.isForOfStatement(current) ||
|
|
55
|
+
ts.isForInStatement(current)
|
|
56
|
+
)
|
|
57
|
+
return true;
|
|
58
|
+
if (isFunctionLike(current)) return false;
|
|
59
|
+
current = current.parent;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const visit = (node: ts.Node): void => {
|
|
65
|
+
if (ts.isAwaitExpression(node) && isInsideLoop(node)) {
|
|
66
|
+
const loc = getLineAndCharacter(sourceFile, node);
|
|
67
|
+
awaitInLoopLocations.push({
|
|
68
|
+
file: fileRelative,
|
|
69
|
+
lineStart: loc.lineStart,
|
|
70
|
+
lineEnd: loc.lineEnd,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
ts.isCallExpression(node) &&
|
|
76
|
+
ts.isPropertyAccessExpression(node.expression)
|
|
77
|
+
) {
|
|
78
|
+
const methodName = node.expression.name.getText(sourceFile);
|
|
79
|
+
if (SYNC_IO_METHODS.has(methodName)) {
|
|
80
|
+
const loc = getLineAndCharacter(sourceFile, node);
|
|
81
|
+
syncIoCalls.push({
|
|
82
|
+
name: methodName,
|
|
83
|
+
lineStart: loc.lineStart,
|
|
84
|
+
lineEnd: loc.lineEnd,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (
|
|
88
|
+
methodName === 'addEventListener' ||
|
|
89
|
+
methodName === 'on' ||
|
|
90
|
+
methodName === 'addListener'
|
|
91
|
+
) {
|
|
92
|
+
const loc = getLineAndCharacter(sourceFile, node);
|
|
93
|
+
listenerRegistrations.push({
|
|
94
|
+
file: fileRelative,
|
|
95
|
+
lineStart: loc.lineStart,
|
|
96
|
+
lineEnd: loc.lineEnd,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (
|
|
100
|
+
methodName === 'removeEventListener' ||
|
|
101
|
+
methodName === 'off' ||
|
|
102
|
+
methodName === 'removeListener'
|
|
103
|
+
) {
|
|
104
|
+
const loc = getLineAndCharacter(sourceFile, node);
|
|
105
|
+
listenerRemovals.push({
|
|
106
|
+
file: fileRelative,
|
|
107
|
+
lineStart: loc.lineStart,
|
|
108
|
+
lineEnd: loc.lineEnd,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (ts.isCallExpression(node)) {
|
|
114
|
+
const text = node.expression.getText(sourceFile);
|
|
115
|
+
if (text === 'setInterval' || text === 'setTimeout') {
|
|
116
|
+
const loc = getLineAndCharacter(sourceFile, node);
|
|
117
|
+
const clearName =
|
|
118
|
+
text === 'setInterval' ? 'clearInterval' : 'clearTimeout';
|
|
119
|
+
const parentBlock = findParentBlock(node);
|
|
120
|
+
const hasCleanup = parentBlock
|
|
121
|
+
? blockContainsCall(parentBlock, sourceFile, clearName)
|
|
122
|
+
: false;
|
|
123
|
+
timerCalls.push({
|
|
124
|
+
kind: text as 'setInterval' | 'setTimeout',
|
|
125
|
+
lineStart: loc.lineStart,
|
|
126
|
+
lineEnd: loc.lineEnd,
|
|
127
|
+
hasCleanup,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
ts.forEachChild(node, visit);
|
|
133
|
+
};
|
|
134
|
+
ts.forEachChild(sourceFile, visit);
|
|
135
|
+
|
|
136
|
+
fileEntry.awaitInLoopLocations = awaitInLoopLocations;
|
|
137
|
+
fileEntry.syncIoCalls = syncIoCalls;
|
|
138
|
+
fileEntry.timerCalls = timerCalls;
|
|
139
|
+
fileEntry.listenerRegistrations = listenerRegistrations;
|
|
140
|
+
fileEntry.listenerRemovals = listenerRemovals;
|
|
141
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { collectPrototypePollutionSites } from './prototype-pollution.js';
|
|
5
|
+
|
|
6
|
+
function parse(code: string, fileName = '/repo/src/test.ts'): ts.SourceFile {
|
|
7
|
+
return ts.createSourceFile(fileName, code, ts.ScriptTarget.ESNext, true);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe('collectPrototypePollutionSites', () => {
|
|
11
|
+
it('detects Object.assign risk', () => {
|
|
12
|
+
const sourceFile = parse(`
|
|
13
|
+
function merge(a: any, b: any) {
|
|
14
|
+
Object.assign(a, b);
|
|
15
|
+
}
|
|
16
|
+
`);
|
|
17
|
+
const sites = collectPrototypePollutionSites(sourceFile);
|
|
18
|
+
expect(sites.some(s => s.kind === 'object-assign')).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('detects deep merge risk', () => {
|
|
22
|
+
const sourceFile = parse(`
|
|
23
|
+
function merge(a: any, b: any) {
|
|
24
|
+
deepMerge(a, b);
|
|
25
|
+
}
|
|
26
|
+
`);
|
|
27
|
+
const sites = collectPrototypePollutionSites(sourceFile);
|
|
28
|
+
expect(sites.some(s => s.kind === 'deep-merge')).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('detects dynamic bracket assignment', () => {
|
|
32
|
+
const sourceFile = parse(`
|
|
33
|
+
function write(obj: Record<string, unknown>, key: string, val: unknown) {
|
|
34
|
+
obj[key] = val;
|
|
35
|
+
}
|
|
36
|
+
`);
|
|
37
|
+
const sites = collectPrototypePollutionSites(sourceFile);
|
|
38
|
+
expect(sites.some(s => s.kind === 'computed-property-write')).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('marks guarded writes when iterating known internal keys', () => {
|
|
42
|
+
const sourceFile = parse(`
|
|
43
|
+
function copy(dst: Record<string, unknown>, src: Record<string, unknown>) {
|
|
44
|
+
for (const key of Object.keys(src)) {
|
|
45
|
+
dst[key] = src[key];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
`);
|
|
49
|
+
const sites = collectPrototypePollutionSites(sourceFile);
|
|
50
|
+
const guarded = sites.filter(
|
|
51
|
+
s => s.kind === 'computed-property-write' && s.guarded
|
|
52
|
+
);
|
|
53
|
+
expect(guarded.length).toBeGreaterThan(0);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
|
|
3
|
+
import { findParentBlock } from './effects.js';
|
|
4
|
+
import { isFunctionLike } from '../ast/helpers.js';
|
|
5
|
+
import { getLineAndCharacter } from '../common/utils.js';
|
|
6
|
+
|
|
7
|
+
const DEEP_MERGE_NAMES = new Set([
|
|
8
|
+
'merge',
|
|
9
|
+
'deepMerge',
|
|
10
|
+
'deepAssign',
|
|
11
|
+
'extend',
|
|
12
|
+
'deepExtend',
|
|
13
|
+
'defaults',
|
|
14
|
+
'defaultsDeep',
|
|
15
|
+
'assign',
|
|
16
|
+
'mixin',
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
/** Check if a computed-property-write key comes from a for..of/for..in loop over known internal iteration */
|
|
20
|
+
function isKeyFromInternalIteration(
|
|
21
|
+
node: ts.ElementAccessExpression,
|
|
22
|
+
sourceFile: ts.SourceFile
|
|
23
|
+
): boolean {
|
|
24
|
+
const keyExpr = node.argumentExpression;
|
|
25
|
+
if (!keyExpr || !ts.isIdentifier(keyExpr)) return false;
|
|
26
|
+
const keyName = keyExpr.getText(sourceFile);
|
|
27
|
+
|
|
28
|
+
let current: ts.Node | undefined = node.parent;
|
|
29
|
+
while (current) {
|
|
30
|
+
if (ts.isForOfStatement(current) || ts.isForInStatement(current)) {
|
|
31
|
+
const init = current.initializer;
|
|
32
|
+
if (init) {
|
|
33
|
+
const initText = init.getText(sourceFile);
|
|
34
|
+
if (initText.includes(keyName)) {
|
|
35
|
+
const expr = current.expression.getText(sourceFile);
|
|
36
|
+
if (
|
|
37
|
+
/Object\.(keys|values|entries|getOwnPropertyNames)\(/.test(expr) ||
|
|
38
|
+
/\.keys\(\)|\.values\(\)|\.entries\(\)/.test(expr) ||
|
|
39
|
+
/Array\.from\(/.test(expr)
|
|
40
|
+
) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (isFunctionLike(current)) break;
|
|
47
|
+
current = current.parent;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Check if the containing block has a __proto__/constructor/prototype key guard */
|
|
53
|
+
function hasProtoKeyGuard(node: ts.Node, sourceFile: ts.SourceFile): boolean {
|
|
54
|
+
const block = findParentBlock(node);
|
|
55
|
+
if (!block) return false;
|
|
56
|
+
const blockText = block.getText(sourceFile);
|
|
57
|
+
return (
|
|
58
|
+
/__proto__|constructor|prototype/.test(blockText) &&
|
|
59
|
+
(blockText.includes('===') ||
|
|
60
|
+
blockText.includes('!==') ||
|
|
61
|
+
blockText.includes('includes(') ||
|
|
62
|
+
blockText.includes('hasOwnProperty'))
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Check if the target object was created with Object.create(null) or is Map/Set */
|
|
67
|
+
function isTargetSafeObject(
|
|
68
|
+
node: ts.ElementAccessExpression,
|
|
69
|
+
sourceFile: ts.SourceFile
|
|
70
|
+
): boolean {
|
|
71
|
+
const objText = node.expression.getText(sourceFile);
|
|
72
|
+
let current: ts.Node | undefined = node.parent;
|
|
73
|
+
while (current) {
|
|
74
|
+
if (ts.isBlock(current) || ts.isSourceFile(current)) {
|
|
75
|
+
const text = current.getText(sourceFile);
|
|
76
|
+
const createNullPattern = new RegExp(
|
|
77
|
+
`${objText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*=\\s*Object\\.create\\(null\\)`
|
|
78
|
+
);
|
|
79
|
+
const mapSetPattern = new RegExp(
|
|
80
|
+
`${objText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*=\\s*new\\s+(Map|Set)\\b`
|
|
81
|
+
);
|
|
82
|
+
if (createNullPattern.test(text) || mapSetPattern.test(text)) return true;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
current = current.parent;
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function collectPrototypePollutionSites(
|
|
91
|
+
sourceFile: ts.SourceFile
|
|
92
|
+
): Array<{
|
|
93
|
+
kind: string;
|
|
94
|
+
detail: string;
|
|
95
|
+
lineStart: number;
|
|
96
|
+
lineEnd: number;
|
|
97
|
+
guarded: boolean;
|
|
98
|
+
}> {
|
|
99
|
+
const sites: Array<{
|
|
100
|
+
kind: string;
|
|
101
|
+
detail: string;
|
|
102
|
+
lineStart: number;
|
|
103
|
+
lineEnd: number;
|
|
104
|
+
guarded: boolean;
|
|
105
|
+
}> = [];
|
|
106
|
+
|
|
107
|
+
const visit = (node: ts.Node): void => {
|
|
108
|
+
if (ts.isCallExpression(node)) {
|
|
109
|
+
const text = node.expression.getText(sourceFile);
|
|
110
|
+
if (text === 'Object.assign' && node.arguments.length >= 2) {
|
|
111
|
+
const loc = getLineAndCharacter(sourceFile, node);
|
|
112
|
+
sites.push({
|
|
113
|
+
kind: 'object-assign',
|
|
114
|
+
detail: `Object.assign() merges properties without __proto__ guard`,
|
|
115
|
+
lineStart: loc.lineStart,
|
|
116
|
+
lineEnd: loc.lineEnd,
|
|
117
|
+
guarded: false,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const calleeName = text.split('.').pop() || '';
|
|
121
|
+
if (DEEP_MERGE_NAMES.has(calleeName) && node.arguments.length >= 1) {
|
|
122
|
+
const loc = getLineAndCharacter(sourceFile, node);
|
|
123
|
+
sites.push({
|
|
124
|
+
kind: 'deep-merge',
|
|
125
|
+
detail: `${calleeName}() deep-merges without prototype guard`,
|
|
126
|
+
lineStart: loc.lineStart,
|
|
127
|
+
lineEnd: loc.lineEnd,
|
|
128
|
+
guarded: false,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (
|
|
134
|
+
ts.isElementAccessExpression(node) &&
|
|
135
|
+
node.argumentExpression &&
|
|
136
|
+
!ts.isStringLiteral(node.argumentExpression) &&
|
|
137
|
+
!ts.isNumericLiteral(node.argumentExpression) &&
|
|
138
|
+
node.parent &&
|
|
139
|
+
ts.isBinaryExpression(node.parent) &&
|
|
140
|
+
node.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
141
|
+
node.parent.left === node
|
|
142
|
+
) {
|
|
143
|
+
const guarded =
|
|
144
|
+
isKeyFromInternalIteration(node, sourceFile) ||
|
|
145
|
+
hasProtoKeyGuard(node, sourceFile) ||
|
|
146
|
+
isTargetSafeObject(node, sourceFile);
|
|
147
|
+
const loc = getLineAndCharacter(sourceFile, node);
|
|
148
|
+
sites.push({
|
|
149
|
+
kind: 'computed-property-write',
|
|
150
|
+
detail: `Dynamic bracket assignment: ${node.getText(sourceFile).slice(0, 40)}`,
|
|
151
|
+
lineStart: loc.lineStart,
|
|
152
|
+
lineEnd: loc.lineEnd,
|
|
153
|
+
guarded,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
ts.forEachChild(node, visit);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
ts.forEachChild(sourceFile, visit);
|
|
161
|
+
return sites;
|
|
162
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { collectSecurityData } from './security.js';
|
|
5
|
+
|
|
6
|
+
import type { FileEntry } from '../types/index.js';
|
|
7
|
+
|
|
8
|
+
function parse(code: string, fileName = '/repo/src/test.ts'): ts.SourceFile {
|
|
9
|
+
return ts.createSourceFile(fileName, code, ts.ScriptTarget.ESNext, true);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function emptyFileEntry(): FileEntry {
|
|
13
|
+
return {
|
|
14
|
+
package: 'test',
|
|
15
|
+
file: 'test.ts',
|
|
16
|
+
parseEngine: 'typescript',
|
|
17
|
+
nodeCount: 0,
|
|
18
|
+
kindCounts: {},
|
|
19
|
+
functions: [],
|
|
20
|
+
flows: [],
|
|
21
|
+
dependencyProfile: {
|
|
22
|
+
internalDependencies: [],
|
|
23
|
+
externalDependencies: [],
|
|
24
|
+
unresolvedDependencies: [],
|
|
25
|
+
declaredExports: [],
|
|
26
|
+
importedSymbols: [],
|
|
27
|
+
reExports: [],
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('collectSecurityData', () => {
|
|
33
|
+
it('detects eval usage - eval("1") → evalUsages contains entry', () => {
|
|
34
|
+
const code = `eval("1");`;
|
|
35
|
+
const sourceFile = parse(code);
|
|
36
|
+
const fileEntry = emptyFileEntry();
|
|
37
|
+
collectSecurityData(sourceFile, 'test.ts', fileEntry);
|
|
38
|
+
expect(fileEntry.evalUsages).toBeDefined();
|
|
39
|
+
expect(fileEntry.evalUsages).toHaveLength(1);
|
|
40
|
+
expect(fileEntry.evalUsages![0].file).toBe('test.ts');
|
|
41
|
+
expect(fileEntry.evalUsages![0].lineStart).toBeGreaterThan(0);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('detects hardcoded secret pattern - API_KEY = "sk-proj-..." → suspiciousStrings', () => {
|
|
45
|
+
const code = `const API_KEY = "sk-proj-abc123def456ghi789";`;
|
|
46
|
+
const sourceFile = parse(code);
|
|
47
|
+
const fileEntry = emptyFileEntry();
|
|
48
|
+
collectSecurityData(sourceFile, 'test.ts', fileEntry);
|
|
49
|
+
expect(fileEntry.suspiciousStrings).toBeDefined();
|
|
50
|
+
expect(fileEntry.suspiciousStrings!.length).toBeGreaterThan(0);
|
|
51
|
+
const secretEntry = fileEntry.suspiciousStrings!.find(
|
|
52
|
+
s => s.kind === 'hardcoded-secret'
|
|
53
|
+
);
|
|
54
|
+
expect(secretEntry).toBeDefined();
|
|
55
|
+
expect(secretEntry!.context).toBe('literal');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('detects SQL injection risk - template literal with SQL keyword and interpolation', () => {
|
|
59
|
+
const code = 'const q = `SELECT * FROM users WHERE id = ${id}`;';
|
|
60
|
+
const sourceFile = parse(code);
|
|
61
|
+
const fileEntry = emptyFileEntry();
|
|
62
|
+
collectSecurityData(sourceFile, 'test.ts', fileEntry);
|
|
63
|
+
expect(fileEntry.suspiciousStrings).toBeDefined();
|
|
64
|
+
const sqlEntry = fileEntry.suspiciousStrings!.find(
|
|
65
|
+
s => s.kind === 'sql-injection'
|
|
66
|
+
);
|
|
67
|
+
expect(sqlEntry).toBeDefined();
|
|
68
|
+
expect(sqlEntry!.snippet).toMatch(/SELECT/i);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('collects regex literal including potentially unsafe patterns', () => {
|
|
72
|
+
const code = 'const r = /(a+)+/;';
|
|
73
|
+
const sourceFile = parse(code);
|
|
74
|
+
const fileEntry = emptyFileEntry();
|
|
75
|
+
collectSecurityData(sourceFile, 'test.ts', fileEntry);
|
|
76
|
+
expect(fileEntry.regexLiterals).toBeDefined();
|
|
77
|
+
expect(fileEntry.regexLiterals!.length).toBeGreaterThan(0);
|
|
78
|
+
expect(fileEntry.regexLiterals![0].pattern).toContain('a');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('no false positive for regex in definition context - regex with secret keyword gets regex-definition', () => {
|
|
82
|
+
const code = 'const re = /api_key=""/;';
|
|
83
|
+
const sourceFile = parse(code);
|
|
84
|
+
const fileEntry = emptyFileEntry();
|
|
85
|
+
collectSecurityData(sourceFile, 'test.ts', fileEntry);
|
|
86
|
+
expect(fileEntry.suspiciousStrings).toBeDefined();
|
|
87
|
+
const entry = fileEntry.suspiciousStrings!.find(
|
|
88
|
+
s => s.context === 'regex-definition'
|
|
89
|
+
);
|
|
90
|
+
expect(entry).toBeDefined();
|
|
91
|
+
expect(entry!.kind).toBe('hardcoded-secret');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('clean code produces no suspicious strings', () => {
|
|
95
|
+
const code = `const x = 1; const msg = "hello"; function foo() { return 2; }`;
|
|
96
|
+
const sourceFile = parse(code);
|
|
97
|
+
const fileEntry = emptyFileEntry();
|
|
98
|
+
collectSecurityData(sourceFile, 'test.ts', fileEntry);
|
|
99
|
+
expect(fileEntry.suspiciousStrings).toBeDefined();
|
|
100
|
+
expect(fileEntry.suspiciousStrings!.length).toBe(0);
|
|
101
|
+
expect(fileEntry.evalUsages).toHaveLength(0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('template literal with SQL keyword', () => {
|
|
105
|
+
const code = 'const sql = `INSERT INTO users (name) VALUES (${name})`;';
|
|
106
|
+
const sourceFile = parse(code);
|
|
107
|
+
const fileEntry = emptyFileEntry();
|
|
108
|
+
collectSecurityData(sourceFile, 'test.ts', fileEntry);
|
|
109
|
+
expect(fileEntry.suspiciousStrings).toBeDefined();
|
|
110
|
+
const sqlEntry = fileEntry.suspiciousStrings!.find(
|
|
111
|
+
s => s.kind === 'sql-injection'
|
|
112
|
+
);
|
|
113
|
+
expect(sqlEntry).toBeDefined();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('process.env access is not flagged as secret', () => {
|
|
117
|
+
const code = 'const key = process.env.API_KEY;';
|
|
118
|
+
const sourceFile = parse(code);
|
|
119
|
+
const fileEntry = emptyFileEntry();
|
|
120
|
+
collectSecurityData(sourceFile, 'test.ts', fileEntry);
|
|
121
|
+
expect(fileEntry.suspiciousStrings).toBeDefined();
|
|
122
|
+
expect(fileEntry.suspiciousStrings!.length).toBe(0);
|
|
123
|
+
});
|
|
124
|
+
});
|