Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  124] [ 5]  / answers: 1 / hits: 32892  / 12 Years ago, tue, october 9, 2012, 12:00:00

There is a Node.js project that sanitizes data and there is an OWASP library for JavaScript that handles sanitization to prevent XSS.



I have been benchmarking these libraries, and they are pretty intensive and maybe an overkill, my application does not need any dynamic HTML (submitted by users, bbtags or what ever, not required at all) so why not do it like this:




  1. Disable < and > characters, don't replace them or anything, just disable them, if the user submits these, give them a warning that these are disabled (client- and server-side validation)

  2. & => &amp;

  3. => &quot;

  4. ' => &#x27;

  5. / => /

  6. Encode submitted URLs (GET parameters etc.)

  7. DOM based XSS is covered since my application uses HTML5 PushState and the backend is fully separated from the frontend.



Would this be enough to protect myself, as I said, my application does not require any HTML submitted by users, so I don't need the < and > tags at all.



Thanks for all the feedback, this is what I use right now:



var pattern = /<(.*)>/;

function hasHtmlTags(string) {
return pattern.test(string);
};

if (hasHtmlTags(userData)) {
// Do something?
} else {
// Create entity.
}


So users can still use their emoticons :< and such, and the function only gets triggered if a combination of < and > is found. So no expensive regular expressions and such, just disable < and > in combination and we should be fine.


More From » security

 Answers
13

Here is a general encode procedure:



var lt = /</g, 
gt = />/g,
ap = /'/g,
ic = //g;
value = value.toString().replace(lt, &lt;).replace(gt, &gt;).replace(ap, &#39;).replace(ic, &#34;);


If your user doesn't submit anything to your server you don't even need the above. If the user submits and you are using the user input then the above should be safe. As long as the '<' and '>' are globally sanitized and the parenthesis also are you are good to go.


[#82665] Monday, October 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
miles

Total Points: 256
Total Questions: 111
Total Answers: 104

Location: Benin
Member since Fri, Mar 24, 2023
1 Year ago
;