Skip to content

fix: add isMountedByGio to check GVfs mount status#313

Closed
liyigang1 wants to merge 1 commit into
linuxdeepin:masterfrom
liyigang1:master
Closed

fix: add isMountedByGio to check GVfs mount status#313
liyigang1 wants to merge 1 commit into
linuxdeepin:masterfrom
liyigang1:master

Conversation

@liyigang1

Copy link
Copy Markdown
Contributor

Add DNetworkMounter::isMountedByGio which uses GIO API to query whether a network URL is mounted via GVfs, returning the mount point. Also integrate the check in mountByDaemon to avoid duplicate mounting.

添加 isMountedByGio 函数,通过 GIO API 检查 GVfs 挂载状态,
并在 mountByDaemon 中集成该检查,避免重复挂载。

Log: 新增 isMountedByGio 函数检查 GVfs 挂载状态
Bug: https://pms.uniontech.com/bug-view-367163.html
Influence: 挂载网络设备时增加 GVfs 挂载检测,避免因 GVfs 已挂载导致重复挂载失败。

@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

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 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

@liyigang1 liyigang1 force-pushed the master branch 5 times, most recently from b27a6f6 to dbda171 Compare June 23, 2026 13:24
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:60分

■ 【总体评价】

代码通过GIO检测修复了网络挂载重复挂载的问题,但存在URI规范化逻辑错误、同步阻塞风险及凭据泄露漏洞
逻辑错误和性能阻塞扣25分,因存在安全漏洞触发最高60分上限规则

■ 【详细分析】

  • 1.语法逻辑(存在错误)✕
    DNetworkMounter::mountByDaemon 函数中,调用 isMountedByGio(address.toLower(), mpt) 时对 URI 执行了 toLower() 操作。根据 RFC 3986 规范,URI 中的百分号编码是区分大小写的(例如 %2F%2f 代表不同的字符)。强制转换为小写会破坏 URI 编码,导致 g_file_new_for_uri 接收到畸形的 URI,从而无法正确匹配到已由 GIO 挂载的卷,使得修复逻辑失效。
    潜在问题:URI 规范化错误导致无法检测到已挂载状态,修复补丁在特定路径下失效
    建议:移除 isMountedByGio 调用时的 toLower(),直接传入原始的 address 字符串,由 GIO 内部处理 URI 的解析与匹配
  • 2.代码质量(一般)✕
    isMountedByGio 函数中的错误日志打印直接输出了传入的 url 参数。在网络挂载场景中,URI 常常包含明文凭据信息(如 smb://user:password@host/share),直接打印会暴露敏感信息,且缺乏对该新增函数的注释说明。
    潜在问题:日志中可能包含明文密码等敏感凭据,且新增函数缺少注释
    建议:在打印日志前对 URI 进行脱敏处理,移除 userinfo 部分,并增加函数头部注释说明其用途与回退机制
  • 3.代码性能(存在性能问题)✕
    isMountedByGio 函数中调用 g_file_find_enclosing_mount(file, nullptr, &error) 时,将 GCancellable 参数设为 nullptr。该 API 是同步阻塞调用,如果目标网络地址对应的服务器无响应或网络不通,此调用会长时间阻塞甚至无限期挂起,导致调用线程被阻塞。
    潜在问题:无响应的网络地址会导致线程长时间阻塞,可能引发界面卡顿或守护进程假死
    建议:创建并传入一个 GCancellable 对象,并配合超时机制(如使用 g_timeout_add 或异步 API)来防止无限期阻塞
  • 4.代码安全(存在 1 个安全漏洞)✕
    漏洞对比统计:新增漏洞 1 个,减少漏洞 0 个,持平 0 个
    代码在日志输出时未过滤敏感信息,存在凭据泄露风险

  • 安全漏洞1(低危):敏感信息泄露 在 DNetworkMounter::isMountedByGio 函数中,当 g_file_find_enclosing_mount 失败时,通过 qWarning() << "gio: cannot find enclosing mount for" << url 直接将完整的网络地址打印到日志中。如果该 URL 包含用户名和密码(如 ftp://user:passwd@ip),攻击者通过读取本地日志文件即可获取明文凭据,导致横向移动风险 ——非常重要

  • 建议:使用 QUrl 解析 url,在日志中仅输出 scheme()host()path(),剥离 userInfo() 部分

■ 【改进建议代码示例】

diff --git a/src/dfm-mount/private/dnetworkmounter.cpp b/src/dfm-mount/private/dnetworkmounter.cpp
index 6ad2fca..safe_mount 100644
--- a/src/dfm-mount/private/dnetworkmounter.cpp
+++ b/src/dfm-mount/private/dnetworkmounter.cpp
@@ -323,7 +323,7 @@ void DNetworkMounter::mountByDaemon(const QString &address, GetMountPassInfo get
 
     QString mpt;
     QString addr(QUrl::fromPercentEncoding(address.toLower().toLocal8Bit()));
-    if (isMounted(addr, mpt) || isMountedByGio(address.toLower(), mpt)) {
+    if (isMounted(addr, mpt) || isMountedByGio(address, mpt)) {
         if (mountResult)
             mountResult(false, Utils::genOperateErrorInfo(DeviceError::kGIOErrorAlreadyMounted), mpt);
         return;
@@ -691,15 +691,25 @@ bool DNetworkMounter::isMounted(const QString &address, QString &mpt)
         return false;
     }
 }
+
+// Detect if the network URI is already mounted by GIO/GVFS to avoid duplicate mount errors
+bool DNetworkMounter::isMountedByGio(const QString &url, QString &mpt)
+{
+    GFile_autoptr file = g_file_new_for_uri(url.toStdString().c_str());
+    if (!file)
+        return false;
+
+    GError_autoptr error = nullptr;
+    GCancellable *cancellable = g_cancellable_new();
+    GMount_autoptr mount = g_file_find_enclosing_mount(file, cancellable, &error);
+    g_object_unref(cancellable);
+    if (!mount) {
+        QUrl parsedUrl(url);
+        QString safeUrl = QString("%1://%2%3").arg(parsedUrl.scheme(), parsedUrl.host(), parsedUrl.path());
+        qWarning() << "gio: cannot find enclosing mount for" << safeUrl << (error ? error->message : "");
+        return false;
+    }
+
+    // Prefer the default location (e.g., the mount's "home" directory)
+    GFile_autoptr defLocation = g_mount_get_default_location(mount);
+    if (defLocation) {
+        g_autofree char *path = g_file_get_path(defLocation);
+        if (path) {
+            mpt = QString::fromUtf8(path);
+            return true;
+        }
+    }
+
+    // Fallback to the mount root
+    GFile_autoptr root = g_mount_get_root(mount);
+    if (root) {
+        g_autofree char *path = g_file_get_path(root);
+        if (path) {
+            mpt = QString::fromUtf8(path);
+            return true;
+        }
+    }
+
+    return false;
+}

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/零宽不换行空格的路径能正确拼接,避免文件操作失败。
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