<?xml-model href='http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng' schematypens='http://relaxng.org/ns/structure/1.0'?><TEI xmlns="http://www.tei-c.org/ns/1.0">
	<teiHeader>
		<fileDesc>
			<titleStmt><title level='a'>Riding out DOMsday: Towards Detecting and Preventing DOM Cross-Site Scripting</title></titleStmt>
			<publicationStmt>
				<publisher></publisher>
				<date>02/01/2018</date>
			</publicationStmt>
			<sourceDesc>
				<bibl> 
					<idno type="par_id">10073113</idno>
					<idno type="doi">10.14722/ndss.2018.23309</idno>
					<title level='j'>2018 Network and Distributed System Security Symposium (NDSS)</title>
<idno></idno>
<biblScope unit="volume"></biblScope>
<biblScope unit="issue"></biblScope>					

					<author>William Melicher</author><author>Anupam Das</author><author>Mahmood Sharif</author><author>Lujo Bauer</author><author>Limin Jia</author>
				</bibl>
			</sourceDesc>
		</fileDesc>
		<profileDesc>
			<abstract><ab><![CDATA[Cross-site scripting (XSS) vulnerabilities are the most frequently reported web application vulnerability. As com- plex JavaScript applications become more widespread, DOM (Document Object Model) XSS vulnerabilities—a type of XSS vulnerability where the vulnerability is located in client-side JavaScript, rather than server-side code—are becoming more common. As the first contribution of this work, we empirically assess the impact of DOM XSS on the web using a browser with taint tracking embedded in the JavaScript engine. Building on the methodology used in a previous study that crawled popular websites, we collect a current dataset of potential DOM XSS vulnerabilities. We improve on the methodology for confirming XSS vulnerabilities, and using this improved methodology, we find 83% more vulnerabilities than previous methodology applied to the same dataset. As a second contribution, we identify the causes of and discuss how to prevent DOM XSS vulnerabilities. One example of our findings is that custom HTML templating designs—a design pattern that could prevent DOM XSS vulnerabilities analogous to parameterized SQL—can be buggy in practice, allowing DOM XSS attacks. As our third contribution, we evaluate the error rates of three static-analysis tools to detect DOM XSS vulnerabilities found with dynamic analysis techniques using in-the-wild examples. We find static-analysis tools to miss 90% of bugs found by our dynamic analysis, though some tools can have very few false positives and at the same time find vulnerabilities not found using the dynamic analysis.]]></ab></abstract>
		</profileDesc>
	</teiHeader>
	<text><body xmlns="http://www.tei-c.org/ns/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink">
<div xmlns="http://www.tei-c.org/ns/1.0"><head>I. INTRODUCTION</head><p>Cross-site scripting (XSS) is the most frequently reported class of web-application vulnerabilities, constituting 25% of web vulnerabilities reported in 2014 <ref type="bibr">[9]</ref>. By compromising client-side browser security using XSS, attackers can gain control over login cookies, passwords, and authentication tokens, and perform application-level actions as users, for example, send emails or make financial transactions <ref type="bibr">[25]</ref>. Preventing XSS typically requires website owners to not only sanitize all untrusted inputs to their web application, but also to sanitize all input that could be received by the client's JavaScript interpreter-a task that can be error-prone due to the complexity of web applications and the widespread use of sensitive functions in JavaScript. Document Object Model cross-site scripting (DOM XSS)-a particular type of XSS vulnerability that occurs entirely in client-side JavaScript-is more and more of a threat as JavaScript on the web becomes increasingly complicated. Traditional methods for defending against XSS vulnerabilities in server-side code-for example, server-side taint tracking or web application firewalls-typically do not apply because the vulnerability lies entirely in client code and servers may not even have logs to detect when an attack occurs.</p><p>In this paper, we aim to answer the following questions about DOM XSS. Are DOM XSS vulnerabilities becoming more or less common? How do state-of-the-art methods for detecting DOM XSS vulnerabilities compare? Are web developers learning to avoid such vulnerabilities through good coding practices, for example, using encoding schemes or design patterns such as HTML templating? What are the causes of DOM XSS? Do shared libraries or web-content-generation frameworks propagate DOM XSS vulnerabilities across a large number of sites?</p><p>To answer these questions, we use a dynamic approach to detect DOM XSS vulnerabilities on the Internet. Prior work showed how to detect DOM XSS vulnerabilities using taint tracking to track flows of attacker-controllable information sources to sensitive sink functions (e.g., eval and document.write) [8], <ref type="bibr">[22]</ref>. The existence of such flows only indicates that data from a source can reach a sink, but does not account for whether the data has been sanitized by the programmer. Thus, once a flow with a potential DOM XSS vulnerability is observed, the flow must be confirmed to be exploitable. In this paper, we show how to more accurately detect whether a flow that is potentially vulnerable is capable of being exploited. Although an attacker can use several types of sources (e.g., cross-origin messages and cookies), we focus, similarly to prior work <ref type="bibr">[22]</ref>, on confirming flows from URL-based sources. These are of particular interest because, compared to other flows, they are easy for attackers to exploit.</p><p>We used this methodology to detect DOM XSS vulnerabilities on Internet. We crawled the homepages and five random subpages of websites on the Alexa Top 10,000 most popular websites list <ref type="bibr">[11]</ref>. Compared to previous work <ref type="bibr">[22]</ref>, we observed both more flows per web page and determined a higher proportion of those flows to be vulnerable, even when using the same methodology as previous work to determine which flows are vulnerable. Using our improved method for determining which flows are vulnerable, we found 83% more vulnerabilities than by using prior methodology <ref type="bibr">[22]</ref>. We believe this indicates that DOM XSS vulnerabilities are becoming more common in the four years since the previous study was undertaken.</p><p>In addition, we qualitatively examined the code paths that led to the vulnerabilities. We observed, for example, that most of the vulnerabilities did not share code, implying that the vulnerabilities we found are due to custom code, rather than the inclusion of buggy shared libraries. We also observed errors in the implementation of HTML templating that allowed XSS vulnerabilities. Templating can be an effective way to prevent DOM XSS vulnerabilities, and is similar to using parameterized SQL queries. We found cases where bespoke templated HTML designs failed to properly encode template values, which attackers could then inject code into.</p><p>Finally, using our collected dataset of DOM XSS vulnerabilities, we evaluated static-analysis tools that are designed to detect DOM XSS. In the past, researchers have compared the effectiveness of vulnerability scanners on synthetic datasets <ref type="bibr">[16]</ref>, <ref type="bibr">[36]</ref>, whereas we used real-world vulnerabilities. We found that static-analysis tools performed poorly at detecting the vulnerabilities found by the dynamic analysis. However, some static tools were shown to have low falsenegative rates and at the same time identify DOM XSS issues not found by the dynamic analysis, suggesting that dynamic analyses and static analyses are finding qualitatively different types of vulnerabilities. Our findings on static-analysis tools suggest that testing using both dynamic and static approaches may be necessary to secure web applications from DOM XSS.</p><p>In summary, our contributions are as follows.</p><p>&#8226; We improve the methodology to confirm DOM XSS vulnerabilities, and find that 83% more detected flows are vulnerable than suggested by prior work <ref type="bibr">[22]</ref>. &#8226; We empirically analyze the prevalence of and causes behind DOM XSS vulnerabilities. This yields a number of insights for example, that HTML templating is error prone to implement and that DOM XSS vulnerabilities are becoming more prevalent. We also provide guidance for preventing DOM XSS vulnerabilities. &#8226; We compare static-analysis tools that detect DOM XSS vulnerabilities, finding them to detect different vulnerabilities than our dynamic analysis. &#8226; We develop a modified version of Chromium for tracking the taint information of strings, which we are releasing as open source. <ref type="foot">1</ref>Next, in Section II, we provide background on DOM XSS vulnerabilities and compare our work to prior work for detecting DOM XSS vulnerabilities. Then, in Section III, we detail our methodology for crawling the Internet for DOM XSS vulnerabilities, and our improved technique for confirming potentially vulnerable flows. In Section IV we describe the results of our experiments for detecting DOM XSS vulnerabilities and evaluating static-analysis tools for detecting DOM XSS. We describe the limitations of our work in Section V. We discuss the implications of our findings in Section VI, and conclude in Section VII.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>II. BACKGROUND AND RELATED WORK</head><p>Here, we cover background and prior work relevant to DOM XSS vulnerabilities. First, we give examples of and general background on DOM XSS vulnerabilities in Section II-A. Then, in Section II-B, we give examples of general XSS defense mechanisms and why those mechanisms fail to adequately apply to DOM XSS. In Section II-C, we cover Fig. <ref type="figure">1</ref>: Example of a DOM XSS vulnerability. An attacker could inject arbitrary markup using document.location as an attack vector by crafting a link that injects an attacker-controlled script into the page. An attacker may execute code by crafting a link like:</p><p>work that discusses the impact of previously discovered vulnerabilities. Next, we describe prior work on detecting DOM XSS vulnerabilities using taint tracking in Section II-D. We describe prior work on comparing web-application scanners in Section II-E. Finally, we describe how static-analysis tools help prevent DOM XSS in Section II-F.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>A. DOM XSS vulnerabilities</head><p>Cross-site-scripting (XSS) vulnerabilities are a type of injection vulnerability in which an attacker can inject arbitrary code into a running web application to, for example, take control of the data and credentials used in the application. For example, attackers may get access to the websites's cookies (which potentially contain login tokens), or may execute user actions with respect to the compromised website <ref type="bibr">[25]</ref>. In XSS, the injected code is JavaScript that runs in a web application with the permissions of the compromised website. Unlike traditional XSS attacks in which an attacker's injection might be the result of a server-side failure to sanitize input, DOM XSS is a relatively new type of XSS vulnerability that occurs purely as a result of JavaScript executing on the client. Figure <ref type="figure">1</ref> shows a example. In the example, an attacker could craft a link that breaks out of the href's single quoted attribute and inject an arbitrary script; for example,</p><p>This link, when clicked, would execute the attacker-controlled code (CODE). An attacker may convince their victims to click on the link using social engineering, or may embed the link in an iframe on a website that the attacker controls. Like traditional XSS bugs, the details depend heavily on the website and the victim's browser. Chromium, for example, does not encode any characters after the '#' symbol, whereas Firefox encodes such characters using URL encoding. Hence, it is not uncommon for a specific exploit to work only in specific browsers <ref type="bibr">[29]</ref>.</p><p>For a DOM XSS vulnerability to be present, there must be a flow of information from a potentially attackercontrolled source to a sensitive sink function. Examples of potentially attacker-controlled sources include: the URL of the document, accessed via the document.location JavaScript object; data passed in cross origin messages using the postMessage API; cookies, accessed via the document.cookie object; and the HTTP referrer accessed by the document.referrer JavaScript object and other methods. Sinks can include any mechanism to execute arbitrary code, for example: the eval function, document.write, JavaScript event handlers (e.g., the "onclick" attribute), and URLs that have a JavaScript scheme (e.g., &lt;a href='javascript:...'&gt;).</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>B. Generic XSS defenses</head><p>Many methods have been used to mitigate or defend against XSS vulnerabilities in general but do not apply to DOM XSS. Server-side taint-tracking and static-analysis techniques fundamentally cannot be applied for detecting client-side vulnerabilities <ref type="bibr">[14]</ref>, <ref type="bibr">[37]</ref>. Content Security Policies (CSPs) also aim to solve the problem. However, the adoption of CSPs has been limited and developers frequently misconfigure the policies, allowing unsafe code to execute <ref type="bibr">[15]</ref>, <ref type="bibr">[38]</ref>. Webapplication firewalls attempt to solve the problem by blocking all requests that match certain patterns (often lists of regular expressions) that indicate an XSS attack is occurring. However, web-application firewalls are known to allow many attacks due to their reliance on simple pattern matching <ref type="bibr">[13]</ref>, <ref type="bibr">[18]</ref>. Furthermore, since DOM XSS exploits might not be sent to the server, the injection may never be visible to a firewall on the network. Dynamic taint tracking, a technique to observe the flows of information throughout a program, has been proposed as a run-time defense against DOM XSS attacks <ref type="bibr">[34]</ref>; however, it requires large infrastructure changes to web browsers. Additionally, taint tracking at run time can decrease performance. The effect of such dynamic instrumentation on performance can potentially be small; however, it often has high variability, where a handful of websites have a serious performance decrease <ref type="bibr">[23]</ref>. In contrast, our work uses taint tracking to detect vulnerabilities, rather than defending against attacks at run time.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>C. Studying the impact of known vulnerabilities</head><p>Prior research has studied the impact of known vulnerabilities on websites. Researchers found that in 2017, 37% of websites included at least one version of a library with a known vulnerability <ref type="bibr">[21]</ref>. Researchers in that work measured the prevalence of websites including old, outdated versions of 72 popular libraries. Other work has found that many websites include third-party JavaScript that does not take all necessary security precautions <ref type="bibr">[24]</ref>. Our focus differs from these works in that, rather than studying the prevalence of already known vulnerabilities, we detect vulnerabilities without prior knowledge of them. In addition, our approach goes beyond vulnerabilities in outdated versions of code in popular libraries and allows us to discover potentially unknown vulnerabilities.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>D. Finding DOM XSS vulnerabilities using taint tracking</head><p>The state of the art for detecting DOM XSS vulnerabilities is using dynamic taint tracking. This technique marks potentially attacker-controlled sources as "tainted" and propagates information about tainted values throughout the program. For example, the taint-tracking engine might mark the concatenation of one tainted piece of information and one untainted as also tainted. When a tainted string is used in a sensitive sink, the taint-tracking engine may flag this as a potential DOM XSS vulnerability.</p><p>A variety of tools and research have used dynamic taint tracking to detect DOM XSS vulnerabilities. The DOMinator tool is a Firefox-based technology that tracks the taint status of strings in JavaScript <ref type="bibr">[8]</ref>. It is the oldest tool to apply taint tracking to JavaScript. In 2013, Lekies et al. showed that DOM XSS is prevalent, and introduced a method for detecting DOM XSS using more precise, byte-level taint tracking of JavaScript code, also accounting for the builtin encoding functions used in JavaScript <ref type="bibr">[22]</ref>. To generate automated exploits, the researchers used a context-specific exploit generation methodology designed to create a workable exploit by analyzing the context in which the tainted string occurs in the sink. Their technique was tested by performing a crawl of the Alexa Top 5,000 websites searching for DOM XSS vulnerabilities. Their work showed that automatically generated exploits can be created and that DOM XSS vulnerabilities affect 9.6% of domains on the Alexa Top 5,000 websites. Our work uses a similar methodology as Lekies et al.'s work for identifying tainted flows, but we use a new and novel method for confirming whether flows are indicative of DOM XSS vulnerabilities. We describe in detail the similarities and differences between our methodologies and results in Sections III and VI.</p><p>Other work, building upon a system for detecting DOM XSS vulnerabilities using browser-agnostic taint tracking <ref type="bibr">[29]</ref>, provided a method to track taint information and inject an extension that sanitizes injected strings at run time just before those strings are inserted into the sensitive sink functions <ref type="bibr">[28]</ref>. The browser-agnostic framework allows detecting vulnerabilities that are specific to certain browsers; however, such vulnerabilities account for a small fraction of all vulnerabilities <ref type="bibr">[29]</ref>. Their work focused on their proposed defense mechanism and the capability of using a browser agnostic taint tracking, in contrast to our work which provides a measurement of the prevalence of DOM XSS vulnerabilities and the ability of static-analysis tools to detect DOM XSS vulnerabilities.</p><p>Researchers have also shown how to use taint tracking to defend against DOM XSS vulnerabilities at run time <ref type="bibr">[34]</ref>. That work began from a list of known DOM XSS vulnerabilities, and showed that in 73% of cases, current client-side filtering technology-the XSS Auditor in webkit-based browsers-fails to filter an attack. The work proposes the use of browser-based taint tracking to more precisely prevent XSS vulnerabilities. However, this requires modification of the browser engine and, as mentioned earlier, can cause performance degradation. The researchers conclude that many domains make use of partlytainted HTML markup injection, and that blocking all such cases would not be feasible, instead recommending a specific heuristic policy to separate safe cases from dangerous cases.</p><p>Additionally, prior work quantitatively examined DOM XSS vulnerabilities <ref type="bibr">[35]</ref>, finding that while many vulnerabilities are of low complexity, some are the result of highly complex JavaScript interactions. The researchers found that many DOM XSS bugs are the result of vulnerable thirdparty scripts, missing knowledge about browser-provided APIs, unaware developers, or incompatible first-and third-party code. Our findings about vulnerability complexity and the role of third-party code are similar; we compare them in detail in Sections IV-A and IV-C. Differently from previous work, we also explore the role of advertising domains, the effectiveness of static-analysis tools, the distribution of vulnerabilities across and within domains, and design-level prevention mechanisms such as HTML templating.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>E. Web-application scanners</head><p>Web-application scanners are commonly used tools to actively test for a variety of security issues in web applications. Scanners use different methods to detect security issues, of which DOM XSS is one. Prior work has surveyed webapplication scanners, finding them to overlook many classes of vulnerabilities, and to be limited by their ability to crawl websites; however, that work did not focus on DOM XSS, and also did not examine web vulnerabilities in the wild, instead creating a test environment <ref type="bibr">[16]</ref>. Other work reports on the false-positive and false-negative rates of web scanners <ref type="bibr">[36]</ref>. That work also tests against a set of manufactured vulnerabilities and not on in-the-wild vulnerabilities and does not focus specifically on DOM XSS vulnerabilities.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>F. Static-analysis tools</head><p>Static-analysis tools that support JavaScript, such as ScanJS <ref type="bibr">[12]</ref> and JSLint [10], have gained popularity. Those tools statically, without executing code, attempt to detect common programming errors in JavaScript, for example, pointing out the use of dangerous functions or undefined variables. In general, static-analysis tools suffer from more false positives than dynamic approaches <ref type="bibr">[27]</ref>, <ref type="bibr">[31]</ref>. However, static-analysis tools are becoming a generally accepted part of the way that software is developed-passing a static analysis without errors is a requirement often listed in style guides (e.g., <ref type="bibr">[1]</ref>, <ref type="bibr">[5]</ref>). In part, this is due to their ability to be run practically and repeatably with little setup, for example, in nightly builds <ref type="bibr">[27]</ref>. Some vulnerability detection tools, like Burp Suite <ref type="bibr">[32]</ref>, also have a static-analysis component. However, the degree to which static-analysis tools can detect and prevent real DOM XSS vulnerabilities is unknown. In addition, JavaScript as a programming language has traditionally been difficult to statically analyze because of its dynamic features (for example, widespread use of the eval function and reliance on dynamic typing). In this work, we study the ability of static-analysis tools to detect DOM XSS vulnerabilities in JavaScript.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>III. METHODOLOGY</head><p>Next, we describe the methodology for our experiments to detect DOM XSS vulnerabilities on the Internet. In Section III-A, we describe how we crawled websites and which web pages we visited. In Section III-B, we discuss the specifics of the taint-tracking engine we developed. In Section III-C, we describe how we confirmed vulnerabilities. Finally, we detail the methodology for testing static-analysis tools in Section III-D.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>A. Crawling for DOM XSS vulnerabilities</head><p>We first crawled the Internet using a browser instrumented to perform taint tracking. The browser collected information about what data flows occurred in the page, and output a log file detailing the flows and the encoding methods applied. Then, for a subset of flows, we tested whether the flow was exploitable by generating example inputs crafted to deliver a payload to the sensitive sink. This methodology builds on the methodology used in prior work for detecting DOM XSS vulnerabilities at scale <ref type="bibr">[22]</ref>. We describe the differences from this prior work in this Section and in Section III-C.</p><p>document.write( '&lt;a href="' + encodeURI(document.location) + '"&gt;Link&lt;/a&gt;');</p><p>Fig. <ref type="figure">2</ref>: Modified example of Figure <ref type="figure">1</ref>, in which the code would have a DOM XSS vulnerability if the encoding function was not applied. In this example, the encodeURI function encodes the location so that the double-quote character cannot be injected.</p><p>To crawl websites, we started by visiting the Alexa Top 10,000 websites, a list of the globally most popular websites <ref type="bibr">[11]</ref>. Then, we collected all the links to other web pages on the home page of each website, and randomly selected five web pages that were hosted on the same domain as the original domain to limit our crawl to a manageable size given our resource constraints. This crawl is broader, but more shallow, than Lekies et al.'s <ref type="bibr">[22]</ref>, and the difference between the two offers the opportunity for new insights about the incidence of DOM XSS vulnerabilities (see Section VI-A). We also obeyed the robots.txt directives, which direct automated programs-robots-as to which pages may be traversed <ref type="bibr">[7]</ref>. We automated the process of visiting web pages and extracting the links on a page by developing and using a browser plugin. Whenever any web page did not load correctly-for example, because of a timeout-we attempted to load the same page three times. If the failed page was not the top-level page of a domain, we attempted to load a different web page in the same domain. Crawling occurred during the summer of 2017, roughly four years after Lekies et al.'s work was published <ref type="bibr">[22]</ref>.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>B. Dynamic taint analysis</head><p>Like prior work <ref type="bibr">[22]</ref>, we instrumented Chromium to perform byte-precise tracking of the provenance of each byte of strings in JavaScript. We will not focus on the design of our taint-aware browser because it is not a core contribution of our work and the design is similar to prior work. We have released the source code for our modified, taint-aware version of Chromium and V8, the JavaScript engine used in Chromium (see <ref type="url">https://github.com/wrmelicher/ChromiumTaintTracking</ref>).</p><p>To summarize the design of the taint-aware browser: We first allocated space in each JavaScript string primitive for a one-byte taint value that stores the provenance of each byte of the string. This allows taint information to be precisely propagated during string concatenation or slicing. In addition to the provenance of each string byte, each bookkeeping byte also records which built-in encoding methods have been applied. For example, using the encodeURIComponent JavaScript function will modify the taint information to reflect that the string has been encoded using the encodeURIComponent function. During taint propagation, matching encoding-decoding pairs will cancel each other. For example, if a string is encoded using encodeURIComponent and later decoded using decodeURIComponent, then the string will be identified as having no encoding applied. The taint information is only stored for string types and not for arbitrary JavaScript objects. This prevents tracking across different data types: for example, parsing a string into an integer and then writing the integer to a string would remove all taint information.</p><p>Our browser checks the arguments of sensitive functions (e.g., the eval function or document.write; see Appendix VIII for an exhaustive list) for tainted bytes. If an argument contains tainted bytes, then a record is written to a log file describing the flow, including: the type of taint, the locations of the tainted bytes, the sensitive sink function, and a stack trace. Afterwards, we analyze the logs to determine which flows are potentially vulnerable to DOM XSS attacks and which flows are not.</p><p>Whether a flow is vulnerable depends on the context of the injection in the HTML or JavaScript, the encoding functions that have been applied, and the source and sink types. For example, if we detect that a tainted value is not encoded and begins in the context of an HTML double-quoted attribute, then that flow is potentially vulnerable. However, if the string is encoded using the encodeURIComponent built-in function, then a double quoted attribute is not vulnerable because the encodeURIComponent function encodes the double quote as "%22". Figure <ref type="figure">2</ref> shows code that would have a DOM XSS vulnerability if an encoding function was not applied. This list of potentially vulnerable flows is then tested to decide whether the flow is actually vulnerable to XSS attacks using a process we describe in Section III-C. One example of a potentially vulnerable flow that is not actually vulnerable to DOM XSS attacks is when the application performs custom sanitization of inputs that is not detected by the taint-tracking engine-for example, by halting execution if the input does not match a certain form that is known to be safe. The log files also contain the stack trace for the sink call of each flow. In addition to making the flow more repeatable for post-hoc manual analysis, this allows us to examine the code path that led to the vulnerability (see Section IV-A).</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>C. Attack confirmation</head><p>By crawling web pages using the taint-aware browser described in Section III-B, we generate a list of potentially vulnerable flows. We then simulate an exploit to test those potentially vulnerable flows to decide whether they are actually vulnerable. We experimented with two methods of automatically crafting injections to test: one used by prior work, which appends the injection to the end of the string <ref type="bibr">[22]</ref>; and a novel method that attempts to more accurately pinpoint the specific bytes of the string in which to inject a payload. For the purposes of automatically crafting injections we limited ourselves to URLbased sources (e.g., the document.location.href object and derivatives like document.location.search, document.location.hash, etc.). Those types of potential vulnerabilities are straightforward to generate potential exploits for, and therefore can be easily verified to be actual vulnerabilities. For the same reasons, they are the flows commonly targeted by attackers <ref type="bibr">[26]</ref>.</p><p>The first method (termed method A), used in prior work <ref type="bibr">[22]</ref>, appended the exploit to the end of the URL. The new method (termed method B), which more accurately pinpoints where in the string to inject the payload, attempts to insert the exploit into the bytes of the source string that match The 9th through 13th bytes of the string-starting with the first 't' in test and ending with the last 't' in test. Generated injection URL: example.url.com/path?a=b#&amp;param=INJECT Fig. <ref type="figure">3</ref>: Explanation of injection methods using an artificial example. In Method A, the injection is inserted at the end of the string. In Method B, we attempt to insert the injection into the parameter value that matches the tainted string in the text of the observed argument to the sensitive sink. INJECT marks the point of injection.</p><p>the tainted bytes in the sink. An example is shown in Figure <ref type="figure">3</ref>.</p><p>The log files contain the information about which bytes of the string are tainted and the semantic source label for those bytes (e.g., from the URL). Therefore, we can infer which bytes of the source will make their way into the sink by examining the string that is injected into the sink and comparing it to the source string. The insight behind this method comes from the observation that many of the values injected into sinks are values of parameters provided in the URL. Our method is designed to capture URL parsing in client-side code. It is relatively commonplace for JavaScript code to manually parse query parameters on the client, for example, by parsing the URL looking for the special characters that signal parameters: ?, &amp;, and =. In this way, the URL is often used to pass parameters to other links or to control the display of the web page. While this method of confirming that a flow is vulnerable is extremely simple, in practice we find the combination of both methods to generate 83% more exploits than just the first method (method A).</p><p>To test candidate exploits purely in the browser, i.e., without affecting the website, we limit our candidate exploits to the part of the URL string after the hash (the '#' character), as this segment of the URL string is not sent to the website hosting the page, but only processed internally by the JavaScript running in the browser.</p><p>We also did not craft actual valid HTML and JavaScript exploits for attack confirmation, but rather crafted a unique string that included characters necessary for an exploit (e.g., the single quote character if injecting a value into a single quoted HTML attribute). In our payload, we injected the string marker&lt;&gt;'" and then examined our sink injection log files for this string.</p><p>We believe that avoiding the use of valid HTML and JavaScript in simulated exploits and targeting only the portion of the URL string after the hash-beyond limiting risk to web servers-leads to simulated exploits that are both easier to generate and less likely to be caught by client or server-side filters. Such filters are notorious for being easily bypassable by humans <ref type="bibr">[13]</ref>, <ref type="bibr">[18]</ref>. However, for an automated injection, we wanted our approach to scale to many websites and detect when an exploit could likely be crafted, instead of being filtered by an easily bypassable defense mechanism. To confirm that a flow was vulnerable to DOM XSS attacks, we searched the logs for the unique injection string in the output. To confirm that our methodology did not yield false positives, we randomly sampled 40 flows that our process flagged as vulnerable and manually developed a working exploit. We found that all 40 instances were vulnerable; therefore, we believe that the vast majority of cases found by our automated method were actual vulnerabilities.</p><p>After confirming vulnerabilities, we qualitatively examined a subset of these vulnerabilities for insights into the root cases of DOM XSS vulnerabilities. For each vulnerability that we manually analyzed, a researcher manually reproduced the vulnerability based on the saved stack trace in our log files. Then, we distilled the vulnerability to a small amount of code that could describe the flow of data in the vulnerability. These code snippets were then analyzed to extract the themes common to vulnerabilities. We classified vulnerabilities by complexity and also noted other interesting aspects of the code that had the vulnerability. Our results for this analysis are presented in Section IV-C.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>D. Static analysis</head><p>After we collected a list of confirmed DOM XSS vulnerabilities, one of the analyses we performed was to evaluate the effectiveness of static-analysis tools to detect these vulnerabilities. We sampled our dataset in two ways to create test sets to evaluate the false-positive rate and the rate with which the tested static-analysis tools detect these vulnerabilities. First, we sampled websites that have known vulnerabilities from our dataset of confirmed DOM XSS vulnerabilities, found using methodology described in Section III-C. Then, we sampled from all websites that we visited to measure the false-positive rate. Note that for measuring the false-positive rate, we sampled from all websites, not only from websites where we did not detect a vulnerability. We sampled in this way so that our sampling would not be biased towards sites that might be less buggy. Sampling from our dataset of known vulnerabilities, rather than using manufactured vulnerabilities, has the benefit that we are using real-world bugs. a) Description of static-analysis tools: We evaluated three tools for detecting DOM XSS vulnerabilities: ScanJS <ref type="bibr">[12]</ref>, esflow <ref type="bibr">[4]</ref>, and the static-analysis tools in Burp Suite Pro <ref type="bibr">[32]</ref>. We also attempted to test jsprime <ref type="bibr">[30]</ref>, but were unable to get it to work without crashing. We focused on opensource or inexpensive proprietary tools that statically detect DOM XSS vulnerabilities. There are variety of other, more expensive proprietary vulnerability scanning tools, including: IBM Security AppScan, Acunetix, Trustwave App Scanner, Retina web application scanner, Qualys web inspect, HP Fortify static code analyzer, and Coverity's JavaScript scanner. However, for our application of scanning a large number of domains, these were prohibitively expensive. Prior work has compared a wide variety of these proprietary tools for general purpose vulnerability detection (i.e., not restricted to DOM XSS vulnerabilities), and found them to have comparable error rates to each other <ref type="bibr">[36]</ref>.</p><p>The static-analysis tools that we chose appear to have different tradeoffs. ScanJS is a tool meant to help people avoid coding practices that lead to, among other things, DOM XSS vulnerabilities. As such, it flags code that could be unsafe without aiming to identify whether the code leads to an exploitable bug. For example, it may point out all locations where the document.write function was used with a nonstatic string as an argument. While this is a good practice to avoid, it is not always indicative of a vulnerability. In fact, the majority of cases are benign. Burp Suite attaches a confidence rating to each potential vulnerability that it flags, giving guidance about which findings are most reliable. Burp Suite also receives code from the website by acting as a proxy between the browser and the website, meaning that it has access to code that is dynamically loaded (e.g., by a &lt;script&gt; tag added during execution) unlike the other tools; however, it still is not able to analyze code that is dynamically generated (e.g., by using the eval function). Esflow is unique in that it often attaches source and sink information to its issue reports for easier debugging.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>IV. RESULTS</head><p>We used the taint-tracking and crawling methodology described in Section III to collect a dataset of tainted flows. We visited 44,722 web pages, which had in total 319,481 frames. One would expect that trying to visit five subpages on each domain, we would have visited 60,000 web pages: 10,000 top level pages and 50,000 subpages. However, we skipped loading 1,761 web pages due to robots.txt directives; and we were unable to load 4,094 web pages after three attempts due to timeouts, 462 because Chromium would not load the page (most often due to SSL warnings), and 26 because Chromium crashed when rendering them. Some of the pages unable to be loaded were top-level pages; in that case we also did not visit other pages on that domain.</p><p>We describe how we detected DOM XSS vulnerabilities using our dynamic analyses in Section IV-A. Then, in Section IV-B, we use the results from our dynamic analysis to evaluate different static-analysis tools for detecting DOM XSS vulnerabilities. Finally, in Section IV-C, we describe the qualitative trends that we observed from manually analyzing a sample of our dataset.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>A. DOM XSS vulnerabilities detected using dynamic analysis</head><p>After crawling our set of web pages, we post-processed the generated taint-tracking logs to generate a list of observed data flows. Each flow has a source, through which an attacker could inject code, and a sink, a sensitive function that consumes data derived from the source of the flow. We tracked flows that have sources that could be potentially manipulated by an attacker, and sinks that could potentially execute JavaScript, including functions that directly execute JavaScript (e.g., eval), functions that inject HTML (e.g., assigning to innerHTML or calling document.write), and JavaScript event handlers. A summary of the sources and sinks that we tracked can be found in Table <ref type="table">I</ref>.    <ref type="bibr">[22]</ref>. *) Excludes the JavaScript location.protocol property as it is not readily exploitable. &#8224;) Applying the uniqueness filter of hosting domain, code location, breakout sequence. &#8225;) not reported in that work. &#167;) Method A appends the injection to the end of the source string. &#8224; &#8224;) Method B inserts the injection into the bytes of the source string which match the tainted bytes in the sink after encodings and decodings have been applied. &#8225; &#8225;) Includes flows from window.name sources because that work includes those exploits.</p><p>Overall, visiting 44,722 pages resulted in 4,140,873 detected flows. We focus on flows with URL sources and HTML or JavaScript sinks, as these are the most straightforward to exploit. Consistently with that, research has generally focused on examining this subset of flows or found it to account for the majority of exploitable flows (e.g., <ref type="bibr">[22]</ref>, <ref type="bibr">[29]</ref>).</p><p>Other flows have preconditions that make automatically exploiting them more difficult at scale. For example, to exploit a cookie flow, an attacker must find a way to manipulate the victim's cookies; for message flows, an attacker must find a potential flow whose code does not check the message origin and also send the message at the proper time, when the receiver is expecting it. With the exception of message flows, URLbased sources account for the largest number of flows to sinks that can execute arbitrary JavaScript (HTML and JavaScript sinks). Hence, these are the flows we analyze, and we show that they lead to many instances of DOM XSS vulnerabilities. Of the 4,140,873 flows we detected, 285,147 (7%) had a URLbased source. a) Confirmed vulnerable flows: We determine whether a tainted flow is vulnerable as follows. We first discard flows in which the tainted value is encoded using a built-in encoding method, for example, the encodeURIComponent function; we are certain that such flows would ordinarily not  III: Summary of the injection methods used to confirm different vulnerabilities. "Only at end" refers to the injection method that inserts the injection at the end of the source (Method A). "Only key-value pair" refers to the injection that inserts the injection in the value of a tainted query key-value pair (Method B). "Both methods" refers to cases where either method would have identified the flow.</p><p>be exploitable. This eliminates 66% of the flows we focus on (URL sources to JavaScript or HTML sinks). We next remove from consideration flows that could not be exploited in Chromium due to Chromium's natural encoding of some URL variables (for example, Chromium automatically encodes the content of document.location.search to prevent the occurrence of any character that would not be allowed in a URL). After removing those types of flows, we determine which of the remaining 1.33% (54,954 flows) of flows are actually vulnerable by attempting injections. A summary of the number of flows removed at each stage compared to previous work with similar methodology <ref type="bibr">[22]</ref> can be seen in Table <ref type="table">II</ref>.</p><p>In our taint-tracking system, we specially mark flows that have multiple, incompatible encodings with a flag that represents the use of multiple encodings, but not specific encodings or in what order. Such flows accounted for 2% of flows overall. While we did not attempt to determine whether these flows were actually vulnerable, there were 716 unique flows of this type that may have been potentially vulnerable (i.e., that could have been included in row 6 of Table <ref type="table">II</ref>). If they had been included, they would account for 12% of potentially vulnerable flows.</p><p>We used two methods to confirm vulnerabilities-each described in Section III-C-based on where to insert the injected payload: inserting the payload at the end of the URL or inserting the payload into the key-value pair from which we observed a flow to a sink. We found that 45% of the confirmed vulnerabilities we detected were due to flows from key-value pairs, 22% of the vulnerabilities were only the result of inserting the payload at the end of the source, and 32% of the vulnerabilities were observed to work with both methods. 2  Table <ref type="table">III</ref> shows the breakdown of how many of the 3,219 unique vulnerabilities came from which injection method. Both methods of injection work in cases where the entire URL is concatenated with markup (i.e., document.location, rather than a specific substring, is included in markup). Our key-value pair injection method identifies vulnerabilities that involve parsing URL parameters, while inserting the injection at the end identifies vulnerabilities in which part of the path or URL besides the URL parameters is used as part of the parameter to the sink.</p><p>To count unique vulnerabilities, we removed duplicates using the same method used as Lekies et al. <ref type="bibr">[22]</ref>: unique bugs 2 Numbers do not add up to 100% due to rounding. The y-axis shows the number of unique vulnerabilities found on a particular domain in log scale. The x-axis shows domains sorted by frequency; for example, ten on the axis shows the domain with the 10th most vulnerabilities. For example, the domain with the most vulnerabilities had nearly 1,987 unique vulnerabilities.</p><p>are identified by their domain, their location in the script, and the context (e.g., inside a double-quoted attribute or the name of an element attribute) of the tainted section of the string argument to the sensitive sink.</p><p>We also computed the number of unique vulnerabilities across different domains, as shown in Figure <ref type="figure">4</ref>. We found that the majority of vulnerabilities come from a handful of domains, and that many domains had only a few unique vulnerabilities or one vulnerability: the ten domains with the most vulnerabilities had in total 2703 unique vulnerabilities; the remaining 354 domains accounted for the remaining 516 vulnerabilities.</p><p>Interestingly, when performing the vulnerability confirmation crawl we observed vulnerabilities in six iframe URLs that were not previously seen in our first crawl. These iframe URLs were part of the confirmation crawl because either they or the top-level pages that included them had previously been marked as potentially containing a vulnerability. The difference in time between collecting data and confirming vulnerabilities was nine days.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>b) Vulnerability attribution by domain and domain category:</head><p>We next attempted to shed light on the cause of the vulnerabilities that we observed by examining where they occurred. Were they due to third-party scripts, old versions of popular libraries, custom code for each website, or other causes? For this measurement, we used the URL of the frame where the vulnerability was found, since this is the context in which an attacker would be able to execute JavaScript, rather than using the URL of the top-level frame. We used the location of the sink as a starting point for determining to which entity to attribute the vulnerability. In particular, we examined the distribution of vulnerabilities in three ways: (1) the domain on the iframe in which the script executed; (2) the domain on which the script was hosted (web pages often import scripts from other domains); and (3) the domain of the top-level page that the user was visiting. Rather than reporting results about individual domains, we report them by the topic category of the Website topic Script topic Fig. <ref type="figure">5</ref>: The count of URL domains and script domains in different website categories. The bar height shows the number of script domains or frame domains with the corresponding category in our dataset of unique vulnerabilities. The y-axis is in logarithic scale. Script domains are the domains that the vulnerable scripts were hosted on. Frame domains are the domain of the frame where the vulnerability was located. Note: the numbers for script domains do not add to 3,219 because some sinks did not have a script URL. This may happen when the sink location is in dynamically generated code.</p><p>domains. We use the Blue Coat K9 classification of domains into topics <ref type="bibr">[2]</ref> for this purpose.</p><p>We found that the vast majority, 2,645 of 3,219 of our unique vulnerabilities (82%), were found to execute inside iframes with domains that were known to serve web advertisements or perform analytics. Other domain types that accounted for many vulnerabilities included shopping and news.</p><p>We also analyzed what type of domains hosted the scripts in which we found vulnerabilities. Similarly to the above result, we found that 2,681 of 3,219 vulnerabilities (83%) were in scripts hosted on advertising and analytics domains. Figure <ref type="figure">5</ref> shows the analysis of the types of script domains and website domains with confirmed vulnerabilities. For this measurement, we used the domain of the script where the sink function call was found. While advertising domains were the most popular source of vulnerable scripts, our data-collection infrastructure did not capture enough information to similarly categorize scripts that did not have potentially vulnerable flows. Hence, while we can report that, in web pages that had at least one flow, 38% of the time the flow originated in a script that was categorized as an advertising script, we cannot determine whether the fraction of advertising-domain scripts that was vulnerable was greater than the fraction of scripts from other domain categories.</p><p>We matched the unique vulnerabilities that we found with the top-level web pages that contained those vulnerabilities in our dataset. Many of the vulnerabilities that we found were on subframes of other web pages, and we wanted to understand how much exposure users would have if they visited the toplevel pages in our dataset. Table <ref type="table">IV</ref> shows the categories of these top-level web pages. Note that there are significantly more data points than unique vulnerabilities because some Script rank % of vulns script was present Fig. <ref type="figure">6</ref>: The percent of stack traces from the dataset of unique vulnerabilities that scripts were found in. The x-axis shows the rank in log scale of the scripts in a sorted list; for example, 10 shows the script that was 10th most frequently present in stack traces. The y-axis is the percent of script URLs that were less than that rank. For example, the first script was present in the stack traces of 0.34% (11 of 3,219) unique vulnerabilities. vulnerabilities were present in web pages that were subframes of multiple web pages. In contrast to Figure <ref type="figure">5</ref>, where the most popular category was web ads and analytics, here the popular topics are news/media (27.7%) and entertainment (12.9%).</p><p>In total, for 282 (8.8%) of the vulnerabilities we found the domain the script was hosted on was different from the domain of the iframe in which the script executed. This suggests that while a non-trivial fraction (8.8%) of vulnerabilities may be caused by developers relying on third-party scripts, the vast majority of vulnerabilities are in the developers' own scripts (or at least scripts hosted locally on their domains). The fraction is smaller than reported in prior work, which found that 22% of code attributable purely to an error by a third party <ref type="bibr">[35]</ref>. This difference could be the result of our different methodology for confirming DOM XSS vulnerabilities.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>c) Vulnerability attribution by script:</head><p>We additionally examined the scripts in the entire stack trace for each vulnerability. The goal of this analysis was to determine whether some scripts occurred in stack traces of vulnerabilities particularly often. Such scripts could be good candidates for adding encoding functions, or for other remediation, as that would prevent many vulnerabilities. At the same time, if some scripts occur in many stack traces of vulnerabilities, this could indicate that developers misunderstand how to correctly use that script.</p><p>For our 3,219 confirmed vulnerabilities, we identified the scripts in the stack trace of each vulnerability, and then counted how many stack traces each script was present in. For this analysis we removed jQuery from our results because many websites use various HTML rendering functions (e.g., the html or append jQuery methods) that are working as intended, but misused by the caller. Hence, we removed any script name that contained the string "jquery" without respect to character case.</p><p>We found that the majority of vulnerable scripts was present in only one unique vulnerability stack trace-implying that the causes of vulnerabilities are unique. Figure <ref type="figure">6</ref> shows the percentage of stack traces that each script is seen in. d) Vulnerabilities in commonly blocked content: Internet folklore has often claimed that ad blocking software protects your computer from XSS vulnerabilities common in ad networks <ref type="bibr">[19]</ref>. Because we found that many of both the target web pages and the vulnerable scripts were located on domains that hosted advertising, we tested how much protection a normal user who uses an ad blocker would have from such vulnerabilities. For this analysis, we used the adblockparser Python library <ref type="bibr">[6]</ref> to simulate what scripts would not be executed if the user was running ad blocking software that obeyed the rules defined in the Adblock EasyList, a popular rule list of advertising content to block <ref type="bibr">[3]</ref>. We counted a vulnerability as being blocked if the Adblock EasyList would block either the script or the entire target URL when the target URL is loaded inside an iframe and not as the main frame. We found that, of the 3,219 unique vulnerabilities, 2,039 (63%) would have been blocked by this simulated configuration of Adblock.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>B. Effectiveness of static-analysis tools</head><p>We next examine whether the vulnerabilities we found could be detected at development time using off-the-shelf static-analysis tools. We find that most could not, although static-analysis tools sometimes found additional bugs.</p><p>More specifically, with our dataset of confirmed vulnerabilities IV-A, we tested static-analysis tools to evaluate their ability to find the same vulnerabilities that the dynamic analysis found. To target JavaScript, dynamic analysis traditionally is seen as having fewer false positives <ref type="bibr">[27]</ref>; however, static analysis is often more helpful for programmers during development because of the lack of customized analysis for adding new code-developers can set up a static-analysis toolchain once to automatically check new code. In addition, static analysis can be more complete-able to detect vulnerabilities in code that was not executed on a particular run of the program. The majority of the vulnerabilities that were caught in our experiment only by static-analysis tools were in this category. a) Overlapping vulnerabilities: We compared the rates at which different tools, described in Section III-D, found the vulnerabilities that we had previously compiled using the dynamic analysis. We found that the tools we tested usually failed to detect the DOM XSS vulnerabilities from our dataset.  V: The percent of vulnerabilities detected by the dynamic analysis that were detected by different static-analysis tools out of a total of 50 web pages with known vulnerabilities.</p><p>The full results comparing different tools are provided in Table <ref type="table">V</ref>. Notably, while Burp Suite, the most promising tool, had a low rate of finding the same errors as the dynamic analysis, it pointed out many potential issues not found by the dynamic analysis. We next describe these findings in more detail.</p><p>We randomly sampled 50 of the 3,219 unique bugs we found using our dynamic analysis. Then, for the two tools that scanned JavaScript code, we downloaded the web page where the vulnerability was located, and all the accompanying JavaScript, and used the downloaded JavaScript as input to the tools. We included any JavaScript embedded in HTML in addition to externally loaded JavaScript. Because the tool was examining the web page statically, we were not able to include JavaScript that is dynamically loaded during the execution of the page. For Burp Suite, we loaded the web page in the Chromium web browser and connected Burp Suite to the browser via a proxy. In this way, all the JavaScript requested by the browser was analyzed by Burp Suite's static-analysis tool. Therefore, it was able to access external scripts that were dynamically loaded during execution.</p><p>We counted a tool as successfully identifing one of these vulnerabilities if it output any error message related to DOM XSS referring to the code where the sink for the vulnerability was located. For Burp Suite and esflow, we manually reviewed all messages and counted how many referred to the exact line number and character offset of the sink that was vulnerable. For ScanJS, we counted messages that referred to the same script location (line number and script name) as the vulnerable sink, provided that they warned of a potential XSS vulnerability. We explicitly terminated the analysis of any program that took longer than one hour. This affected four of the 50 web pages tested with esflow. In practice, we believe that this is realistic in that static-analysis tools must give results within a reasonable time to be useful to developers. We do not count as matches cases in which the tool detects a vulnerability that was not a part of our dataset. b) False positive rates: We compared the false positive rates of the tools by sampling tool output and manually deciding whether the particular snippets of code that the tool flagged could be exploitable. We found Burp Suite to have no false positives, while the other tools had many false positives. Table <ref type="table">VI</ref> shows the results for all tools.</p><p>We randomly sampled 50 out of all the URLs that we visited in our dataset. We randomly sampled from all URLs, and not just URLs for which we did not confirm vulnerabilities, so that our results for false positive rates would not be biased towards websites that are more secure, and therefore more likely to have false positives than true positives. We then  analyzed the scripts on those pages with each of the tested static-analysis tools. Each tool would report findings on the pages related to DOM XSs. We randomly sampled 20 of those findings, except for esflow, which only reported 19 findings.</p><p>We then manually examined each finding, and the piece of code that it referred to, to determine whether that piece of code could be exploitable. In doing so we aimed to simulate how a developer assessing the same code and reviewing the output of the tool would categorize the bug.</p><p>Our guiding criteria for manually counting true and false positives in the tools' output was to look for flows from exploitable sources (e.g., URLs, cross-origin messages) to exploitable sinks (e.g., document.write, eval) without encoding that would render the flow benign. Thus, we counted flows as true positives even if the identified block of code was not executed during the page load during which our dynamic analysis detected a vulnerability. This may happen because the function is dead code, or because that particular page load did not happen to execute the vulnerable function. We believe this method of measuring false positives is a conservative estimate, because in reality some of the identified vulnerabilities may be in dead code. We also did not include findings that were not related to DOM XSS vulnerabilities-for example, warnings about bad coding practices-as either false positives or false negatives. We aimed to measure the number of actionable vulnerabilities that could be detected by our tested staticanalysis tools.</p><p>Deciding whether a "bug" reported by a static-analysis tool is exploitable was a judgment call. We believe that any bias in the judgment is likely to be biased towards marking something as non-exploitable when it could be exploited, because it is easier to show how a piece of code might be dangerous, but much harder to confirm that code is safe in all cases. In practice, we believe this closely matches how an engineer reading the output of such a tool would label the output.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>C. Qualitative trends in DOM XSS vulnerabilities</head><p>To gain greater insight into the causes that give rise to DOM XSS vulnerabilities, we manually, qualitatively analyzed two subsets of the vulnerabilities detected by our dynamic analysis. First, we randomly selected from the unique vulnerabilities; however, we noticed that a large portion of these vulnerabilities was semantically very similar, despite being unique bugs according to our uniqueness criteria (script location, hosting domain, and context; also used in previous work <ref type="bibr">[22]</ref>). Therefore, we also selected a separate subsample of vulnerabilities, in which we first randomly selected 20 domains on which vulnerabilities had been found, and then selected a random vulnerability on each domain. This allowed us to get a sample that is conceptually more representative of the types of bugs that occur across different domains, and hence of the types of problems that are likely to be encountered by different organizations. A summary of the trends we observed is located in Table <ref type="table">VII</ref>. a) Vulnerability complexity: First, we found that some bugs were extremely simple, such as concatenating the entire URL into an HTML or JavaScript execution function. Examples of why this happened were creating a form where the form submission attached a return URL for the current page, or passing the web page's URL as a query parameter for the source of an iframe. In addition to simple concatenation, we also found cases where the URL was stored in a non-local variable that could be assigned to in code that was far away from the sink (e.g., in a different file or function). b) Failed mitigation behaviors: In addition, for eight of the forty vulnerabilities the relevant code was more complex and spanned multiple functions. Three of those eight utilized custom template processing that did not perform encoding based on the context in the template, resulting in insecure templating code. Another vulnerability was due to an attempt to perform custom, but highly incomplete, filtering-removing all instances of &lt;script&gt; tags, but still leaving open many other ways for an exploit to occur, for example, by using event handler code like &lt;img src=x onerror='INJECTION'/&gt;. For two other vulnerabilities, code involved in the flow was dynamically generated using the eval function, meaning that such code would typically not be visible to static-analysis tools.</p><p>We did not observe any failed attempts to use custom encoding functions. Combined with the fact that many of the bugs were shallow-a finding echoed by <ref type="bibr">[35]</ref>-this suggests that perhaps engineers were not aware that URLs could contain characters that could be used to inject markup.</p><p>When manually reproducing vulnerabilities, we also observed cases where complex control-flow paths must be followed to execute the vulnerable piece of code. For example, we could not reproduce one specific vulnerability until realizing that the vulnerable code was only executed if the screen width was larger than 1,024 pixels, as it had been in our original data collection. In another case, the vulnerable section of code was executed on some page loads but not on others. We discuss code coverage further in Section VI.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>V. LIMITATIONS</head><p>Despite being less straightforward to automatically exploit in the context of a live website, other types of flows besides the ones we focused on (URL to HTML and JavaScript flows) may also be vulnerable. For example, we observed (during manual analysis) a flow from a cookie source to an HTML sink that could be exploitable by a second flow from a URL source into a cookie sink on the same web page. The page could be exploited by crafting a special URL, from which content would flow through the document's cookie into the HTML sink. Other work has observed XSS vulnerabilities that derive from cookie sources and could be exploitable by web attackers <ref type="bibr">[39]</ref>. Vulnerabilities that exploit the JavaScript postMessage API have also been reported <ref type="bibr">[33]</ref>. Location sinks can be leveraged to create more potent phishing websites, in which an attacker may craft a URL that points to the target website but is redirected to a phishing website via assignment to document.location in JavaScript. A victim might assume that they were on a benign website because the hostname in the URL they clicked on was benign. We speculate that these other types of flows might be similar to the types of flows we study here and would be a good avenue for future work.</p><p>We sampled only a subset of the web pages on the Internet, and on the pages we sampled, we did not exercise much dynamic functionality-for example, by clicking on webpage elements or entering text-nor were we able to visit web pages behind log-in barriers. On one hand, this allowed our analysis to scale to large numbers of vulnerabilities and websites, but on the other, the vulnerabilities we detected may not be representative of all vulnerabilities. Nonetheless, we found many vulnerabilities through our analyses. Our manual analysis of vulnerabilities may also exhibit similar biases: We performed a more in-depth analysis only of a subset of our results. This subset was by necessity small so that it would be feasible to manually analyze. We do not suggest that the examined vulnerabilities are representative of our dataset, but we analyzed them in depth to give greater insight into at least some vulnerabilities.</p><p>Due to slight differences in methodology, the comparison of our results to previous work may not be perfectly accurate. Differences in results may be due to implementation differences, although for the parts of methodology that are shared between our work and Lekies et al.'s <ref type="bibr">[22]</ref>, we tried to reproduce previous methodology faithfully.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>VI. DISCUSSION</head><p>We performed this study to measure the prevalence of DOM XSS vulnerabilities, evaluate and inform the design of static-analysis tools, and assess the viability of other methods for preventing DOM XSS vulnerabilities. We first discuss how the raw results of our measurement study compare to previous work that used similar methodology to measure XSS vulnerabilities (Section VI-A), teasing out which differences are the result of methodology and which reflect a change in the prevalence of DOM XSS vulnerabilities. We then leverage our quantitative and qualitative analyses of the sources and nature of DOM XSS vulnerabilities to discuss the weaknesses of some suggested countermeasures (Section VI-B). Finally, we further interpret the results of our examination of staticanalysis tools and suggest how these tools could be improved to catch more DOM XSS vulnerabilities (Section VI-C).</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>A. Comparing measurements on DOM XSS vulnerabilities</head><p>Our methodology for detecting DOM XSS vulnerabilities replicates and builds on Lekies et al.'s <ref type="bibr">[22]</ref>. We extend Lekies et al.'s methodology by adding another method for determining whether a bug is exploitable, namely, inserting a potential exploit into query key-value pairs rather than just at the end of the URL. When inserting the injection at the end of the URL, we find that a roughly similar fraction of flows is vulnerable as reported by Lekies et al. <ref type="bibr">[22]</ref>. However, using both methods of inserting the injection, we identify 83% more confirmed vulnerabilities than when just inserting the exploit at the end. This suggests that previous work, as well as our own, may substantially undercount the number of vulnerable flows.</p><p>Our methodology differed from that of <ref type="bibr">Lekies et al.</ref>, in that we visited twice as many top-level domains, but fewer subpages for each domain (see Section III-A). We believe this is the main cause of different findings for the number of domains that have at least one vulnerability. Previous work found 9.6% of domains to have at least one vulnerability, while we found 3.8% of domains to have one. Interesting, this shows that vulnerabilities are not systemic, i.e., a domain that has at least one vulnerability is not likely to have that vulnerability (or different ones) on a preponderance of pages. Since some parts of pages hosted on the same domain are often shared across most pages, this implies that DOM XSS vulnerabilities are usually not in this shared content.</p><p>Where our methodology and that of Lekies et al. are most directly comparable-when relying only on the simpler method of confirming vulnerabilities and examining the ratio of vulnerabilities to number tainted flows or to number of pages visited-our results are generally similar, although overall our results suggest an increase in the number of vulnerabilities over time. In our work, we find more flows per page-on average, 92.6 flows per page compared to an average of 48.5 flows per page in Lekies et al.'s work. Additionally, normalizing by the number of flows we found more vulnerabilities: We found 0.04% of flows to be vulnerable, while Lekies et al. reported 0.03%. Normalizing by the number of pages visited, we also found more vulnerabilities: 1,754 vulnerabilities on 44,722 pages (3.9%); previous work found 6,167 vulnerabilities on 504,275 web pages (1.2%). We speculate that this difference is because JavaScript programs are becoming more complex, and as a consequence DOM XSS vulnerabilities are becoming more frequent.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>B. Preventing DOM XSS</head><p>In Section IV-A, we showed that the unique vulnerabilities typically did not involve many of the same scripts: the stack traces of the exploits of different vulnerabilities were generally composed of different scripts. We interpret this to mean that most DOM XSS vulnerabilities are due to custom code, and not library code that is shared by many domains.</p><p>One way to prevent DOM XSS vulnerabilities is to detect them before the software is released. We believe that a promising direction for finding DOM XSS vulnerabilities at scale is using techniques that analyze larger portions of the program space. The problem of code coverage of dynamic analysis techniques is not new or specific to DOM XSS vulnerabilities; however, it can be a bigger hurdle for large-scale analysis of web applications than for traditional programs. Running many versions of a web application may require a large amount of network bandwidth for reloading web pages, which can make it difficult to scale. Solutions that avoid reloading the page to explore more sections of the program should be explored, as well as methods to force execution down alternate program paths. In particular, work on fuzzing parameters for traditional XSS <ref type="bibr">[17]</ref> and on forcing JavaScript execution through different code paths <ref type="bibr">[20]</ref> holds promise.</p><p>Our analysis also provides additional evidence of the risks of developing custom versions of common design patterns. While using design patterns for templated HTML-a practice analogous to parameterized SQL queries-is generally a good approach to preventing DOM XSS vulnerabilities, it is important to correctly implement the details. For example, we observed three instances of bespoke HTML template implementations that did not apply encoding functions to the values of the templates. In general, custom templating implementations can be error prone, because differences in context can be easy to overlook. For example, to be safe from XSS, a value in a templated HTML statement that is inside a script tag must first have encoding applied for the HTML parser and then for the JavaScript parser.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>C. Static-analysis tools</head><p>We next further interpret the results of running the staticanalysis tools on a sample of vulnerable scripts (see Section IV-B). These results suggest that many vulnerabilities may currently escape both static and dynamic analyses. We also leverage our results to suggest ways to extend static-analysis tools to catch more bugs.</p><p>In Section IV-B, we measured the false positive rate of different static-analysis tools and the rate at which tools correctly identified vulnerabilities from our dataset of known vulnerabilities using dynamic taint tracking. For the false positive rate, we empirically sampled the tools output and manually decided whether a tool's finding was a false positive or a true positive. However, we were unable to empirically measure the false negative rate overall. This is because it was not feasible for us to know all possible vulnerabilities in noncontrived application. Instead, we measure the rate at which static-analysis tools can detect known bugs that are detected with a different methodology.</p><p>Our analysis of Burp Suite, the best-performing staticanalysis tool we tested, showed low false-positive rates but also an inability to detect most of the vulnerabilities identified by the dynamic analysis. Together, these two measurements imply that the static-analysis tools were detecting largely different vulnerabilities that our analysis. The dataset of vulnerabilities with which we tested static-analysis tools, however, was limited to vulnerabilities detected through our dynamic analysis. In our test, we visited a large number of web pages but did not attempt to exercise much of the web application's functionality, for example, by clicking on fields, entering data into forms, or sending messages to pages. It is likely that such activities would reveal more vulnerabilities. In addition, in our dataset, we found many of the vulnerabilities to be shallow, in that they involved a straightforward concatenation of data from a source into the parameter to a sensitive sink function. This is similar to prior findings <ref type="bibr">[35]</ref>. Indeed, it could be that the majority of vulnerabilities are more complex and would be better detected by static-analysis tools. Given that we know that there are many bugs that escape either analysis, we speculate that there may be many more bugs that escape both analyses.</p><p>ScanJS generally appears to identify poor coding practices that lead to DOM XSS vulnerabilities, rather than detecting such vulnerabilities. Indeed, many of the suggestions that the tool gives revolve around the use of certain functions being dangerous (e.g., document.write or eval). While ScanJS unfortunately did not detect many vulnerabilities on our dataset, we believe it to be useful for, e.g., enforcing coding standards.</p><p>Based on our experiments, we can make some recommendations for improving tools. One area where static-analysis tools could improve is the ability to track flows across function boundaries. We found a non-negligible number of such vulnerabilities (20% in the domain sampling setting in Section IV-C) and tracking such flows can be difficult statically, especially when there are many branches. Another aspect of static-analysis tools that could use improvement is the ability to track flows that go through objects. For example, a tainted string is sometimes stored as the key or value in a JavaScript object and later used in a computation. Finally, one constraint of static-analysis tools that is especially limiting in JavaScript is the inability to analyze dynamically generated code. A hybrid static-analysis tool that analyzes new code before it is executed in the browser might be better able to detect such vulnerabilities.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>VII. CONCLUSION</head><p>We studied how to detect and prevent DOM XSS vulnerabilities in JavaScript code. In this work, we improved on the methodology to confirm DOM XSS vulnerabilities, finding 83% more vulnerabilities than by using previous methodology applied to the same dataset. We used our methodology for detecting DOM XSS vulnerabilities to empirically measure the prevalence of DOM XSS vulnerabilities on the Internet, finding them to be more common now than when previously measured in 2013. With our collected dataset of DOM XSS vulnerabilities, we also compared the ability of static-analysis tools to detect the same bugs that dynamic analysis techniques found, finding static-analysis tools to detect different types of bugs, with little overlap. A summary of our findings can be found in Figure <ref type="figure">7</ref>. We are in the process of notifying the website owners of the vulnerabilities we discovered.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Measurement</head><p>&#8226; Our key-value pair injection method in conjunction with prior method found 83% more vulnerabilities than found using only prior method of injection <ref type="bibr">[22]</ref>.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. IV-A</head><p>&#8226; We identified what has changed and what remains the same in DOM XSS over a 4-year span by building on top of a prior experiment.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. VI-A</head></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>XSS trends</head><p>&#8226; We found more tainted flows overall and a higher rate of vulnerable flows than previous work, which suggests that DOM XSS is getting worse.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. VI-A</head><p>&#8226; Vulnerabilities are concentrated on a small number of iframe owners and script hosting sites.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. IV-A</head><p>&#8226; 83% of vulnerabilities are due to code hosted on advertising and analytics domains.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. IV-A</head><p>What contributes to XSS</p><p>&#8226; DOM XSS vulnerabilities are likely not systemic within domains.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. VI-A</head><p>&#8226; Vulnerabilities are often in unique, custom code, not in shared libraries.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. IV-A</head><p>&#8226; Incorrectly implemented bespoke HTML templating, a defense against XSS, introduces XSS vulnerabilities.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. IV-C</head></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>XSS prevention</head><p>&#8226; Ad blocking would block many of the vulnerabilities and is an effective client-side protection tool.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. IV-A</head><p>&#8226; Incorrectly implemented templating leads to vulnerabilities and possibly false sense of security.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Sec. VI-B</head><p>&#8226; The three popular (low-cost or free) static-analysis tools we tested are not effective at finding the vulnerabilities found using our dynamic tool; however, Burp often finds vulnerabilities not found by our tool.</p><p>Sec. VI-C Fig. <ref type="figure">7</ref>: Summary of findings.</p></div><note xmlns="http://www.tei-c.org/ns/1.0" place="foot" n="1" xml:id="foot_0"><p>https://github.com/wrmelicher/ChromiumTaintTracking document.write( '&lt;a href="' + document.location + '"&gt;Link&lt;/a&gt;');</p></note>
		</body>
		</text>
</TEI>
