ADsafety: Type-Based Verification of JavaScript Sandboxing

Joe Gibbs Politz, Spiridon Aristides Eliopoulos, Arjun Guha, Shriram Krishnamurthi

Introduction

Modern web pages are often "mashups" that embed third-party JavaScript—such as advertisements, maps, or social widgets—alongside trusted first-party code. Because all JavaScript on a page typically runs in the same global environment, a malicious widget can hijack the page, steal sensitive data, or load external malware. To mitigate these risks, companies employ language-based web sandboxes like Yahoo's ADsafe, Google's Caja, or Facebook's FBJS.

The architecture of a typical JavaScript sandbox, showing the flow from static checks to runtime wrapping.

These sandboxes generally use a two-pronged defense: a static checker (like JSLint) to reject dangerous code patterns before execution, and a trusted runtime library to intercept and sanitize actions dynamically. However, JavaScript's complexity—including implicit type conversions and prototype chains—makes manual verification of these sandboxes extremely difficult. This paper introduces a novel type system to formally verify the security properties of the ADsafe sandbox.

Formalizing Security with Type Systems

The authors developed a custom static type system to bridge the gap between static linting and runtime enforcement. Their approach involves several key innovations:

  • Formalizing the Linter: The rules of JSLint were formalized into a recursive type called Widget. This type describes the exact subset of JavaScript allowed by the sandbox, explicitly blacklisting dangerous object fields.
  • String Set Types: Since JavaScript objects are accessed via strings (e.g., obj["property"]), the authors enriched their type system with "string sets." This allows the checker to mathematically prove that blacklisted property names are never used for lookups.
  • Flow Typing: To handle JavaScript's reliance on dynamic checks (like typeof), the system uses flow typing to narrow types within specific code branches, reducing false positives.
The Ref type definition, illustrating how dangerous properties like 'eval' and '__proto__' are blacklisted (marked with skull icons).

By defining the "shape" of linted code, the authors could mechanically prove how the runtime library would handle untrusted inputs. As shown in the type definitions, security is enforced by ensuring that references to sensitive properties are effectively "poisoned" or absent from the accessible type space.

Key Results and Vulnerabilities Found

The ADsafety type checker successfully verified the core safety guarantees of ADsafe while uncovering several severe vulnerabilities in its production code:

  • Formal Guarantees: The authors proved that if a widget passes JSLint and the runtime type-checks, it cannot load new code or obtain direct DOM references.
  • JSLint Exploits: The system revealed that JSLint inadvertently allowed underscores in quoted field names, which could be exploited to trick the runtime and steal the global window object.
  • Runtime Logic Bugs: The checker flagged a missing type check in ADsafe's CSS styling function. An attacker could pass an object with a malicious toString method to bypass regex filters and execute arbitrary JavaScript.
  • Disproving Non-Interference: While ADsafe claimed widgets were isolated from each other, the authors found they could communicate by mutating shared built-ins like Array.concat.

Conclusion

The ADsafety project demonstrates that the ad-hoc rules of static linters can be rigorously formalized as types. By attempting to make the sandbox code compile under a strict type system, the authors identified exploits that had evaded human reviewers for years. While some complex logic required manual refactoring to verify, the tool proved highly efficient, verifying the 1,800-line ADsafe runtime in just 20 seconds. This research highlights that type systems are a powerful, automated tool for securing the "slippery" environment of modern JavaScript.

Summary updated

BibTeX
@inproceedings{politz2011adsafety,
  author = {Joe Gibbs Politz and Spiridon Aristides Eliopoulos and Arjun Guha and Shriram Krishnamurthi},
  title = {{ADsafety: Type-Based Verification of JavaScript Sandboxing}},
  booktitle = {USENIX Security},
  year = {2011},
  url = {https://openreview.net/forum?id=qxBFnEYRsI},
}
Figure