fix(cli): use HOME env instead of hardcoded "/home/" path prefix check#1718
Conversation
dengbo11
commented
Jul 2, 2026
- Replace hardcoded "/home/" string prefix check with HOME environment variable to support non-standard home directories
- Add null and empty string checks for HOME env to handle edge cases
- Append trailing slash to HOME path before prefix matching to avoid false positive on paths like /home/bob-xxx
- Fix minor indentation on LogE call
There was a problem hiding this comment.
Code Review
This pull request replaces the hardcoded '/home' path check with a dynamic check using the 'HOME' environment variable to avoid mapping files under the user's home directory. The review feedback correctly identifies an edge case where the home directory itself (without a trailing slash) would fail the prefix check and be incorrectly mapped, and offers a code suggestion to fix this issue.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| auto homeStr = std::string(homePath); | ||
| if (homeStr.back() != '/') { | ||
| homeStr.push_back('/'); | ||
| } | ||
| if (auto tmp = target.string(); tmp.rfind(homeStr, 0) == 0) { | ||
| return target; | ||
| } |
There was a problem hiding this comment.
If the target path is exactly the user's home directory (e.g., /home/bob), appending a trailing slash to homeStr (making it /home/bob/) will cause the prefix check tmp.rfind(homeStr, 0) == 0 to fail. As a result, the home directory itself will be incorrectly mapped to /run/host/rootfs/home/bob.
To fix this, we should strip any trailing slashes from the configured HOME path first, and then check if the target path is either exactly equal to the home directory or starts with the home directory followed by a slash.
auto homeStr = std::string(homePath);
while (homeStr.size() > 1 && homeStr.back() == '/') {
homeStr.pop_back();
}
if (auto tmp = target.string(); tmp == homeStr || tmp.rfind(homeStr == "/" ? "/" : homeStr + "/", 0) == 0) {
return target;
}
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
- Replace hardcoded "/home/" string prefix check with HOME environment variable to support non-standard home directories - Add null and empty string checks for HOME env to handle edge cases - Append trailing slash to HOME path before prefix matching to avoid false positive on paths like /home/bob-xxx - Fix minor indentation on LogE call
deepin pr auto review★ 总体评分:60分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 #include <pwd.h>
#include <unistd.h>
// 获取真实的用户主目录,不依赖可伪造的环境变量
struct passwd *pw = getpwuid(getuid());
if (pw == nullptr || pw->pw_dir == nullptr || pw->pw_dir[0] == '\0') {
LogE("failed to get real user home directory");
return target;
}
// Don't map files under the user's home directory
auto homeStr = std::string(pw->pw_dir);
if (homeStr.back() != '/') {
homeStr.push_back('/');
}
if (auto tmp = target.string(); tmp.rfind(homeStr, 0) == 0) {
return target;
} |
|
@dengbo11: The following test failed, say
Full PR test history. Your PR dashboard. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: dengbo11, reddevillg The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |