# Leaking the Observable runtime in a module

**URL:** <https://talk.observablehq.com/t/leaking-the-observable-runtime-in-a-module/6284>\
**Category:** Show and tell\
**Created:** [February 24, 2022, 5:38pm UTC](https://talk.observablehq.com/t/leaking-the-observable-runtime-in-a-module/6284 "2022-02-24T17:38:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![edeboursetty](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/edeboursetty/32/4875_2.png) [@edeboursetty](https://talk.observablehq.com/u/edeboursetty)\
**Post date:** [February 24, 2022, 5:38pm UTC](https://talk.observablehq.com/t/leaking-the-observable-runtime-in-a-module/6284/1 "2022-02-24T17:38:33Z")

</div>

Hi, just thought I would post this here, cause I don’t think that’s a major security problem, but I managed to leak the runtime object in a notebook by importing a broken module that contained an “eval” statement.

See:

> **[Runtime Hack (part I)](https://observablehq.com/@edeboursetty/runtime-hack-part-i)**

and

> **[Runtime Hack (part II)](https://observablehq.com/@edeboursetty/runtime-hack-part-ii)**

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [February 24, 2022, 8:10pm UTC](https://talk.observablehq.com/t/leaking-the-observable-runtime-in-a-module/6284/2 "2022-02-24T20:10:27Z")

</div>

Oh wow, that’s so much better than the ugly hack in [Accessing a Notebook’s Runtime](https://observablehq.com/@mootari/access-runtime), thanks for sharing!

Note that you don’t have to use two notebooks. Instead just reimport from the current one:

```javascript
r = {
  try { return eval('runtime') }
  catch(e) {}
}

```

```javascript
import {r as runtime} from '@edeboursetty/runtime-hack-part-i' // this notebook

```

And then e.g.

```javascript
Object.getOwnPropertyNames(runtime)

```

```javascript
Array.from(runtime._variables).map(v => v._name)

```

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [February 24, 2022, 8:17pm UTC](https://talk.observablehq.com/t/leaking-the-observable-runtime-in-a-module/6284/3 "2022-02-24T20:17:35Z")

</div>

I would also recommend to generalize this to

```javascript
getScoped = name => {
  try { return eval(name); }
  catch(e) {}
}

```

then

```javascript
import {getScoped as _getScoped} from 'THIS_NOTEBOOK'

```

and e.g.

```javascript
_getScoped('define1')

```

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [February 24, 2022, 9:13pm UTC](https://talk.observablehq.com/t/leaking-the-observable-runtime-in-a-module/6284/4 "2022-02-24T21:13:51Z")

</div>

Alright, so I’ve been trying to understand why this only works when called inside a module, and the reason is Observable’s editor UI.

As we know `eval` runs in the scope in which it is called, _unless_ it has been called indirectly (e.g. `window.eval` instead of `eval`). In that case the code only has access to the global scope. _Spoiler: The `eval` that we call in a notebook is a direct reference._

When we download and host the notebook locally, and run `eval("runtime")` directly, we actually get access to the runtime instance. So what’s different?

Observable dynamically reevaluates each cell’s snippet when you edit a cell’s code. It does so via the following function:

```javascript
function Cn(e, t) {
    try {
        return (0,
        eval)(`"use strict";(\n${e}\n)\n//# sourceURL=observablehq-${t}`)
    } catch (e) {
        return function(e) {
            return ()=>{
                throw e
            }
        }(e)
    }
}

```

where `e` contains the compiled cell source, and t contains the node/cell ID. When we take a closer look we’ll note `(0, eval)`, which is `eval` being called _indirectly_.

Modules on the other hand get loaded the good old-fashioned way via `import()`:

```javascript
                    a = async function(e, t) {
                    if ("string" == typeof e)
                        return import(e).catch((()=>{
                            throw new TypeError(`Notebook '${t}' failed to load`)
                        }
                        ));

```

which is why there’s no indirection, and thus `eval` has access to the variable’s scope (and its parent scopes).

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [February 24, 2022, 9:41pm UTC](https://talk.observablehq.com/t/leaking-the-observable-runtime-in-a-module/6284/5 "2022-02-24T21:41:04Z")

</div>

I don’t see a security problem here. (As @mootari demonstrated, there are other ways to access the current Runtime.) Our primary security model is the sandbox: the sandbox is restricted, but within the sandbox, you can write whatever code you want. It’s not feasible to try to enforce restrictions within the sandbox with a highly dynamic language like JavaScript.

That said, I also would not rely on this technique! This could easily break with changes to our compiler in the future.

---

<div class="post-metadata">

**Author:** ![edeboursetty](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/edeboursetty/32/4875_2.png) [@edeboursetty](https://talk.observablehq.com/u/edeboursetty)\
**Post date:** [February 25, 2022, 10:20am UTC](https://talk.observablehq.com/t/leaking-the-observable-runtime-in-a-module/6284/6 "2022-02-25T10:20:14Z")

</div>

Cool, I didn’t know about @mootari’s attempt. Javascript lets you do crazy stuff indeed…

Re: security, yeah the sandbox is definitely the barrier. I was worried that the observer had access somehow to the main page. I have no idea how you guys do to change the \_offset from the observer when opening/closing the code editor for instance.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [February 25, 2022, 11:48am UTC](https://talk.observablehq.com/t/leaking-the-observable-runtime-in-a-module/6284/7 "2022-02-25T11:48:31Z")

</div>

> [@edeboursetty](#):
>
> I have no idea how you guys do to change the \_offset from the observer when opening/closing the code editor for instance.

Via messages sent from the parent frame to the sandbox: [Observable Editor Events / Fabian Iwand | Observable](https://observablehq.com/@mootari/observable-editor-events)

Messages from the sandbox are sent for stuff like content height changes, extracting the title and downloading data/images. type `window.addEventListener('message', e => console.log(e.data))` in the dev tools console (with the “top” context selected) to monitor them.
