JSON and Math are empty inside sandbox functions, so any function that uses them fails on every run

オープン 初心者向け
#598 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, typescript
領域
backend, testing

調査の方向性

リンク先の lib/services/ai-functions/sandbox-worker.cjs の行から始め、サンドボックスが JSON と Math をコピーする箇所を確認し、既存のサンドボックス関数のテストを探します。関連するテストを実行し、サンドボックス関数内で JSON.stringify と Math のメソッドの両方を呼び出すテストを1つ追加します。両方の組み込みオブジェクトがそこで動作し、テストが成功すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Sandbox functions (every trigger except http: on_signup, row events, schedules, on_webhook and manual) run in a vm context whose JSON and Math are frozen copies made with Object.assign:

https://github.com/backenly/backenly/blob/364c901eee7a1aaf46e3a0a4e5f16217b1b20591/lib/services/ai-functions/sandbox-worker.cjs#L197-L205

Object.assign copies only enumerable own properties, and the built-in methods (JSON.stringify, JSON.parse, Math.round, Math.random and the rest) are not enumerable. Both copies are empty objects, and they shadow the context's own JSON and Math. A function that calls any of them throws TypeError: JSON.stringify is not a function (or Math.round is not a function) on every run.

Generated and agent-written functions use these all the time: Math.round(amount * 100) for a Stripe amount, JSON.stringify for a request body or a log line.

The same construction outside the worker:

const vm = require('vm')
const frozenJSON = Object.freeze(Object.assign(Object.create(null), JSON))
const context = vm.createContext(Object.assign(Object.create(null), { JSON: frozenJSON }))
vm.runInContext('typeof JSON.stringify', context)    // 'undefined'
vm.runInContext('JSON.stringify({ a: 1 })', context) // TypeError: JSON.stringify is not a function

Or deploy a manual function whose body is return JSON.stringify({ a: 1 }) and run it.

Done when JSON and Math work inside the sandbox, with a test that runs a sandbox function using both.

主要言語
TypeScript
スター
56
フォーク
19
平均マージ
14時間 4分
マージ済み PR(30日)
366

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

backenly/backenly のほかの issue

backenly/backenly の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。