Skip to content

Fix regex validator crash on invalid patterns in jquery.validate.unobtrusive.js#67025

Open
ishaq2321 wants to merge 1 commit into
dotnet:mainfrom
ishaq2321:fix/jquery-validate-regex-crash
Open

Fix regex validator crash on invalid patterns in jquery.validate.unobtrusive.js#67025
ishaq2321 wants to merge 1 commit into
dotnet:mainfrom
ishaq2321:fix/jquery-validate-regex-crash

Conversation

@ishaq2321

@ishaq2321 ishaq2321 commented Jun 4, 2026

Copy link
Copy Markdown

Fixes #67028

Summary

The regex custom validator in jquery.validate.unobtrusive.js would throw a SyntaxError when new RegExp(params) encountered an invalid regex pattern (e.g., [, *, unbalanced parentheses). This caused all form validation on the page to crash.

Fix

Wrapped the new RegExp() call in a try-catch block. When an invalid pattern is encountered, validation is skipped (returns true) rather than crashing. Server-side validation at form submission time continues to serve as the fallback layer.

Changes

  • src/Identity/UI/src/assets/V4/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js: Added try-catch around new RegExp(params).exec(value)
  • src/Identity/UI/src/assets/V4/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js: Equivalent minified fix

Finding Source

Found via bb_security static analysis tool during ASP.NET Core codebase review.

Co-authored-by: Muhammad Ishaq Khan muhammadishaqkhan.2321@gmail.com

Copilot AI review requested due to automatic review settings June 4, 2026 23:22
@github-actions github-actions Bot added the area-identity Includes: Identity and providers label Jun 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates the unobtrusive jQuery Validation regex rule to avoid throwing runtime errors when an invalid regex pattern is provided, preventing client-side validation from crashing.

Changes:

  • Wrapped new RegExp(params) usage in try/catch inside the regex validator method.
  • Simplified the return condition to explicit null checks and boolean operators.
  • Added comments explaining the fallback behavior when the regex pattern is invalid.

Comment on lines +347 to +354
try {
var match = new RegExp(params).exec(value);
return match !== null && match.index === 0 && match[0].length === value.length;
} catch (e) {
// Invalid regex pattern - skip validation to avoid crashing all form validation.
// Server-side validation at form submission time provides the fallback.
return true;
}
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 4, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @ishaq2321. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@ishaq2321

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@ishaq2321 ishaq2321 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you for the review. We considered this carefully and believe returning true is the correct approach for this specific scenario:

  1. The regex pattern originates from the server — it comes from the [RegularExpression] data annotation attribute, which is validated at compile/build time in ASP.NET Core. An invalid pattern would fail during app startup, not at runtime on the client.

  2. In the edge case where a malformed pattern reaches the browser, returning false means all legitimate user input gets rejected — a worse UX than skipping client-side validation. The browser would show validation errors for every input, confusing users.

  3. Server-side validation is the authoritative layer — The server re-validates at form submission time regardless of what the client does. Skipping client-side validation simply means the server becomes the single validation point for that specific field.

  4. The original bug was catastrophic — Without this fix, a single malformed pattern crashed ALL validation for the entire form. This fix eliminates that crash while preserving the most important validation layer (server).

@ishaq2321 ishaq2321 force-pushed the fix/jquery-validate-regex-crash branch from a9f3809 to 020b952 Compare June 5, 2026 00:17
…trusive.js

The 'regex' custom validator in jquery.validate.unobtrusive.js would
throw a SyntaxError when the regex pattern from data-val-regex-pattern
was invalid (e.g., unbalanced brackets or malformed patterns).
This caused the entire form validation to crash with no recovery.

This change wraps the new RegExp() call in a try-catch block so that
invalid patterns skip validation instead of crashing. Server-side
validation at form submission time continues to serve as the fallback
layer for malformed patterns.

Fixes dotnet#67028
Co-authored-by: Muhammad Ishaq Khan <muhammadishaqkhan.2321@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-identity Includes: Identity and providers community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix regex validator crash on invalid patterns in jquery.validate.unobtrusive.js

2 participants