Skip to content

fix(clerk-js): Incomplete string escaping or encoding within password complexity #5786

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-zoos-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Fix incomplete string escaping or encoding within password complexity.
4 changes: 2 additions & 2 deletions packages/clerk-js/src/utils/passwords/complexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
let specialCharsRegex: RegExp;
if (config.allowed_special_characters) {
// Avoid a nested group by escaping the `[]` characters
let escaped = config.allowed_special_characters.replace('[', '\\[');
escaped = escaped.replace(']', '\\]');
let escaped = config.allowed_special_characters.replace(/\[/g, '\\[');

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI 2 months ago

To fix the issue, we need to ensure that backslashes in the allowed_special_characters string are properly escaped before constructing the regular expression. This can be achieved by adding a replace call to escape backslashes (\) before escaping square brackets ([ and ]). The updated code will replace each backslash with a double backslash (\\), ensuring that the resulting regex is valid and behaves as intended.

The changes will be made in the createTestComplexityCases function, specifically in the block where allowed_special_characters is processed (lines 15–18). No additional imports or dependencies are required.


Suggested changeset 1
packages/clerk-js/src/utils/passwords/complexity.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/packages/clerk-js/src/utils/passwords/complexity.ts b/packages/clerk-js/src/utils/passwords/complexity.ts
--- a/packages/clerk-js/src/utils/passwords/complexity.ts
+++ b/packages/clerk-js/src/utils/passwords/complexity.ts
@@ -14,4 +14,5 @@
   if (config.allowed_special_characters) {
-    // Avoid a nested group by escaping the `[]` characters
-    let escaped = config.allowed_special_characters.replace(/\[/g, '\\[');
+    // Avoid a nested group by escaping backslashes and the `[]` characters
+    let escaped = config.allowed_special_characters.replace(/\\/g, '\\\\');
+    escaped = escaped.replace(/\[/g, '\\[');
     escaped = escaped.replace(/\]/g, '\\]');
EOF
@@ -14,4 +14,5 @@
if (config.allowed_special_characters) {
// Avoid a nested group by escaping the `[]` characters
let escaped = config.allowed_special_characters.replace(/\[/g, '\\[');
// Avoid a nested group by escaping backslashes and the `[]` characters
let escaped = config.allowed_special_characters.replace(/\\/g, '\\\\');
escaped = escaped.replace(/\[/g, '\\[');
escaped = escaped.replace(/\]/g, '\\]');
Copilot is powered by AI and may make mistakes. Always verify output.
escaped = escaped.replace(/\]/g, '\\]');

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI 2 months ago

To fix the issue, we need to ensure that backslashes in the allowed_special_characters string are properly escaped before constructing the regular expression. This can be achieved by adding an additional replace call to escape backslashes (\) before escaping square brackets ([ and ]). The g flag should be used in the regular expression to ensure all occurrences are replaced.

The updated code will:

  1. First escape all backslashes by replacing each \ with \\.
  2. Then escape square brackets ([ and ]) as before.

This ensures that the resulting string is safe to use in a regular expression.


Suggested changeset 1
packages/clerk-js/src/utils/passwords/complexity.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/packages/clerk-js/src/utils/passwords/complexity.ts b/packages/clerk-js/src/utils/passwords/complexity.ts
--- a/packages/clerk-js/src/utils/passwords/complexity.ts
+++ b/packages/clerk-js/src/utils/passwords/complexity.ts
@@ -15,3 +15,4 @@
     // Avoid a nested group by escaping the `[]` characters
-    let escaped = config.allowed_special_characters.replace(/\[/g, '\\[');
+    let escaped = config.allowed_special_characters.replace(/\\/g, '\\\\');
+    escaped = escaped.replace(/\[/g, '\\[');
     escaped = escaped.replace(/\]/g, '\\]');
EOF
@@ -15,3 +15,4 @@
// Avoid a nested group by escaping the `[]` characters
let escaped = config.allowed_special_characters.replace(/\[/g, '\\[');
let escaped = config.allowed_special_characters.replace(/\\/g, '\\\\');
escaped = escaped.replace(/\[/g, '\\[');
escaped = escaped.replace(/\]/g, '\\]');
Copilot is powered by AI and may make mistakes. Always verify output.
specialCharsRegex = new RegExp(`[${escaped}]`);
} else {
specialCharsRegex = /[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/;
Expand Down