跳到主要内容

AI代码审计项目

1. OCR​

Open Code Review 将阿里巴巴经过实战检验的代码审查 Agent 引入您的工作流程。连接任意 LLM,数据完全私有,获得开发者真正采纳的审查意见。

ocr scan 用于扫描仓库中的完整文件,不依赖 Git diff。执行以下命令即可开始:

ocr scan --repo /path/to/repo

建议正式扫描前先预览实际会提交给模型的文件,避免扫描静态资源、测试文件或无关目录:

ocr scan --repo /path/to/repo --preview

扫描完成后,可以通过 WebUI 查看结果:

ocr viewer

在无图形界面的环境中,可以只启动服务并输出访问地址:

ocr viewer --open=never

也可以使用命令行查看某个扫描会话的评论。先列出会话 ID:

ocr session list --repo /path/to/repo

再查看指定会话:

ocr session comments c7581b9b-240a-45ae-aecd-63bad9932954 --repo /path/to/repo

如果只关注 Critical / High 级别的安全问题,可以使用 --severity 和 --category 进行筛选:

ocr session comments c7581b9b-240a-45ae-aecd-63bad9932954 \
--repo /path/to/repo \
--severity critical,high \
--category security

添加 --json 后,结果会以 JSON 格式输出,便于交给 jq、CI 或其他自动化工具处理:

ocr session comments c7581b9b-240a-45ae-aecd-63bad9932954 \
--repo /path/to/repo \
--severity critical,high \
--category security \
--json
备注

需要注意,--severity 和 --category 属于扫描后的结果筛选,不会减少扫描阶段的 Token 消耗。

image-20260918222453193

OCR 内置规则通常同时覆盖 Bug、安全漏洞、性能、代码风格、文档和可维护性等多个方面,因此提示词较长,Token 开销也较大。为了降低扫描成本,可以在扫描前通过自定义 rule.json 完成过滤:

  • 排除 CSS、HTML、JS、图片等静态或前端资源。
  • 排除测试、示例、生成代码、第三方依赖和构建目录。
  • 使用仅关注 Critical / High 安全漏洞的规则替换内置语言规则。(无法完全过滤)
  • 使用 --path 将扫描范围进一步限制到后端代码目录。
  • 使用 --preview 确认过滤结果,避免为无关文件消耗 Token。

自定义 rule 加载规则顺序:

优先级来源路径说明
1(最高)--rule 参数用户指定CLI 覆盖;只要提供就总是生效。
2项目配置<repoDir>/.opencodereview/rule.json项目级规则——可安全提交。
3全局配置~/.opencodereview/rule.json用户级偏好。
4(最低)系统默认内嵌 system_rules.json覆盖常见语言的内置规则。

可以把我的扫描规则放到全局配置,内容如下:

{
"exclude": [
"**/*.{css,scss,sass,less,styl,stylus,html,htm,xhtml,shtml,vue,svelte,astro,md,mdx,markdown,svg,png,jpg,jpeg,gif,webp,avif,bmp,ico,woff,woff2,ttf,otf,eot,map}",
"**/*.{js,jsx,ts,tsx,mjs,cjs}",
"**/*.min.*",
"**/{node_modules,dist,build,out,coverage,public,static,assets,frontend,front-end,www,vendor,third_party,third-party,generated,gen}/**",
"**/{test,tests,__tests__,testdata,fixtures,examples,example,samples}/**",
"**/*_test.go",
"**/src/test/java/**/*.java",
"**/src/test/**/*.kt",
"**/*.test.{js,jsx,ts,tsx}",
"**/*.spec.{js,jsx,ts,tsx}",
"**/__tests__/**",
"**/test/**/*_test.py",
"**/tests/**/*_test.py",
"**/*_test.py",
"**/*_spec.rb",
"**/spec/**/*_spec.rb",
"**/*Test.java",
"**/*Tests.java",
"**/*_test.rs",
"**/oh_modules/**",
"**/*.test.ets",
"**/*.{snap,lock,sum}"
],
"rules": [
{
"path": "**/*",
"rule": "ocr-security-critical-high.md"
}
]
}

ocr-security-critical-high.md 内容(放到rule 同目录中):

# Backend Security Review: Critical and High Findings Only

## Scope

Review this file only for exploitable backend security vulnerabilities. Ignore
style, formatting, naming, architecture, maintainability, performance, test
coverage, documentation, and general correctness unless the issue directly
creates a security vulnerability.

Do not report frontend presentation issues, static asset issues, dependency
upgrade suggestions, best-practice warnings, hardening ideas, or theoretical
risks without a concrete attack path.

## Reporting Threshold

Report a finding only when all of the following are true:

1. The code contains a concrete vulnerable source-to-sink flow or a clearly
broken security control.
2. An attacker can control the relevant input or security decision.
3. The vulnerable path is reachable in the application's normal runtime.
4. Existing validation, authorization, encoding, parameterization, allowlists,
or framework protections do not prevent exploitation.
5. The realistic impact is Critical or High.

If the evidence is incomplete, the issue depends on unknown deployment
settings, or the impact is Medium or Low, do not report it.

## Critical

Classify as Critical only when the issue can cause one or more of:

- Unauthenticated remote code execution.
- Authentication bypass with broad administrative access.
- Arbitrary file write or command execution with system-level impact.
- Direct access to production secrets, private keys, or full credential sets.
- Unauthorized bulk access to sensitive production data.
- Complete database compromise through a remotely reachable path.
- Full tenant or privilege boundary bypass without prior authentication.

## High

Classify as High when the issue can cause one or more of:

- Authenticated remote code execution.
- SQL injection, NoSQL injection, command injection, template injection, or
expression-language injection with meaningful data or execution impact.
- Server-side request forgery reaching internal services, metadata endpoints,
control planes, or privileged networks.
- Insecure deserialization leading to code execution or privilege escalation.
- Path traversal or arbitrary file read/write affecting sensitive files.
- Broken object-level or function-level authorization exposing sensitive data
or privileged operations.
- Authentication or session vulnerabilities that permit account takeover.
- XXE with file disclosure, SSRF, or denial of service against critical paths.
- Server-side template injection, unsafe file upload leading to execution, or
unrestricted redirect chains that expose credentials or trusted tokens.
- Hardcoded production credentials or cryptographic keys that directly grant
access to a sensitive system.
- Use of broken cryptography where an attacker can decrypt, forge, or bypass a
security boundary.

## Security Checks

Prioritize these vulnerability classes:

- Injection: SQL, NoSQL, OS command, LDAP, XPath, expression language, template,
header, log, and deserialization sinks.
- Access control: missing authentication, broken authorization, IDOR, privilege
escalation, tenant isolation failures, and unsafe administrative endpoints.
- Server-side attacks: SSRF, XXE, path traversal, arbitrary file operations,
unsafe archive extraction, and unsafe URL fetching.
- Execution: insecure deserialization, unsafe reflection, dynamic code loading,
script-engine abuse, and unsafe native command execution.
- Credentials and cryptography: production secrets in source, insecure key
handling, signature bypass, predictable randomness in security tokens, and
broken encryption or hashing used as a security control.
- Web security: stored XSS in privileged backend flows, session fixation,
insecure trust of client-controlled identity, CSRF on sensitive state changes,
and unsafe redirects that leak credentials.

## Required Evidence

For every finding, include:

- Severity: Critical or High.
- Category: Security.
- Exact file and line.
- The attacker-controlled source and the dangerous sink.
- The missing or bypassable security control.
- A concise exploitation path.
- The concrete security impact.
- A minimal remediation that preserves intended behavior.

Do not invent context that is not present in the file or available repository
context. Do not report a finding merely because a dangerous API is used when
the input is constant, trusted, parameterized, or protected by an effective
control.

If no Critical or High security vulnerability is confirmed, return no findings.