10000 Core: fix race condition in remote validation rules by bidord · Pull Request #2435 · jquery-validation/jquery-validation · GitHub
[go: up one dir, main page]

Skip to content

Core: fix race condition in remote validation rules #2435

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 1 commit into from
Jul 15, 2022
Merged
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
17 changes: 11 additions & 6 deletions src/ajax.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Ajax mode: abort
// usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
// $.ajaxAbort( port );
// if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()

var pendingRequests = {},
Expand All @@ -10,9 +11,7 @@ if ( $.ajaxPrefilter ) {
$.ajaxPrefilter( function( settings, _, xhr ) {
var port = settings.port;
if ( settings.mode === "abort" ) {
if ( pendingRequests[ port ] ) {
pendingRequests[ port ].abort();
}
$.ajaxAbort( port );
pendingRequests[ port ] = xhr;
}
} );
Expand All @@ -24,12 +23,18 @@ if ( $.ajaxPrefilter ) {
var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode,
port = ( "port" in settings ? settings : $.ajaxSettings ).port;
if ( mode === "abort" ) {
if ( pendingRequests[ port ] ) {
pendingRequests[ port ].abort();
}
$.ajaxAbort( port );
pendingRequests[ port ] = ajax.apply( this, arguments );
return pendingRequests[ port ];
}
return ajax.apply( this, arguments );
};
}

// Abort the previous request without sending a new one
$.ajaxAbort = function( port ) {
if ( pendingRequests[ port ] ) {
pendingRequests[ port ].abort();
delete pendingRequests[ port ];
}
};
27 changes: 26 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,9 @@ $.extend( $.validator, {
val = this.elementValue( element ),
result, method, rule, normalizer;

// Abort any pending Ajax request from a previous call to this method.
this.abortRequest( element );

// Prioritize the local normalizer defined for this element over the global one
// if the former exists, otherwise user the global one in case it exists.
if ( typeof rules.normalizer === "function" ) {
Expand Down Expand Up @@ -1095,6 +1098,10 @@ $.extend( $.validator, {
return !$.validator.methods.required.call( this, val, element ) && "dependency-mismatch";
},

elementAjaxPort: function( element ) {
return "validate" + element.name;
},

startRequest: function( element ) {
if ( !this.pending[ element.name ] ) {
this.pendingRequest++;
Expand Down Expand Up @@ -1130,6 +1137,24 @@ $.extend( $.validator, {
}
},

abortRequest: function( element ) {
var port;

if ( this.pending[ element.name ] ) {
port = this.elementAjaxPort( element );
$.ajaxAbort( port );

this.pendingRequest--;

// Sometimes synchronization fails, make sure pendingRequest is never < 0
if ( this.pendingRequest < 0 ) {
this.pendingRequest = 0;
}

delete this.pending[ element.name ];
}
},

previousValue: function( element, method ) {
method = typeof method === "string" && method || "remote";

Expand Down Expand Up @@ -1570,7 +1595,7 @@ $.extend( $.validator, {
data[ element.name ] = value;
$.ajax( $.extend( true, {
mode: "abort",
port: "validate" + element.name,
port: this.elementAjaxPort( element ),
dataType: "json",
data: data,
context: validator.currentForm,
Expand Down
30 changes: 30 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,36 @@ QUnit.test( "Fix #697: remote validation uses wrong error messages", function( a
} );
} );

QUnit.test( "Fix #2434: race condition in remote validation rules", function( assert ) {
var e = $( "#username" ),
done1 = assert.async(),
v = $( "#userForm" ).validate( {
rules: {
username: {
required: true,
remote: {
url: "users.php"
}
}
},
messages: {
username: {
remote: $.validator.format( "{0} in use" )
}
}
} );

e.val( "Peter" );
v.element( e );

e.val( "" );
v.element( e );
setTimeout( function() {
assert.equal( v.errorList[ 0 ].message, "This field is required." );
done1();
} );
} );

QUnit.module( "additional methods" );

QUnit.test( "phone (us)", function( assert ) {
Expand Down
0