Make timeout per invocation more specific

This commit is contained in:
Adria Navarro 2024-02-08 16:20:33 +01:00
parent c57ccbc046
commit 1e101744de
5 changed files with 11 additions and 11 deletions

View File

@ -2031,7 +2031,7 @@ describe.each([
describe("Formula JS protection", () => { describe("Formula JS protection", () => {
it("should time out JS execution if a single cell takes too long", async () => { it("should time out JS execution if a single cell takes too long", async () => {
await config.withEnv({ JS_PER_EXECUTION_TIME_LIMIT_MS: 20 }, async () => { await config.withEnv({ JS_PER_INVOCATION_TIMEOUT_MS: 20 }, async () => {
const js = Buffer.from( const js = Buffer.from(
` `
let i = 0; let i = 0;
@ -2071,7 +2071,7 @@ describe.each([
it("should time out JS execution if a multiple cells take too long", async () => { it("should time out JS execution if a multiple cells take too long", async () => {
await config.withEnv( await config.withEnv(
{ {
JS_PER_EXECUTION_TIME_LIMIT_MS: 20, JS_PER_INVOCATION_TIMEOUT_MS: 20,
JS_PER_REQUEST_TIME_LIMIT_MS: 40, JS_PER_REQUEST_TIME_LIMIT_MS: 40,
}, },
async () => { async () => {

View File

@ -71,8 +71,8 @@ const environment = {
SELF_HOSTED: process.env.SELF_HOSTED, SELF_HOSTED: process.env.SELF_HOSTED,
HTTP_MB_LIMIT: process.env.HTTP_MB_LIMIT, HTTP_MB_LIMIT: process.env.HTTP_MB_LIMIT,
FORKED_PROCESS_NAME: process.env.FORKED_PROCESS_NAME || "main", FORKED_PROCESS_NAME: process.env.FORKED_PROCESS_NAME || "main",
JS_PER_EXECUTION_TIME_LIMIT_MS: JS_PER_INVOCATION_TIMEOUT_MS:
parseIntSafe(process.env.JS_PER_EXECUTION_TIME_LIMIT_MS) || 1000, parseIntSafe(process.env.JS_PER_INVOCATION_TIMEOUT_MS) || 1000,
JS_PER_REQUEST_TIME_LIMIT_MS: parseIntSafe( JS_PER_REQUEST_TIME_LIMIT_MS: parseIntSafe(
process.env.JS_PER_REQUEST_TIME_LIMIT_MS process.env.JS_PER_REQUEST_TIME_LIMIT_MS
), ),

View File

@ -18,7 +18,7 @@ export function init() {
vm = new IsolatedVM({ vm = new IsolatedVM({
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT, memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
timeout: env.JS_PER_EXECUTION_TIME_LIMIT_MS, invocationTimeout: env.JS_PER_INVOCATION_TIMEOUT_MS,
perRequestLimit: env.JS_PER_REQUEST_TIME_LIMIT_MS, perRequestLimit: env.JS_PER_REQUEST_TIME_LIMIT_MS,
}) })
.withContext(ctxToPass) .withContext(ctxToPass)

View File

@ -47,7 +47,7 @@ export class IsolatedVM implements VM {
private isolate: ivm.Isolate private isolate: ivm.Isolate
private vm: ivm.Context private vm: ivm.Context
private jail: ivm.Reference private jail: ivm.Reference
private timeout: number private invocationTimeout: number
private perRequestLimit?: number private perRequestLimit?: number
private moduleHandler = new ModuleHandler() private moduleHandler = new ModuleHandler()
@ -56,11 +56,11 @@ export class IsolatedVM implements VM {
constructor({ constructor({
memoryLimit, memoryLimit,
timeout, invocationTimeout,
perRequestLimit, perRequestLimit,
}: { }: {
memoryLimit: number memoryLimit: number
timeout: number invocationTimeout: number
perRequestLimit?: number perRequestLimit?: number
}) { }) {
this.isolate = new ivm.Isolate({ memoryLimit }) this.isolate = new ivm.Isolate({ memoryLimit })
@ -72,7 +72,7 @@ export class IsolatedVM implements VM {
[this.resultKey]: { out: "" }, [this.resultKey]: { out: "" },
}) })
this.timeout = timeout this.invocationTimeout = invocationTimeout
this.perRequestLimit = perRequestLimit this.perRequestLimit = perRequestLimit
} }
@ -155,7 +155,7 @@ export class IsolatedVM implements VM {
throw new Error(`"${specifier}" import not allowed`) throw new Error(`"${specifier}" import not allowed`)
}) })
script.evaluateSync({ timeout: this.timeout }) script.evaluateSync({ timeout: this.invocationTimeout })
const result = this.getResult() const result = this.getResult()
return result return result

View File

@ -10,7 +10,7 @@ class ScriptRunner {
this.code = `(() => {${script}})();` this.code = `(() => {${script}})();`
this.vm = new IsolatedVM({ this.vm = new IsolatedVM({
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT, memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
timeout: JS_TIMEOUT_MS, invocationTimeout: JS_TIMEOUT_MS,
}).withContext(context) }).withContext(context)
} }