A maybe slightly safer-ish wrapper around eval Function constructors

Overview

evalish

A maybe slightly safer-ish wrapper around eval Function constructors

Please maybe try something else first.. Please.


NPM Version License Minified gzip size

evalish is a small helper library that only exports a wrapper for the Function constructor: SafeFunction.

The SafeFunction constructor allows you to evaluate code and dynamically create a new function. In most environments, which at least don't have their CSP configured to disallow this, this will give you a fully executable function based on a string. As Function by default is a little safer than eval and runs everything in the global context, SafeFunction goes a step further and attempts to isolate the environment as much as possible.

It only does three simple things:

  • Isolate the global object and uses a separate object using a with statement
  • Wraps all passed through globals, like Array, in a recursive masking object that disallows access to object prototype properties
  • In the browser: Creates an iframe element and uses that frame's globals instead to prvent prototype pollution.

If you haven't run away screaming yet, maybe that's what you're looking for. Just a bit more safety. But really, I wrote this just for fun and I haven't written any tests yet and neither have I tested all edge cases. The export being named SafeFunction is really just ambitious.

However, if you found a way to break out of SafeFunction and did something to the outside JS environment, let me know and file an issue. I'm curious to see how far evalish would have to go to fully faux-isolate eval'ed code!

Usage

First install evalish alongside react:

yarn add evalish
# or
npm install --save evalish

You'll then be able to import SafeFunction and pass it argument names and code, just like the regular Function constructor.

import { SafeFunction } from 'evalish';

new SafeFunction('a', 'b', 'return a + b')(1, 2); // returns `3`
new SafeFunction('return window')(); // returns `undefined`
new SafeFunction('return Array.isArray.constructor')(); // returns `undefined`
You might also like...

Maybe better conditionals feat. tagged template literals

condicional Maybe better conditionals Report Bug · Request Feature About The Project ⚠️ `condicional` is based on a PEG grammar and is not battle-test

Oct 12, 2022

A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM deffiniation and appropriate file structure.

Welcome to function-stencil 👋 A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM

Jun 20, 2022

Tries to execute sync/async function, returns a specified default value if the function throws

good-try Tries to execute sync/async function, returns a specified default value if the function throws. Why Why not nice-try with it's 70+ million do

Dec 8, 2022

Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Dec 19, 2022

Thin wrapper around Rant-Lang for Obsidian.md

Thin wrapper around Rant-Lang for Obsidian.md

Obsidian Rant-Lang Thin wrapper around the Rant language Rust crate to be used in Obsidian. "Rant is a high-level procedural templating language with

Jul 12, 2022

A thin wrapper around arweave-js for versioned permaweb document management.

🎁 ar-wrapper A thin wrapper around arweave-js for versioned permaweb document management. Helps to abstract away complexity for document storage for

May 12, 2022

A simple nodejs module which is wrapper around solc that allows you to compile Solidity code

A simple nodejs module which is wrapper around solc that allows you to compile Solidity code

Simple Solidity Compiler It's a simple nodejs module which is wrapper around solc that allows you to compile Solidity code and get the abi and bytecod

Feb 21, 2022

A nuxt 2 wrapper around derrickreimer/fathom-client to be able to use usefathom.com in all its glory

A nuxt 2 wrapper around derrickreimer/fathom-client to be able to use usefathom.com in all its glory

This package is a nuxt 2 wrapper around derrickreimer/fathom-client to be able to use usefathom.com in all its glory. Thanks to @derrickreimer for this framework agnostic library ❤️‍🔥.

Aug 18, 2022

A jQuery plugin wrapper around Bootstrap Alerts, to create Notifications (Toasts)

A jQuery plugin wrapper around Bootstrap Alerts, to create Notifications (Toasts)

bootstrap-show-notification A jQuery plugin wrapper around Bootstrap 4 Alerts, to show them as toasts (also called notifications) dynamically from Jav

Aug 22, 2022
Owner
Phil Pluckthun
👋 strangely typed • n+1 semi-archived OSS experiment libs 🗻 • core team for styled-components & urql • @StellateHQ
Phil Pluckthun
🔧 Alternative to 'eval' in JavaScript that is customizable and safer!

?? better-eval An alternative to eval() in JavaScript that is customizable and safer! The eval function sucks, and there lacks alternatives that provi

Bharadwaj Duggaraju 32 Sep 14, 2022
we try to make a tiny p2p client spec, maybe for sigchain gossip thing, maybe for simple blockchain thing

mininode Mininode is a tiny p2p client for prototyping p2p protocols. It is a specification for a set of interfaces that I made to make it easier to t

Nikolai Mushegian 8 Nov 23, 2022
ish.ninja is a free online platform to allocate a unique ish.ninja sub handle name for a BlueSky account.

ish.ninja ish.ninja is a free online platform to allocate a unique ish.ninja sub handle name for a BlueSky account. It is built using Next.js, Xata, T

Ishaan Bedi 6 May 9, 2023
🏎See how your favorite F1 team is progressing in the Constructors Championship

F1 Championship Stats ?? See how your favorite F1 team is progressing in the Constructors Championship using the PlanetScale serverless driver for Jav

PlanetScale 102 Dec 20, 2022
Mongo Strict is a TypeScript based smart MongoDB ORM, It makes the usage of MongoDB safer, easier and faster with a better performance...

mongo-strict mongo-strict is compatible with mongo >= 5 Mongo Strict is a TypeScript-based smart MongoDB ORM, It makes the usage of MongoDB safer, eas

Mohamed Kamel 4 Sep 22, 2022
Cloud Native, DevOps, GitOps, Open Source, industry news, culture, and the 'ish between.

DevOps'ish DevOps'ish is authored by Chris Short and Made in Michigan. What is DevOps? I'm glad you asked. Newsletter Writing Process Everyone needs a

Chris Short 7 Jan 12, 2022
The world's greatest open source 3D CAM software. (Maybe one day.)

Meshmill is open source 3D CAM software for Linux. It turns STL models into G-code for CNC machines. This is beta-quality software. Use it at your own

James Stanley 20 Dec 21, 2022
A social-media mock app for the ones who love to read - and maybe show it off

?? ?? Cachalote ?? ?? Share what you are reading and find people who also likes it - or not! What does it do? This app focuses on three main questions

Thaís França 3 May 22, 2022
A Typescript assembly(ish) interpreter with a nice web UI

Assembly-Online Example Fibonacci Program MOV R0, #1 MOV R1, #1 fib: MOV R2, R0 ADD R0, R0, R1 MOV R1, R2 B print CMP R0, #40 BGT exit B fib exit: \ I

null 3 May 23, 2022
Extended version of create-t3-app to make it even faster to start (or maybe slower)

create-T3-app with extra tools/config out of the box create-t3-app is one of the fastest and easiest way to scaffold fullstack app. create-t3-extended

A Riolly C 7 Jan 4, 2023