Skip to content

PM-971 Allow hyphen in url - asset library #1621

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

Merged
merged 8 commits into from
Mar 26, 2025
Merged

PM-971 Allow hyphen in url - asset library #1621

merged 8 commits into from
Mar 26, 2025

Conversation

himaniraghav3
Copy link
Collaborator

What's in this PR?

Changes the regex to allow hyphen in url in the form to add new links for Asset Library

@@ -58,7 +58,7 @@
/**
* regex for url validation
*/
const urlRegex = /((https?):\/\/)?(www.)?[a-z0-9]+(\.[a-z]{2,}){1,3}(#?\/?(?:[a-zA-Z0-9#]+))*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/
const urlRegex = /((https?):\/\/)?(www\.)?[\w-]+(\.[a-z]{2,}){1,3}(#?\/?(?:[a-zA-Z0-9#-]+))*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/

Check failure

Code scanning / CodeQL

Inefficient regular expression High

This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '#'.

Copilot Autofix

AI about 1 month ago

To fix the problem, we need to remove the ambiguity in the regular expression that can cause exponential backtracking. Specifically, we should modify the part [a-zA-Z0-9#-]+ to ensure that the character # is not matched in multiple ways. We can achieve this by using a more specific character class or by restructuring the regular expression to avoid ambiguous matches.

The best way to fix this without changing existing functionality is to replace [a-zA-Z0-9#-]+ with a more precise pattern that avoids ambiguity. For example, we can use [\w-]+ to match word characters and hyphens, and handle the # character separately if needed.

Suggested changeset 1
src/util/validation.js

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/util/validation.js b/src/util/validation.js
--- a/src/util/validation.js
+++ b/src/util/validation.js
@@ -60,3 +60,3 @@
  */
-const urlRegex = /((https?):\/\/)?(www\.)?[\w-]+(\.[a-z]{2,}){1,3}(#?\/?(?:[a-zA-Z0-9#-]+))*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/
+const urlRegex = /((https?):\/\/)?(www\.)?[\w-]+(\.[a-z]{2,}){1,3}(#?\/?(?:[\w-]+))*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/
 
EOF
@@ -60,3 +60,3 @@
*/
const urlRegex = /((https?):\/\/)?(www\.)?[\w-]+(\.[a-z]{2,}){1,3}(#?\/?(?:[a-zA-Z0-9#-]+))*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/
const urlRegex = /((https?):\/\/)?(www\.)?[\w-]+(\.[a-z]{2,}){1,3}(#?\/?(?:[\w-]+))*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/

Copilot is powered by AI and may make mistakes. Always verify output.
…ression

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

@vas3a vas3a left a comment

Choose a reason for hiding this comment

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

This is looking good to me. It's FE code. if someone wants to "hack" the regex, good for them, they'll block their browser window. no need to spend more time on this if it fixed the ticket iisue.

@himaniraghav3 himaniraghav3 merged commit c7d59d7 into develop Mar 26, 2025
1 of 2 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.

2 participants