langgenius / dify-sandbox

A lightweight, fast, and secure code execution environment that supports multiple programming languages
https://docs.dify.ai/development/backend/sandbox
Apache License 2.0
434 stars 97 forks source link

symbolic links in `python_lib_path` are not copied on `initDependencies` #48

Closed tmokmss closed 2 months ago

tmokmss commented 2 months ago

When I include /usr/lib/x86_64-linux-gnu as python_lib_path in config.yaml, symbolic links in the directory are not copied to /var/sandbox/sandbox-python/usr/lib/x86_64-linux-gnu/ at all.

The source directory contains symbolic links:

$ ls -la /usr/lib/x86_64-linux-gnu
total 42212
drwxr-xr-x. 1 root root   16384 Aug 24 15:37 .
drwxr-xr-x. 1 root root      30 Aug 24 15:37 ..
drwxr-xr-x. 1 root root      50 Aug 12 00:00 e2fsprogs
drwxr-xr-x. 1 root root      63 Aug 13 01:12 engines-3
drwxr-xr-x. 1 root root   16384 Aug 24 15:37 gconv
drwxr-xr-x. 1 root root      21 Aug 13 01:13 krb5
-rwxr-xr-x. 2 root root  214776 Aug 14 14:06 ld-linux-x86-64.so.2
-rw-r--r--. 2 root root   14552 Aug 14 14:06 libBrokenLocale.so.1
lrwxrwxrwx. 1 root root      18 Jan 17  2023 libacl.so.1 -> libacl.so.1.1.2301
-rw-r--r--. 2 root root   38832 Jan 17  2023 libacl.so.1.1.2301
-rw-r--r--. 2 root root   14408 Aug 14 14:06 libanl.so.1
lrwxrwxrwx. 1 root root      19 May 25  2023 libapt-pkg.so.6.0 -> libapt-pkg.so.6.0.0
-rw-r--r--. 2 root root 2067760 May 25  2023 libapt-pkg.so.6.0.0
lrwxrwxrwx. 1 root root      23 May 25  2023 libapt-private.so.0.0 -> libapt-private.so.0.0.0
-rw-r--r--. 2 root root  497904 May 25  2023 libapt-private.so.0.0.0
lrwxrwxrwx. 1 root root      19 Jan 17  2023 libattr.so.1 -> libattr.so.1.1.2501
-rw-r--r--. 2 root root   26544 Jan 17  2023 libattr.so.1.1.2501
lrwxrwxrwx. 1 root root      17 Feb  9  2023 libaudit.so.1 -> libaudit.so.1.0.0
-rw-r--r--. 2 root root  128952 Feb  9  2023 libaudit.so.1.0.0
lrwxrwxrwx. 1 root root      17 Mar 28 09:52 libblkid.so.1 -> libblkid.so.1.1.0
-rw-r--r--. 2 root root  355328 Mar 28 09:52 libblkid.so.1.1.0
lrwxrwxrwx. 1 root root      15 Sep 19  2022 libbz2.so.1 -> libbz2.so.1.0.4
lrwxrwxrwx. 1 root root      15 Sep 19  2022 libbz2.so.1.0 -> libbz2.so.1.0.4
...

And the dist directory only contains actual files:

$ ls -la /var/sandbox/sandbox-python/usr/lib/x86_64-linux-gnu/
total 42212
drwxr-xr-x.  1 root root   16384 Aug 25 06:19 .
drwxr-xr-x.  1 root root      30 Aug 24 15:38 ..
drwxr-xr-x.  2 root root      50 Aug 25 06:19 e2fsprogs
drwxr-xr-x.  2 root root      63 Aug 25 06:19 engines-3
drwxr-xr-x.  3 root root   16384 Aug 25 06:19 gconv
drwxr-xr-x.  3 root root      21 Aug 25 06:19 krb5
-rwxr-xr-x.  2 root root  214776 Aug 14 14:06 ld-linux-x86-64.so.2
-rw-r--r--.  2 root root   14552 Aug 14 14:06 libBrokenLocale.so.1
-rw-r--r--.  2 root root   38832 Jan 17  2023 libacl.so.1.1.2301
-rw-r--r--.  2 root root   14408 Aug 14 14:06 libanl.so.1
-rw-r--r--.  2 root root 2067760 May 25  2023 libapt-pkg.so.6.0.0
-rw-r--r--.  2 root root  497904 May 25  2023 libapt-private.so.0.0.0
-rw-r--r--.  2 root root   26544 Jan 17  2023 libattr.so.1.1.2501
-rw-r--r--.  2 root root  128952 Feb  9  2023 libaudit.so.1.0.0
-rw-r--r--.  2 root root  355328 Mar 28 09:52 libblkid.so.1.1.0
-rw-r--r--.  2 root root   74688 Sep 19  2022 libbz2.so.1.0.4
...

This result in some shared objects using symbolic links not found when executing code:

Example Python code:

import xml.parsers.expat

This results in the below error

Traceback (most recent call last):\n File \"/var/sandbox/sandbox-python/tmp/14c654f4_11b3_42c0_b8e5_cd4874d3efee.py\", line 48, in \n File \"\", line 1, in \n File \"/usr/local/lib/python3.10/xml/parsers/expat.py\", line 4, in \n from pyexpat import *\nImportError: libexpat.so.1: cannot open shared object file: No such file or directory\nerror: exit status 255

I guess env.sh should be fixed:

https://github.com/langgenius/dify-sandbox/blob/e5b7e58a03b95c7744badbdea1d9151ce0e6dd17/internal/core/runner/python/env.sh#L13-L28