c# - How to check if string will fire “A potentially dangerous Request.Form value was detected…” error -


similar this question don't care characters will/won't cause error fire. i'm more curious method can call, check myself, if current string fire above error message.

to give bit of background, i'm creating random passwords when user forgets theirs , needs reset. unfortunately, random password generator "accidentally" created 1 &# in recently. caused page break when user tried login it.

as mentioned in plenty of posts around topic, validaterequest=false (or <httpruntime requestvalidationmode="2.0" /> .net 4.0) can used turn off .net checking these exploits, don't see reason lose layer of security when i'm 1 creating string in first place. , telling random generator re-randomize on incomplete list (<, &#, etc) doesn't seem cleanest solution i'd use same method of checking .net using.

microsoft's explanation of exploits in question , being done guard against them here.

this guy talks finding function called isdangerousstring after digging in reflector, i'm not able find function use it. he's referring .net 1.1 , i'm working .net 3.5

the asp.net class validates requests system.web.crosssitescriptingvalidation, , method want isdangerousstring. unfortunately, both marked internal, can't access them directly. have several options:

option 1: call isdangerousstring via reflection. however, microsoft change method @ time, break applicaton.

option 2: decompile isdangerousstring , copy own application. see code below.

option 3: call membership.generatepassword. returns password guaranteed pass request validation.

excerpts asp.net crosssitescriptingvalidation class (via .net reflector):

private static char[] startingchars = new char[] { '<', '&' }; internal static bool isdangerousstring(string s, out int matchindex) { matchindex = 0; int startindex = 0; while (true) { int num2 = s.indexofany(startingchars, startindex); if (num2 < 0) { return false; } if (num2 == (s.length - 1)) { return false; } matchindex = num2; char ch = s[num2]; if (ch != '&') { if ((ch == '<') && ((isatoz(s[num2 + 1]) || (s[num2 + 1] == '!')) || ((s[num2 + 1] == '/') || (s[num2 + 1] == '?')))) { return true; } } else if (s[num2 + 1] == '#') { return true; } startindex = num2 + 1; } } private static bool isatoz(char c) { return (((c >= 'a') && (c <= 'z')) || ((c >= 'a') && (c <= 'z'))); } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -