Skip to content

Commit 1a8cf99

Browse files
committed
refactor: migrate prompts package under pure_auto_codeql
Move prompt Python modules and markdown assets into pure_auto_codeql.prompts, keep thin top-level re-export shims, and route prompts_dir/codeql_compose/MCP filesystem access to the new canonical location.
1 parent bc93326 commit 1a8cf99

58 files changed

Lines changed: 1674 additions & 1557 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Information/ghsa_fetch.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
"""Legacy re-export of ``pure_auto_codeql.information.ghsa_fetch``.
1+
"""Legacy re-export of ``pure_auto_codeql.information.ghsa_fetch``."""
22

3-
``from Information import ghsa_fetch`` and ``import Information.ghsa_fetch``
4-
both resolve to the canonical module object.
5-
"""
6-
7-
from pure_auto_codeql.information import ghsa_fetch as _impl
3+
from importlib import import_module
84
import sys as _sys
95

10-
_sys.modules[__name__] = _impl
6+
_sys.modules[__name__] = import_module("pure_auto_codeql.information.ghsa_fetch")

Information/nvd_info_fetch.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
"""Legacy re-export of ``pure_auto_codeql.information.nvd_info_fetch``.
1+
"""Legacy re-export of ``pure_auto_codeql.information.nvd_info_fetch``."""
22

3-
``from Information import nvd_info_fetch`` and ``import Information.nvd_info_fetch``
4-
both resolve to the canonical module object.
5-
"""
6-
7-
from pure_auto_codeql.information import nvd_info_fetch as _impl
3+
from importlib import import_module
84
import sys as _sys
95

10-
_sys.modules[__name__] = _impl
6+
_sys.modules[__name__] = import_module("pure_auto_codeql.information.nvd_info_fetch")

docs/package_architecture.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ migration window.
1414
| LLM configuration imports | `pure_auto_codeql.configuration` | Canonical facade |
1515
| repo path helpers | `pure_auto_codeql.paths` | Introduced (`get_repo_root`, `prompts_dir`) |
1616
| `Information` | `pure_auto_codeql.information` | Migrated (top-level re-export shim kept) |
17+
| `prompts` | `pure_auto_codeql.prompts` | Migrated (top-level re-export shim kept; `.md` assets co-located) |
1718
| `api` | `pure_auto_codeql.api` | Planned staged migration |
1819
| `core` | `pure_auto_codeql.core` | Planned staged migration |
1920
| `services` | `pure_auto_codeql.services` | Planned staged migration |
2021
| `utils` | `pure_auto_codeql.utils` | Planned staged migration |
2122
| `tools` | `pure_auto_codeql.tools` | Planned staged migration |
22-
| `prompts` | `pure_auto_codeql.prompts` | Planned staged migration |
2323

2424
## Compatibility Surface
2525

@@ -35,6 +35,7 @@ explicitly removes them:
3535
- `from utils import ...`
3636
- `from tools import ...`
3737
- `from Information import ...`
38+
- `from prompts import ...`
3839

3940
Compatibility shims should be quiet by default. Tests should cover both the new
4041
canonical imports and supported legacy imports before a module is moved.

prompts/__init__.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
# Prompts module for storing and managing prompt templates
2-
"""
3-
Prompts模块用于存储和管理各种提示词模板。
1+
"""Legacy prompts package.
2+
3+
Implementation lives under ``pure_auto_codeql.prompts``. This top-level package
4+
re-exports the public API for compatibility.
45
"""
56

6-
from .source_prompts import (
7+
from pure_auto_codeql.prompts import (
8+
SinkPromptManager,
9+
VerificationPromptManager,
10+
build_batch_path_analysis_prompt,
11+
build_path_analysis_prompt,
12+
build_sink_prompt,
713
build_source_analysis_prompt,
814
build_source_analysis_with_codeql_prompt,
915
build_source_analysis_with_sink_prompt,
16+
build_verification_requirement,
1017
get_language_specific_focus,
1118
get_language_specific_instructions,
12-
)
13-
14-
from .sink_prompt_manager import (
15-
SinkPromptManager,
16-
prompt_manager,
17-
build_sink_prompt,
1819
get_supported_languages,
19-
)
20-
21-
from .path_analysis_prompts import (
22-
build_path_analysis_prompt,
23-
build_batch_path_analysis_prompt,
24-
)
25-
26-
from .verification_prompt_manager import (
27-
VerificationPromptManager,
28-
verification_prompt_manager,
29-
load_verification_template,
30-
build_verification_requirement,
3120
get_supported_verification_languages,
21+
load_verification_template,
22+
prompt_manager,
23+
verification_prompt_manager,
3224
)
3325

3426
__all__ = [
@@ -48,4 +40,4 @@
4840
"load_verification_template",
4941
"build_verification_requirement",
5042
"get_supported_verification_languages",
51-
]
43+
]

prompts/codeql_prompts.py

Lines changed: 4 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,6 @@
1-
"""
2-
CodeQL generation prompt strategies with different strictness levels.
1+
"""Legacy re-export of `pure_auto_codeql.prompts.codeql_prompts`."""
32

4-
This module provides prompt variants for retry mechanism when dataflow queries return empty results.
5-
Each retry level progressively relaxes source/sink constraints to increase the chance of finding paths.
6-
"""
3+
from importlib import import_module
4+
import sys as _sys
75

8-
9-
def get_codeql_generation_prompt_suffix(retry_count: int = 0) -> str:
10-
"""
11-
根据重试次数返回不同严格程度的CodeQL生成提示词后缀。
12-
13-
Args:
14-
retry_count: 当前重试次数 (0=首次生成, 1-3=重试)
15-
16-
Returns:
17-
提示词后缀字符串,用于指导生成更宽松的source/sink定义
18-
"""
19-
if retry_count == 0:
20-
# 标准prompt - 首次生成
21-
return """
22-
## 生成要求
23-
- 生成精确的数据流查询,准确定义source点和sink点
24-
- source点应该是明确的用户输入来源
25-
- **sink点定义原则(重要)**:
26-
- **必须优先定义Sink点分析报告中的sink点**
27-
- **注意**:
28-
- 如果代码段有多次调用同名方法,可以添加包名或其他条件进一步限制
29-
- Sink 点报告中的 sink 点是必需的,必须精确匹配报告中的调用位置
30-
- 对于其他场景:sink点应该是明确的危险操作点
31-
- 确保查询逻辑严谨,避免误报
32-
"""
33-
34-
elif retry_count == 1:
35-
# 第一次重试 - 适度放宽
36-
return """
37-
## 生成要求(放宽版本 - 重试1/3)
38-
⚠️ 前一次查询未找到数据流路径,请适度放宽条件:
39-
40-
- **放宽source点定义**:
41-
- 除了直接的用户输入(如HTTP参数、文件读取),也考虑间接的数据来源
42-
- 包含可能被用户控制的配置项、环境变量等
43-
- 考虑通过多层传递的用户数据
44-
45-
- **放宽sink点定义**:
46-
- **必须包含Sink点分析报告中的sink点**
47-
- **三种方法精确定位 sink 点**(可以适当放宽某些条件):
48-
1. `mc.getEnclosingCallable().hasName("xxx")` - 被调用的方法名(必需)
49-
2. `mc.getMethod().hasName("xxx")` - 当前方法调用名(必需)
50-
3. 文件路径匹配(可以放宽为目录级别匹配,如 `%/controller/%` 而不是具体文件名)
51-
- 可以考虑多个相关的调用点,使用 `or` 连接
52-
- 除了明显的危险操作,也包含潜在的风险点
53-
- 考虑间接调用的危险函数
54-
- 包含可能导致安全问题的辅助函数
55-
- **注意**:仍然禁止使用底层调用(如 `java.lang` 包)作为限制条件
56-
57-
- 保持数据流分析的基本逻辑,但降低匹配的严格程度
58-
"""
59-
60-
elif retry_count == 2:
61-
# 第二次重试 - 进一步放宽
62-
return """
63-
## 生成要求(进一步放宽 - 重试2/3)
64-
⚠️ 前两次查询均未找到路径,请进一步放宽条件:
65-
66-
- **更宽泛的source点**:
67-
- 包含所有可能的外部输入源
68-
- 考虑反射、动态加载等间接输入方式
69-
- 包含数据库查询结果、缓存数据等
70-
- 放宽对输入验证的要求
71-
72-
- **更宽泛的sink点**:
73-
- **必须包含Sink点分析报告中的sink点**
74-
- **进一步放宽定位条件**:
75-
- 方法名匹配可以使用模糊匹配(如包含特定关键词)
76-
- 文件路径可以只匹配目录或模块名
77-
- 可以考虑同一类型的多个方法调用
78-
- 使用 `or` 连接多个相关的调用点
79-
- 包含所有可能产生副作用的操作
80-
- 考虑间接的、多层调用的危险点
81-
- 包含日志记录、序列化等可能的风险点
82-
- 降低对sink点类型的限制
83-
84-
- 适当放宽数据流的污点传播规则
85-
- 考虑更多的中间节点和传播路径
86-
"""
87-
88-
elif retry_count >= 3:
89-
# 第三次重试 - 最宽松策略
90-
return """
91-
## 生成要求(最宽松策略 - 重试3/3)
92-
⚠️ 这是最后一次重试,请使用最宽松的策略:
93-
94-
- **最大范围的source点**:
95-
- 包含任何可能的外部数据来源
96-
- 不限制输入的类型和来源
97-
- 包含所有参数、返回值、字段访问等
98-
- 考虑任何可能被外部影响的数据
99-
100-
- **最大范围的sink点**:
101-
- **必须包含Sink点分析报告中的sink点作为核心条件**
102-
- **最宽松的定位策略**:
103-
- 可以只使用方法名或文件路径中的一个条件
104-
- 可以使用非常宽泛的模式匹配
105-
- 考虑所有相关的方法调用和操作
106-
- 使用 `or` 连接所有可能相关的调用点
107-
- 包含所有可能产生影响的操作
108-
- 不限制操作的类型和危险程度
109-
- 包含所有方法调用、字段赋值等
110-
- 考虑任何可能产生副作用的代码
111-
- **注意**:在此阶段可以适当考虑底层 API,但仍需以报告中的 sink 点为主
112-
113-
- **最宽松的数据流规则**:
114-
- 放宽污点传播的限制
115-
- 包含更多的隐式数据流
116-
- 考虑控制流依赖
117-
- 降低路径可达性的要求
118-
119-
- 优先找到任何可能的数据流路径,即使可能包含误报
120-
"""
121-
122-
else:
123-
# 默认返回标准prompt
124-
return get_codeql_generation_prompt_suffix(0)
125-
126-
127-
def get_retry_strategy_description(retry_count: int) -> str:
128-
"""
129-
获取当前重试策略的简短描述。
130-
131-
Args:
132-
retry_count: 当前重试次数
133-
134-
Returns:
135-
策略描述字符串
136-
"""
137-
strategies = {
138-
0: "标准策略",
139-
1: "放宽策略(重试1/3)",
140-
2: "进一步放宽(重试2/3)",
141-
3: "最宽松策略(重试3/3)",
142-
}
143-
return strategies.get(retry_count, strategies[0])
6+
_sys.modules[__name__] = import_module("pure_auto_codeql.prompts.codeql_prompts")

prompts/cpp_sink_prompt.py

Lines changed: 4 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,6 @@
1-
"""
2-
C++ sink analysis prompt template for UnifiedSinkPathAgent.
3-
"""
1+
"""Legacy re-export of `pure_auto_codeql.prompts.cpp_sink_prompt`."""
42

5-
from pathlib import Path
3+
from importlib import import_module
4+
import sys as _sys
65

7-
8-
def build_cpp_sink_prompt(cve_analysis: str, source_path: str, diff_path: str = "") -> str:
9-
"""构建C++ sink分析的提示词。"""
10-
return f"""你是一名资深的 CodeQL 安全研究员与 C/C++ 漏洞审计专家。
11-
12-
任务目标:基于提供的 CVE 分析、代码差异和文件路径,定位可能的漏洞接收点(Sink),并输出结构化的审计报告。
13-
14-
你的任务列表:
15-
**你的任务列表:**
16-
分析代码补丁 (Diff)
17-
18-
目标:将其作为定位Sink点的关键线索。
19-
20-
行动:分析补丁前后的代码变化,初步确定Sink点可能的位置。
21-
22-
使用searchfile工具定位目标文件!!!
23-
searchfile工具定位目标文件!!!
24-
searchfile工具定位目标文件!!!
25-
searchfile工具定位目标文件!!!
26-
27-
行动:根据 {source_path} 结合补丁分析,使用searchfile工具找到包含Sink点的具体源代码文件。
28-
29-
优化:不允许查看无关文件或者其他文件。
30-
31-
捷径:如果你已知道具体文件名(如 xxxx.java),请通过diff文件直接定位该文件。
32-
33-
读取并分析目标文件
34-
35-
**重要:对于大文件(>1000行),必须采用分段读取策略!**
36-
37-
步骤1:先使用ripgrep工具搜索关键函数名(从CVE分析或diff中提取,如unpack_tlv_router_cap、buffer、overflow等关键词),获取函数所在的行号范围。
38-
39-
步骤2:根据搜索结果,使用read_text_file工具的head或tail参数分段读取:
40-
- 如果函数在文件前半部分:使用head参数读取前N行(包含函数)
41-
- 如果函数在文件后半部分:使用tail参数读取后N行(包含函数)
42-
- 如果函数在中间:先读取头部(了解文件结构),再读取包含函数的关键区域
43-
- 每次读取后,根据内容决定是否需要继续读取其他区域
44-
45-
步骤3:严格根据diff文件中的代码更改,对比分析源代码,找出并确认Sink点。
46-
47-
核心:严格根据diff文件中的代码更改,对比分析源代码,找出并确认Sink点,一般Sink点就在diff对应的文件中,禁止再搜索其他文件!!!。
48-
49-
约束:禁止搜索与diff无关的文件。禁止一次性读取整个大文件(>1000行),必须分段读取。
50-
额外约束:调用文件搜索或内容搜索时,必须仅匹配 C/C++ 源文件(.c .cc .cpp .cxx .h .hh .hpp .hxx)。
51-
52-
生成分析报告
53-
54-
触发条件:一旦在源文件中根据diff更改的行确认Sink点存在。
55-
56-
行动:立即开始撰写分析报告。
57-
58-
**输入信息:**
59-
60-
1. **CVE分析结果**:
61-
```
62-
{cve_analysis}
63-
```
64-
65-
2. **代码差异文件路径**:
66-
```
67-
{diff_path}
68-
{source_path}
69-
```
70-
* **作用**: 这是定位Sink的关键线索。通过分析补丁前后的代码变化,找出sink点所在的具体位置,再根据{source_path}找到具体文件(尽量少地去看文件)去分析出sink点的具体信息。
71-
72-
3. **文件系统根目录 (MCP server-filesystem)**: `{Path.cwd() / 'projects'}`。所有工具访问的文件路径必须在该目录内,且以此为基准的相对路径。
73-
74-
**可用工具:**
75-
76-
* `ripgrep` (或 `search`): 用于快速搜索文件中的函数名、关键词,获取行号位置(**大文件分析前必须先使用此工具定位**)。
77-
- **重要**: ripgrep 工具使用相对于当前工作目录的路径。
78-
- **路径格式**: 使用相对于当前工作目录的路径,例如:`projects/CVE-2000-0973/source_code/...`
79-
- **如果遇到路径错误**: 可以先使用 `search_files` 工具找到文件的确切路径,然后再使用 ripgrep 搜索文件内容。
80-
- **替代方案**: 如果 ripgrep 工具路径有问题,可以直接使用 `read_text_file` 工具读取文件,然后手动搜索关键词。
81-
* `server-filesystem`: 用于读取文件内容(重要限制:只读取sink点所在的文件,不额外读取其他文件)。
82-
* 工具参数约束:
83-
- 调用 `read_text_file` 时,不能同时指定 `head` 与 `tail`;如需同时查看文件顶部与底部,请分两次分别读取。
84-
- **对于大文件(>1000行),必须先使用ripgrep搜索定位,再使用head/tail参数分段读取,避免一次性读取整个文件导致关键代码被截断。**
85-
86-
**输出格式 (必须严格遵守,不能有任何额外的注释或解释和多的标题):**
87-
88-
````markdown
89-
### Sink 定位报告:[在此填写 CVE 编号]
90-
91-
#### 1. 漏洞类型与风险概述
92-
- 描述:例如命令执行、任意文件写入、SQL 注入、反序列化执行、格式化字符串等
93-
94-
#### 2. Sink 位置清单
95-
- 文件路径:`[精确文件路径]`
96-
- 函数/方法:`[涉及 Sink 的函数或方法]`
97-
- 相关敏感 API:`[system/exec/write/strcpy/sql/模板等]`
98-
- 触发条件(若已知):`[输入来源或前置条件]`
99-
100-
#### 3. 代码片段(必要时)
101-
```c
102-
// 片段,避免长注释;必要时使用 "// SINK:" 标注关键点
103-
void vulnerable() {{
104-
// SINK: 在此标注关键调用点
105-
}}
106-
```
107-
108-
#### 4. 初步数据流说明
109-
- 一句话串联可能的来源到 Sink 的路径,例如:用户输入 -> 解析 -> 未验证 -> system/exec
110-
111-
#### 5. 备注
112-
- 未覆盖范围、可能的误报/漏报原因
113-
````
114-
115-
**执行规则:**
116-
* 你可以直接调用工具,无需事先征求同意。
117-
* 整个过程必须保持自主性,直接按步骤执行并输出最终报告。
118-
* 使用 `server-filesystem` 的 `read_text_file` 时,严格避免同时设置 `head` 与 `tail` 参数;需要两段内容时请分两次读取并在报告中标注来源。
119-
* 如果分析后无法明确找到Sink点,请在报告的"分析与理由"部分清楚地说明,并解释可能的原因(例如,漏洞逻辑复杂,关键代码不在提供的文件范围内等)。
120-
"""
6+
_sys.modules[__name__] = import_module("pure_auto_codeql.prompts.cpp_sink_prompt")

0 commit comments

Comments
 (0)