在 Web 应用程序路由中发现认证(authn)和授权(authz)安全漏洞:

Web 应用程序 HTTP 路由认证和授权漏洞是当今最常见的安全问题之一。以下行业标准资源强调了问题的严重性:
使用 pip 安装 route-detect:
$ python -m pip install --upgrade route-detect
你可以通过以下命令检查 route-detect 是否安装正确:
$ echo 'print(1 == 1)' | semgrep --config $(routes which test-route-detect) -
Scanning 1 file.
Findings:
/tmp/stdin
routes.rules.test-route-detect
Found '1 == 1', your route-detect installation is working correctly
1┆ print(1 == 1)
Ran 1 rule on 1 file: 1 finding.
route-detect 使用 routes CLI 命令,并提供以下命令树:
routes
whichviz首先,确保已安装 semgrep 并将其包含在 PATH 中。
[!重要]
route-detect用来显示代码片段的 Semgrep 功能已被移到了他们的云应用后面。详情请参见 #10762。不过,早期版本的 Semgrep 仍支持此行为。使用route-detect时,请确保安装1.97.0之前的 Semgrep 版本。可以通过以下命令完成:python -m pip install 'semgrep<1.97.0'。
使用 which 子命令让 semgrep 指向正确的 Web 应用程序规则:
$ semgrep --config $(routes which django) path/to/django/code
使用 viz 子命令在浏览器中可视化路由信息:
$ semgrep --json --config $(routes which django) --output routes.json path/to/django/code
$ routes viz --browser routes.json
如果不确定要查找哪个框架,可以使用特殊的 all ID 检查所有:
$ semgrep --json --config $(routes which all) --output routes.json path/to/code
如果有自定义的认证或授权逻辑,可以复制 route-detect 的规则:
$ cp $(routes which django) my-django.yml
然后根据需要修改规则,像上面一样运行:
$ semgrep --json --config my-django.yml --output routes.json path/to/django/code
$ routes viz --browser routes.json
首先,确保已安装 codeql 并将其包含在 PATH 中。
使用 which 子命令首先安装包的依赖,然后运行相应的查询:
$ codeql pack install $(poetry run routes which -c rails)
$ codeql database analyze \
--output routes.sarif \
--format sarif-latest \
--sarif-add-file-contents \
--no-group-results \
-- \
/path/to/codeql/db \
$(routes which --codeql rails)
使用 viz 子命令在浏览器中可视化路由信息:
$ routes viz --codeql --browser routes.sarif
route-detect 使用 poetry 进行依赖和配置管理。
在开始之前,使用以下命令安装项目依赖:
$ poetry install --with dev
使用以下命令检查所有项目文件:
$ poetry run pre-commit run --all-files
使用以下命令运行 Python 测试:
$ poetry run pytest --cov
使用以下命令运行 Semgrep 规则测试:
$ poetry run semgrep --test --config routes/rules/ tests/test_rules/
使用以下命令运行 CodeQL 查询测试:
$ codeql test run routes/queries/rails/test/
| 语言 | 框架 | Semgrep | CodeQL |
|---|
| Python | Django | ✅ | ❌ |
| Python | Django REST framework | ✅ | ❌ |
| Python | Flask | ✅ | ❌ |
| Python | Sanic | ✅ | ❌ |
| Python | FastAPI | ✅ | ❌ |
| PHP | Laravel | ✅ | ❌ |
| PHP | Symfony | ✅ | ❌ |
| PHP | CakePHP | ✅ | ❌ |
| Ruby | Rails | ❌ | ✅ |
| Ruby | Grape | ✅ | ❌ |
| Java | JAX-RS | ✅ | ❌ |
| Java | Spring | ✅ | ❌ |
| Go | Gorilla | ✅ | ❌ |
| Go | Gin | ✅ | ❌ |
| Go | Chi | ✅ | ❌ |
| JavaScript/TypeScript | Express | ✅ | ❌ |
| JavaScript/TypeScript | React | ✅ | ❌ |
| JavaScript/TypeScript | Angular | ✅ | ❌ |