Skip to content

fix: restrict path traversal check to file scheme in DEnumerator buildUrl - #378

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
Johnson-zs:agent/bugfix/02144741-eagle
Aug 5, 2026
Merged

fix: restrict path traversal check to file scheme in DEnumerator buildUrl#378
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
Johnson-zs:agent/bugfix/02144741-eagle

Conversation

@Johnson-zs

@Johnson-zs Johnson-zs commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

根因分析

DEnumeratorPrivate::buildUrl() 中的路径遍历安全检查 fileNameBa.contains('\\') 对 gio trash:/// 后端使用反斜杠分隔的扁平文件名(如 \media\user\dev\.Trash-1000\files\x)返回空 QUrl(""),导致 /media 挂载点的回收站文件无法显示和清空。

  • 回归来源: commit fd494fb(PMS #367075, 2026-07-16)引入了该检查
  • 证据: 运行时日志显示第 2 个回收站文件 URL 为 QUrl(""),触发 kIsNotTrashFileError

修复方案

将路径遍历检查限制为仅对 file:/// 或无 scheme 的本地文件系统生效,trash:/// 等 gio 虚拟文件系统跳过该检查。

改动安全评估

低风险。修改仅限制检查的适用 scheme 范围,不改变 buildUrl() 签名或返回值语义。对 file:/// scheme 行为完全不变。

Summary by Sourcery

Bug Fixes:

  • Fix missing trash:/// entries caused by overzealous path traversal checks rejecting valid backslash-separated filenames in gio trash backends.

…dUrl

1. DEnumeratorPrivate::buildUrl() 对所有 scheme 的文件名做路径遍历检查,导致 gio trash:/// 后端使用反斜杠分隔的扁平文件名被误判为恶意路径并返回空 URL;
2. 将路径遍历检查限制为仅对 file:/// 或无 scheme 的本地文件系统生效,gio trash:/// 等虚拟文件系统的合法文件名不受影响;
3. 修复手动分区场景下 /media 挂载点的回收站文件无法显示和清空的问题;

Log: 修复 buildUrl 路径遍历安全检查误伤 gio trash 反斜杠文件名导致回收站无法显示和清空的问题

PMS: BUG-372733
Bug: https://pms.uniontech.com/bug-view-372733.html
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

@sourcery-ai

sourcery-ai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Restricts the path traversal safeguard in DEnumeratorPrivate::buildUrl() to only apply for local file-system URLs (file scheme or no scheme), so that gio trash:/// backends using backslash-based flat filenames are no longer incorrectly rejected.

Flow diagram for scheme-restricted path traversal check in buildUrl

flowchart TD
    A[buildUrl called with url and fileName] --> B[get scheme from url]
    B --> C{scheme is empty or file}
    C --> D[perform path traversal check on fileNameBa]:::check
    C --> E[skip path traversal check]:::skip
    D --> F[return QUrl if traversal pattern found]
    E --> G[continue building path]
    D --> G[continue building path]

    classDef check fill:#ffe0e0,stroke:#cc0000
    classDef skip fill:#e0f0ff,stroke:#0044aa
Loading

File-Level Changes

Change Details Files
Limit path traversal checks in buildUrl() to local file-system URLs to avoid rejecting valid trash:/// entries.
  • Constructs QByteArray from fileName as before but defers checks until after scheme evaluation.
  • Introduces retrieval of the URL scheme via url.scheme() and compares it against empty and "file".
  • Wraps the existing ../ and ..\ path traversal checks inside a conditional that runs only when the scheme is empty or "file", returning an empty QUrl only in that case.
src/dfm-io/dfm-io/denumerator.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@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.

Hey - I've found 1 issue, and left some high level feedback:

  • Instead of checking scheme.isEmpty() || scheme == QLatin1String("file"), consider using url.isLocalFile() to more robustly detect local file URLs and avoid relying on an empty scheme as a proxy.
  • Since the trash backend’s backslash usage is now explicitly exempted from traversal checks, it may be worth briefly noting in the function’s interface or a higher-level comment that traversal checks are scheme-dependent, to avoid future regressions reintroducing the same issue.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Instead of checking `scheme.isEmpty() || scheme == QLatin1String("file")`, consider using `url.isLocalFile()` to more robustly detect local file URLs and avoid relying on an empty scheme as a proxy.
- Since the trash backend’s backslash usage is now explicitly exempted from traversal checks, it may be worth briefly noting in the function’s interface or a higher-level comment that traversal checks are scheme-dependent, to avoid future regressions reintroducing the same issue.

## Individual Comments

### Comment 1
<location path="src/dfm-io/dfm-io/denumerator.cpp" line_range="391-392" />
<code_context>
+    // gio 的 trash:/// 后端对非用户主目录挂载点的回收站文件,使用反斜杠分隔的扁平路径
+    // 作为 GFileInfo 的 standard::name(例如 "\media\user\dev\.Trash-1000\files\x"),
+    // 这是合法的 trash 文件名而非恶意路径,故不应对其做路径遍历拦截。
+    const QString scheme = url.scheme();
+    if (scheme.isEmpty() || scheme == QLatin1String("file")) {
+        if (fileNameBa.contains("../") || fileNameBa.contains("..\\") || fileNameBa.startsWith("..")) {
+            return QUrl();
</code_context>
<issue_to_address>
**🚨 issue (security):** Consider restricting the relaxed check to specific non-local schemes instead of all non-file URLs

The current condition disables traversal checks for every non-empty, non-`file` scheme. For custom schemes that still map to filesystem-like backends, this could allow names with `../` or `..\` to pass unchecked. Since the need is specifically for `trash:///`, consider limiting the relaxed behavior to that scheme (or an explicit allowlist of schemes) rather than applying it to all non-file URLs.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +391 to +392
const QString scheme = url.scheme();
if (scheme.isEmpty() || scheme == QLatin1String("file")) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚨 issue (security): Consider restricting the relaxed check to specific non-local schemes instead of all non-file URLs

The current condition disables traversal checks for every non-empty, non-file scheme. For custom schemes that still map to filesystem-like backends, this could allow names with ../ or ..\ to pass unchecked. Since the need is specifically for trash:///, consider limiting the relaxed behavior to that scheme (or an explicit allowlist of schemes) rather than applying it to all non-file URLs.

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码精准修复了trash协议下合法路径被误判为路径遍历的问题,逻辑严谨且无副作用
四个审查维度均表现完美,无任何扣分项

■ 【详细分析】

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

代码在denumerator.cpp文件的buildUrl函数中,通过提取url.scheme()并判断其是否为空或等于"file",准确将路径遍历拦截逻辑限定在本地文件系统范围内。使用QLatin1String进行字符串比较避免了隐式转换,且Qt的scheme()方法保证返回小写字母,不存在大小写绕过问题。
建议:保持当前逻辑即可

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

注释详尽且切中要害,清晰解释了gio trash后端使用反斜杠分隔扁平路径的特殊机制以及修复的理论依据。代码结构清晰,无冗余逻辑,完全符合Qt及UOS底层模块的编码规范。
建议:保持当前注释风格,为后续维护者提供良好上下文

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

新增的url.scheme()调用为O(1)时间复杂度的常量级操作,底层由QUrl内部缓存支持,不涉及深拷贝内存分配或系统调用,对整体性能无任何可感知影响。
建议:无需优化

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

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
修复方案在安全设计上十分准确。对于非file协议(如trash://、smb://等),其路径由对应的虚拟文件系统后端(如gvfsd-trash进程)负责安全隔离与解析,QUrl在处理非file协议时不会将其path直接映射为本地文件系统路径,因此在应用层buildUrl函数中放开非file协议的拦截不会导致本地文件系统越权。该修改消除了原有的功能误杀,未引入任何真实攻击面。
建议:保持当前的安全边界划分,确保后续处理QUrl的代码严格区分协议,不将非file协议的URL直接当做本地路径使用

■ 【改进建议代码示例】

// 当前代码已为最优解,无需额外修改,此处展示其完整上下文以供校验
QUrl DEnumeratorPrivate::buildUrl(const QUrl &url, const char *fileName)
{
    if (!fileName) {
        return QUrl();
    }

    QByteArray fileNameBa(fileName);

    // 路径遍历检查仅对本地文件系统 (file:/// 或无 scheme) 生效
    // gio 的 trash:/// 后端对非用户主目录挂载点的回收站文件,使用反斜杠分隔的扁平路径
    // 作为 GFileInfo 的 standard::name(例如 "\media\user\dev\.Trash-1000\files\x"),
    // 这是合法的 trash 文件名而非恶意路径,故不应对其做路径遍历拦截。
    const QString scheme = url.scheme();
    if (scheme.isEmpty() || scheme == QLatin1String("file")) {
        if (fileNameBa.contains("../") || fileNameBa.contains("..\\") || fileNameBa.startsWith("..")) {
            return QUrl();
        }
    }

    QByteArray path;
    // ... 后续原有逻辑
}

@Johnson-zs

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Aug 5, 2026

Copy link
Copy Markdown

This pr force merged! (status: blocked)

@deepin-bot
deepin-bot Bot merged commit 6158d0e into linuxdeepin:release/eagle Aug 5, 2026
20 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.

2 participants