Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

fix new Function(<string>) allowing for arbitrary code execution #149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/__tests__/cloudworker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,14 @@ describe('cloudworker', () => {
expect(res.headers.get('x-colo')).toEqual('YYZ')
done()
})

test('throws on eval', async () => {
const bindings = { EvalError: EvalError }
expect(() => { new Cloudworker(`eval('true')`, { bindings }) }).toThrow(new EvalError('Code generation from strings disallowed for this context')) // eslint-disable-line no-new
})

test('throws on new Function(<string>)', async () => {
const bindings = { EvalError: EvalError }
expect(() => { new Cloudworker(`new Function('true')`, { bindings }) }).toThrow(new EvalError('Code generation from strings disallowed for this context')) // eslint-disable-line no-new
})
})
Copy link

@xtuc xtuc Mar 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a test for disallowing Function.constructor("console.log(1)")() and similarly ({}).constructor.constructor("console.log(1)")()?

Also note that code generation applies to wasm as well:

  const arr = [];
  new WebAssembly.Module(arr)

should be disallowed

3 changes: 2 additions & 1 deletion lib/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { FetchEvent } = require('./runtime/fetch-event')
const { crypto } = require('./runtime/crypto')
const { TextDecoder, TextEncoder } = require('./runtime/text-encoder')
const { atob, btoa } = require('./runtime/base64')
const { FunctionProxy } = require('./runtime/function')

class Context {
constructor (addEventListener, cacheFactory, bindings = {}) {
Expand Down Expand Up @@ -44,7 +45,7 @@ class Context {
this.EvalError = EvalError
this.Float32Array = Float32Array
this.Float64Array = Float64Array
this.Function = Function
this.Function = FunctionProxy
this.Int8Array = Int8Array
this.Int16Array = Int16Array
this.Int32Array = Int32Array
Expand Down
13 changes: 13 additions & 0 deletions lib/runtime/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// We bind `Function` in from the parent for `instanceof`
// purposes, but we don't want to disallow code generation
// from strings with new Function(<string>). So, we use this Proxy!

// Security note: there may well be ways to hack around this. Enforcing
// this rule is about consistency with CF workers, not allowing for
// fully untrusted code.

module.exports.FunctionProxy = new Proxy(Function, {
construct: () => {
throw new EvalError('Code generation from strings disallowed for this context')
},
})