I want to do a show of hands. How many people here know what eval is? How many people have heard that you should never, ever, ever use eval? How many people use it anyway? Awesome. I love you. Okay. So eval is basically a function that turns strings into behavior. It is a way to take some expression in some language. Pretty much all dynamic languages have some sort of core function to take in a string, interpret it in some way, interpret it as instructions in some particular language, and then turn it into some kind of function, some kind of result. You can use it to do basic math. You can use it to make new functions inside your language. The Python eval interprets the string as Python. The JavaScript eval interprets it as JavaScript. Most of computing is about turning strings of some form into behavior of some form. And if you come from a Lisp background, this seems really natural to you because the division between code and data is kind of thin in some languages. Lisp expressions are also the programming form. So this kind of interpretation, you're getting instructions as a string and you're trying to figure out what exactly you should do with that goes way back. Forty years ago, if you were a serious programmer, you'd be lusting after one of these babies. This is the VT100. This is the terminal. You would connect it with a serial port to the mainframe or maybe over a modem to the university computer. You could type your commands into a shell on that. You could run your programs, run the compiler, see the output of it. The interesting thing about the VT100 is that this is one of the very first to implement these ANSI escape sequences. So this was a series of bytes that the server could send down that would cause the cursor to move. It could drive the cursor around on the screen and it could paint letters in different places. So this enabled things like visual editors like VI and Emacs. In comparison to the teletype that preceded it, you weren't limited to one line at a time kind of append only text output. You could actually have this interactive process because the server was telling your terminal what to do and how to drive stuff around. A little while later, 1990, Sir Tim Berners-Lee gives us the web, gives us HTML. This is another situation where the server is sending instructions down to the client. This time the instructions are in the form of this declarative markup language that's telling the client some constraints. It's saying you can put some text here, but I want you to make it look like a title. You get to decide what title means. You get to decide how big the screen is, how to lay stuff out. But you are sending instructions down to the client side and letting the browser decide how to lay it out. A couple of years later, Brendan Knight gives us JavaScript. This is even more, gives the server even more control over what's happening on the browser side. You've got a full blown Turing complete language imperative running on the client with access to this kind of funny I.O. channel called the DOM where it can move stuff around. But the big thing this enables is that the local browser, the server can send something to your browser that can react to user input without spending the whole round trip going back to the server. So things can become much faster, much more interactive. You can do local checking on a lot of stuff. Over the two decades since then, JavaScript in the browser has gained even more power, has access to local storage, can talk to the network, can do audio, can do webcam, microphone, all kinds of stuff. It might not be entirely obvious, but the underlying trait of all of these systems is that the server is effectively sending a program to us to execute locally. Or at least something kind of morally equivalent to a program. It might be in a really narrow language it just talks about cursor up, cursor left, paint text, or it might be something more obviously programming language like JavaScript. So all these I.O. channels have become more complex but it's basically still mobile code. The server is sending a program to this client device and the client is executing it somehow. So we have this run time environment. Maybe it's a web browser, maybe it's an operating system, and its job is to mediate the interaction between these various bits of program coming down off the wire and local objects. A big part of that job is to keep the program from completely taking over the computer. This might have been not that big of a deal back in the VT100 days when the worst thing that that could do is draw rude pictures on your screen. But these days web browser runs on a general purpose computer. We have lots of other files, lots of other resources on our machine. We don't want to expose those to code coming off the web. In addition we've got web pages coming from different servers and we want to protect those things against each other. So this is not a new problem, this notion of wanting to accept instructions without becoming completely vulnerable. If you're a fan of Greek mythology you might remember this guy, Ulysses, the sirens, these flying ladies who have this beautiful lovely song with the one downside that when you hear it you go mad and drown yourself in the sea. So Ulysses told his sailors to plug their ears with wax. He told them this way you're not going to drown. That's a pretty severe form of sandboxing. You're not really allowing much interaction between this song coming down. But he wanted to hear it for himself. So he told his sailors to tie him to the mast of the ship and said no matter how much I beg, no matter how much I plead, do not untie me because I don't want to drown in the ocean. So it's as if the sirens delivered their song in the form of a program and he wanted to grant that program access to the enjoy the music function but not to the go drown yourself in the sea function. So you can think of the web browser as this sort of arena in which the server delivers its champion. It sends this program down and the browser or the operating system is responsible for executing it in a confined environment with specific access to the host. And you need some way of expressing what that access ought to be. In a browser, the same origin policy can be thought of as a fairly coarse boundary around the influence that code coming from different locations gets to have. And that arena can be pretty big. So client server systems have just two parties. But things that are much more interesting when you have three or more. The decentralized web promises to connect multiple data providers in some really interesting ways. Some of the examples we've seen here, the beaker browser, a lot of the dat stuff. You can imagine a system in which you have a local user agent which is the kind of runtime environment, the browser or the expression engine. And it's getting code from some application provider. So that place is telling me what application to run. I'm going to connect it to a data provider from somewhere else. I have the private keys for the data provider. I'm not allowing the application to see those private keys. I'm only allowing it to see some subset of my storage. And then maybe that application is publishing a flicker or an Instagram-like feed of pictures that other people are going to search. So it needs to publish some stuff to a pub sub server. And somebody else is going to subscribe to that feed. So you have all of these different places that instructions and code might be coming from. For all of these links, there are reasons why you might want to send some code over that wire to explain what you want it to do. And we need to be able to handle safe interaction between all of these different places. And even if you aren't comfortable with accepting code and behavior from other systems into your local private sanctum, then I hate to break it to you, but you're already doing this all of the time. Because any non-trivial application, in fact, every trivial application you might put together is including libraries and modules coming from dozens, probably hundreds of different authors. And I'm sure nobody has the time to go and audit every single line of code to go look at those things. So you're already combining pieces from lots of places. If you're familiar with the ESLint Trojan from about a week or two ago on NPM, it's already a pretty difficult situation. And if there were some way of confining these libraries and modules using the same techniques that we apply to code coming from remote places and only give those pieces as much power as they really need, our programs would be a whole lot safer. Okay. So if we want these communication, these agents that are communicating to share instructions and behavior with each other, then eval is a really good useful tool for that, very flexible. You don't have to decide ahead of time what the code is going to be. In this kind of example, I'm doing a kind of MapReduce like function. You have a database that has a bunch of different rows to it. You are asking for a predicate function from somebody in the form of a string. That function gets to evaluate every row and say, yep, this is what I want to do. No, this is not a row I'm interested in. So that's great. What's the problem with eval? Why don't we use it all the time? The biggest problem is that in JavaScript at least, eval has access to the global scope. So in addition to being able to say that you can access the row and make decisions about whether you like this row or not, it also has access to the global scope. It can climb the scope chain and see every other variable visible in your program at the point of the eval, including this database object. And it can do something like delete all of the rows from it. That's pretty bad. We really don't want to let it do that. So the first thing we do is to turn this into what we call a to argument safe eval. Rather than getting access to the global scope, we add this second argument that we call endowments. And this says the code being evaluated only gets to see these names in its global scope. It doesn't get to see everything else. So this is the first step that you need to block an attack like that. In this case, when it tries to reach for this database string, it's not even defined in its environment. It's as if you had typoed the variable name and we're accessing some incorrect thing. You just get a reference error from it. So second interesting thing, JavaScript, you may or might know, primordials in JavaScript are the base components of the language like number and math and date and array. The kind of core standard library objects. And guess what? In JavaScript, those are mutable. I can go and say array.prototype.push equals something totally different. And now every array in that entire program, when I say array.push, it'll behave differently. So in this example, I'm doing a dumb little array.pop here as a counter to say you only get four queries. I decrement the number of queries you have left by popping something off the array when the array is empty. All right. You're done. But if the attacker can go and change what array.pop does, well, okay, then that's a problem. So we defend against this by freezing these primordials. We say that we are going to fix the value of array.pop, everything else such that nobody else can change it. So the library that we're introducing here is called secure ECMAScript. You can get more information at that URL. This lets you create an environment in which you can evaluate strings safely. You get this two argument eval, all the primordials are frozen. And it lets you set up a safe place to evaluate code. If you go to the web page, there's a little demo that we coded up there in which the defender code has, if you've ever seen war games, the scene where the computer is trying to guess the launch codes, it's kind of like that. So you paste the attacker code in on the right hand side and that gets access to a single guess function. It's not supposed to be able to get to anything other than that. So you can take a look at that there. When you set this up, the very first environment that code executes in, so in a browser context, the very first page load that's loading in that initial script, we call that the primal realm. In Node.js, this is going to be your top level main program gets to execute in this realm. That is where all of the interesting powers are. That's where you get access to a file system, where you get access to network connectivity. We are trying to protect that. We're not going to evaluate any unsafe code inside that. Instead, with SESS, what you create is this SESS realm, the very first call that you do is going to make a new environment in which you can evaluate other strings. And what you do in that environment is to make these protective wrappers. So I'm going to make an object that encapsulates one of those powerful things like local storage or network access and make some subset of that power available to yet other objects. And eventually, you end up evaluating this code that's coming in from some other source knowing that it only gets access to objects that give it certain powers. If you have access to a certain amount of power, you have network access, you can build a new object that gives less of that power to somebody else. We call those attenuators or facets or sometimes forwarders. And you express the limitations that you want to impose with more code. So it's a very expressive way of explaining to somebody how much power you want to give them. And that first environment you load in, it's a little bit like loading in a virtual monitor or a hypervisor or an operating system. You know, the bare metal computer has all of this interesting I O channel to it. But you want to make an environment where smaller programs called user space programs get access to a particularly well defined subset. So you can use SESS to build one of these environments. And the particular things we add to the language there is this top level object called SESS in which you can make a new realm. SESS.confine is our safe to argument evaluator from there. And we also introduce a function called def, which is basically just freezing an object so that you can return it from a function safely. So that if I give an op, if part of the API for my facility is to return an object to somebody, the person I'm giving it to can't modify that object and maybe cause some surprising behavior. So it's kind of reducing the accidental mutability in the language. This is part of a larger set of projects that we're working on to define some subsets of JavaScript for different purposes and I'm not going to go into all of this, but you can see some more text on there that Mark has written on the repo for it. But the basic idea is that in the long run we're trying to define some good ways of getting behavior from one system to another. So in the same way that JSON has turned out to be a pretty universal way of getting data from one machine to another when it's in the right shape, we're hoping that the smallest language called Jessie is a way of getting behavior from one system to another. So that's it. Please take a look at the repository agoric slash sess to go and check some stuff out. And if only Ulysses had had sess to work with, then he could have evaluated the Siren song in an environment where he lets himself hear the beautiful music but conveniently leaves out the whole drown yourself stuff. I'll be around at lunch for questions if you guys want to ask some stuff. Thank you again for Brian Warner.