Skip to content

fix(route): safe decode path with % #2434

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 5 commits into
base: main
Choose a base branch
from

Conversation

holyfata
Copy link

@holyfata holyfata commented Aug 2, 2025

Summary

Related Issue

#2372

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@Copilot Copilot AI review requested due to automatic review settings August 2, 2025 02:26
Copy link

netlify bot commented Aug 2, 2025

Deploy Preview for rspress ready!

Name Link
🔨 Latest commit c728fd5
🔍 Latest deploy log https://app.netlify.com/projects/rspress/deploys/68904cf7a7469b00080cb333
😎 Deploy Preview https://deploy-preview-2434--rspress.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 88 (🟢 up 11 from production)
Accessibility: 97 (no change from production)
Best Practices: 92 (🟢 up 9 from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@CLAassistant
Copy link

CLAassistant commented Aug 2, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

netlify bot commented Aug 2, 2025

Deploy Preview for rspress-v2 ready!

Name Link
🔨 Latest commit c728fd5
🔍 Latest deploy log https://app.netlify.com/projects/rspress-v2/deploys/68904cf7462c7a0008537587
😎 Deploy Preview https://deploy-preview-2434--rspress-v2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where URLs containing invalid percent-encoded characters (like %dex) would cause decodeURIComponent to throw an error. The fix introduces a safe decoding function that handles malformed percent encodings gracefully.

  • Adds safeDecodeURIComponent function to handle malformed percent-encoded URLs
  • Replaces direct decodeURIComponent usage in normalizeHref with the safe version
  • Adds test coverage for URLs with invalid percent encoding

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/shared/src/runtime-utils/utils.ts Implements safe URL decoding function and integrates it into normalizeHref
packages/shared/src/runtime-utils/utils.test.ts Adds test case for URL with invalid percent encoding

SoonIter
SoonIter previously approved these changes Aug 4, 2025
Copy link
Member

@SoonIter SoonIter left a comment

Choose a reason for hiding this comment

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

thanks❤

// decodeURIComponent will throw error if the url is not valid
// this function will replace the invalid characters with %25
export function safeDecodeURIComponent(uri: string) {
return decodeURIComponent(uri.replace(/%/g, '%25'));
Copy link
Collaborator

@JounQin JounQin Aug 4, 2025

Choose a reason for hiding this comment

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

Doesn't it change the meaning of original %*? For example if I have a url with %20 which represents a space , after this change, a url contains %20 will be changed as %2520...

cc @SoonIter

Copy link
Author

Choose a reason for hiding this comment

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

I think this restores the original meaning of the %. For decodeURI, % is an escape character, but in the case of a%b.md, it simply represents the % character itself. To make decodeURI recognize the original meaning of this character, we need to replace % with %25 before using decodeURI.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It makes %20 unavailable anymore.

Copy link
Author

Choose a reason for hiding this comment

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

I was thinking too simply. I will try to filter all % encodings.

Copy link
Author

Choose a reason for hiding this comment

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

Here is my final solution

  • Processing strategy:
      1. First attempt direct decoding, return result if successful
      1. If failed, check each %xx sequence individually:
        • Check if it's a valid hexadecimal format
        • Try to decode the specific %xx sequence
        • If decoding fails (e.g., invalid UTF-8 sequence), replace with %25xx
        • If format is incorrect, replace % with %25
      1. Finally decode the fixed string

Copy link
Member

Choose a reason for hiding this comment

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

sorry, this algorithm is too expensive for us to maintain...

Do other frameworks have this problem, how do they solve it, and are there any available packages already?

@holyfata holyfata requested a review from JounQin August 4, 2025 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants