Skip to content

fix(search): decode base64 keywords before updating search#622

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:master
May 22, 2026
Merged

fix(search): decode base64 keywords before updating search#622
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:master

Conversation

@dengzhongyuan365-dev

@dengzhongyuan365-dev dengzhongyuan365-dev commented May 21, 2026

Copy link
Copy Markdown
Member
  • Updated the search functionality to decode base64 encoded keywords before passing them to the search update method. This ensures proper handling of encoded search terms in the application.

bug: https://pms.uniontech.com/bug-view-362267.html

lzwind
lzwind previously approved these changes May 21, 2026
- Updated the search functionality to decode base64 encoded keywords before passing them to the search update method. This ensures proper handling of encoded search terms in the application.

bug: https://pms.uniontech.com/bug-view-362267.html
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev

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

1 similar comment
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev

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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeeX,你的智能编程助手。我已经仔细审查了你提供的 Git Diff 记录。

本次代码变更主要做了两件事:一是更新了版权年份至 2026;二是在处理 search 逻辑时,增加了对 Base64 编码关键词的解码功能(先 Base64 解码,再 URL 解码),并将打包后的代码同步更新。

以下是我对本次代码变更在语法逻辑、代码质量、代码性能和代码安全方面的详细审查意见及改进建议:

1. 语法与逻辑

  • Base64 与 URL 编码的顺序逻辑
    代码中的解码逻辑是 decodeURIComponent(atob(keyword)),这意味着原数据是先 URL 编码,再 Base64 编码的。请确认这与调用方的编码顺序是否一致。如果调用方是先 Base64 编码再 URL 编码,那么这里的解码顺序应该反过来,即 atob(decodeURIComponent(keyword))
  • atob 对 Unicode 字符的处理缺陷
    JavaScript 原生的 atob() 方法只能正确解码 Latin1 (ISO-8859-1) 字符。如果搜索关键词包含中文或其他非 ASCII 字符(如 "中文"),通常的编码方式是 btoa(encodeURIComponent(str))。此时如果直接使用 atob() 解码,会得到乱码的 URI 编码字符串,虽然后续经过 decodeURIComponent 可以还原,但这依赖于 atob 不抛出异常。
    更严重的是,如果 Base64 解码后的二进制串不符合 UTF-8 规范,decodeURIComponent() 会直接抛出 URIError 异常,导致程序崩溃。
  • this 指向问题(在打包代码中)
    App.js 源码中,你使用了 this.isbase64(keyword)。但在 React 的 Class 组件中,如果 isbase64 不是通过箭头函数定义或在构造函数中 bind 过,在复杂的调用链或异步操作中可能会丢失 this 上下文。从打包后的代码 _this4.isbase64(keyword) 可以看出,Babel 已经处理了上下文,但建议在源码中确认 isbase64 方法的绑定情况。

2. 代码质量

  • 松散的相等比较
    代码中使用了 list[1] == 'search'。在 JavaScript 中,建议始终使用严格相等运算符 ===,以避免隐式类型转换带来的不可预期行为。
  • 方法命名不规范
    this.isbase64 违反了 JavaScript 的小驼峰命名规范。建议改为 this.isBase64
  • 遗留的调试代码
    console.log("============>search...", list[2]); 属于调试代码。如果是生产环境,建议移除,或使用统一的日志管理工具控制输出级别,避免在控制台暴露用户搜索的敏感关键词。

3. 代码性能

  • 正则表达式性能
    虽然本次 Diff 没有展示 isbase64 的实现,但通常判断 Base64 会使用正则表达式(如 /^[A-Za-z0-9+/]+={0,2}$/)。如果该正则表达式没有使用 ^$ 限定首尾,或者过于复杂,可能会引发回溯问题,影响性能。请确保正则表达式的精准和高效。
  • 不必要的解码操作
    如果传入的关键词绝大多数情况下都不是 Base64 编码的,那么每次都执行 isbase64 正则校验会带来额外的 CPU 开销。建议在调用方(如果是你们自己的客户端)明确传入一个标识位(如 isEncoded=true),而不是在前端去“猜测”是否编码过。

4. 代码安全

  • 异常处理缺失(致命问题)
    atob(keyword) 在遇到格式不正确的 Base64 字符串时会抛出 DOMException 异常;decodeURIComponent() 在遇到格式错误的转义序列时会抛出 URIError。当前代码没有 try...catch 保护,一旦恶意用户构造一段非法字符串作为搜索词,前端应用将直接崩溃(白屏)。
  • XSS 与注入风险
    keyword 经过 decodeURIComponent(atob(keyword)) 解码后,如果直接交给 global.qtObjects.search.updateSearch 处理,且底层使用了 innerHTML 或类似的不安全 DOM 操作,可能会导致 XSS 漏洞。请确保 updateSearch 内部对解码后的字符串进行了安全的转义处理。
  • 敏感信息泄露
    console.log 打印了用户的搜索关键词,这可能在开发者工具中暴露用户的搜索隐私。

🌟 改进后的代码建议

针对以上问题,我为你重构了这部分代码,增加了健壮的异常处理,修复了命名规范,并移除了调试代码:

// 建议将 isbase64 重命名为 isBase64,并确保其内部正则高效准确
// 示例:isBase64 = (str) => /^[A-Za-z0-9+/]+={0,2}$/.test(str);

} else if (list[1] === 'search') { // 使用严格相等 ===
    // 移除 console.log,避免敏感信息泄露
    let keyword = list[2];
    
    // 使用 try-catch 包裹解码逻辑,防止非法字符串导致页面崩溃
    if (this.isBase64(keyword)) { // 修正方法名小驼峰命名
        try {
            // 注意:请确认编码顺序!
            // 如果原逻辑是先 URL 编码再 Base64 编码,则保持下面这行
            keyword = decodeURIComponent(atob(keyword));
            
            // 如果原逻辑是先 Base64 编码再 URL 编码,请替换为下面这行
            // keyword = atob(decodeURIComponent(keyword));
        } catch (error) {
            console.error('Failed to decode search keyword:', error);
            // 解码失败时,降级使用原始关键词,或者赋空值,保证程序不崩溃
            keyword = list[2]; 
        }
    }
    
    // 确保传入的 keyword 是安全的字符串
    global.qtObjects.search.updateSearch(keyword);
} else {
    // ...
}

总结:本次改动业务逻辑上没有大问题,但缺乏异常保护是致命的,极易被恶意输入击溃。请务必加上 try-catch,并严格测试包含中英文混合、特殊符号(如 %, +, /)的搜索词在 Base64 编解码场景下的表现。

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot

deepin-bot Bot commented May 22, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: blocked)

@deepin-bot deepin-bot Bot merged commit 721a24d into linuxdeepin:master May 22, 2026
18 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