@codacy/tools-opengrep-1

中文文档
0.28.0发布于 2026/11/09MITTypeScriptCommonJS近 30 天下载 1.2万

Opengrep adapter — CLI-mode multi-language SAST scanner

原始使用文档
文档目录9 节
  1. Table of Contents
  2. Overview
  3. Rule bundling
  4. Keeping it in step with the Codacy image
  5. Updating patterns
  6. Updating rules
  7. Updating the Opengrep version
  8. Development
  9. Notes for maintainers

@codacy/tools-opengrep-1

Table of Contents


Overview

Multi-language SAST (Static Application Security Testing) scanner using the Opengrep binary. Supports 30+ languages via YAML rule files. Uses the CLI execution strategy.

PropertyValue
Tool IDSemgrep (legacy, for Codacy platform compatibility)
Display nameOpengrep
Codacy UUID6792c561-236d-41b7-ba5e-9d6bee0d548b
StrategyCLI
Languages30+ (Python, JavaScript, TypeScript, Java, Go, Ruby, C, C++, etc.)
Binaryopengrep
File patterns~70 extensions derived from the languages above, plus Dockerfile
Pattern count~2500
Extra dependencyyaml (for YAML rule config parsing/generation)

Rule bundling

Opengrep uses YAML rule files for detection. The adapter bundles ~2500 rules into src/rules.yaml, sourced from:

  1. opengrep/opengrep-rules — the registry rules, pinned at the same commit as the Codacy image
  2. GitLab SAST rules — MIT-licensed, from gitlab.com/gitlab-org/security-products/sast-rules
  3. Codacy custom rules — docs/codacy-rules*.yaml in codacy/codacy-opengrep

The bundled src/rules.yaml is committed to the repository for consistency and reproducibility.

Keeping it in step with the Codacy image

All three sources, their pinned commits and their file filters are mirrored from codacy/codacy-opengrep's internal/docgen/parsing.go. Keep them that way: the image is the reference implementation, and scripts/fetch-rules.mjs says which upstream function each piece came from.

Three mistakes in that mirroring cost 296 of 2534 patterns (12%), every one of them silent — the pattern stays in patterns.json, init --auto enables it, the config claims it is active, and it simply never fires:

  • Rule ids were derived from the file path. GitLab ids are written in the file and must be taken verbatim. Path derivation looks right for the top-level tree (java/crypto/rule-X.yml really does hold java_crypto_rule-X) and is wrong for the licence-tiered one, where rules/lgpl-cc/properties/spring/accesscontrol/rule-Y.yml holds properties_spring_rule-Y. The sharpest case is rule-JwtDecodeWithoutVerify.yml, whose id uses an underscore where the filename uses a hyphen. 265 of 588 GitLab rules were lost this way.
  • The registry source was upstream semgrep/semgrep-rules, not Opengrep's fork. The two share most ids, so the output looks plausible while individual rules go missing.
  • YAML was read and rewritten line by line. That corrupted multi-line flow scalars — the generated file stopped parsing, and opengrep then reported zero findings for the entire run rather than an error — and it skipped every tests/ directory, where some shipped rules live.

test/rules-coverage.test.ts is the guard: it fails if any catalogued pattern has no rule, and if the generated file does not parse. Run pnpm fetch-rules when it does; the script prints the orphaned ids.

Updating patterns

# Re-fetch pattern metadata from the Codacy API
pnpm prefetch

# Commit the result
git add src/patterns.json

Pattern IDs follow the format Semgrep_python.lang.security.audit.exec-detected.exec-detected (dot-separated rule paths).

Updating rules

# Re-fetch and bundle rules from all three sources
pnpm fetch-rules

# This clones the rule repos, extracts matching rules, and writes src/rules.yaml
# Only rules whose IDs match src/patterns.json are included — and the script now
# warns loudly about the reverse, a catalogued pattern with no rule to run.

# Commit the result
git add src/rules.yaml

The fetch-rules script (scripts/fetch-rules.mjs):

  • Clones semgrep/semgrep-rules at the pinned commit
  • Clones GitLab SAST rules (latest)
  • Clones codacy/codacy-opengrep for custom rules
  • Extracts rules, assigns path-based IDs
  • Filters to only rules matching src/patterns.json
  • Writes concatenated YAML to src/rules.yaml

To update the pinned semgrep-rules commit, edit SEMGREP_RULES_COMMIT in scripts/fetch-rules.mjs.

Updating the Opengrep version

  1. Update preferredVersion in src/adapter.ts
  2. Run pnpm test to verify compatibility
  3. Consider re-running pnpm fetch-rules if the new version supports new rule syntax

Development

pnpm build        # Build with tsup
pnpm test         # Run tests (requires opengrep in PATH)
pnpm fetch-rules  # Re-bundle rules from upstream sources
pnpm prefetch     # Re-fetch pattern metadata from Codacy API

Installing the binary:

The adapter auto-installs opengrep into ~/.codacy/tools/ when run with --install-dependencies (no manual step needed):

codacy-analysis analyze . --tool opengrep --install-dependencies

install() downloads the matching statically-linked binary from GitHub releases (no archive to unpack) and chmod +xes it (not on Windows). The platform/arch/libc → asset mapping mirrors opengrep's official install.sh and install.ps1:

PlatformAsset
macOS x64opengrep_osx_x86
macOS arm64opengrep_osx_arm64
Linux x64 (glibc)opengrep_manylinux_x86
Linux arm64 (glibc)opengrep_manylinux_aarch64
Linux x64 (musl/Alpine)opengrep_musllinux_x86
Linux arm64 (musl/Alpine)opengrep_musllinux_aarch64
Windows x64opengrep_windows_x86.exe
Windows arm64 (emulated)opengrep_windows_x86.exe

To install manually instead:

curl -fsSL https://raw.githubusercontent.com/opengrep/opengrep/main/install.sh | bash
# Windows (PowerShell)
irm https://raw.githubusercontent.com/opengrep/opengrep/main/install.ps1 | iex

Notes for maintainers

  • Preset mappings (src/pattern-mappings.json): prefixes are the upstream rule paths (Semgrep_<lang>.<framework>.…), which already name the framework, so this mapping is prefix-only — Opengrep's tags are topical (security, audit) and would over-match. 22 keys were added, taking ~91 default-on patterns out of repos that lack the framework: Pyramid (16), PyCryptodome (12), Kubernetes/Helm (14, via yaml.kubernetes. + codacy.k8s.; the kubernetes signal matches loose manifests under k8s//kubernetes//manifests/, not just kustomization.yaml, or the most common layout would have lost all 14), GitHub Actions (5), AWS CDK (5), Jinja2 (2), and the smaller Boto3/Twilio/Android/Sequelize/Doctrine families. The YAML-language families matter most: before this they fired in any repo containing a single .yaml file.

    The *.aws-lambda. packs (34 default-on across Python, JavaScript, Ruby and Go) are claimed by seven keys — serverless-framework, cloudformation, aws-cdk, boto3, aws-sdk, aws-sdk-go and lang:Terraform. Handler code carries no manifest of its own; the only evidence is how it is deployed or which SDK it calls, and any one of the seven is enough. Terraform earns a key because deploying a Lambda with aws_lambda_function leaves no other trace, and the SDK telltales had to learn npm's v2 aws-sdk, Go's v1 aws-sdk-go and PyPI's botocore/aws-lambda-powertools — Lambda runtimes preinstall boto3, so it is often absent from requirements.txt entirely.

    Semgrep_python.jinja2. is claimed by jinja2 and flask: Flask hard-depends on Jinja2, so gating on a direct jinja2 declaration alone dropped both Error-severity autoescape rules from most real Flask repos.

    Deliberately still universal, guarded by a test so it stays that way: *.lang.* (the generic per-language packs), csharp.dotnet. (the platform, not a library), terraform.* (already gated by the Terraform language), problem-based-packs.* (cross-language), and apex. / generic.visualforce. — Apex and VisualForce are Salesforce-only languages, so the language gate already scopes them and keying them on salesforce would only add a way to miss them in a Salesforce repo with no sfdx-project.json.

  • Tool ID is Semgrep for legacy compatibility with the Codacy platform. The displayName is set to Opengrep for CLI display. Never change the tool ID without coordinating with the Codacy platform team.

  • Config materialization: When Codacy patterns are specified, the adapter loads src/rules.yaml, filters to enabled rules, and writes a filtered config to .codacy/generated/opengrep-rules.yaml.

  • check_id is the bare rule ID: every invocation passes --no-rewrite-rule-ids. Without it Opengrep prefixes check_id with the config file's directory relative to cwd (.codacy/generated/x.yaml → codacy.generated.), and the adapter used to reproduce that path math to strip the prefix again — never verified on Windows, where a mismatch would silently drop every finding at the enabled-pattern check. With the flag the rule ID maps to a pattern ID directly on every OS.

  • Config file search: .semgrep.yaml, .opengrep.yaml, .semgrep.yml, .opengrep.yml (in that order).

  • Opengrep errors: Parse errors (syntax errors, partial parsing) from Opengrep's JSON errors array are converted to structured AnalysisError objects with filePath. Stderr (scan status table) is logged at debug level only.

  • Rule ID format: Semgrep-rules use {dir}.{filename}.{ruleId} (dots), GitLab rules use {dir}_{filename} (underscores), Codacy custom rules keep their original IDs.

  • Install / binary download: install() downloads the bare per-platform binary from https://github.com/opengrep/opengrep/releases/download/v{version}/{asset} and chmod +xes it into ~/.codacy/tools/. The release tag carries a v prefix (v1.30.0) even though preferredVersion does not. On Linux the libc flavour is detected with an ldd /bin/sh probe (musl → musllinux, else manylinux); detection failures fall back to glibc/manylinux. On Windows the binary is saved as opengrep.exe; upstream builds only x64, which install.ps1 also installs on ARM64 (emulated), so we do the same. From opengrep 2.0.0 the Windows asset becomes opengrep_windows_x86.zip (exe + DLLs) — bumping preferredVersion past 2.0 needs an extraction step. The argv chunk budget drops to MAX_ARGV_BYTES (30,000) on Windows, whose command line is capped at 32,767 characters. Unlike install.sh, the Codacy-managed install does not verify the cosign .cert/.sig signatures — if signature verification becomes a requirement, fetch those sibling assets and validate before chmod.

  • Argv-size chunking (no directory walk): The adapter always passes Opengrep an explicit file list — the runner's already git-ignored, exclude-filtered target set — so Opengrep never walks the repository tree itself. chunkFilesByArgvBudget() splits that list into the fewest, largest chunks that each fit ARGV_BYTE_BUDGET (200 KB, well under ARG_MAX), and each chunk is scanned in its own opengrep scan invocation; findings and errors are merged across chunks. Chunk by size, not count: every invocation re-compiles rules.yaml, so fewer/larger chunks amortize that fixed cost. The earlier directory/“root” fallback was removed because on huge repos (a single tracked file at the repo root collapsed the minimal-cover dirs to .) it made Opengrep walk the entire tree — re-scanning excluded/generated/vendored files and timing out at the runner's 10-minute limit.

  • File-size cap: invocations pass --max-target-bytes 1000000 (~1 MB). This restores Opengrep's default size cap — the adapter previously passed 0, which disables the cap, so giant generated/minified bundles were scanned and dominated runtime. The cap applies to explicitly-listed files (verified on v1.16.4). Note: --exclude-minified-files is not used — that flag is rejected by the bundled binary unless --experimental (a different engine) is also passed, so we rely on the size cap instead. (The runner also enforces a tighter 150 KB per-file limit centrally before files reach the adapter, so this --max-target-bytes is now a secondary defense.)

  • Partial results on timeout: the per-chunk loop checks ctx.signal (the runner's timeout abort) between chunks and passes it to spawnTool, so an in-flight scan is killed promptly. On abort the adapter keeps the findings from completed chunks and adds a single Timeout error (→ status: "partial") rather than losing everything. This is why chunking matters beyond ARG_MAX: it's the granularity at which a long scan can be stopped with results preserved.

版本与发布记录

@codacy/tools-opengrep-1 共发布 36 个版本,首次发布于 2026/03/24;过去 12 个月发布了 36 个版本。

版本发布日期距今
0.28.0latest2026/11/09今天
0.27.12026/11/054 天前
0.27.02026/11/036 天前
0.26.02026/10/3010 天前
0.25.02026/10/1921 天前
0.24.02026/09/251 个月前

关于 @codacy/tools-opengrep-1 的常见问题

@codacy/tools-opengrep-1 是什么?

Opengrep adapter — CLI-mode multi-language SAST scanner

如何安装 @codacy/tools-opengrep-1?

在项目目录运行 npm install @codacy/tools-opengrep-1,或使用 pnpm add / yarn add / bun add。

@codacy/tools-opengrep-1 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codacy/tools-opengrep-1 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@codacy/tools-opengrep-1 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@codacy/tools-opengrep-1 可以商用吗?

@codacy/tools-opengrep-1 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codacy/tools-opengrep-1 的下载量有多少?

根据 npm 官方下载统计,@codacy/tools-opengrep-1 在 2026/10/10 – 2026/11/08 这 30 天内被下载 12,106 次,日均约 404 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codacy/tools-opengrep-1 还在维护吗?

最新版本 0.28.0 发布于 2026/11/09(今天);至今共发布 36 个版本,其中过去 12 个月发布了 36 个;首次发布于 2026/03/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。