Skip to content

Conversation

@SoonIter
Copy link
Member

@SoonIter SoonIter commented Apr 22, 2025

close #10

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

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

@JounQin JounQin Apr 22, 2025

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,

…ession

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…ession

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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 High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '--'.

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

Check failure

Code scanning / CodeQL

Inefficient regular expression High

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

Copilot Autofix

AI 8 months 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