Skip to content

fix: limit paste size to prevent UI freeze - #520

Open
liyigang1 wants to merge 1 commit into
linuxdeepin:masterfrom
liyigang1:agent/pms-bug-bot/efe0fea4b91d
Open

liyigang1 wants to merge 1 commit into
linuxdeepin:masterfrom
liyigang1:agent/pms-bug-bot/efe0fea4b91d

Conversation

@liyigang1

@liyigang1 liyigang1 commented Sep 15, 2026

Copy link
Copy Markdown

Root Cause Analysis

The onPaste() method in WebRichTextEditor (src/views/webrichtexteditor.cpp:537-566) passes clipboard content directly to the Chromium render engine via page()->triggerAction(QWebEnginePage::Paste) without any size limit. When a user pastes extremely large text (e.g. 50MB), Chromium's HTML parser and layout engine consume excessive resources, blocking the UI thread and causing the application to freeze. Key evidence: the entire codebase has length limits for titles (64 chars), folder names (64 chars), and audio duration (20 min), but no limit exists for note body content.

Fix Approach

Added MAX_NOTE_CONTENT_LEN (100,000 characters) constant in globaldef.h and size checks in all three paste paths within onPaste(): voice paste (isVoicePaste), voice HTML paste (HTML containing voiceBox class), and normal text paste (else branch). When the limit is exceeded, a ContentTooLong dialog is shown and the paste is aborted early, preventing the oversized content from reaching the Chromium render engine. A new ContentTooLong enum value and corresponding dialog message were added to vnotemessagedialog.h/.cpp.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • The onPaste() method signature is unchanged; the fix only adds early-return size checks before existing paste logic, with no modification to normal paste flow behavior
  • Blame history confirms onPaste was never previously modified for content size limits — this is purely additive, with no risk of reverting prior bug fixes (PMS 365817, 354073, 354083 are all unrelated to paste size logic)
  • Existing unit tests use small text content that will not trigger the new threshold, ensuring backward compatibility

Business Impact Scope

  • Note editor paste functionality — affects all paste operations (Ctrl+V, right-click menu) in the voice note editor
  • Normal-sized text paste, voice paste, and image paste are unaffected
  • Only oversized content (>100,000 characters) triggers the new limit dialog
  • User scenario: pasting 50MB text now shows a "content too long" dialog instead of freezing the application

Verification Suggestion

  1. Verify normal text paste (small and medium) works as before
  2. Verify voice paste and image paste are unaffected
  3. Verify pasting text exceeding 100,000 characters shows the "content too long" dialog and does not freeze

根因分析

WebRichTextEditoronPaste() 方法(src/views/webrichtexteditor.cpp:537-566)通过 page()->triggerAction(QWebEnginePage::Paste) 将剪贴板内容直接交给 Chromium 渲染引擎,无任何大小限制。当用户粘贴巨量文本(如 50MB)时,Chromium 的 HTML 解析和排版引擎消耗过多资源,阻塞 UI 线程导致应用卡死。关键证据:全项目对标题(64字符)、文件夹名(64字符)、音频时长(20分钟)均有长度限制,但笔记正文无任何限制。

修复方案

globaldef.h 中新增 MAX_NOTE_CONTENT_LEN(100,000 字符)常量,并在 onPaste() 的三条粘贴路径中增加大小校验:语音粘贴(isVoicePaste)、语音 HTML 粘贴(含 voiceBox 类的 HTML)、普通文本粘贴(else 分支)。超限时弹出 ContentTooLong 提示框并提前返回,阻止超大内容进入 Chromium 渲染引擎。同时在 vnotemessagedialog.h/.cpp 中新增 ContentTooLong 枚举值及对应提示文案。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • onPaste() 函数签名未变,修复仅在现有粘贴逻辑前增加 early-return 大小校验,不改变正常粘贴流程行为
  • blame 历史确认 onPaste 从未做过内容大小限制——本次为纯新增,不存在撤销历史修复的风险(PMS 365817、354073、354083 均与粘贴大小逻辑无关)
  • 现有单元测试使用小文本内容,不会触发新增阈值校验,向后兼容

业务影响范围

  • 笔记编辑区粘贴功能 — 影响语音记事本编辑区的所有粘贴操作(Ctrl+V、右键菜单)
  • 正常大小文本粘贴、语音粘贴、图片粘贴不受影响
  • 仅超大内容(>100,000 字符)触发新增限制提示框
  • 用户场景:粘贴 50MB 文本时弹出"内容过长"提示框,不再卡死应用

验证建议

  1. 验证正常文本粘贴(小段和中段)功能正常
  2. 验证语音粘贴和图片粘贴不受影响
  3. 验证粘贴超过 100,000 字符的文本时弹出"内容过长"提示框且不卡死

Summary by Sourcery

Limit pasted note content and provide a warning dialog when the maximum size is exceeded.

Bug Fixes:

  • Prevent the note editor from freezing when users paste excessively large content by rejecting pasted text over 100,000 characters.

Enhancements:

  • Apply the content-size safeguard consistently across voice, voice HTML, and regular text paste paths while leaving image pastes unaffected.

@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

@sourcery-ai

sourcery-ai Bot commented Sep 15, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR prevents UI freezes caused by very large pastes by enforcing a 100,000-character limit across all text-related paste paths, rejecting oversized content with a dedicated warning while preserving existing behavior for normal text, voice, and image pastes.

Sequence diagram for bounded paste handling

sequenceDiagram
    participant User
    participant Editor as WebRichTextEditor
    participant Clipboard
    participant Dialog as VNoteMessageDialog
    participant Chromium as QWebEnginePage
    participant JS as JsContent

    User->>Editor: onPaste(isVoicePaste)
    alt voice paste
        Editor->>Clipboard: text()
        alt text length > MAX_NOTE_CONTENT_LEN
            Editor->>Dialog: exec()
        else within limit
            Editor->>Chromium: triggerAction(Paste)
        end
    else clipboard paste
        Editor->>Clipboard: mimeData()
        alt voice HTML
            alt HTML length > MAX_NOTE_CONTENT_LEN
                Editor->>Dialog: exec()
            else within limit
                Editor->>JS: callJsPasteHtml(html)
            end
        else image
            Editor->>JS: insertImages(imageData)
        else text length > MAX_NOTE_CONTENT_LEN
            Editor->>Dialog: exec()
        else within limit
            Editor->>Chromium: triggerAction(Paste)
        end
    end
Loading

File-Level Changes

Change Details Files
Adds a centralized maximum note-content length and blocks oversized paste payloads before they reach the web rendering engine.
  • Defines a 100,000-character paste threshold.
  • Checks clipboard text for voice-paste, voice HTML, and normal-text paths.
  • Shows a modal warning and aborts the paste when the threshold is exceeded.
src/globaldef.h
src/views/webrichtexteditor.cpp
Introduces a dedicated user-facing message for rejected oversized paste content.
  • Adds the ContentTooLong dialog type.
  • Displays a warning explaining that the content was not pasted to prevent a possible application freeze.
src/dialog/vnotemessagedialog.h
src/dialog/vnotemessagedialog.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 reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

1. Root cause: onPaste() in WebRichTextEditor passes clipboard
   content directly to Chromium render engine without any size limit,
   pasting extremely large text (e.g. 50MB) causes UI thread freeze
2. Fix: add MAX_NOTE_CONTENT_LEN (100000 chars) check in all paste
   paths (voice paste, voice HTML paste, normal text paste), show
   ContentTooLong dialog and return early when limit is exceeded
3. Impact: normal-sized paste is unaffected, only oversized content
   triggers the limit dialog, preventing application freeze

Log: Prevent application freeze when pasting extremely large text

Influence:
1. Test normal text paste (small and medium) works as before
2. Test voice paste and image paste are unaffected
3. Test pasting text exceeding 100000 characters shows a dialog

fix: 限制粘贴内容大小防止界面卡死

1. 根因:WebRichTextEditor 的 onPaste() 将剪贴板内容直接交给
   Chromium 渲染,无内容大小限制,粘贴巨量文本(如50MB)导致UI线程阻塞
2. 方案:在所有粘贴路径(语音粘贴、语音HTML粘贴、普通文本粘贴)增加
   MAX_NOTE_CONTENT_LEN(100000字符)校验,超限时弹出 ContentTooLong
   提示框并返回,不再将超大内容交给渲染引擎
3. 影响:正常大小粘贴不受影响,仅超大内容触发限制提示框

Log: 防止粘贴巨量文本导致应用卡死

Influence:
1. 测试正常文本粘贴(小段和中段)功能正常
2. 测试语音粘贴和图片粘贴不受影响
3. 测试粘贴超过100000字符的文本时弹出提示框

PMS: BUG-261415
@liyigang1
liyigang1 force-pushed the agent/pms-bug-bot/efe0fea4b91d branch from da08ab7 to 0db94c7 Compare September 15, 2026 08:31
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 96 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 96 分,大于 70 分通过阈值,代码质量符合要求。本次提交通过添加粘贴内容大小限制有效修复了 UI 卡死问题,逻辑清晰,实现合理,仅存在少量代码重复和轻微性能优化空间。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 语法正确,逻辑清晰。所有粘贴路径的长度校验逻辑放置正确,提前返回防止超大内容进入 Chromium 渲染引擎。剪贴板获取代码移至函数开头是合理的重构,保证了所有路径都能访问 mimeData。


2. 代码质量 ✅

评价: 良好 ✅ 通过

潜在问题:

  1. src/views/webrichtexteditor.cpp:561 - 代码重复:相同的长度校验+对话框代码块在 onPaste() 中重复3次,建议提取为辅助函数

建议: 建议将重复的校验逻辑提取为辅助函数,例如:bool WebRichTextEditor::isContentTooLong(const QMimeData* mimeData) { if (mimeData->text().length() > MAX_NOTE_CONTENT_LEN) { VNoteMessageDialog dlg(VNoteMessageDialog::ContentTooLong); dlg.exec(); return true; } return false; },然后在各路径中调用 if (isContentTooLong(mimeData)) return; 以减少代码重复


3. 代码性能 ✅

评价: 优秀 ✅ 通过

潜在问题:

  1. src/views/webrichtexteditor.cpp:555 - mimeData->text() 会加载全部剪贴板文本到内存,建议先检查 hasText() 再调用 text()

建议: 在调用 text() 前,可以先通过 mimeData->hasText() 判断是否存在文本数据,避免对纯图片或语音数据进行不必要的 text() 调用。例如:if (mimeData->hasText() && mimeData->text().length() > MAX_NOTE_CONTENT_LEN) { ... }


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: 无安全风险。本次变更通过添加输入内容大小校验,增强了应用的健壮性,防止超大输入导致的拒绝服务问题。剪贴板内容仅用于长度比较,未传递给任何危险操作。


💡 改进建议代码示例

// 建议重构:提取重复的校验逻辑为辅助函数
bool WebRichTextEditor::isContentTooLong(const QMimeData* mimeData)
{
    if (mimeData->hasText() && mimeData->text().length() > MAX_NOTE_CONTENT_LEN) {
        VNoteMessageDialog contentTooLong(VNoteMessageDialog::ContentTooLong);
        contentTooLong.exec();
        return true;
    }
    return false;
}

// 在 onPaste() 各路径中调用:
void WebRichTextEditor::onPaste(bool isVoicePaste)
{
    QClipboard *clipboard = QApplication::clipboard();
    const QMimeData *mimeData = clipboard->mimeData();

    if (isVoicePaste) {
        if (isContentTooLong(mimeData)) return;
        return page()->triggerAction(QWebEnginePage::Paste);
    }

    auto html = mimeData->html();
    if (html.contains(QRegularExpression("<div class=\"[^\"]*voiceBox"))) {
        if (isContentTooLong(mimeData)) return;
        JsContent::instance()->callJsPasteHtml(html);
        return;
    }
    // ...
    } else {
        if (isContentTooLong(mimeData)) return;
        page()->triggerAction(QWebEnginePage::Paste);
    }
}

本报告由 AI 代码审查工具自动生成

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