Skip to content

Commit b72e111

Browse files
authored
fix(vm): use node child process for RCE (#2389)
* fix(vm): use node child process for RCE * ack PR comments * cleanup oprhaned processes * cleaned up * ack pr comment * fix path * use spawn instead of fork * acked PR comments
1 parent 300aaa5 commit b72e111

File tree

5 files changed

+617
-338
lines changed

5 files changed

+617
-338
lines changed

apps/sim/app/api/function/execute/route.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,12 @@ export async function POST(req: NextRequest) {
895895
userCodeStartLine = wrapperLines.length + 1
896896

897897
let codeToExecute = resolvedCode
898+
let prependedLineCount = 0
898899
if (isCustomTool) {
899-
const paramDestructuring = Object.keys(executionParams)
900-
.map((key) => `const ${key} = params.${key};`)
901-
.join('\n')
900+
const paramKeys = Object.keys(executionParams)
901+
const paramDestructuring = paramKeys.map((key) => `const ${key} = params.${key};`).join('\n')
902902
codeToExecute = `${paramDestructuring}\n${resolvedCode}`
903+
prependedLineCount = paramKeys.length
903904
}
904905

905906
const isolatedResult = await executeInIsolatedVM({
@@ -920,14 +921,25 @@ export async function POST(req: NextRequest) {
920921
})
921922

922923
const ivmError = isolatedResult.error
924+
// Adjust line number for prepended param destructuring in custom tools
925+
let adjustedLine = ivmError.line
926+
let adjustedLineContent = ivmError.lineContent
927+
if (prependedLineCount > 0 && ivmError.line !== undefined) {
928+
adjustedLine = Math.max(1, ivmError.line - prependedLineCount)
929+
// Get line content from original user code, not the prepended code
930+
const codeLines = resolvedCode.split('\n')
931+
if (adjustedLine <= codeLines.length) {
932+
adjustedLineContent = codeLines[adjustedLine - 1]?.trim()
933+
}
934+
}
923935
const enhancedError: EnhancedError = {
924936
message: ivmError.message,
925937
name: ivmError.name,
926938
stack: ivmError.stack,
927939
originalError: ivmError,
928-
line: ivmError.line,
940+
line: adjustedLine,
929941
column: ivmError.column,
930-
lineContent: ivmError.lineContent,
942+
lineContent: adjustedLineContent,
931943
}
932944

933945
const userFriendlyErrorMessage = createUserFriendlyErrorMessage(

0 commit comments

Comments
 (0)