DOM Based Cross Site Scripting or XSS of the Third Kind


We all know what Cross Site Scripting (XSS) is, right? It’s that vulnerability wherein one sends malicious data (typically HTML stuff with Javascript code in it) that is echoed back later by the application in an HTML context of some sort, and the Javascript code gets executed. Well, wrong. There’s a kind of XSS which does not match this description, at least not in some fundamental properties. The XSS attacks described above are either “non-persistent”/“reflected” (i.e. the malicious data is embedded in the page that is returned to the browser immediately following the request) or “persistent”/“stored” (in which case the malicious data is returned at some later time). But there’s also a third kind of XSS attacks - the ones that do not rely on sending the malicious data to the server in the first place! While this seems almost contradictory to the definition or to common sense, there are, in fact, two well described examples for such attacks. This technical note discusses the third kind of XSS, dubbed “DOM Based XSS”. No claim is made to novelty in the attacks themselves, of course, but rather, the innovation in this write-up is about noticing that these belong to a different flavor, and that flavor is interesting and important.

Application developers and owners need to understand DOM Based XSS, as it represents a threat to the web application, which has different preconditions than standard XSS. As such, there are many web applications on the Internet that are vulnerable to DOM Based XSS, yet when tested for (standard) XSS, are demonstrated to be “not vulnerable”. Developers and site maintainers (and auditors) need to familiarize themselves with techniques to detect DOM Based XSS vulnerabilities, as well as with techniques to defend against them, both therewhich are different than the ones applicable for standard XSS.


The reader is assumed to possess basic knowledge of XSS ([1], [2], [3],[4], [8]). XSS is typically categorized into “non-persistent” and “persistent” ([3], “reflected” and “stored” accordingly, as defined in [4]). “Non-persistent” means that the malicious (Javascript) payload is echoed by the server in an immediate response to an HTTP request from the victim. “Persistent” means that the payload is stored by the system, and may later be embedded by the vulnerable system in an HTML page provided to a victim. As mentioned in the summary, this categorization assumes that a fundamental property of XSS is having the malicious payload move from the browser to the server and back to the same (in non-persistent XSS) or any (in persistent XSS) browser. This paper points out that this is a misconception. While there are not many counterexamples in the wild, the mere existence of XSS attacks which do not rely on the payload embedded by the server in some response page, is of importance as it has a significant impact on detection and protection methods. This is discussed in the document.

Example and Discussion

Before describing the basic scenario, it is important to stress that the techniques outlined here were already demonstrated in public (e.g.[5], [6] and [7]). As such, it is not claimed that the below are new techniques (although perhaps some of the evasion techniques are).


The prerequisite is for the vulnerable site to have an HTML page that uses data from the document.location or document.URL ordocument.referrer (or any various other objects which the attacker can influence) in an insecure manner.

具有漏洞的网站有HTML页面以不安全的方式使用从 document.location或者document.URL ordocument.referrer(或者其他攻击者可以控制的其他对象)中获取的数据。

NOTE for readers unfamiliar with those Javascript objects: when Javascript is executed at the browser, the browser provides the Javascript code with several objects that represent the DOM (Document Object Model). The document object is chief among those objects, and it represents most of the page’s properties, as experienced by the browser. This document object contains many sub-objects, such aslocation, URL and referrer. These are populated by the browser according to the browser’s point of view (this is significant, as we’ll see later with the fragments). So, document.URL anddocument.location are populated with the URL of the page, as the browser understands it. Notice that these objects are not extracted of the HTML body - they do not appear in the page data. The documentobject does contain a body object that is a representation of the parsed HTML.


It is not uncommon to find an application HTML page containing Javascript code that parses the URL line (by accessing document.URL or document.location) and performs some client side logic according to it. The below is an example to such logic.


In analogy to the example in [2] (and as an essentially identical scenario to the one in [7]), consider, for example, the following HTML page (let’s say this is the content of 

var pos=document.URL.indexOf("name=")+5;
Welcome to our system


Normally, this HTML page would be used for welcoming the user, e.g.:

However, a request such as:

would result in an XSS condition. Let’s see why: the victim’s browser receives this link, sends an HTTP request to, and receives the above (static!) HTML page. The victim’s browser then starts parsing this HTML into DOM. The DOM contains an object called document, which contains a property called URL, and this property is populated with the URL of the current page, as part of DOM creation. When the parser arrives to the Javascript code, it executes it and it modifies the raw HTML of the page. In this case, the code referencesdocument.URL, and so, a part of this string is embedded at parsing time in the HTML, which is then immediately parsed and the Javascript code found (alert(…)) is executed in the context of the same page, hence the XSS condition.

如果请求输入<script>alert(document.cookie)</script>,可以导致XSS的情况。查看下原因:受害者浏览器收到这个链接,发送HTTP请求至,并接受上述的HTML页面。受害者浏览器会解析HTML至DOM中。DOM包含document对象,该对象包含一个URL属性,该属性以当前页面的URL进行填充,作为创建DOM的一部分。当解析器遇到Javascript 代码时,会执行代码并修改原来的HTML页面。在这个例子中,代码引用document.URL,在HTML解析过程中会嵌入部分字符串,进而解析并执行javascript代码,造成xss的情况。

1. The malicious payload was not embedded in the raw HTML page at any time (unlike the other flavors of XSS).


2. This exploit only works if the browser does not modify the URL characters. Mozilla automatically encodes < and > (into %3C and %3E, respectively) in the document.URL when the URL is not directly typed at the address bar, and therefore it is not vulnerable to the attack as shown in the example. It is vulnerable to attacks if < and > are not needed (in raw form). Microsoft Internet Explorer 6.0 does not encode <and >, and is therefore vulnerable to the attack as-is.

Of course, embedding in the HTML directly is just one attack mount point, there are various scenarios that do not require < and >, and therefore Mozilla in general is not immune from this attack.

这种利用仅在浏览器不改变URL字符时有效。当URL不是直接输入至地址栏中时,Mozilla自动在document.URL中重编码<和>(一般为%3C %3E) ,所以在这个例子中就不存在漏洞。如果攻击不需要< 和> ,那么还可能被攻击。Microsof IE 6.0不会重编码<和> ,所以就存在漏洞。


Evading standard detection and prevention technologies

In the above example, it may be argued that still, the payload did arrive to the server (in the query part of the HTTP request), and so it can be detected just like any other XSS attack. But even that can be taken care of. Consider the following attack: 


Notice the number sign (#) right after the file name. It tells the browser that everything beyond it is a fragment, i.e. not part of the query. Microsoft Internet Explorer (6.0) and Mozilla do not send the fragment to the server, and therefore, the server would see the equivalent of, so the payload would not even be seen by the server. We see, therefore, that this evasion technique causes the major browsers not to send the malicious payload to the server.

#右边的部分,仅是一个片段,并不是查询的一部分。Micrsoft IE(6.0)和Mozilla 不会将其发生给服务器,所以服务器仅能看到 ,所以payload不会被服务器看到。所以这种规避技术使大多数浏览器不能将恶意payload发送到服务器。

Sometimes, it’s impossible to completely hide the payload: in [5] and[6], the malicious payload is part of the username, in a URL that looks like http://username@host/. The browser, in such case, sends a request with Authorization header containing the username (the malicious payload), and thus, the payload does arrive to the server (Base64 encoded - so IDS/IPS would need to decode this data first in order to observe the attack). Still, the server is not required to embed this payload in order for the XSS condition to occur.

有时,不可能完全隐藏payload:在文献[5]和[6]中,恶意payload在URL中作为用户名的一部分,看起来像 http://username@host/ 。在这种情况下,浏览器发送包含用户名(恶意代码伪造)的认证头的请求,那么恶意代码即可以到达服务器(Base64 编码 - 所以 IDS/IPS 为了阻止这类攻击需要解码这些数据)。

Obviously, in situations where the payload can be completely hidden, online detection (IDS) and prevention (IPS, web application firewalls) products cannot fully defend against this attack, assuming the vulnerable script can indeed be invoked from a known location. Even if the payload has to be sent to the server, in many cases it can be crafted in such way to avoid being detected, e.g. if a specific parameter is protected (e.g. the name parameter in the above example), then a slight variation of the attack may succeed: 


A more strict security policy would require that the name parameter be sent (to avoid the above tricks with names and number sign). We can therefore send this:


If the policy restricts the additional parameter name (e.g. to foobar), then the following variant would succeed:

如果限制附加了另外一个参数名(如 foobar),那么攻击形式可变为:

Note that the ignored parameter (foobar) must come first, and it contains the payload in its value.


The scenario in [7] is even better from the attacker’s perspective, since the full document.location is written to the HTML page (the Javascript code does not scan for a specific parameter name). Therefore, the attacker can completely hide the payload e.g. by sending:

文献[7]中的方案从攻击者的角度来看更好,因为document.location全部写进了HTML页面,所以,攻击者可以完全隐藏恶意代码,如发 送:


Even if the payload is inspected by the server, protection can be guaranteed only if the request in its fullness is denied, or if the response is replaced with some error text. Consider [5] and [6] again, if the Authorization header is simply removed by an intermediate protection system, it has no effect as long as the original page is returned. Likewise, any attempt to sanitize the data on the server, either by removing the offending characters or by encoding them, is ineffective against this attack.


In the case of document.referrer , the payload is sent to the server through the Referer header. However, if the user’s browser, or an intermediate device eliminates this header, then there’s no trace of the attack - it may go completely unnoticed. 

在document.referrer例子中,payload通过Referer头发送到服务器端。但是,如果用户的浏览器或者中间设备消除了头,那么就无非跟踪该攻击  --- 攻击可以在不注意的情况下进行。

To generalize, traditional methods of:
  1. HTML encoding output data at the server side  HTML在服务器端编码输出数据
  2. Removing/encoding offending input data at the server side  在服务器端移除/编码可能的恶意输入数据
Do not work well against DOM Based XSS. 

Regarding automatic vulnerability assessment by way of fault injection (sometimes called fuzzing) won’t work, since products that use this technology typically evaluate the results according to whether the injected data is present in the response page or not (rather than execute the client side code in a browser context and observe the runtime effects). However, if a product is able to statically analyze a Javascript found in a page, then it may point out suspicious patterns (see below). And of course, if the product can execute the Javascript (and correctly populating the DOM objects), or simulate such execution, then it can detect this attack.

一般Fuzzing自动漏洞评估不可行,因为Fuzzing工具多通过评估注入数据是否显示在响应页面中来测定结果(而不是在浏览器中执行客户端代码并观察运行影响)。但是,如果产品能够静态分析页面中的Javascript,那么它可以给出可疑的模式。进而,如果该产品能够执行javascript 且能够准确的处理DOM对象,或者是模拟执行,那么可以检测这种攻击。

Manual vulnerability assessment using a browser would work because the browser would execute the client side (Javascript) code. Of course, a vulnerability assessment product may adopt this kind of technology and execute client side code to inspect the runtime effects.


Effective defenses

1. Avoiding client side document rewriting, redirection, or other sensitive actions, using client side data. Most of these effects can be achieved by using dynamic pages (server side). 避免客户端document重写、重定向或者其他敏感操作、使用客户端数据。这些操作的效果可以通过服务器端的动态页面实现。

2. Analyzing and hardening the client side (Javascript) code. Reference to DOM objects that may be influenced by the user (attacker) should be inspected, including (but not limited to):


  • document.URL
  • document.URLUnencoded
  • document.location (and many of its properties)
  • document.referrer
  • window.location (and many of its properties)

Note that a document object property or a window object property may be referenced syntactically in many ways - explicitly (e.g.window.location), implicitly (e.g. location), or via obtaining a handle to a window and using it (e.g. handle_to_some_window.location).

Special attention should be given to scenarios wherein the DOM is modified, either explicitly or potentially, either via raw access to the HTML or via access to the DOM itself, e.g. (by no means an exhaustive list, there are probably various browser extensions):

  • Write raw HTML, e.g.:
    • document.write(…)
    • document.writeln(…)
    • document.body.innerHtml=…
  • Directly modifying the DOM (including DHTML events), e.g.:
    • document.forms[0].action=… (and various other collections)
    • document.attachEvent(…)
    • document.create…(…)
    • document.execCommand(…)
    • document.body. … (accessing the DOM through the body object)
    • window.attachEvent(…)
  • Replacing the document URL, e.g.:
    • document.location=… (and assigning to location’s href, host and hostname)
    • document.location.hostname=…
    • document.location.replace(…)
    • document.location.assign(…)
    • document.URL=…
    • window.navigate(…)
  • Opening/modifying a window, e.g.:
    • window.location.href=… (and assigning to location’s href, host and hostname)
  • Directly executing script, e.g.:
    • eval(…)
    • window.execScript(…)
    • window.setInterval(…)
    • window.setTimeout(…)

To continue the above example, an effective defense can be replacing the original script part with the following code, which verifies that the string written to the HTML page consists of alphanumeric characters only: 

  var pos=document.URL.indexOf("name=")+5;
  var name=document.URL.substring(pos,document.URL.length);
  if (name.match(/^[a-zA-Z0-9]$/))
        window.alert("Security error");

Such functionality can (and perhaps should) be provided through a generic library for sanitation of data (i.e. a set of Javascript functions that perform input validation and/or sanitation). The downside is that the security logic is exposed to the attackers - it is embedded in the HTML code. This makes it easier to understand and to attack it. While in the above example, the situation is very simple, in more complex scenarios wherein the security checks are less than perfect, this may come to play.

3. Employing a very strict IPS policy in which, for example, page welcome.html is expected to receive a one only parameter named “name”, whose content is inspected, and any irregularity (including excessive parameters or no parameters) results in not serving the original page, likewise with any other violation (such as an Authorization header or Referer header containing problematic data), the original content must not be served. And in some cases, even this cannot guarantee that an attack will be thwarted.

A note about redirection vulnerabilities

The above discussion is on XSS, yet in many cases, merely using a client side script to (insecurely) redirect the browser to another location is considered vulnerability in itself. In such cases, the above techniques and observations still apply.


While most XSS attacks described in public do indeed depend on the server physically embedding user data into the response HTML pages, there are XSS attacks that do not rely on server side embedding of the data. This has material significance when discussing ways to detect and prevent XSS. To date, almost all detection and prevention techniques discussed in public assume that XSS implies that the server receives malicious user input and embeds it in an HTML page. Since this assumption doesn’t hold (or only very partially holds) for the XSS attacks described in this paper, many of the techniques fail to detect and prevent this kind of attacks.

The XSS attacks that rely on server side embedding of user data are categorized into “non-persistent” (or “reflected”) and “persistent” (or “stored”). It is thus suggested that the third kind of XSS, the one that does not rely on server side embedding, be named “DOM Based XSS”. 

Here is a comparison between standard XSS and DOM Based XSS:


Standard XSS


Root cause

Insecure embedding of client input in HTML outbound page

Insecure reference and use (in a client side code) of DOM objects that are not fully controlled by the server provided page


Web developer (CGI)

Web developer (HTML)

Page nature

Dynamic only (CGI script)

Typically static (HTML), but not necessarily.

Vulnerability Detection

  • Manual Fault injection
  • Automatic Fault Injection
  • Code Review (need access to the page source)
  • Manual Fault Injection
  • Code Review (can be done remotely!)

Attack detection

  • Web server logs
  • Online attack detection tools (IDS, IPS, web application firewalls)

If evasion techniques are applicable and used - no server side detection is possible

Effective defense

  • Data validation at the server side
  • Attack prevention utilities/tools (IPS, application firewalls)
  • Data validation at the client side (in Javascript)
  • Alternative server side logic


Note: the URLs below are up to date at the time of writing (July 4th, 2005). Some of these materials are live documents, and as such may be updated to reflect the insights of this paper.

[1] “CERT Advisory CA-2000-02 - Malicious HTML Tags Embedded in Client Web Requests”, CERT, February 2nd, 2000

[2] “Cross Site Scripting Explained”, Amit Klein, June 2002

[3] “Cross-Site Scripting”, Web Application Security Consortium, February 23rd, 2004

[4] “Cross Site Scripting (XSS) Flaws”, The OWASP Foundation, updated 2004

[5] “Thor Larholm security advisory TL#001 (IIS allows universal CrossSiteScripting)”, Thor Larholm, April 10th, 2002

(see also Microsoft Security Bulletin MS02-018

[6] “ISA Server Error Page Cross Site Scripting”, Brett Moore, July 16th, 2003

(see also Microsoft Security Bulletin MS03-028 and a more elaborate description in “Microsoft ISA Server HTTP error handler XSS”, Thor Larholm, July 16th, 2003

[7] “Bugzilla Bug 272620 - XSS vulnerability in internal error messages”, reported by Michael Krax, December 23rd, 2004

[8] “The Cross Site Scripting FAQ”, Robert Auger, May 2002 (revised August 2003)

About the author

Amit Klein is a renowned web application security researcher. Mr. Klein has written many research papers on various web application technologies--from HTTP to XML, SOAP and web services--and covered many topics--HTTP request smuggling, insecure indexing, blind XPath injection, HTTP response splitting, securing .NET web applications, cross site scripting, cookie poisoning and more. His works have been published in Dr. Dobb's Journal, SC Magazine, ISSA journal, and IT Audit journal; have been presented at SANS and CERT conferences; and are used and referenced in many academic syllabi. 

Mr. Klein is a WASC (Web Application Security Consortium) member. 

The current copy of this document can be here:

Information on the Web Application Security Consortium's Article Guidelines can be found here:

A copy of the license for this document can be found here:

想对作者说点什么? 我来说一句