Skip to content

fix: 存在潜在的 ReDoS 漏洞或低效正则表达式 #17619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mmmsssttt404
Copy link

@mmmsssttt404 mmmsssttt404 commented Apr 30, 2025

这个 PR 做了什么? (简要描述所做更改)
报告项目中潜在正则表达式拒绝服务 (ReDoS) 漏洞或低效的正则表达式。在上下文中使用特制的输入字符串时,可能会导致极高的 CPU 使用率、应用程序冻结或拒绝服务攻击。

value = value.replace(/(\d+)px/g, '$1PX')

return value.replace(/(\d*\.?\d+)rem/g, (match, m1) => parseFloat(m1, 10) * 16 + 'px')

将正则改为

https://github.com/mmmsssttt404/taro/blob/196bc7a5b880890c3894c85c37977098bb75c5dd/packages/css-to-react-native/src/index.js#L62

https://github.com/mmmsssttt404/taro/blob/e41414ff8e402e8366bf3476fc658ce470bcd21b/packages/css-to-react-native/src/transforms/rem.js#L2

(?<!\d)(\d+)px
(?<!\d)((?:\d*\.\d+)|\d+)rem

改之前 测试指令后程序一直运行

屏幕截图 2025-04-29 225932

改之后

{3EFF0494-EBBC-4ECB-9C2C-508908EDDE54}

步骤
1.git clone https://gitclone.com/github.com/mmmsssttt404/taro.git
2.cd tero
3.pnpm i
4.pnpm build
5.pnpm --filter taro-css-to-react-native test

这个 PR 是什么类型? (至少选择一个)

  • 错误修复(Bugfix) issue: fix #
  • 新功能(Feature)
  • 代码重构(Refactor)
  • TypeScript 类型定义修改(Typings)
  • 文档修改(Docs)
  • 代码风格更新(Code style update)
  • 其他,请描述(Other, please describe):

这个 PR 涉及以下平台:

  • 所有小程序
  • 微信小程序
  • 支付宝小程序
  • 百度小程序
  • 字节跳动小程序
  • QQ 轻应用
  • 京东小程序
  • 快应用平台(QuickApp)
  • Web 平台(H5)
  • 移动端(React-Native)
  • 鸿蒙(harmony)

Summary by CodeRabbit

  • 测试
    • 新增了针对大数值 CSS 解析性能的测试,确保在极端情况下处理速度小于 1 秒。
  • 修复
    • 优化了 px 和 rem 单位的正则表达式匹配,避免在长数字串中出现误匹配,提升了单位转换的准确性。

Copy link

coderabbitai bot commented Apr 30, 2025

## Walkthrough

本次更新主要针对 CSS 单位处理的正则表达式进行了增强,优化了 px 和 rem 单位的匹配逻辑,防止正则表达式被滥用导致性能问题(ReDos)。同时,新增了一个测试用例,用于验证 transform 函数在处理极大数值时的性能表现,确保其在 1 秒内完成处理。所有更改均未影响公共 API 的声明,仅涉及内部实现和测试。

## Changes

| 文件/路径                                                         | 变更摘要                                                         |
|-------------------------------------------------------------------|------------------------------------------------------------------|
| packages/css-to-react-native/src/index.js                         | 更新 px 单位的正则表达式,加入负向前瞻,防止误匹配和性能问题。    |
| packages/css-to-react-native/src/transforms/rem.js                | 更新 rem 单位的正则表达式,加入负向前瞻,增强匹配准确性。         |
| packages/css-to-react-native/__tests__/index.spec.js              | 新增 ReDos 测试套件,验证 transform 在极端输入下的性能。          |

## Sequence Diagram(s)

```mermaid
sequenceDiagram
    participant Test as ReDos Test
    participant Transform as transform()
    participant Regex as Regex Engine

    Test->>Transform: 传入超长数值的 CSS 字符串
    Transform->>Regex: 使用新正则处理 px/rem 单位
    Regex-->>Transform: 返回处理结果
    Transform-->>Test: 返回处理耗时

Poem

🐇
正则升级巧防御,
px、rem 皆分明。
巨数入测不慌乱,
秒内转换真轻盈。
代码如风守安全,
兔子鼓掌笑盈盈!


<!-- walkthrough_end -->
<!-- internal state start -->


<!-- DwQgtGAEAqAWCWBnSTIEMB26CuAXA9mAOYCmGJATmriQCaQDG+Ats2bgFyQAOFk+AIwBWJBrngA3EsgEBPRvlqU0AgfFwA6NPEgQAfACgjoCEYDEZyAAUASpETZWaCrKNxU3bABsvkCiQBHbGlcSHFcLzpIACIAM3gADy5ADW1ACnVAHXtUwBC3SBsSABF8AGVIQHh7QDxbQDQjQDl5QAlTQG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wwXvveGC0CxDf6TA/RD5d4GC179z16N6/zN6ghHa/ztz6BAA= -->

<!-- internal state end -->
<!-- finishing_touch_checkbox_start -->

<details open="true">
<summary>✨ Finishing Touches</summary>

- [ ] <!-- {"checkboxId": "7962f53c-55bc-4827-bfbf-6a18da830691"} --> 📝 Generate Docstrings

</details>

<!-- finishing_touch_checkbox_end -->
<!-- tips_start -->

---

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=NervJS/taro&utm_content=17619):

- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
  - `I pushed a fix in commit <commit_id>, please review it.`
  - `Generate unit testing code for this file.`
  - `Open a follow-up GitHub issue for this discussion.`
- Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples:
  - `@coderabbitai generate unit testing code for this file.`
  -	`@coderabbitai modularize this function.`
- PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
  - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.`
  - `@coderabbitai read src/utils.ts and generate unit testing code.`
  - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.`
  - `@coderabbitai help me debug CodeRabbit configuration file.`

### Support

Need help? Create a ticket on our [support page](https://www.coderabbit.ai/contact-us/support) for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

### CodeRabbit Commands (Invoked using PR comments)

- `@coderabbitai pause` to pause the reviews on a PR.
- `@coderabbitai resume` to resume the paused reviews.
- `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
- `@coderabbitai full review` to do a full review from scratch and review all the files again.
- `@coderabbitai summary` to regenerate the summary of the PR.
- `@coderabbitai generate docstrings` to [generate docstrings](https://docs.coderabbit.ai/finishing-touches/docstrings) for this PR.
- `@coderabbitai generate sequence diagram` to generate a sequence diagram of the changes in this PR.
- `@coderabbitai resolve` resolve all the CodeRabbit review comments.
- `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository.
- `@coderabbitai help` to get help.

### Other keywords and placeholders

- Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed.
- Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description.
- Add `@coderabbitai` anywhere in the PR title to generate the title automatically.

### CodeRabbit Configuration File (`.coderabbit.yaml`)

- You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository.
- Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json`

### Documentation and Community

- Visit our [Documentation](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->

This was referenced May 3, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
packages/css-to-react-native/__tests__/index.spec.js (4)

3641-3641: 建议遵循项目的分号使用规范

根据静态分析提示,此行有多余的分号。虽然这不影响功能,但建议保持代码风格一致性。

-  jest.setTimeout(1000);
+  jest.setTimeout(1000)
🧰 Tools
🪛 ESLint

[error] 3641-3641: Extra semicolon.

(semi)


3642-3644: 建议移除多余的空行

根据静态分析提示,代码块不应该有空行填充。建议移除块内的空行以保持代码风格一致性。

  it('should finish transform of 100k zeros + "p" in under 1s', () => {
-

    const bigValue = '0'.repeat(100000) + 'p';
+
+   const bigValue = '0'.repeat(100000) + 'p'
🧰 Tools
🪛 ESLint

[error] 3642-3644: Block must not be padded by blank lines.

(padded-blocks)


[error] 3644-3644: Extra semicolon.

(semi)


3651-3656: 建议统一移除末尾分号,保持代码风格一致

根据静态分析提示,这些行有多余的分号。为保持代码风格一致性,建议移除。

-    const start = performance.now();
+    const start = performance.now()
     transform(cssString, {
       scalable: false,
       parseMediaQueries: false
-    });
-    const duration = performance.now() - start;
-    expect(duration).toBeLessThan(1000);
+    })
+    const duration = performance.now() - start
+    expect(duration).toBeLessThan(1000)
🧰 Tools
🪛 ESLint

[error] 3654-3654: Extra semicolon.

(semi)


[error] 3655-3655: Extra semicolon.

(semi)


[error] 3656-3656: Extra semicolon.

(semi)


3640-3657: 性能测试策略是有效的,但可以考虑更全面的测试方法

这个测试有效验证了修复后的正则表达式对潜在 ReDoS 攻击的防御能力。不过,考虑到正则表达式的复杂性,可以添加更多边缘情况的测试,如不同模式的重复字符或特殊字符组合,以确保修复的全面性。

可以考虑添加多种类型的潜在恶意输入测试,例如:

  1. 带有大量重复字符的边缘情况
  2. 具有不同模式重复的字符串
  3. 包含正则表达式中特殊字符的长字符串

这将使测试更加全面,确保正则表达式优化在各种情况下都有效。

🧰 Tools
🪛 ESLint

[error] 3641-3641: Extra semicolon.

(semi)


[error] 3642-3644: Block must not be padded by blank lines.

(padded-blocks)


[error] 3644-3644: Extra semicolon.

(semi)


[error] 3648-3648: Extra semicolon.

(semi)


[error] 3650-3650: Extra semicolon.

(semi)


[error] 3654-3654: Extra semicolon.

(semi)


[error] 3655-3655: Extra semicolon.

(semi)


[error] 3656-3656: Extra semicolon.

(semi)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 196bc7a and 91b1f57.

📒 Files selected for processing (3)
  • packages/css-to-react-native/__tests__/index.spec.js (1 hunks)
  • packages/css-to-react-native/src/index.js (1 hunks)
  • packages/css-to-react-native/src/transforms/rem.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/css-to-react-native/src/index.js
  • packages/css-to-react-native/src/transforms/rem.js
🧰 Additional context used
🪛 ESLint
packages/css-to-react-native/__tests__/index.spec.js

[error] 3641-3641: Extra semicolon.

(semi)


[error] 3642-3644: Block must not be padded by blank lines.

(padded-blocks)


[error] 3644-3644: Extra semicolon.

(semi)


[error] 3648-3648: Extra semicolon.

(semi)


[error] 3650-3650: Extra semicolon.

(semi)


[error] 3654-3654: Extra semicolon.

(semi)


[error] 3655-3655: Extra semicolon.

(semi)


[error] 3656-3656: Extra semicolon.

(semi)

🔇 Additional comments (1)
packages/css-to-react-native/__tests__/index.spec.js (1)

3639-3658: 新增的 ReDos 测试套件有效验证了正则表达式优化

这个测试套件很好地验证了正则表达式优化的效果,确保处理极大数值(10万个零后跟"p")的性能在1秒内完成。这与PR中修复的潜在ReDoS漏洞直接相关,是一个很好的回归测试。

🧰 Tools
🪛 ESLint

[error] 3641-3641: Extra semicolon.

(semi)


[error] 3642-3644: Block must not be padded by blank lines.

(padded-blocks)


[error] 3644-3644: Extra semicolon.

(semi)


[error] 3648-3648: Extra semicolon.

(semi)


[error] 3650-3650: Extra semicolon.

(semi)


[error] 3654-3654: Extra semicolon.

(semi)


[error] 3655-3655: Extra semicolon.

(semi)


[error] 3656-3656: Extra semicolon.

(semi)

@Single-Dancer
Copy link
Collaborator

@mmmsssttt404 同学修复一下 lint 问题

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