forked from nlohmann/json
-
Notifications
You must be signed in to change notification settings - Fork 0
testreport2 #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joyfaker
wants to merge
1
commit into
develop
Choose a base branch
from
testreport2
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
testreport2 #18
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #include <iostream> | ||
| #include <cstring> | ||
| #include <vector> | ||
| #include <string> | ||
| #include <cstdlib> | ||
| #include <ctime> | ||
|
|
||
| using namespace std; | ||
|
|
||
| // 1. 硬编码敏感信息 (CWE-798) | ||
| // SAST 会扫描变量名(如 api_key, secret)和高熵字符串 | ||
| const string AWS_SECRET_KEY = "AKIAIOSFODNN7EXAMPLE"; | ||
|
|
||
| class UserManager { | ||
| public: | ||
| // 2. 也是硬编码,且作为类成员 | ||
| string db_password = "root"; | ||
|
|
||
| // 3. SQL 注入 (CWE-89) | ||
| // 直接拼接字符串构建 SQL 查询是 C++ Web 后端常见的严重漏洞 | ||
| void queryUser(string userId) { | ||
| string query = "SELECT * FROM users WHERE id = '" + userId + "'"; | ||
| cout << "Executing query: " << query << endl; | ||
| } | ||
|
|
||
| // 4. 命令注入 (CWE-78) | ||
| // 允许外部输入直接进入 system() 函数 | ||
| void pingHost(string ipAddress) { | ||
| string cmd = "ping -c 4 " + ipAddress; | ||
| system(cmd.c_str()); | ||
| } | ||
| }; | ||
|
|
||
| void legacyBufferOverflow(char* input) { | ||
| char buffer[10]; | ||
|
|
||
| // 5. 经典的栈缓冲区溢出 (CWE-121) | ||
| // 虽然是 C++,但混用 C 风格字符串处理函数非常常见且危险 | ||
| strcpy(buffer, input); | ||
| } | ||
|
|
||
| void memoryLeakAndRawPointers() { | ||
| // 6. 内存泄漏 (CWE-401) | ||
| // 使用了 new 但没有 delete | ||
| int* data = new int[100]; | ||
| data[0] = 10; | ||
|
|
||
| // 抛出异常可能导致 delete 永远不执行 (异常安全问题) | ||
| if (data[0] == 10) { | ||
| // throw runtime_error("Error occurred!"); | ||
| return; | ||
| } | ||
|
|
||
| delete[] data; | ||
| } | ||
|
|
||
| void iteratorInvalidation() { | ||
| vector<int> numbers = {1, 2, 3, 4, 5}; | ||
|
|
||
| // 7. 迭代器失效 (CWE-835/Logic Error) | ||
| // 在遍历 vector 时进行 push_back 会导致底层数组重新分配, | ||
| // 从而使迭代器失效,导致未定义行为或崩溃。 | ||
| for (auto it = numbers.begin(); it != numbers.end(); ++it) { | ||
| if (*it == 3) { | ||
| numbers.push_back(6); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void weakRandomness() { | ||
| // 8. 弱伪随机数生成器 (CWE-338) | ||
| // srand/rand 不适合用于安全相关的随机数生成 | ||
| srand(time(NULL)); | ||
| int token = rand(); | ||
| cout << "Security Token: " << token << endl; | ||
| } | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| UserManager um; | ||
|
|
||
| if (argc < 2) { | ||
| return 1; | ||
| } | ||
|
|
||
| // 模拟攻击路径 | ||
| um.queryUser(argv[1]); // 传入 "' OR '1'='1" 即可注入 | ||
|
|
||
| legacyBufferOverflow(argv[1]); | ||
|
|
||
| memoryLeakAndRawPointers(); | ||
|
|
||
| iteratorInvalidation(); | ||
|
|
||
| weakRandomness(); | ||
|
|
||
| // 9. 被除数为零 (CWE-369) | ||
| int x = 0; | ||
| int y = 100 / x; | ||
|
|
||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 AI 代码审查发现问题
📋 问题概述
📍 问题详情
🟡 问题 1 | 严重程度:
MEDIUM| 行号:1-10💬 详细说明:
ex_wundo和ex_rundo声明为static,但 line 8464 和 8474 处的函数定义缺少static关键字。C 语言标准规定,若函数先被声明为静态链接,后续定义也必须为静态,否则构成约束违规(constraint violation),将直接导致编译失败。📝 问题代码:
💡 修复建议:
✅ 修复示例:
🔗 参考链接
无