This post summarizes the doctoral dissertation "Semantics and Types for Safe Web Programming" by Arjun Guha (Brown University, 2012).
TL;DR
JavaScript is the "Wild West" of programming—dynamic, quirky, and notoriously difficult to secure. This dissertation tames the chaos by defining $\lambda_{JS}$, a clean formal core for the language, and a novel type system that handles JavaScript’s most "un-typable" features to prove the security of web sandboxes and find real-world bugs.
What Problem Does This Paper Address?
JavaScript is the lingua franca of the web, yet it was designed without a formal specification. Its behavior is defined by massive, informal standards and inconsistent browser implementations. This makes it nearly impossible to reason about program correctness or security.
The author identifies three specific gaps:
- Complexity: JavaScript has "quirks" like the
thiskeyword,withstatements, and implicit type conversions that confound traditional analysis. - Dynamic Objects: Unlike Java or C++, JavaScript objects are essentially extensible dictionaries. They can change shape at runtime, making standard static type systems too rigid.
- Sandbox Security: Web platforms use "sandboxes" (like ADsafe) to run untrusted third-party code. These sandboxes rely on ad-hoc manual reviews and "linting" tools which are frequently bypassed by clever exploits.
Methods
The dissertation approaches these problems through Semantics Engineering:
- $\lambda_{JS}$ and Desugaring: Instead of trying to analyze the 200+ pages of the JavaScript specification directly, the author created $\lambda_{JS}$, a tiny core calculus (only 3 pages). He then built a "desugaring" tool that translates complex JavaScript into this simple core.
- Flow Typing: To handle JavaScript’s idiomatic use of runtime type tests (e.g.,
if (typeof x === "string")), the author developed a system that integrates type checking with dataflow analysis. This allows the type checker to "narrow" the type of a variable based on the control path. - Fluid Object Types: This is a new type language designed for dynamic objects. It uses string patterns (like regular expressions) to describe collections of fields and presence annotations to track whether a field is on the object itself or inherited through the prototype chain.
- Assisted Refactoring: A tool was developed to infer type annotations by running programs and observing values at runtime, making it easier to add types to existing, undocumented code.
Datasets
The author validated his work using several real-world sources:
- Mozilla JavaScript Test Suite: Used to ensure $\lambda_{JS}$ is "bug-compatible" with actual browser engines like V8 and SpiderMonkey.
- Chrome Experiments & Google Gadgets: A collection of 15 third-party programs (~4,500 lines of code) used to test the type-checker’s ability to document and verify existing code.
- ADsafe: The author analyzed the 2.5 KLOC (thousand lines of code) ADsafe runtime library and its sample widgets to verify security properties.
- WebIDL: 4,000 lines of interface definitions for browser APIs were processed to create a type environment for the DOM.
Key Insights and Ideas
- Desugaring is the Secret Sauce: By showing that "messy" features like
withandthiscan be translated into simple functions and variables in $\lambda_{JS}$, the author proved that JavaScript is actually lexically scoped under the hood. - Types as Security Proofs: The author demonstrated that security isn't just about "blacklisting" bad words. By defining a
Widgettype, he could prove that if a program type-checks, it physically cannot access dangerous browser capabilities likeXMLHttpRequest. - The "Aha" Moment in ADsafe: While type-checking ADsafe, the author found that a common JavaScript pattern—passing an object to a regex test—could be exploited. If the object has a malicious
toString()method, it can bypass security checks. This led to a real-world exploit and a "shrimp bounty" reward from ADsafe’s creator.
Key Results
- Adequacy: The $\lambda_{JS}$ interpreter produced identical output to major browser engines on thousands of tests.
- Low Overhead: The type system required refactoring only about 6.7% of the code in third-party gadgets, proving it is practical for real-world use.
- Security Vulnerabilities: The tool discovered several previously unknown bugs in ADsafe, including a Firefox-specific exploit and cases where JSLint (a popular tool) allowed dangerous field names.
- Performance: The type-checker is fast, processing benchmarks in approximately one second on standard hardware.
Limitations and Future Work
- No
eval: The system explicitly excludeseval, as it is a "security nightmare" that breaks static analysis. - Strong Updates: The current system doesn't support "strong updates" (permanently changing a field's type), which is common during object initialization.
- Scale: While 7,000 lines were checked, scaling to million-line codebases would require more sophisticated automated type inference.
- Future Directions: The author suggests applying these techniques to other scripting languages like Python and Ruby, and using types to safely bypass expensive runtime security checks to improve performance.
Summary updated