Skip to content

fix(webrichtexteditor): add image drag-and-drop validation#344

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pppanghu77:master
Apr 28, 2026
Merged

fix(webrichtexteditor): add image drag-and-drop validation#344
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pppanghu77:master

Conversation

@pppanghu77
Copy link
Copy Markdown
Contributor

  • Implement MIME type checking to only accept image files (jpg, png, bmp)
  • Add URL validation logic to filter valid image formats during drag operations
  • Update dragEnterEvent and dragMoveEvent handlers to properly handle image file validation before accepting proposed actions
  • Prevent non-image files from being processed in the rich text editor Log: fix(webrichtexteditor): add image drag-and-drop validation Bug: https://pms.uniontech.com/bug-view-354083.html

@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这份代码的修改意图是限制 WebRichTextEditor 只接受图片文件的拖放操作,这是一个很好的功能增强,有助于提升用户体验和控件的安全性。

以下是对这段代码的审查意见,包括语法逻辑、代码质量、代码性能和代码安全方面的改进建议:

1. 代码逻辑与功能

  • 逻辑正确性:代码逻辑基本正确,能够区分 URL(文件路径)和直接的图像数据,并过滤非图片文件。
  • 扩展性:硬编码了 "jpg", "png", "bmp" 三种格式。如果未来需要支持 gifwebpjpeg,需要修改多处代码。建议将支持的格式定义为常量或配置。
  • 重复代码dragEnterEventdragMoveEvent 中的验证逻辑完全一致。这违反了 DRY(Don't Repeat Yourself)原则。如果逻辑需要调整(例如增加格式),需要同时修改两个地方,容易出错。

2. 代码质量

  • 可读性:目前的 if-else 嵌套和循环结构略显冗长,可以提取为一个私有辅助函数来提高可读性。
  • 宏的使用:原代码中使用了 Q_UNUSED(e),新代码移除了它,这是正确的,因为 e 现在被使用了。

3. 代码性能

  • 循环效率:使用了 break 语句,一旦发现有效图片就立即停止循环,这是一个好的优化,避免了不必要的遍历。
  • 文件后缀获取QFileInfo(url.path()).suffix() 涉及字符串操作和文件系统路径解析。在拖拽移动事件 (dragMoveEvent) 中,如果用户快速移动鼠标,可能会频繁触发此逻辑。虽然对于少量文件影响不大,但仍有优化空间(见下文改进建议)。

4. 代码安全

  • 路径解析url.path() 返回的是 URL 的路径部分。在某些操作系统或特殊 URL 构造下,直接使用 QFileInfo 解析可能存在边缘情况,但在处理本地文件拖拽时通常是安全的。
  • 安全性增强:仅仅检查后缀名是不够的。恶意用户可以将可执行文件重命名为 .jpg。虽然这里只是决定是否接受拖拽(acceptignore),真正的文件处理应该在 dropEvent 中进行,但在 dropEvent 中务必要再次验证文件内容的实际类型(Magic Number),而不仅仅是后缀。

改进建议与重构代码

建议将验证逻辑提取为一个私有函数 isAcceptableDrop,并使用 QStringList 管理支持的格式。

修改后的代码示例:

// 在头文件 WebRichTextEditor.h 的 private 部分声明
private:
    bool isAcceptableDrop(const QMimeData *mimeData) const;
    static const QStringList s_supportedImageFormats; // 声明静态常量

// 在源文件 WebRichTextEditor.cpp 中定义和实现
const QStringList WebRichTextEditor::s_supportedImageFormats = {"jpg", "jpeg", "png", "bmp", "gif"};

bool WebRichTextEditor::isAcceptableDrop(const QMimeData *mimeData) const
{
    if (mimeData->hasImage()) {
        return true;
    }

    if (mimeData->hasUrls()) {
        const QList<QUrl> urls = mimeData->urls();
        for (const QUrl &url : urls) {
            // 使用 isLocalFile 检查更安全,path() 在本地文件上表现更好
            if (url.isLocalFile()) {
                const QString suffix = QFileInfo(url.toLocalFile()).suffix().toLower();
                if (s_supportedImageFormats.contains(suffix)) {
                    return true; // 只要有一个有效文件即接受
                }
            }
        }
    }
    
    return false;
}

// 修改后的 dragEnterEvent
void WebRichTextEditor::dragEnterEvent(QDragEnterEvent *event)
{
    if (isAcceptableDrop(event->mimeData())) {
        event->acceptProposedAction();
    } else {
        event->ignore();
    }
}

// 修改后的 dragMoveEvent
void WebRichTextEditor::dragMoveEvent(QDragMoveEvent *e)
{
    if (isAcceptableDrop(e->mimeData())) {
        e->acceptProposedAction();
    } else {
        e->ignore();
    }
}

详细改进点说明:

  1. 消除重复:创建了 isAcceptableDrop 函数,dragEnterEventdragMoveEvent 只需调用该函数,代码量大幅减少,维护性提高。
  2. 配置化格式:使用 static const QStringList s_supportedImageFormats 集中管理支持的图片格式。修改格式只需改动一处,并增加了 jpeggif 支持。
  3. 更安全的 URL 处理:使用 url.isLocalFile()url.toLocalFile() 替代 url.path()toLocalFile() 会处理 URL 编码并转换为操作系统特定的路径分隔符,比 path() 更健壮。
  4. 算法简化:使用 QStringList::contains 替代多个 || 判断,代码更简洁。
  5. 关于 dropEvent 的提示:虽然 diff 中未包含 dropEvent,请确保在 dropEvent 中再次验证文件类型(例如通过 QImageReader::canRead()),以防止后缀名欺骗带来的安全隐患。

- Implement MIME type checking to only accept image files (jpg, png, bmp)
- Add URL validation logic to filter valid image formats during drag operations
- Update dragEnterEvent and dragMoveEvent handlers to properly handle
  image file validation before accepting proposed actions
- Prevent non-image files from being processed in the rich text editor
Log: fix(webrichtexteditor): add image drag-and-drop validation
Bug: https://pms.uniontech.com/bug-view-354083.html
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: max-lvs, pppanghu77

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

@pppanghu77
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented Apr 28, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit 034f84a into linuxdeepin:master Apr 28, 2026
18 of 21 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