Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Question about using JavaScript Function constructor as a sandbox

Use Case

I need to run some user submitted JavaScript which will interact with an API object that only contains data and API functions. I am expecting simple linear logic from user submitted code which will only use data provided by the API object as input and will write output to the API object as well.

My Plan

const userSubmittedCode = //get it from somewhere

// If user code contains keywords like 'this', 'prototype', 'constructor', etc, do not continue.
// (Basically anything that the user won't need but seems dangerous)

const callUserCode = Function(
    "apiObject", 
    "const globalThis = window = document = {}; // + anything I want to hide" + userSubmittedCode);
callUserCode(myAPIObject);

// Do things according to myAPIObject's internal state.

I am not super familiar with JavaScript and am not sure if this is sufficient (don't really care about memory exhaustion and timeout attacks right now). Any thoughts would be welcome.

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can store the keywords which you don't want the string to contain in an array, and use Array.some to check whether the string contains one of the items:

const bannedKeywords = ['this', 'prototype', 'constructor']

function isSafe(str){
   return !bannedKeywords.some(e => str.includes(e))
}

console.log(isSafe('a'))
console.log(isSafe('this'))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!