Skip to content

feat: add transformWin32RelativePath#15

Open
SoonIter wants to merge 4 commits into
mainfrom
syt/add-beforeSerialize-afterSerialize
Open

feat: add transformWin32RelativePath#15
SoonIter wants to merge 4 commits into
mainfrom
syt/add-beforeSerialize-afterSerialize

Conversation

@SoonIter

@SoonIter SoonIter commented Apr 22, 2025

Copy link
Copy Markdown
Member

close #10

@SoonIter
SoonIter force-pushed the syt/add-beforeSerialize-afterSerialize branch from cfa3a99 to cbe265e Compare April 22, 2025 09:32
Comment thread src/normalize.ts Outdated

export function normalizeWin32RelativePath(p: string): string {
return p.replace(
/(['"`])(\.\.([\\/]))+([\w-]+\3)+[^\\/]*\1/g,

@JounQin JounQin Apr 22, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Oh, this was just my temporary workaround for that issue, it didn't cover one level sibling relative path: ./.

Suggested change
/(['"`])(\.\.([\\/]))+([\w-]+\3)+[^\\/]*\1/g,
/(['"`])(\.\.?([\\/]))+([\w-]+\3?)+[^\\/]*\1/g,

Comment thread src/normalize.ts Fixed
Comment thread src/normalize.ts Fixed
…ession

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread src/normalize.ts Fixed
Comment thread src/normalize.ts Fixed
…ession

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread src/normalize.ts
Comment on lines +23 to +28
return p.replace(
/(['"`])(\.\.?([\\/]))+((?:[^\W_]|-)+\3?)+[^\\/]*\1/g,
(match: string) => {
return match.replace(/\\/g, '/');
},
);

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data

This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of '--'.
Comment thread src/normalize.ts

export function normalizeWin32RelativePath(p: string): string {
return p.replace(
/(['"`])(\.\.?([\\/]))+((?:[^\W_]|-)+\3?)+[^\\/]*\1/g,

Check failure

Code scanning / CodeQL

Inefficient regular expression

This part of the regular expression may cause exponential backtracking on strings starting with '"./' and containing many repetitions of '-'.

Copilot Autofix

AI about 1 year ago

To fix the issue, we need to remove the ambiguity in the regular expression. The problematic part (?:[^\W_]|-)+ can be rewritten to explicitly match either alphanumeric characters or hyphens without ambiguity. This can be achieved by replacing [^\W_] with a more specific character class, such as [a-zA-Z0-9], and ensuring that the hyphen - is handled separately. The updated regular expression will avoid exponential backtracking while maintaining the intended functionality.

The specific change will be made on line 24 of src/normalize.ts.


Suggested changeset 1
src/normalize.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/normalize.ts b/src/normalize.ts
--- a/src/normalize.ts
+++ b/src/normalize.ts
@@ -23,3 +23,3 @@
   return p.replace(
-    /(['"`])(\.\.?([\\/]))+((?:[^\W_]|-)+\3?)+[^\\/]*\1/g,
+    /(['"`])(\.\.?([\\/]))+([a-zA-Z0-9-]+\3?)+[^\\/]*\1/g,
     (match: string) => {
EOF
@@ -23,3 +23,3 @@
return p.replace(
/(['"`])(\.\.?([\\/]))+((?:[^\W_]|-)+\3?)+[^\\/]*\1/g,
/(['"`])(\.\.?([\\/]))+([a-zA-Z0-9-]+\3?)+[^\\/]*\1/g,
(match: string) => {
Copilot is powered by AI and may make mistakes. Always verify output.
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.

Relative paths are not transformed

3 participants