title: Javascript XSS Prevention
body: <p>There is a Node.js project that sanitizes data and there is an OWASP library for JavaScript that handles sanitization to prevent XSS.</p>
  
  <p>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:</p>
  
  <ol>
  <li>Disable "<code>&lt;</code>" and "<code>&gt;</code>" 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)</li>
  <li><code>&amp;</code> => <code>&amp;amp;</code></li>
  <li><code>"</code> => <code>&amp;quot;</code></li>
  <li><code>'</code> => <code>&amp;#x27;</code></li>
  <li><code>/</code> => <code>&#x2F;</code></li>
  <li>Encode submitted URLs (GET parameters etc.)</li>
  <li>DOM based XSS is covered since my application uses HTML5 PushState and the backend is fully separated from the frontend.</li>
  </ol>
  
  <p>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 <code>&lt;</code> and <code>&gt;</code> tags at all.</p>
  
  <p>Thanks for all the feedback, this is what I use right now:</p>
  
  <pre><code>var pattern = /&lt;(.*)&gt;/;
  
  function hasHtmlTags(string) {
      return pattern.test(string);
  };
  
  if (hasHtmlTags(userData)) {
      // Do something?
  } else {
      // Create entity.
  }
  </code></pre>
  
  <p>So users can still use their emoticons :&lt; and such, and the function only gets triggered if a combination of &lt; and > is found. So no expensive regular expressions and such, just disable &lt; and > in combination and we should be fine.</p>