1

A persistent follow-up of an admittedly similar question I had asked: What security restrictions should be implemented in allowing a user to upload a Javascript file that directs canvas animation?

I like to think I know JS decent enough, and I see common characters in all the XSS examples I've come accoss, which I am somewhat familiar with. I am lacking good XSS examples that could bypass a securely sound, rationally programmed system. I want people to upload html5 canvas creations onto my site. Any sites like this yet? People get scared about this all the time it seems, but what if you just wanted to do it for fun for yourself and if something happens to the server then oh well it's just an animation site and information is spread around like wildfire anyway so if anyone cares then i'll tell them not to sign up.

If I allow a single textarea form field to act as an IDE using JS for my programming language written in JS, and do string replacing, filtering, and validation of the user's syntax before finally compiling it into JS to be echoed by PHP, how bad could it get for me to host that content? Please show me how you could bypass all of my combined considerations, with also taking into account the server-side as well:

  • If JavaScript is disabled, preventing any POST from getting through, keeping constant track of user session.
  • Namespacing the Class, so they can only prefix their functions and methods with EXAMPLE.
  • Making instance
  • Storing my JS Framework in an external (immutable in the browser?) JS file, which needs to be at the top of the page for the single textarea field in the form to be accepted, as well as a server-generated key which must follow it. On the page that hosts the compiled user-uploaded canvas game/animation (1 per page ONLY), the server will verify the correct JS filename string before echoing the rest out.
  • No external script calls! String replacing on client and server.
  • Allowing ONLY alphanumeric characters, dashes and astericks.
  • Removing alert, eval, window, XMLHttpRequest, prototyping, cookie, obvious stuff. No native JS reserved words or syntax.
  • Obfuscating and minifying another external JS file that helps to serve the IDE and recognize the programming language's uniquely named Canvas API methods.
  • When Window unloads, store the external JS code in to two dynamically generated form fields to be checked by the server in POST. All the original code will be cataloged in the DB thoroughly for filtering purposes.
  • Strict variable naming conventions ('example-square1-lengthPROPERTY', 'example-circle-spinMETHOD')
  • Copy/Paste Disabled, setInterval to constantly check if enabled by the user. If so, then trigger a block to the database, change window.location immediately and check the session ID through POST to confirm in case JS becomes disabled between that timeframe.

I mean, can I do it then? How can one do harm if they can't use HEX or ASCII and stuff like that?

Community
  • 1
  • 1
dmoonman
  • 77
  • 1
  • 7
  • you want to completely disable javascript? – galchen Oct 28 '11 at 03:43
  • 2
    In answer to "Any sites that do this yet?" - http://jsfiddle.net/ is the obvious example; http://www.hascanvas.com/ and http://www.sketchpad.cc/ do this for processing.js – nrabinowitz Oct 28 '11 at 04:06
  • So wait, people are uploading html to your site and you are displaying it? Do you understand the definition of xss? – rook Oct 28 '11 at 04:22

2 Answers2

0

I think there are a few other options.

Good places to go for real-life XSS tests, by the way, are the XSS Cheat Sheet and HTML5 Security Cheetsheet (newer). The problem with that, however, is that you want to allow Javascript but disallow bad Javascript. This is a different, and more complex, goal than the usual way of preventing XSS, by preventing all scripts.

Hosting on a separate domain

I've seen this referred to as an "iframe jail".

The goal with XSS attacks is to be able to run code in the same context as your site - that is, on the same domain. This is because the code will be able to read and set cookies for that domain, intiate user actions or redress your design, redirect, and so forth.

If, however, you have two separate domains - one for your site, and another which only hosts the untrusted, user-uploaded content, then that content will be isolated from your main site. You could include it in an iframe, and yet it would have no access to the cookies from your site, no access to redress or alter the design or links outside its iframe, and no access to the scripting variables of your main window (since it is on a different domain).

It could, of course, set cookies as much as it likes, and even read back the ones that it set. But these would still be isolated from the cookies for your site. It would not be able to affect or read your main site's cookies. It could also include other code which could annoy/harrass the user, such as pop-up windows, or could attempt to phish (you'd need to make it visually clear in your out-of-iframe UI that the content served is not part of your site). However, this is still sandboxed from your main site, where you own personal payload - your session cookies and the integrity of your overarching page design and scripts, is preserved. It would carry no less but no more risk than any site on the internet that you could embed in an iframe.

Using a subset of Javascript

Subsets of Javascript have been proposed, which provide compartmentalisation for scripts - the ability to load untrusted code and have it not able to alter or access other code if you don't give it the scope to do so.

Look into things like Google CAJA - whose aim is to enable exactly the type of service that you've described:

Caja allows websites to safely embed DHTML web applications from third parties, and enables rich interaction between the embedding page and the embedded applications. It uses an object-capability security model to allow for a wide range of flexible security policies, so that the containing page can effectively control the embedded applications' use of user data and to allow gadgets to prevent interference between gadgets' UI elements.

One issue here is that people submitting code would have to program it using the CAJA API. It's still valid Javascript, but it won't have access to the browser DOM, as CAJA's API mediates access. This would make it difficult for your users to port some existing code. There is also a compilation phase. Since Javascript is not a secure language, there is no way to ensure code cannot access your DOM or other global variables without running it through a parser, so that's what CAJA does - it compiles it from Javascript input to Javascript output, enforcing its security model.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • Dude xss cheat sheet? Wasn't that last updated in like 1999? – rook Oct 28 '11 at 04:36
  • Looks like around 2006, but it is still the best I know of. Do you know of a more comprehensive modern equivalent? I'd amend my answer with it. – thomasrutter Oct 28 '11 at 05:01
  • Clearly you haven't tried anything on that page, the **vast majority** of it doesn't work. For instance anything like `src=javascript:alert(1)` has been fixed. – rook Oct 28 '11 at 13:38
  • You know Rook, you seem like a really nice person. – thomasrutter Oct 28 '11 at 16:13
-1

htmlprufier consists of thousands of regular expressions that attempt "purify" html into a safe subset that is immune to xss. This project is bypassesed very few months, because it isn't nearly complex enough to address the problem of XSS.

Do you understand the complexity of XSS?

Do you know that javascript can exist without letters or numbers?

Okay, they very first thing I would try is inserting a meta tag that changes the encoding to I don't know lets say UTF-7 which is rendered by IE. Within this utf-7 enocded html it will contain javascript. Did you think of that? Well guess what there is somewhere between a hundred thousand and a a few million other vectors I didn't think of.

The XSS cheat sheet is so old my grandparents are immune to it. Here is a more up to date version.

(Oah and by the way you will be hacked because what you are trying to do fundamentally insecure.)

rook
  • 66,304
  • 38
  • 162
  • 239
  • I believe that htmlpurifier is for purifying *HTML*, and it removes all scripts. If so that wouldn't be of assistance. – thomasrutter Oct 28 '11 at 05:05
  • @thomasrutter would you agree that a safe subset of javascirpt is even more difficult to obtain? – rook Oct 28 '11 at 06:15
  • Yes, but projects like [CAJA](http://code.google.com/p/google-caja/) and [AdSafe](http://www.adsafe.org/) have achieved it. – thomasrutter Oct 28 '11 at 06:29
  • @thomasrutter no, they **TRY** to achieve it. The whole point in hacking is proving that it doesn't stop all conditions. Your statement shows that you do not write Proofs of Concept. – rook Oct 28 '11 at 13:20
  • I'm not really sure what you are saying here. Are you making an argument against CAJA and AdSafe's security model? – thomasrutter Oct 28 '11 at 15:14