Semi-Automated Removal of Eval from Javascript Programs

Semi-Automated Removal of Eval from Javascript Programs

Eval Begone! Semi-Automated Removal of Eval from JavaScript Programs Fadi Meawad Gregor Richards Floreal´ Morandat Jan Vitek Purdue University Abstract understandability, efficiency and safety. As strings passed to Eval endows JavaScript developers with great power. It allows eval may come from any source, including computation, user input or another website, and as eval is capable of performing developers and end-users, by turning text into executable 1 code, to seamlessly extend and customize the behavior of any task , it can serve as a “black hole” both for analysis and deployed applications as they are running. With great power for maintenance; to understand its behavior, one must know comes great responsibility, though not in our experience. In every potential argument. Consider the following JavaScript previous work we demonstrated through a large corpus study expression: that programmers wield that power in rather irresponsible and eval ( x ) arbitrary ways. We showed that most calls to eval fall into a small number of very predictable patterns. We argued that Depending on the value bound to variable x, the state of any those patterns could easily be recognized by an automated heap-allocated mutable value and the bindings of local varia- algorithm and that they could almost always be replaced with bles in scope can be modified as a side effect of evaluating safer JavaScript idioms. In this paper we set out to validate this statement. While some languages can enforce some mod- our claim by designing and implementing a tool, which we icum of data abstraction, JavaScript has very little in the way call Evalorizer, that can assist programmers in getting rid of of encapsulation mechanisms. The impact of an eval can span their unneeded evals. We use the tool to remove eval from over the entire heap. a real-world website and validated our approach over logs The existence of eval is a quandary for those wishing to taken from the top 100 websites with a success rate over 97% perform static analyses on JavaScript code or enforce any under an open world assumption. kind of semantic invariants. With an unknown string, eval has Categories and Subject Descriptors D.2.3 [Software Engi- no locality guarantees, no time or memory bounds, not even neering]: Coding Tools and Techniques—Program editors; a termination guarantee. Since the strings frequently come D.2.7 [Software Engineering]: Distribution, Maintenance, from outside sources and are as such completely unknown, and Enhancement—Restructuring, reverse engineering, and static analyses are forced to assume the worst, losing all reengineering potential gains from the analysis, and dynamic analyses are at best forced to reevaluate every time an eval is encountered. General Terms Languages It is common for researchers to simply ignore it [1, 2, 12, 23], Keywords Dynamic Languages, JavaScript, Reflection, Dy- claim it is very rare [9], assume its use is innocuous [10], namic Analysis or acknowledge its problems but simply produce a warning when it is used [13], resulting in unsound or even unsafe 1. Introduction results. In security literature, in particular, eval is viewed as a serious threat [22], and it is frequently forbidden [16], JavaScript’s eval function gives programmers the ability filtered [6] or wrapped [6], but all of these solutions imply a to run JavaScript code generated at runtime. This gives flexibility or speed penalty. programmers extraordinary flexibility, but at the cost of Most dynamic languages have an eval function or similar feature, and in many it is considered harmful, though not all. The R programming language is an example where eval is a Permission to make digital or hard copies of all or part of this work for personal or key mechanism for language extensibility [17]. In previous classroom use is granted without fee provided that copies are not made or distributed work [21], we showed that eval is ubiquitous in the largest and for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute most popular websites on the Internet; we speculated that over to lists, requires prior specific permission and/or a fee. 1 JavaScript provides several other such functions, such as setTimeout and Copyright c ACM [to be supplied]. $10.00 Function, but we focus on eval for much of our discussion. 75% of evals could be replaced by other mechanisms within gnarly evals take their argument from outside the JavaScript JavaScript. In the present paper, we set out to validate that program. This leaves migration as the third path. This is a claim and end up demonstrating that over 95% of the evals path where we attempt wean users off their addiction to eval invocations in real websites can be replaced with less general by showing them how to rewrite their code without it. We mechanisms with very little work on the programmer’s part. recognize that eval can be useful either because the program Our working hypothesis is that no more than 5% of all evals actually requires the flexibility that it brings to the table or are actually needed. because it is a handy crutch during an early phase of the Examining the most common use cases of eval, we observe program’s life, but argue that in most cases, there is a point that programmers decide to use eval for one of the following where the same functionality can be achieved without it. reasons: they use eval to parse JSON and malformed-JSON. We propose a simple dynamic approach to get rid of eval. For parsing JSON, until recently some browsers did not The technique we present in this paper detects how eval is have native JSON parsers, but this is no longer the case. used through dynamic analysis of calls to eval. It categorizes Current native JSON parsers does not handle malformed- the strings passed as argument to each call site of eval and JSON. Others use eval to access or modify properties based proposes generic replacements that do not involve calls to on user input. It might require some small parsing to handle eval. We realize our proposal in a tool which we name the the input without using eval. And the last category of users is Evalorizer. This tool aids in the evolution of eval-utilizing the one executing code coming from third party with no prior code to eval-free code by presenting replacements that fit the knowledge of its shape. Most modern browsers have native real use of each call site. This allows programmers to use JSON parsers that is usually faster than eval for a JSON string, eval in the development phase, when its flexibility may be and it only accepts JSON strings, thus improving the security. most beneficial, then to gradually remove it in preference Parsing user input to extract the correct expression might be of simpler, safer and more readable solutions. We use a hard to program manually, but once eval is used we loose the grammar inference algorithm to determine the used patterns possibility of using static analysis as well as suffer the risk of any given eval call site as a restricted subset of JavaScript’s of code injection. When running third party code, we have grammar, then generate succinct code that will handle all noticed that it is usually the same or at least has the same the same patterns, but with greater constraint. Additionally shape. But the user would not be able to figure out what kind to aiding developers in removing evals, Evalorizer is also of strings are being executed and how to write a parser for capable of dynamic eval removal. This technique can be used them. for verification, for measurement, or to perform analyses We see three possible roads to an eval-free Internet: prohi- on otherwise hostile programs without intervention of the bition, prevention and migration. Prohibition is conceptually original developers. Though we focus on JavaScript, eval is the simplest. If calls to eval are simply disallowed, the prob- certainly not unique to that language. Our implementation lem is no more, but this comes at a cost in expressive power. is specific to JavaScript, but our techniques are not, and are Eval occasionally is the only practical way to achieve a certain applicable to any language which provides a function similar degree of customizability in the behavior of a website, so for- to eval as well as other, safer reflective capabilities. bidding it would reduce the expressive power of JavaScript. In We evaluate the benefits of our approach by successfully other cases, eval is a way to delay design and implementation migrating five real world websites. Furthermore, we use data decisions, thus allowing developers to deploy applications obtained from the 100 top websites and evaluate the quality faster. Again, losing that would decrease the usefulness of the of our inference algorithm. We also measure the runtime language for rapid development. The second path is to pre- performance impact of our technique. vent eval by proving, ahead of time, that they are not needed. Our tools and data are freely available at: This can be done through static program analysis techniques which construct approximate models of the program and can http://sss.cs.purdue.edu/projects/dynjs determine which strings flow into a particular eval call site. 2. Use case Indeed, there are many constant and quasi-constant strings that are passed in as argument to eval. A static analysis could We motivate Evalorizer with a typical use case. Assume that be coupled with a compiler optimization that compiles the a web programmer, finding that the existing calls to eval on evals to equivalent code.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    14 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us