KHAGAN AYDIN

The MySpace Worm: One Million Profiles in a Day

Nobody broke into MySpace. A profile page did the work, one visitor at a time.

· 5 min read

On October 4 a MySpace user put a small piece of script into his own profile. Anyone who looked at that profile had the script run in their browser. It added its author as a friend, wrote one line of text into the visitor’s own profile, and then copied itself there too. Now that visitor’s profile carried the script, and everyone who looked at it was next.

In under twenty hours it had run for more than one million users. MySpace took the site down to clean it up.

No password was stolen. No server was broken into. Nothing was taken at all, which is why some people are treating it as a prank. I think that reading is wrong, and this piece explains why.

What kind of bug this is

The name is cross-site scripting. The site takes text written by one user and shows it to another user as part of a page. If that text can contain script, the script runs in the second user’s browser, and the browser treats it as part of the site.

That last point is the whole problem. The browser has one rule for deciding what a script may do: where did the page come from? A script that arrives inside a MySpace page is MySpace, as far as the browser can tell. It can read what the page shows. It can send requests with the user’s login attached. It can do anything the user could do by clicking.

So the worm did not need to break the login. It ran inside the login.

How it got past the filter

MySpace knew profiles were dangerous. It let users decorate their pages with their own markup, and it ran a filter to remove anything that looked like script. The filter stripped script tags. It stripped the word that starts a script link. It stripped the event attributes that run code on a click or a page load.

The worm went around each of those rules:

  • Script hidden in a style. The filter removed script tags but allowed style attributes. One browser, the one most visitors used, would run script placed inside a style rule. So the code went there.
  • A banned word split in two. The filter looked for one exact word. The author broke it across two lines. The filter no longer saw the word. The browser ignored the line break and saw it fine.
  • Banned names built from pieces. Where the filter blocked the name of a function, the code joined two halves of the name together at run time.

Every one of these is the same lesson. The filter was a list of bad things. The author needed only one thing the list had missed, and a browser accepts a great many ways of writing the same instruction. A list of bad things is never finished.

How it spread without the user doing anything

Getting script to run is half of a worm. The other half is making it travel.

The code used the browser’s ability to send requests in the background, the same feature that makes the new map and mail sites feel fast. With it, the script quietly did what a user would do by hand: it opened the add a friend page, read the confirmation value the site put there, and sent the confirmation. Then it opened the edit profile page and saved a new profile with the script inside.

MySpace did ask for a confirmation step, with a value that changed each time. That protects against a request forged from another site. It did nothing here, because the script was not on another site. It was on MySpace, so it could read the value like any page could.

The numbers follow from the shape. One profile infects its visitors. Each of those infects their visitors. Popular profiles get thousands of views a day. Growth like that looks slow for the first hour and then it does not.

Why “it only added a friend” is the wrong comfort

The author chose a harmless payload. The bug did not require that choice.

The same script, running with the same access, could have:

  • Read every private message in the visitor’s inbox and sent it elsewhere
  • Changed the email address on the account, then asked for a password reset
  • Shown a fake login box on a real MySpace page and collected the password
  • Sent messages to every friend, in the visitor’s name, with a link

On a social site that is embarrassing. Now picture the same flaw in a site that holds money or medical records or company mail. The page comes from the bank, the address bar says the bank, the padlock is closed, and the script inside the page belongs to someone else.

That is why this event matters far outside MySpace. Any site that shows one user’s text to another user has the same exposure: comments, forums, reviews, profile pages, support tickets, web mail.

What to change

Encode on the way out. The reliable fix is not to find bad input. It is to make sure that text from a user is always written into the page as text, never as markup. Every character that has a meaning in the page, such as the angle brackets and the quote marks, is converted to its harmless form at the moment it is printed. Text treated this way cannot become code, no matter what it contains. This is a few lines in any language, and it has to happen everywhere user text is shown.

If users must be allowed markup, allow a short list. MySpace’s feature was custom profiles, so it could not encode everything. Then the rule flips: do not remove what is bad, keep only what is known to be good. A small set of tags, a small set of attributes, nothing else. Read the markup properly, rebuild it from the allowed parts, and throw away the rest. Style rules that can carry script are not on the list.

Limit what a stolen session can do. Ask for the password again before changing an email address or a password. Then a script inside the page cannot quietly take the account.

Keep the login cookie away from script. Browsers are starting to support a cookie setting that hides the cookie from script. It does not stop a worm like this one, which never needed to read the cookie, but it stops the simplest theft.

Test for it. Put script into every field your application later displays, and see what comes back. If your test text appears in the page exactly as you typed it, you have the bug.

What I take from it

Three things, and none of them is about MySpace.

First, the web’s security model trusts the page. Once a site lets a stranger’s script into its own page, the browser cannot help.

Second, a filter that lists bad input loses to a patient person. Deciding what is allowed is the only version that holds.

Third, the features that make sites pleasant, pages that update without reloading and users who shape their own pages, are the same features that let this spread in a day. We are going to build a lot more of both. The sites that print user text carefully will be fine. The rest will get a worm whose author is less polite.