modern type systems for dynamic languages ravi chugh

173
Modern Type Systems for Dynamic Languages Ravi Chugh

Upload: rosaline-arnold

Post on 12-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Modern Type Systems for Dynamic Languages Ravi Chugh

ModernType Systems for

Dynamic Languages

Ravi Chugh

Page 2: Modern Type Systems for Dynamic Languages Ravi Chugh

2

Web Platform

ScriptingLanguages

Untrusted

Code

Page 3: Modern Type Systems for Dynamic Languages Ravi Chugh

3

Web Platform

Goal: Rigorous Foundations

Untrusted

Code

ScriptingLanguages

Types

+ Logic

Page 4: Modern Type Systems for Dynamic Languages Ravi Chugh

4

1995

<html>

</html>

Username

Login

Password

Web Page = Static HTMLWeb Page + LinksWeb Page + Client-Side Code

<script type=“javascript”>

loginButton.onclick = function () {

if (passwordBox.value == “”) {

alert(“Enter password!”);

} else {

...

} }

</script>

Page 5: Modern Type Systems for Dynamic Languages Ravi Chugh

5

1995

… an easy-to-use “scripting language”as a companion to Java

No Static Types

“Dynamic” Features Make itEasy to Experiment…

Brendan Eich,JavaScript creator

Page 6: Modern Type Systems for Dynamic Languages Ravi Chugh

2014

Page 7: Modern Type Systems for Dynamic Languages Ravi Chugh

2014Asked whether Gmail would ever open up an API for developers to code add-ons and plug-ins with more official support and stability, Kowitz suggested it was unlikely. Gmail is based on “hundreds of thousands” of lines of JavaScript, Kowitz said, and it's a code base that “changes so quickly.” That said, Jackson said the Gmail team "loves" to see third-party functionality being developed, and the team tries to make developers aware of changes they know will affect those products. Article on LifeHacker

Page 8: Modern Type Systems for Dynamic Languages Ravi Chugh

2014Asked whether Gmail would ever open up an API for developers to code add-ons and plug-ins with more official support and stability, Kowitz suggested it was unlikely. Gmail is based on “hundreds of thousands” of lines of JavaScript, Kowitz said, and it's a code base that “changes so quickly.” That said, Jackson said the Gmail team "loves" to see third-party functionality being developed, and the team tries to make developers aware of changes they know will affect those products. Article on LifeHacker

“Scripting”?!?

Page 9: Modern Type Systems for Dynamic Languages Ravi Chugh

9

http://stackoverflow.com/tags02-26-14

Count Tag599,605 C#583,322 Java555,164 JavaScript532,851 PHP273,086 Python268,725 C++129,483 C

97,154 Ruby15,044 Haskell

2,175 OCaml

2014

JavaScript isPervasive

Page 10: Modern Type Systems for Dynamic Languages Ravi Chugh

10

http://stackoverflow.com/tags02-26-14

Other Dynamic Languages Are, Too

2014 Count Tag

599,605 C#

583,322 Java

555,164 JavaScript

532,851 PHP

273,086 Python

268,725 C++

129,483 C

97,154 Ruby

15,044 Haskell

2,175 OCaml

JavaScript isPervasive

Page 11: Modern Type Systems for Dynamic Languages Ravi Chugh

11

News

ShoppingBanking 2014

JavaScript isPervasive

Page 12: Modern Type Systems for Dynamic Languages Ravi Chugh

12

News

ShoppingBanking

Third-Party Ads

2014

JavaScript isPervasive

Page 13: Modern Type Systems for Dynamic Languages Ravi Chugh

13

News

ShoppingBanking

Third-Party Ads

Third-Party Apps

2014

JavaScript isPervasive

Page 14: Modern Type Systems for Dynamic Languages Ravi Chugh

14

2014

JavaScript isPervasive

Third-Party

Page 15: Modern Type Systems for Dynamic Languages Ravi Chugh

15

No longerjust “scripting”

2014

… an easy-to-use “scripting language”as a companion to Java

Page 16: Modern Type Systems for Dynamic Languages Ravi Chugh

16

1. Development Tools2. Reliability / Security3. Performance

But JS is hard to reason about!

Significant Interest For:

Page 17: Modern Type Systems for Dynamic Languages Ravi Chugh

17

Static TypesMy Ph.D.

JavaScript, Python, Ruby, PHPShare Common Challenges

New Techniques Apply Broadly,Even to Statically Typed Languages

for Dynamic Languages

Page 18: Modern Type Systems for Dynamic Languages Ravi Chugh

18

Describe sets of run-time values

“integers”

“booleans” “objects with foo field”

“non-nullpointers”

“urls that are safe to visit”

Static Types

Page 19: Modern Type Systems for Dynamic Languages Ravi Chugh

19

Prevent classes of run-time errors

“integers”

“booleans” “objects with foo field”

“non-nullpointers”

“urls that are safe to visit”

“multiplyint and bool”

“navigating to evil.com”“redirect to evil.com”

“foo fieldnot found”“foo fieldnot found”

“null pointer exception”

“null pointer exception”

Static Types

Page 20: Modern Type Systems for Dynamic Languages Ravi Chugh

20

f(x)

“expected args”“actual args”

Type1 Type2

“function call produces no error”✓

Page 21: Modern Type Systems for Dynamic Languages Ravi Chugh

21

f(x)

“expected args”“actual args”

Type1 Type2

“function call may produce error”✗

Page 22: Modern Type Systems for Dynamic Languages Ravi Chugh

22

For all programs p in

If p has a type T in

Then p does not failwith any error e in

PROGRAMS

TYPES

ERRORS

Page 23: Modern Type Systems for Dynamic Languages Ravi Chugh

23

For all programs p in

If p has a type T in

Then p does not failwith any error e in

PROGRAMS

TYPES

ERRORS

GOAL: Reason AboutMore Programs

Page 24: Modern Type Systems for Dynamic Languages Ravi Chugh

24

For all programs p in

If p has a type T in

Then p does not failwith any error e in

PROGRAMS

TYPES

ERRORS

GOAL: Reason AboutMore Programs

GOAL: EliminateMore Errors

Page 25: Modern Type Systems for Dynamic Languages Ravi Chugh

25

TYPES

Page 26: Modern Type Systems for Dynamic Languages Ravi Chugh

26

TYPES

Types + LogicRefinement Types

Page 27: Modern Type Systems for Dynamic Languages Ravi Chugh

27

Type1 Type2“expected args”“actual args”

{x|p }

{x|q }

{x|p }

{x|q }

Types + LogicRefinement Types

Page 28: Modern Type Systems for Dynamic Languages Ravi Chugh

28

Types

Refinement Types

Nested Refinements [POPL ’12]Key to Encode Dynamic Features

Logic

{x|p } f :: {y|q }

Page 29: Modern Type Systems for Dynamic Languages Ravi Chugh

Expressiveness

Usability

Types

VerificationHoare Logics

Model CheckingAbstract Interpretation

Theorem Proving…

+ Logic Dependent JavaScript (DJS)[POPL ’12, OOPSLA ’12]

Page 30: Modern Type Systems for Dynamic Languages Ravi Chugh

Why is JavaScript Important?Why is JavaScript Hard?Static Types via LogicSecurity via LogicLooking Ahead

30

OutlineWhy JavaScript? Types via Logic Security via Logic Looking AheadChallenges

TYPES

PROGRAMS

ERRORS

Page 31: Modern Type Systems for Dynamic Languages Ravi Chugh

Why is JavaScript Important?Why is JavaScript Hard?Static Types via LogicSecurity via LogicLooking Ahead

31

OutlineWhy JavaScript? Types via Logic Security via Logic Looking AheadChallenges

TYPES

PROGRAMS

ERRORS

Page 32: Modern Type Systems for Dynamic Languages Ravi Chugh

Why is JavaScript Hard?

32

OutlineWhy JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Lightweight ReflectionDictionary ObjectsAdditional Challenges

PROGRAMS

Page 33: Modern Type Systems for Dynamic Languages Ravi Chugh

33

function negate(x) {

if (typeof x == “number”) {

}

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

xQ: What is my “type”?

Page 34: Modern Type Systems for Dynamic Languages Ravi Chugh

34

All JavaScript Values

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

{x|tag(x) = “number” }

{x|tag(x) = “boolean” }

true false

{x|tag(x) = “object” }

{ } { “f”:17 }

{ “f”:42, “g”:true }...

Q: What is my “type”? x17 42

3.14-1

...

Page 35: Modern Type Systems for Dynamic Languages Ravi Chugh

35

function negate(x) {

if (typeof x == “number”) {

}

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

xQ: What is my “type”?A: Described by “tag”

Page 36: Modern Type Systems for Dynamic Languages Ravi Chugh

36

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

function negate(x) {

if (typeof x == “number”) {

return 0 - x;

} else {

return !x;

}

}

“expected args”

values with tag“number” OR “boolean”

Page 37: Modern Type Systems for Dynamic Languages Ravi Chugh

Why is JavaScript Hard?

37

OutlineWhy JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Lightweight Reflection

Dictionary ObjectsAdditional Challenges

PROGRAMS

Page 38: Modern Type Systems for Dynamic Languages Ravi Chugh

38

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

obj.f means obj[“f”]

a.k.a.“maps”“hash tables”“associative arrays”

Objects are “Dictionaries”that mapstring keysto values

Page 39: Modern Type Systems for Dynamic Languages Ravi Chugh

39

methodto invoke

methodtable

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

function dispatch(table, op, i, j) { }

Page 40: Modern Type Systems for Dynamic Languages Ravi Chugh

40

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

var integerOps = { “+” : function (n, m) { … } , “-” : function (n, m) { … } };

function dispatch(table, op, i, j) { var fn = table[op]; return fn(i, j); }

dispatch (integerOps, “*”, 17, 42);

“foo fieldnot found”“*” key

not found

Page 41: Modern Type Systems for Dynamic Languages Ravi Chugh

41

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

function dispatch(table, op, i, j) { var fn = table[op]; return fn(i, j); }

“expected args”

table object with an op key that stores a function on numbers

Dependencybetween types

of arguments

Page 42: Modern Type Systems for Dynamic Languages Ravi Chugh

Why is JavaScript Hard?

42

OutlineWhy JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Lightweight ReflectionDictionary ObjectsAdditional Challenges

PROGRAMS

Page 43: Modern Type Systems for Dynamic Languages Ravi Chugh

Why is JavaScript Hard?

43

OutlineWhy JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Lightweight ReflectionDictionary Objects Extensible ObjectsPrototype InheritanceJavaScript Peculiarities

PROGRAMS

Page 44: Modern Type Systems for Dynamic Languages Ravi Chugh

Unsupported in Prior Work

Why is JavaScript Hard?

44

OutlineWhy JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Lightweight ReflectionDictionary Objects Extensible ObjectsPrototype InheritanceJavaScript Peculiarities

PROGRAMS

Page 45: Modern Type Systems for Dynamic Languages Ravi Chugh

Why is JavaScript Important?Why is JavaScript Hard?Static Types via LogicSecurity via LogicLooking Ahead

45

OutlineWhy JavaScript? Types via Logic Security via Logic Looking AheadChallengesChallenges Types via Logic

PROGRAMS

TYPES

ERRORS

Page 46: Modern Type Systems for Dynamic Languages Ravi Chugh

46

All JavaScript Values{x|tag(x) = “number” }

true false

{ } { “f”:17 }

{ “f”:42, “g”:true }...

17 42

3.14-1

...

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

Page 47: Modern Type Systems for Dynamic Languages Ravi Chugh

47

All JavaScript Values{x|tag(x) = “number” }

17 42

3.14-1

...

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

{x|tag(x) = “boolean” }

{x|tag(x) = “object” }

true false

{ } { “f”:17 }

{ “f”:42, “g”:true }...

Page 48: Modern Type Systems for Dynamic Languages Ravi Chugh

48

All JavaScript Values

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

{ } { “f”:17 }

{ “f”:42, “g”:true }...

true false

{x|integer(x) }

{x|x > 0 }

17 42

3.14-1

...

Page 49: Modern Type Systems for Dynamic Languages Ravi Chugh

49

All JavaScript Values

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

“set of values x s.t. formula p is true”

{x|p }Refinement Types

Page 50: Modern Type Systems for Dynamic Languages Ravi Chugh

50

f(x)

“expected args”“actual args”

Type1 Type2

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

Page 51: Modern Type Systems for Dynamic Languages Ravi Chugh

51

f(x)

“expected args”“actual args”

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

{x|p }

{x|q }

{x|p }

{x|q }

Page 52: Modern Type Systems for Dynamic Languages Ravi Chugh

52

Subtyping is ImplicationSource of Expressive Power

Type1 Type2

Why JavaScript? Security via Logic Looking AheadChallenges Types via Logic

{x|p }

{x|q }

{x|p }

{x|q }

Page 53: Modern Type Systems for Dynamic Languages Ravi Chugh

Static Types via Logic

53

OutlineWhy JavaScript? Security via Logic Looking AheadChallenges Types via Logic

Lightweight ReflectionDictionary ObjectsKey Technical InnovationsEvaluation

TYPES

Page 54: Modern Type Systems for Dynamic Languages Ravi Chugh

54

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

function negate(x) {

if (typeof x == “number”) {

return 0 - x;

} else {

return !x;

} }

Type Annotation in Comments

//: negate :: (x:NumOrBool) NumOrBool

{v|tag(v) = “number” ∨ tag(v) = “boolean” }

NumOrBool

Syntactic Sugarfor Common Types

Page 55: Modern Type Systems for Dynamic Languages Ravi Chugh

55

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

function negate(x) {

if (typeof x == “number”) {

return 0 - x;

} else {

return !x;

} }

//: negate :: (x:NumOrBool) NumOrBool

{x|tag(x) = “number” }

“expected args”

{x|tag(x) = “number” }

{x|tag(x) = “number” }

{x|tag(x) = “number” }✓

Page 56: Modern Type Systems for Dynamic Languages Ravi Chugh

56

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

function negate(x) {

if (typeof x == “number”) {

return 0 - x;

} else {

return !x;

} }

//: negate :: (x:NumOrBool) NumOrBool

“expected args”

{x|tag(x) = “boolean” }

✓{x|not(tag(x) = “number”)

{x| ∧ (tag(x) = “number” ∨

{x| tag(x) = “boolean”) }

Page 57: Modern Type Systems for Dynamic Languages Ravi Chugh

57

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

function negate(x) {

if (typeof x == “number”) {

return 0 - x;

} else {

return !x;

} }

//: negate :: (x:NumOrBool) NumOrBool

Logic EnablesPath-SensitiveReasoning on

Branches

Page 58: Modern Type Systems for Dynamic Languages Ravi Chugh

58

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

function negate(x) {

if (typeof x == “number”) {

return 0 - x;

} else {

return !x;

} }

Logic Enables Types to Depend on Values

//: negate :: (x:NumOrBool) { y|tag(y) = tag(x) }

Page 59: Modern Type Systems for Dynamic Languages Ravi Chugh

Static Types via Logic

59

OutlineWhy JavaScript? Security via Logic Looking AheadChallenges

Lightweight ReflectionDictionary ObjectsKey Technical InnovationsEvaluation

Types via Logic

TYPES

Page 60: Modern Type Systems for Dynamic Languages Ravi Chugh

60

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

All JavaScript Values

{x|tag(x) = “object” }

...

{ }

{ “f”:true }

{ “f”:17 }

{ “f”:42, “g”:null }

Page 61: Modern Type Systems for Dynamic Languages Ravi Chugh

61

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

All JavaScript Values

{x|tag(x) = “object” has(x,“f”) }

...

{ }

{ “f”:true }

{ “f”:17 }

{ “f”:42, “g”:null }

Page 62: Modern Type Systems for Dynamic Languages Ravi Chugh

62

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

All JavaScript Values

{x|tag(x) = “object” {x|has(x,“f”) {x|tag(sel(x,“f”)) = “number” }

...

{ }

{ “f”:true }

{ “f”:17 }

{ “f”:42, “g”:null }

Page 63: Modern Type Systems for Dynamic Languages Ravi Chugh

63

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

//: dispatch ::

//: ({ table | tag(table) = “object” },

//: { op | tag(op) = “string”

//: { op | ∧ has(table,op)

//: { op | ∧ sel(table,op) :: (Num,Num) Num },

//: { i | tag(i) = “number” },

//: { j | tag(j) = “number” })

//: Num

function dispatch(table, op, i, j) { var fn = table[op]; return fn(i, j); }

Page 64: Modern Type Systems for Dynamic Languages Ravi Chugh

64

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

//: dispatch ::

//: (table : { sel(table,op) :: (Num,Num) Num },

//: op : Str,

//: i : Num,

//: j : Num)

//:

//:

//: Num

function dispatch(table, op, i, j) { var fn = table[op]; return fn(i, j); }

Page 65: Modern Type Systems for Dynamic Languages Ravi Chugh

65

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

var integerOps = { “+” : …, “-” : … };

dispatch (integerOps, “*”, 17, 42);

{op|op = “*” }

“actual args”{op|has(integerOps,op) }

Type Checking Prevents Run-Time Error

{op|op = “*” }

{op|has(integerOps,op) }✗

Page 66: Modern Type Systems for Dynamic Languages Ravi Chugh

Static Types via Logic

66

OutlineWhy JavaScript? Security via Logic Looking AheadChallenges

Lightweight ReflectionDictionary ObjectsKey Technical InnovationsEvaluation

Types via Logic

Page 67: Modern Type Systems for Dynamic Languages Ravi Chugh

67

PriorRefinement

Types

✓✗

lightweight reflection

dictionary objects

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

extensible objects

prototype inheritance

JavaScript peculiarities

✗✗✗

Dependent JavaScript

(DJS)[POPL ’12, OOPSLA ’12]

✓✓✓✓✓

Page 68: Modern Type Systems for Dynamic Languages Ravi Chugh

Key Challenge:Function Subtyping

as Logical Implication68

PriorRefinement

Types

✗dictionary objects

Dependent JavaScript

(DJS)[POPL ’12, OOPSLA ’12]

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Page 69: Modern Type Systems for Dynamic Languages Ravi Chugh

69

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

42 + negate (17)

{f|f :: }(x:Num) Num “expected function”

{f|f :: } (x:NumOrBool) { y|tag(y) = tag(x) }

Page 70: Modern Type Systems for Dynamic Languages Ravi Chugh

{f|f :: }

70

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

(x:NumOrBool) { y|tag(y) = tag(x) }

{f|f :: }(x:Num) Num

How to Prove

?How to Encode Type System Inside Logic?

Prior Refinement SystemsDisallow Function Types Inside Logic!

Page 71: Modern Type Systems for Dynamic Languages Ravi Chugh

71

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

T ::= {x|p }

| T1 T2

Types Refinement Logic

Satisfiability Modulo Theories (SMT)

SAT + Decidable Theories

p q✓✗

SMT Solver(e.g. Z3)

[Prior State-of-the-Art]

Page 72: Modern Type Systems for Dynamic Languages Ravi Chugh

72

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

T ::= {x|p }

| T1 T2

p ::= p ∧ q | p ∨ q | p

| x = y | x > y | …

| tag(x) = “number” | …

| sel(x,k) = 17 | …

Types Refinement Logic

• Boolean Connectives• Equality• Linear Arithmetic• Uninterpreted Functions• McCarthy Arrays

Page 73: Modern Type Systems for Dynamic Languages Ravi Chugh

73

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

T ::= {x|p }

| T1 T2

p ::= p ∧ q | p ∨ q | p

| x = y | x > y | …

| tag(x) = “number” | …

| sel(x,k) = 17 | …

Types Refinement Logic

{v|tag(v) = “number” ∨ tag(v) = “boolean” }NumOrBool

Enables Union Types and Lightweight Reflection

Page 74: Modern Type Systems for Dynamic Languages Ravi Chugh

74

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

T ::= {x|p }

| T1 T2

p ::= p ∧ q | p ∨ q | p

| x = y | x > y | …

| tag(x) = “number” | …

| sel(x,k) = 17 | …

Types Refinement Logic

Enables Dictionary Types with Dynamic Keys …{d|tag(sel(d,k)) = “boolean” ∧

{d|tag(sel(d,“f”)) = “function” ∧

{d|sel(d,“f”) ??? }

But DisallowsFunctions inDictionaries!

Page 75: Modern Type Systems for Dynamic Languages Ravi Chugh

75

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

p ::= p ∧ q | p ∨ q | p

| x = y | x > y | …

| tag(x) = “number” | …

| sel(x,k) = 17 | …

| sel(x,k) :: T1 T2 | …

Types Refinement Logic

Goal: Refer to Type System Inside LogicGoal: Without Giving up Decidability

T ::= {x|p }

| T1 T2

Solution: Nested Refinements!

Page 76: Modern Type Systems for Dynamic Languages Ravi Chugh

76

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

T ::= {x|p } p ::= p ∧ q | p ∨ q | p

| x = y | x > y | …

| tag(x) = “number” | …

| sel(x,k) = 17 | …

| sel(x,k) :: T1 T2 | …

Nested Refinements [POPL ’12]1. Decidable Encoding2. Precise Subtyping3. Type Soundness Proof

Types Refinement Logic

Page 77: Modern Type Systems for Dynamic Languages Ravi Chugh

77

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

{f|f :: } (x:NumOrBool) { y|tag(y) = tag(x) }

{f|f :: }(x:Num) Num

Decidable Encoding

Uninterpreted Predicate in Logic

Distinct UninterpretedConstants in Logic…

Page 78: Modern Type Systems for Dynamic Languages Ravi Chugh

{f|f :: }

78

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

(x:NumOrBool) { y|tag(y) = tag(x) }

{f|f :: }(x:Num) Num

Decidable Encoding

Uninterpreted Predicate in Logic

w/ Types Nested Inside

Distinct UninterpretedConstants in Logic…

Page 79: Modern Type Systems for Dynamic Languages Ravi Chugh

79

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Decidable Encoding

{f|f :: } (x:NumOrBool) { y|tag(y) = tag(x) }

{f|f :: }(x:Num) Num

✗SMTSolver

Trivially Decidable, but Imprecise

Page 80: Modern Type Systems for Dynamic Languages Ravi Chugh

80

p f :: (x:S1) S2

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

{f|f :: }(x:T1) T2

{f|p }

Precise Subtyping

Step 1:

Step 2:

Find such that(x:S1) S2

SMTSolver✓

T1 S1 ✓✓S2 T2x:T1

Compare and(x:S1) S2 (x:T1) T2

“contra-variance”

“co-variance”

Page 81: Modern Type Systems for Dynamic Languages Ravi Chugh

81

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

{f|f :: }(x:Num) Num{f|f = negate }

Precise Subtyping

Step 1:

Step 2:

f = negate

f :: (x:NumOrBool) { y|tag(y) = tag(x) }SMTSolver✓

Page 82: Modern Type Systems for Dynamic Languages Ravi Chugh

82

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

{f|f :: }(x:Num) Num{f|f = negate }

Precise Subtyping

Step 1:

Step 2:

f = negate

f :: (x:NumOrBool) { y|tag(y) = tag(x) }

Num

NumOrBool ✓✓{y|tag(y) = tag(x) } Nu

mx:Num

✓SMT

Solver✓

Page 83: Modern Type Systems for Dynamic Languages Ravi Chugh

83

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Precise Subtyping

Step 1:

Step 2:

Decidable Reasoningvia SMT

Precise Function Subtypingvia Syntactic Techniques

+

{f|f :: }{f|f = negate } ✓(x:Num) Num

Page 84: Modern Type Systems for Dynamic Languages Ravi Chugh

84

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Type Soundness Proof

Logic 0

Conventional ProofsRequire Logic to be an Oracle …

… but Nesting Introduces CircularityThat Prevents Typical Inductive Proofs

Type System 0

Page 85: Modern Type Systems for Dynamic Languages Ravi Chugh

85

Type System ∞

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Type Soundness Proof

Logic 0

Logic 1

Logic 2

Type System 1

Type System 2

Type System 0

… …

Proof Technique:Stratify into Infinite Hierarchy

Page 86: Modern Type Systems for Dynamic Languages Ravi Chugh

86

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

T ::= {x|p } p ::= p ∧ q | p ∨ q | p

| x = y | x > y | …

| tag(x) = “number” | …

| sel(x,k) = 17 | …

| sel(x,k) :: T1 T2 | …

Nested Refinements [POPL ’12]

Types Refinement Logic

Key to Encode Dictionary Objects(Even in Statically Typed Languages!)

Page 87: Modern Type Systems for Dynamic Languages Ravi Chugh

87

lightweight reflection

dictionary objects

extensible objects

prototype inheritance

JavaScript peculiarities

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Dependent JavaScript

(DJS)[POPL ’12, OOPSLA ’12]

✓✓✓✓✓

PriorRefinement

Types

✓✗✗✗✗

path-sensitivity

nested refinements

flow-sensitivity

logical encoding

logical encoding

Page 88: Modern Type Systems for Dynamic Languages Ravi Chugh

JavaScript

JavaScript, Python, Ruby

88

lightweight reflection

dictionary objects

extensible objects

prototype inheritance

JavaScript peculiarities

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Dependent JavaScript

(DJS)[POPL ’12, OOPSLA ’12]

✓✓✓✓✓

path-sensitivity

nested refinements

flow-sensitivity

logical encoding

logical encoding

Page 89: Modern Type Systems for Dynamic Languages Ravi Chugh

Statically Typed Languages(Java, C++, OCaml, Haskell, …)

89

lightweight reflection

dictionary objects

extensible objects

prototype inheritance

JavaScript peculiarities

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Dependent JavaScript

(DJS)[POPL ’12, OOPSLA ’12]

✓✓✓✓✓

path-sensitivity

nested refinements

flow-sensitivity

logical encoding

logical encoding✗

Page 90: Modern Type Systems for Dynamic Languages Ravi Chugh

Statically Typed Languages(Java, C++, OCaml, Haskell, …)

90

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Dependent JavaScript

(DJS)[POPL ’12, OOPSLA ’12]

✓✓✓

path-sensitivity

nested refinements

flow-sensitivity

General Techniques Can BeUsed to Eliminate More

ERRORS

Page 91: Modern Type Systems for Dynamic Languages Ravi Chugh

Static Types via Logic

91

OutlineWhy JavaScript? Security via Logic Looking AheadChallenges

Lightweight ReflectionDictionary ObjectsKey Technical InnovationsEvaluation

Types via Logic

TYPES

Page 92: Modern Type Systems for Dynamic Languages Ravi Chugh

92

306a

408(+33%)

LOCbefore/after

13 Excerpts from: JavaScript, Good Parts SunSpider Benchmark Suite Google Closure Library

Benchmarks

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

Chosen to Stretch Expressiveness Limits of DJS

Page 93: Modern Type Systems for Dynamic Languages Ravi Chugh

93

306a

408(+33%)

LOCbefore/after

13 Excerpts from: JavaScript, Good Parts SunSpider Benchmark Suite Google Closure Library

Benchmarks

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

9 Browser Extensions from: [Guha et al. Oakland ’11]

321a

383(+19%)

1,003a

1,027(+2%)

2 Examples from: Google Gadgets

1,630

1,818(+12%)

TOTALS

Page 94: Modern Type Systems for Dynamic Languages Ravi Chugh

94

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

1,630

1,818(+12%)

TOTALS

Relatively Simple Syntactic Sugar andType Inference Have Helped Significantly

Opportunities for Improved Type Inference:• Iterative Predicate Abstraction• Bootstrap by Running Test Cases [STOP ’11]• Translating Programs from TypeScript

1,818(+12%)

Type Annotations

Page 95: Modern Type Systems for Dynamic Languages Ravi Chugh

95

LOCbefore/after

13 Excerpts from: JavaScript, Good Parts SunSpider Benchmark Suite Google Closure Library

306a

408(+33%)

Benchmarks

9 Browser Extensions from: [Guha et al. Oakland ’11]

321a

383(+19%)

2 Examples from: Google Gadgets

1,003a

1,027(+2%)

1,630

1,818(+12%)

10 sec

Running Time

3 sec

19 sec

32 sec

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

TOTALS

Page 96: Modern Type Systems for Dynamic Languages Ravi Chugh

96

1,818(+12%)

32 sec

Why JavaScript? Types via Logic Security via Logic Looking AheadChallenges

1,630

TOTALS

32 sec

Relatively Simple Optimizations So Far:• Avoid SMT Solver When Possible• Reduce Precision for Common Patterns• Rely on Nested Refinements When Needed

Performance

Page 97: Modern Type Systems for Dynamic Languages Ravi Chugh

Static Types via Logic

97

OutlineWhy JavaScript? Security via Logic Looking AheadChallenges

Lightweight ReflectionDictionary ObjectsKey Technical InnovationsEvaluation

Types via Logic

TYPES

Page 98: Modern Type Systems for Dynamic Languages Ravi Chugh

Types via Logic

Why is JavaScript Important?Why is JavaScript Hard?Static Types via LogicSecurity via LogicLooking Ahead

98

OutlineWhy JavaScript? Security via Logic Looking AheadChallenges Types via Logic Security via Logic

TYPES

ERRORS

PROGRAMS

Page 99: Modern Type Systems for Dynamic Languages Ravi Chugh

99

OutlineWhy JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Security via Logic Browser Extensions

Types for Fine-Grained Policies

ERRORS

Page 100: Modern Type Systems for Dynamic Languages Ravi Chugh

100

… New Yorker article, “The Itch,” …

Page 101: Modern Type Systems for Dynamic Languages Ravi Chugh

101

Page 102: Modern Type Systems for Dynamic Languages Ravi Chugh

102

PRINT

Page 103: Modern Type Systems for Dynamic Languages Ravi Chugh

“?printable=true”

103

“I want every article in print

mode”

Page 104: Modern Type Systems for Dynamic Languages Ravi Chugh

Browser Extensions

Third-Party DevelopersCustomize

Browser

104

Page 105: Modern Type Systems for Dynamic Languages Ravi Chugh

Browser Extensions

PRINTNEW YORKER

105

Page 106: Modern Type Systems for Dynamic Languages Ravi Chugh

Browser Extensions

for (var elt in doc.getEltsByTagName(“a”)) {

var url = elt.getAttr(“href”);

if (url.match(/^newyorker.com/)) {

elt.setAttr(“href”, url + “?printable=true”);

} }

106

Page 107: Modern Type Systems for Dynamic Languages Ravi Chugh

Browser Extensions

PRINTNEW YORKER

Document (DOM)

host-site.com

107

?printable=true

newyorker.com

Page 108: Modern Type Systems for Dynamic Languages Ravi Chugh

Browser Extensions

PRINTNEW YORKER

Document (DOM)

host-site.com

108

?printable=true

newyorker.comevil.com EVIL

PasswordsBank Accts

History

Network Manager

OtherSensitive

APIs

JavaScriptEngine

BUGGY

Page 109: Modern Type Systems for Dynamic Languages Ravi Chugh

109

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Policy Expressiveness

Access All Resources

Page 110: Modern Type Systems for Dynamic Languages Ravi Chugh

110

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Access Some Resources

cannot access DOM cannot access History

Policy Expressiveness

Page 111: Modern Type Systems for Dynamic Languages Ravi Chugh

111

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Policy Expressiveness

coarse-grainedand uninformative!

cannot access DOM cannot access History

Page 112: Modern Type Systems for Dynamic Languages Ravi Chugh

112

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Policy Expressiveness

coarse-grainedand uninformative!

similar situation forother app platforms

Page 113: Modern Type Systems for Dynamic Languages Ravi Chugh

113

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Policy Expressiveness

Fine-Grained PoliciesGOAL

Page 114: Modern Type Systems for Dynamic Languages Ravi Chugh

114

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

can write “a” elementsbut only “href” attribute

when original “href” valuestarts with “newyorker.com”and is prefix of new value

Fine-Grained PoliciesGOAL

Page 115: Modern Type Systems for Dynamic Languages Ravi Chugh

115

Language-Based SecurityIf Language Could Enforce SomeSecurity-Related Policies …

Then Reduce Dependence On:• Testing• Code Reviews• Dynamic Checking

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Page 116: Modern Type Systems for Dynamic Languages Ravi Chugh

116

For all programs p in

If p has a type T in

Then p does not failwith any error e in

PROGRAMS

TYPES

ERRORS

Page 117: Modern Type Systems for Dynamic Languages Ravi Chugh

117

For all programs p in

If p has a type T in

Then p does not failwith any error e in

PROGRAMS

TYPES

ERRORSGOAL: Eliminate SomeSecurity-Related Errors

Page 118: Modern Type Systems for Dynamic Languages Ravi Chugh

118

PROGRAMS

TYPES

ERRORSGOAL: Eliminate SomeSecurity-Related Errors

But How?

Page 119: Modern Type Systems for Dynamic Languages Ravi Chugh

119

Subtyping is ImplicationSource of Expressive Power

Type1 Type2{x|p }

{x|q }

{x|p }

{x|q }

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Page 120: Modern Type Systems for Dynamic Languages Ravi Chugh

120

OutlineWhy JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Security via Logic Browser Extensions

Types for Fine-Grained Policies

ERRORS

Page 121: Modern Type Systems for Dynamic Languages Ravi Chugh

121

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Static Verification (DJS)

Security Expert

User

reviews only policy

reads informative policy

Developerwrites policy in logic!

Page 122: Modern Type Systems for Dynamic Languages Ravi Chugh

122

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Network Manager

request, …

JS Engine

eval, …

DOM

getAttr, setAttr, …

request ::

url:Str

Str

Page 123: Modern Type Systems for Dynamic Languages Ravi Chugh

123

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Network Manager

request, …

JS Engine

eval, …

DOM

getAttr, setAttr, …

request ::

{url:Str|WhiteListed(url)}

Str

Page 124: Modern Type Systems for Dynamic Languages Ravi Chugh

124

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Network Manager

request, …

JS Engine

eval, …

DOM

getAttr, setAttr, …

eval ::

s:Str

Void

Page 125: Modern Type Systems for Dynamic Languages Ravi Chugh

125

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Network Manager

request, …

JS Engine

eval, …

DOM

getAttr, setAttr, …

eval ::

{s:Str|not(Tainted(s))}

Void

Page 126: Modern Type Systems for Dynamic Languages Ravi Chugh

126

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Network Manager

request, …

JS Engine

eval, …

DOM

getAttr, setAttr, …

getAttr ::

elt:Element

attr:Str

Str

Page 127: Modern Type Systems for Dynamic Languages Ravi Chugh

127

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Network Manager

request, …

JS Engine

eval, …

DOM

getAttr, setAttr, …

getAttr ::

elt:Element

{attr:Str|CanRead(elt,attr)}

Str

Page 128: Modern Type Systems for Dynamic Languages Ravi Chugh

128

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Network Manager

request, …

JS Engine

eval, …

DOM

getAttr, setAttr, …

setAttr ::

elt:Element

attr:Str

y:Str

Void

Page 129: Modern Type Systems for Dynamic Languages Ravi Chugh

129

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Network Manager

request, …

JS Engine

eval, …

DOM

getAttr, setAttr, …

setAttr ::

elt:Element

attr:Str

{y:Str|CanWrite(elt,attr,y)}

Void

Page 130: Modern Type Systems for Dynamic Languages Ravi Chugh

130

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Network Manager

request, …

JS Engine

eval, …

DOM

getAttr, setAttr, …

Refined APIs Mediate Access

Typecheck Extension with Policy

Page 131: Modern Type Systems for Dynamic Languages Ravi Chugh

131

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

forall url.

not (url = “evil.com”)

WhiteListed (url)

var s = request(“evil.com”);

✗finite blacklist

Page 132: Modern Type Systems for Dynamic Languages Ravi Chugh

132

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

//: url:Str

var s = request(url);

✗might beblacklisted!

forall url.

not (url = “evil.com”)

WhiteListed (url)

Page 133: Modern Type Systems for Dynamic Languages Ravi Chugh

forall url.

not (url = “evil.com”)

WhiteListed (url)

133

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

if (url != “evil.com”) {

var s = request(url);

}

Page 134: Modern Type Systems for Dynamic Languages Ravi Chugh

134

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

if (url != “evil.com”) {

var s = request(url);

eval(s);

}

s:Str might be tainted!

forall url.

not (url = “evil.com”)

WhiteListed (url)

Page 135: Modern Type Systems for Dynamic Languages Ravi Chugh

135

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

if (url != “evil.com”) {

var s = request(url);

s = sanitize(s);

eval(s);

} ✓STATICALLY VERIFIED!

s:Str

{s:Str|not(Tainted(s))}

forall url.

not (url = “evil.com”)

WhiteListed (url)

Page 136: Modern Type Systems for Dynamic Languages Ravi Chugh

136

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

WhiteListed (“ny.com”)

WhiteListed (“golf.com”)

forall url.

WhiteListed (url)

not (Tainted (request (url)))

trust thatwhitelisted servers

return untainted strings

Page 137: Modern Type Systems for Dynamic Languages Ravi Chugh

137

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

var s = request(“ny.com”);

eval(s);✓ no need to sanitize

WhiteListed (“ny.com”)

WhiteListed (“golf.com”)

forall url.

WhiteListed (url)

not (Tainted (request (url)))

STATICALLY VERIFIED!

Page 138: Modern Type Systems for Dynamic Languages Ravi Chugh

138

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

forall e, oldUrl, newUrl.

ValueOf (e, “href”, oldUrl) ∧

StrPrefix (“newyorker.com”, oldUrl) ∧

StrPrefix (oldUrl, newUrl)

CanWrite (e, “href”, newUrl)

for (var elt in doc.getEltsByTagName(“a”)) {

var url = elt.getAttr(“href”);

if (url.match(/^newyorker.com/)) {

elt.setAttr(“href”, url + “?printable=true”);

} }

Page 139: Modern Type Systems for Dynamic Languages Ravi Chugh

139

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

ValueOf (elt, “href”, url) ∧

StrPrefix (“newyorker.com”, url) ∧

StrPrefix (url, url + “?printable=true”)

CanWrite (e, “href”, url+“?printable=true”)

for (var elt in doc.getEltsByTagName(“a”)) {

var url = elt.getAttr(“href”);

if (url.match(/^newyorker.com/)) {

elt.setAttr(“href”, url + “?printable=true”);

} }

Page 140: Modern Type Systems for Dynamic Languages Ravi Chugh

140

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

ValueOf (elt, “href”, url) ∧

StrPrefix (“newyorker.com”, url) ∧

StrPrefix (url, url + “?printable=true”)

CanWrite (e, “href”, url+“?printable=true”)

for (var elt in doc.getEltsByTagName(“a”)) {

var url = elt.getAttr(“href”);

if (url.match(/^newyorker.com/)) {

elt.setAttr(“href”, url + “?printable=true”);

} }

✓✓

Page 141: Modern Type Systems for Dynamic Languages Ravi Chugh

141

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

ValueOf (elt, “href”, url) ∧

StrPrefix (“newyorker.com”, url) ∧

StrPrefix (url, url + “?printable=true”)

CanWrite (e, “href”, url+“?printable=true”)

for (var elt in doc.getEltsByTagName(“a”)) {

var url = elt.getAttr(“href”);

if (url.match(/^newyorker.com/)) {

elt.setAttr(“href”, url + “?printable=true”);

} }

✓✓✓

Page 142: Modern Type Systems for Dynamic Languages Ravi Chugh

142

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

ValueOf (elt, “href”, url) ∧

StrPrefix (“newyorker.com”, url) ∧

StrPrefix (url, url + “?printable=true”)

CanWrite (e, “href”, url+“?printable=true”)

for (var elt in doc.getEltsByTagName(“a”)) {

var url = elt.getAttr(“href”);

if (url.match(/^newyorker.com/)) {

elt.setAttr(“href”, url + “?printable=true”);

} }

✓✓✓

✓STATICALLY VERIFIED!

Page 143: Modern Type Systems for Dynamic Languages Ravi Chugh

143

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

ValueOf (elt, “href”, url) ∧

StrPrefix (“newyorker.com”, url) ∧

StrPrefix (url, url + “?printable=true”)

CanWrite (e, “href”, url+“?printable=true”)

for (var elt in doc.getEltsByTagName(“a”)) {

var url = elt.getAttr(“href”);

if (url.match(/^newyorker.com/)) {

elt.setAttr(“href”, “evil.com”);

} }

Page 144: Modern Type Systems for Dynamic Languages Ravi Chugh

144

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

ValueOf (elt, “href”, url) ∧

StrPrefix (“newyorker.com”, url) ∧

StrPrefix (url, “evil.com”)

CanWrite (e, “href”, “evil.com”)

for (var elt in doc.getEltsByTagName(“a”)) {

var url = elt.getAttr(“href”);

if (url.match(/^newyorker.com/)) {

elt.setAttr(“href”, “evil.com”);

} }

Page 145: Modern Type Systems for Dynamic Languages Ravi Chugh

145

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

ValueOf (elt, “href”, url) ∧

StrPrefix (“newyorker.com”, url) ∧

StrPrefix (url, “evil.com”)

CanWrite (e, “href”, “evil.com”)

for (var elt in doc.getEltsByTagName(“a”)) {

var url = elt.getAttr(“href”);

if (url.match(/^newyorker.com/)) {

elt.setAttr(“href”, “evil.com”);

} }

✓✓✗

Page 146: Modern Type Systems for Dynamic Languages Ravi Chugh

146

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Types for Security+ JavaScript Verification

= Fine-Grained Web Security

[ESOP ’10, Guha et al. ’11]

[POPL ’12, OOPSLA ’12]

[current]

Secure Extensions Written in OCaml

Preliminary Benchmarks

Page 147: Modern Type Systems for Dynamic Languages Ravi Chugh

147

Why JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Guha et al. ’11]

Secure Extensions Written in OCamlPreliminary Benchmarks

Ported to DJS (~400 LOC so far)

Well-typed programsdon’t have run-time errorsand conform to security policies

Page 148: Modern Type Systems for Dynamic Languages Ravi Chugh

148

OutlineWhy JavaScript? Security via LogicTypes via Logic Looking AheadChallenges

Security via Logic

Browser ExtensionsTypes for Fine-Grained Policies

ERRORS

Page 149: Modern Type Systems for Dynamic Languages Ravi Chugh

Why is JavaScript Important?Why is JavaScript Hard?Static Types via LogicSecurity via LogicLooking Ahead

149

OutlineWhy JavaScript? Types via Logic Security via Logic Looking AheadChallenges Security via Logic Looking Ahead

TYPES

ERRORS

PROGRAMS

Page 150: Modern Type Systems for Dynamic Languages Ravi Chugh

150

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Dependent JavaScript

Untrusted

Code

ScriptingLanguages

Web Platform

>>>

Page 151: Modern Type Systems for Dynamic Languages Ravi Chugh

151

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Dependent JavaScript

Untrusted

Code

ScriptingLanguages

PolicySpecification

Web Platform

>>>

Page 152: Modern Type Systems for Dynamic Languages Ravi Chugh

152

Static Verification (DJS)

Security Expert

User

Developer must write policy

must read policy

and

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Page 153: Modern Type Systems for Dynamic Languages Ravi Chugh

153

Developer must write policy

• Infer from traces• Incentives from platform

must read policyUser

• Traces to demonstrate behavior• Community review-and-recommend services

must read policySecurity Expert

• Modeling tools to analyze policies

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Page 154: Modern Type Systems for Dynamic Languages Ravi Chugh

154

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Dependent JavaScript

Untrusted

Code

ScriptingLanguages

PolicySpecification

DynamicEnforcement

Web Platform

>>>

Page 155: Modern Type Systems for Dynamic Languages Ravi Chugh

155

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Static Verification (DJS)

Security Expert

User

Developer

Page 156: Modern Type Systems for Dynamic Languages Ravi Chugh

156

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

eval(“…”);

…Dynamic

Code Loading

Impossible to statically analyzecode you don’t have!

Page 157: Modern Type Systems for Dynamic Languages Ravi Chugh

157

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Static Verification (DJS)

Security Expert

User

Developer

DynamicEnforcement

Page 158: Modern Type Systems for Dynamic Languages Ravi Chugh

158

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

DynamicEnforcement

eval(“…”);

//: #assume

New Types

Old Types

New Types

Page 159: Modern Type Systems for Dynamic Languages Ravi Chugh

159

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Limits of Static Reasoning

“expected”“actual”

p q✓ Definitely Safe

Maybe Unsafe✗p q(But Maybe Safe…)

Page 160: Modern Type Systems for Dynamic Languages Ravi Chugh

160

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Limits of Static Reasoning

p q Definitely Safe

Maybe Unsafe✗p qDefinitely Unsafep q✓

“expected”“actual”

Page 161: Modern Type Systems for Dynamic Languages Ravi Chugh

161

Dynamic Enforcement

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Limits of Static Reasoning• Accept Program, Rather than Reject

• Rely on Programmer-Trusted or• Dynamically-Checked Assumption

✗ Maybe Unsafep q

Page 162: Modern Type Systems for Dynamic Languages Ravi Chugh

162

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Dependent JavaScript

Untrusted

Code

ScriptingLanguages

PolicySpecification

DynamicEnforcement

Trust Base

Web Platform

>>>

Page 163: Modern Type Systems for Dynamic Languages Ravi Chugh

163

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Static Verification (DJS)

Security Expert

User

reviews only policy

Developer

DynamicEnforcement

Page 164: Modern Type Systems for Dynamic Languages Ravi Chugh

164

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Static Verification (DJS)

Security Expertreviews only policy

trust

trust SMTSolver

Page 165: Modern Type Systems for Dynamic Languages Ravi Chugh

Eliminate from TCB à laProof-Carrying Code [PLDI ’10]

Eliminate from TCB via Formal Verification

165

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Static Verification (DJS)

Security Expert

trust

SMTSolver

trust

Page 166: Modern Type Systems for Dynamic Languages Ravi Chugh

166

PolicySpecification

DynamicEnforcement

Trust Base

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Dependent JavaScript

Untrusted

Code

ScriptingLanguages

Web Platform

ToolSupport

>>>

Page 167: Modern Type Systems for Dynamic Languages Ravi Chugh

167

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

• Generate Tests and Find Bugsfor Unannotated Programs

• Support Refactoring and Code Completionfor Annotated Programs

• Provide Analysis and Documentation Toolsfor Large, Popular Libraries like jQuery

• Web-Based Interfaces to Collect Statisticsabout Programming Patterns, Errors

Tools for Web Development

Page 168: Modern Type Systems for Dynamic Languages Ravi Chugh

168

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Inferred Annotationsare in Comments

Flow-SensitiveType Information

by Hoveringover Program Point

Page 169: Modern Type Systems for Dynamic Languages Ravi Chugh

169

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Inferred Annotationsare in Comments

Flow-SensitiveType Information

by Hoveringover Program Point

Many Software Engineering Challenges!

Page 170: Modern Type Systems for Dynamic Languages Ravi Chugh

172

Why JavaScript? Types via Logic Security via LogicChallenges Looking Ahead

Dependent JavaScript

Untrusted

Code

ScriptingLanguages

ToolSupport

PolicySpecification

DynamicEnforcement

Trust Base

Web PlatformTypes

+ Logic

Page 171: Modern Type Systems for Dynamic Languages Ravi Chugh

173

[STOP ’11][POPL ’12]

[OOPSLA ’12][ML ’13]

[ESOP ’10]

[PLDI ’10][PLDI ’09]

Program Analysisfor Security

Dataflow Analysis for Race Detection

[PLDI ’08]

PL TechniquesTypes

+ Logic

Page 172: Modern Type Systems for Dynamic Languages Ravi Chugh

174

Dynamic Languages are Pervasive

DJS: Precise Static Types via Logic

Towards Fine-Grained Web Security

ravichugh.com

Thanks!

Page 173: Modern Type Systems for Dynamic Languages Ravi Chugh

175