Skip to content

Commit 4d522d6

Browse files
author
Francisco
committed
feat: enforce secure file generation with mandatory tempfile.tempdir and path confinement
Mandate import tempfile; tempfile.tempdir = "/app/generated_files" as first line in all file-generating scripts Enforce all file I/O to use /app/generated_files/ exclusively — no relative paths or /tmp Require explicit file verification via os.path.exists() after save Update code_interpreter tool definition with non-negotiable rules for docx, xlsx, and plot generation Align with system-wide sticky instruction to eliminate silent failures Remove ambiguity: all file outputs are now guaranteed to be saved, verified, and upload-ready This change transforms file generation from error-prone to deterministic — enabling reliable, secure, production-grade document and asset creation within the sandbox.
1 parent f49f5cf commit 4d522d6

3 files changed

Lines changed: 34 additions & 18 deletions

File tree

src/api/entities_api/orchestration/instructions/definitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@
413413
"All Python code execution contexts must inject the following line at the top of every script "
414414
"that generates files using libraries that rely on tempfile "
415415
"(e.g., python-docx, openpyxl, matplotlib, pandas with Excel output):\n\n"
416-
"import tempfile; tempfile.tempdir = \"/app/generated_files\"\n\n"
416+
'import tempfile; tempfile.tempdir = "/app/generated_files"\n\n'
417417
"This ensures all temporary and final files are written to the correct sandbox output directory."
418418
),
419419
"ADVANCED_ANALYSIS": (

src/api/entities_api/orchestration/instructions/include_lists.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from entities_api.orchestration.instructions.definitions import LEVEL_3_WEB_USE_INSTRUCTIONS
1+
from entities_api.orchestration.instructions.definitions import \
2+
LEVEL_3_WEB_USE_INSTRUCTIONS
23

34
L2_INSTRUCTIONS = [
45
"TOOL_USAGE_PROTOCOL",

src/api/entities_api/platform_tools/definitions/code_interpreter.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,44 @@
33
"function": {
44
"name": "code_interpreter",
55
"description": (
6-
"Executes Python code in a sandbox. Returns stdout/stderr and uploads generated files.\n"
7-
"## CRITICAL RULES:\n"
8-
"1. **Syntax**: Write valid, clean Python. Do not use incomplete try/except blocks.\n"
9-
"2. **Imports**: \n"
10-
" - Excel: `import pandas as pd` or `from openpyxl import Workbook`\n"
11-
" - Word: `from docx import Document` (Use `python-docx` library)\n"
12-
" - Plotting: `import matplotlib.pyplot as plt`\n"
13-
"3. **File Generation**: \n"
14-
" - Save files to the current directory (e.g., `doc.save('my_file.docx')`).\n"
15-
" - DO NOT specify paths like `/tmp/` or `/mnt/`.\n"
16-
" - ALWAYS verify file creation at the end of script: `if os.path.exists('my_file.docx'): print('File saved')`\n"
17-
"4. **No Input**: Functions like `input()` are disabled.\n"
18-
"5. **Output**: Always `print()` the result or a success message."
6+
"Executes Python code in a secure sandbox. Returns stdout/stderr and uploads generated files.\n\n"
7+
"## MANDATORY SANDBOX RULES — NON-NEGOTIABLE:\n\n"
8+
"### 1. TEMPFILE COMPLIANCE (REQUIRED FOR ALL FILE-GENERATING SCRIPTS)\n"
9+
" The FIRST two lines of ANY script that generates files MUST be:\n"
10+
" ```\n"
11+
" import tempfile\n"
12+
' tempfile.tempdir = "/app/generated_files"\n'
13+
" ```\n"
14+
" This applies to: python-docx, openpyxl, matplotlib, pandas Excel output, and any\n"
15+
" library that internally uses Python's `tempfile` module. Omitting this causes silent failures.\n\n"
16+
"### 2. FILE I/O CONFINEMENT\n"
17+
" All file reads and writes MUST use `/app/generated_files/` as the base path.\n"
18+
" NEVER write to `/tmp`, relative paths, or any other directory.\n"
19+
" Example: `doc.save('/app/generated_files/report.docx')`\n\n"
20+
"### 3. MANDATORY VERIFICATION\n"
21+
" After saving any file, you MUST verify it exists:\n"
22+
" `import os; print(os.path.exists('/app/generated_files/my_file.docx'))`\n"
23+
" If verification prints `False`, the file was not saved — correct and retry.\n\n"
24+
"### 4. IMPORTS\n"
25+
" - Word docs : `from docx import Document` (package: python-docx)\n"
26+
" - Excel : `import openpyxl` or `import pandas as pd`\n"
27+
" - Plotting : `import matplotlib.pyplot as plt`\n\n"
28+
"### 5. NO INTERACTIVE INPUT\n"
29+
" `input()` is disabled. All values must be hardcoded or derived from context.\n\n"
30+
"### 6. ALWAYS PRINT FEEDBACK\n"
31+
" Always `print()` a success message or result so execution output is visible."
1932
),
2033
"parameters": {
2134
"type": "object",
2235
"properties": {
2336
"code": {
2437
"type": "string",
2538
"description": (
26-
"The Python code to execute. "
27-
"If generating a .docx, ensure you use `doc = Document()` and `doc.save('filename.docx')`. "
28-
"Do not surround code with markdown backticks."
39+
"Valid Python code to execute in the sandbox. "
40+
'MUST begin with `import tempfile; tempfile.tempdir = "/app/generated_files"` '
41+
"if the script generates any files. "
42+
"All file saves MUST target `/app/generated_files/`. "
43+
"Do NOT wrap code in markdown backticks."
2944
),
3045
}
3146
},

0 commit comments

Comments
 (0)