Skip to content

Commit 371d0fe

Browse files
authored
fix(query): fix recusive wrapper codes in python udf (#18219)
1 parent ca5a61c commit 371d0fe

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/query/service/src/pipelines/processors/transforms/transform_udf_script.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -255,31 +255,32 @@ import os
255255
import sys
256256
from pathlib import Path
257257
258-
ALLOWED_BASE = Path("/tmp")
258+
import builtins, sys
259+
if "DATABEND_RESTRICTED_PYTHON" not in sys._xoptions:
260+
sys._xoptions['DATABEND_RESTRICTED_PYTHON'] = '1'
259261
260-
_original_open = open
261-
_original_os_open = os.open if hasattr(os, 'open') else None
262+
ALLOWED_BASE = Path("/tmp")
263+
_original_open = open
264+
_original_os_open = os.open if hasattr(os, 'open') else None
262265
263-
def safe_open(file, mode='r', **kwargs):
264-
file_path = Path(file).resolve()
266+
def safe_open(file, mode='r', **kwargs):
267+
file_path = Path(file).resolve()
265268
266-
try:
267-
file_path.relative_to(ALLOWED_BASE)
268-
except ValueError:
269-
raise PermissionError(f"Access denied: {file} is outside allowed directory")
269+
try:
270+
file_path.relative_to(ALLOWED_BASE)
271+
except ValueError:
272+
raise PermissionError(f"Access denied: {file} is outside allowed directory")
270273
271-
return _original_open(file, mode, **kwargs)
274+
return _original_open(file, mode, **kwargs)
272275
273-
def safe_os_open(path, flags, mode=0o777):
274-
file_path = Path(path).resolve()
275-
try:
276-
file_path.relative_to(ALLOWED_BASE)
277-
except ValueError:
278-
raise PermissionError(f"Access denied: {path} is outside allowed directory")
279-
return _original_os_open(path, flags, mode)
276+
def safe_os_open(path, flags, mode=0o777):
277+
file_path = Path(path).resolve()
278+
try:
279+
file_path.relative_to(ALLOWED_BASE)
280+
except ValueError:
281+
raise PermissionError(f"Access denied: {path} is outside allowed directory")
282+
return _original_os_open(path, flags, mode)
280283
281-
import builtins, sys
282-
if "DATABEND_RESTRICTED_PYTHON" not in sys._xoptions:
283284
builtins.open = safe_open
284285
if _original_os_open:
285286
os.open = safe_os_open
@@ -288,7 +289,6 @@ if "DATABEND_RESTRICTED_PYTHON" not in sys._xoptions:
288289
for module in dangerous_modules:
289290
if module in sys.modules:
290291
del sys.modules[module]
291-
sys._xoptions['DATABEND_RESTRICTED_PYTHON'] = '1'
292292
"#;
293293

294294
impl RuntimeBuilder<arrow_udf_runtime::python::Runtime> for PyRuntimeBuilder {

0 commit comments

Comments
 (0)