Skip to content

fix: prevent BOM character loss in path concatenation#317

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
liyigang1:master
Jun 25, 2026
Merged

fix: prevent BOM character loss in path concatenation#317
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
liyigang1:master

Conversation

@liyigang1

Copy link
Copy Markdown
Contributor

Use std::string for directory path concatenation to avoid QString's normalization of UTF-8 BOM (U+FEFF / zero-width no-break space).

使用 std::string 进行路径拼接,避免 QString 对 UTF-8 BOM
(零宽不换行空格) 的规范化导致字节丢失。

Log: 修复路径拼接时 BOM 字符丢失的问题
Bug: https://pms.uniontech.com//bug-view-367075.html
Influence: 修复后包含 BOM/零宽不换行空格的路径能正确拼接,避免文件操作失败。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @liyigang1, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@liyigang1 liyigang1 force-pushed the master branch 2 times, most recently from 6935030 to 86e004e Compare June 24, 2026 06:27
Use std::string for directory path concatenation to avoid QString's
normalization of UTF-8 BOM (U+FEFF / zero-width no-break space).

使用 std::string 进行路径拼接,避免 QString 对 UTF-8 BOM
(零宽不换行空格) 的规范化导致字节丢失。

Log: 修复路径拼接时 BOM 字符丢失的问题
Bug: https://pms.uniontech.com//bug-view-367075.html
Influence: 修复后包含 BOM/零宽不换行空格的路径能正确拼接,避免文件操作失败。
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码修复了空指针解引用、路径遍历和BOM头丢失问题,防御逻辑严密且性能无损
代码质量优秀,注释清晰,完全符合安全编码规范,仅因极端边界字符处理未做显式防御扣5分

■ 【详细分析】

  • 1.语法逻辑(正确)✓

修复前直接使用未检查的 const char *fileName 构造 QString 存在崩溃风险,修复后增加了空指针判断 if (!fileName) 并提前返回空 QUrl。同时增加了 urlPath.isEmpty() 判断,覆盖了 URL 路径为空的边界情况,逻辑严密。
潜在问题:当 url.path() 包含非 UTF-8 编码的非法 Unicode 字符时,toUtf8() 会静默替换为 U+FFFD,可能导致生成的绝对路径与底层文件系统实际路径不一致
建议:在调用 toUtf8() 前可增加 urlPath.toUtf8().toPercentEncoding() 或确认 URL 来源的合法性,防止非法字符破坏路径

  • 2.代码质量(优秀)✓

代码结构清晰,将 QString 替换为 QByteArray 进行底层字节数组拼接,从根本上解决了 QString 构造时自动剥离 BOM 头(efbbbf)的隐式行为。关键防御逻辑处增加了详细注释,说明了防御目的(防空指针、防路径遍历、防 BOM 剥离),可维护性强。
潜在问题:无
建议:无

  • 3.代码性能(高效)✓

使用 QByteArray 直接进行字节数组拼接,避免了 QString 内部复杂的 Unicode 转换和隐式共享开销。增加的 contains 检查为 O(N) 复杂度,由于文件名长度通常极短,对整体性能无影响。
潜在问题:无
建议:无

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码成功修复了原有的空指针解引用崩溃风险、路径遍历越权漏洞以及 BOM 头丢失导致的路径错误问题。通过严格校验 fileName 禁止包含 /\...,在 Linux 文件系统环境下有效阻断了目录遍历攻击面。

  • 建议:保持当前的安全防御策略,后续若需支持特殊文件系统可针对性扩展校验逻辑

■ 【改进建议代码示例】

--- a/src/dfm-io/dfm-io/denumerator.cpp
+++ b/src/dfm-io/dfm-io/denumerator.cpp
@@ -476,7 +476,8 @@ QUrl DEnumeratorPrivate::buildUrl(const QUrl &url, const char *fileName)
         path = QByteArray("/") + fileNameBa;
     } else {
-        QByteArray dirPath = urlPath.toUtf8();
+        // 使用 toPercentEncoding 处理可能存在的非 UTF-8 字符,防止路径被静默篡改
+        QByteArray dirPath = urlPath.toUtf8().toPercentEncoding("/", "");
         if (!dirPath.endsWith('/')) {
             dirPath.append('/');
         }

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Johnson-zs, liyigang1

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Johnson-zs

Copy link
Copy Markdown
Contributor

/merge

@deepin-bot deepin-bot Bot merged commit 52aa590 into linuxdeepin:master Jun 25, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants