Introduction
Analyzing the security of modern web applications is notoriously difficult due to the highly dynamic nature of JavaScript. Specifically, the ability to generate and execute code at runtime via eval often acts as a "black hole" for static analysis tools, which typically struggle to predict the behavior of code that does not yet exist. This research presents a significant step toward solving this by providing a formal, static information flow analysis for a language that embraces dynamic code generation through staged metaprogramming.
In computer security, information flow analysis is used to ensure that sensitive ("high") data does not leak into public ("low") outputs—a property known as noninterference. While this is well-understood for static languages, it remains a challenge for dynamic ones. By focusing on staged metaprogramming—which uses structured "quotations" (like box and unbox) instead of unstructured strings—the authors establish a mathematical foundation for securing runtime code generation.
The SLamJS Language
The authors introduce a new core language called SLamJS (Staged Lambda JS). It is designed to capture the essential complexities of JavaScript, including dynamic typing and prototype-based inheritance, while adding principled staging constructs.
As shown in the syntax above, SLamJS includes three primary staging constructs:
box e: Quotes a code fragmentefor later use.unbox e: Splices a previously quoted code fragment into a new context.run e: Executes a quoted code fragment.
By using these structured constructs instead of raw strings, the analysis can track the structure of generated code even before it is executed.
Methodology: Staged 0CFA and Augmented Semantics
To analyze SLamJS, the researchers employ a multi-layered approach combining control-flow analysis with dependency tracking.
Augmented Semantics
First, they modify the language's internal operational semantics to include dependency markers. This "augmented semantics" allows the formal tracking of how a value at the end of a program relates to the inputs at the beginning.
Staged 0CFA
Second, they adapt standard 0th-order Control Flow Analysis (0CFA). While traditional 0CFA tracks which functions are called at specific sites, this extended version tracks which "boxed" code fragments flow into which run statements. By layering dependency constraints on top of this CFA, the analysis can detect both direct data leaks and indirect "control-flow" leaks (e.g., branching on a secret).
Formal Verification
Crucially, the analysis is not just algorithmic; it is formally verified. The authors proved the soundness of their analysis using the Coq theorem prover. This ensures that if the analysis deems a program secure, it is mathematically guaranteed to satisfy the noninterference property.
Evaluation and Results
The authors evaluated an OCaml implementation of their analysis on a suite of 19 hand-crafted examples. These benchmarks covered classic indirect flows, Church-encoded booleans, and complex staging patterns where code is nested and executed across different scopes.
Key findings include:
- Precision: An "Improved Analysis" variant successfully distinguishes between identically named variables in different scopes, reducing false alarms.
- Practicality: The implementation correctly identified dependencies in complex scenarios, such as proving that a result did not depend on a specific input even when that input was passed through a
run(box(...))sequence. - Verified Foundation: The mechanization in Coq provides a high-assurance baseline for future research into dynamic language security.
Limitations and Future Work
While a major step forward, the current analysis has limitations. It uses coarse datatypes (treating all numbers as a single type), which can lead to over-conservative results. Furthermore, SLamJS currently lacks imperative loops and exceptions. Future work aims to bridge the "string gap" by developing string analyses that can reliably convert raw eval calls into the structured staged constructs analyzed here, eventually scaling these techniques to full-scale web applications.
Summary updated