首页 > 解决方案 > 通过 Polyglot API 执行 Python 始终需要完整的系统 IO 访问权限

问题描述

我正在尝试通过 polyglot API 执行一些非常简单的 python 代码。当尝试运行以下简单的测试时:

@Test
public void printSomTextPythonTest() throws ScriptExecutionException, IOException {

    String code = "print('Python will print this to the console!')";    

    String[] supportedLangs = { "js", "python", "R" };

    Context testContext = Context.newBuilder(supportedLangs)
            .allowAllAccess(false)
            .allowHostAccess(false)
            .allowHostClassLoading(false)
            .allowIO(false)
            .allowNativeAccess(false)
            .allowCreateThread(false)               
            .build();

    Source source = Source.newBuilder("python", code, "pyScript").build();              
    Value result = testContext.eval(source);

    testContext.close();
}

或者:

@Test
public void setAVariablePythonTest() throws ScriptExecutionException, IOException {

    String code = "someNumber = 11";

    String[] supportedLangs = { "js", "python", "R" };

    Context testContext = Context.newBuilder(supportedLangs)
            .allowAllAccess(false)
            .allowHostAccess(false)
            .allowHostClassLoading(false)
            .allowIO(false)
            .allowNativeAccess(false)
            .allowCreateThread(false)               
            .build();

    Source source = Source.newBuilder("python", code, "pyScript").build();              
    Value result = testContext.eval(source);

    testContext.close();
}

我收到此错误(下面的堆栈跟踪):

org.graalvm.polyglot.PolyglotException: java.lang.SecurityException: Operation is not allowed for: /code/polyglot-test

如果我在构建上下文时将 allowIO 更改为 true,则代码运行良好并给出预期结果。我还尝试了更复杂的代码,结果相同。

为什么要执行 Python 代码需要 IO 访问?

在 JS 中编写和执行的等效代码不需要将 allowIO 设置为 true,所以在我看来,它是 Python 特有的东西。

谢谢你的帮助。

更新

我也一直在用 R 作为客人语言进行测试。运行以下测试:

@Test
public void helloWorldRTest() throws ScriptExecutionException, IOException {

    String code = "print(\"R will print this to the console!\")";       

    String[] supportedLangs = { "js", "python", "R" };

    Context testContext = Context.newBuilder(supportedLangs)
            .allowAllAccess(false)
            .allowHostAccess(false)
            .allowHostClassLoading(false)
            .allowIO(false)
            .allowNativeAccess(false)
            .allowCreateThread(false)               
            .build();

    Source source = Source.newBuilder("R", code, "rScript").build();                
    Value result = testContext.eval(source);

    testContext.close();
}

我收到以下错误:

FastR 意外失败:从 /Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/R/lib/libR.dylib 加载 libR 时出错。如果在 NFI 后端运行,您是否提供了 libtrufflenfi.so 的位置作为系统属性“truffle.nfi.library”的值?当前值为“空”。您的系统上是否存在 OpenMP 运行时库 (libgomp.so)?例如,这个库通常是 GCC 包的一部分。详细信息:宿主环境不允许访问本机代码。

但是,通过将“allowNativeAccess”设置为 true,代码可以正常运行而不会出错。

不同的语言是否需要不同类型的访问权限才能运行?

在我的用例中,我试图尽可能地对执行进行沙箱化。我希望脚本,无论它们是用什么语言编写的,都只能访问提供给它们的数据。以来宾语言运行的脚本不应该对主机系统有任何访问权限。这是可以实现的吗?

部分堆栈跟踪(如果需要调试,我可以提供完整的堆栈跟踪):

org.graalvm.polyglot.PolyglotException:java.lang.SecurityException:不允许操作:/code/polyglot-test at com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java:489) at com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) 在 com.oracle.truffle.api.TruffleFile.checkAccess(TruffleFile.java:983) 在 com.oracle.truffle.api。 com.oracle.graal.python.builtins.modules.PosixModuleBuiltins$StatNode.stat(PosixModuleBuiltins.java:404) 上 com.oracle.graal.python.builtins.modules.PosixModuleBuiltins$ 上的 TruffleFile.exists(TruffleFile.java:102) StatNode.doStat(PosixModuleBuiltins.java:397) 在 com.oracle.graal.python.builtins.modules.PosixModuleBuiltinsFactory$StatNodeFactory$StatNodeGen.executeAndSpecialize(PosixModuleBuiltinsFactory.java:855) 在 com.oracle.graal.python.builtins.modules.PosixModuleBuiltinsFactory$StatNodeFactory$StatNodeGen.execute(PosixModuleBuiltinsFactory.java:807) 在 com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode$BuiltinBinaryCallNode.execute(BuiltinFunctionRootNode.java:103) 在com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode.execute(BuiltinFunctionRootNode.java:229) at stat(Unknown) at stat(../../../../../Library/Java/JavaVirtualMachines /graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/python/lib-graalpython/posix.py:51:2230-2247) 在 _path_stat(../../../../ ../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/python/lib-python/3/importlib/_bootstrap_external.py:82:2759-2772)在_path_is_mode_type( ../../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0。0-rc4/Contents/Home/jre/languages/python/lib-python/3/importlib/_bootstrap_external.py:88:2901-2916) 在 _path_isdir(../../../../../在 path_hook_for_FileFinder(../ ../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/python/lib-python/3/importlib/_bootstrap_external.py: 1333:50397-50413) 在 PathFinder._path_hooks(../../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/ python/lib-python/3/importlib/_bootstrap_external.py:1083:40517-40526) 在 PathFinder._path_importer_cache(../../../../../Library/Java/JavaVirtualMachines/graalvm-ee- 1.0。0-rc4/Contents/Home/jre/languages/python/lib-python/3/importlib/_bootstrap_external.py:1107:41279-41299) 在 PathFinder._get_spec(../../../../. ./Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/python/lib-python/3/importlib/_bootstrap_external.py:1135:42391-42421)在 PathFinder.find_spec (../../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/python/lib-python/3/importlib/ _bootstrap_external.py:1166:43686-43722) 在 _find_spec(../../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/语言/python/lib-python/3/importlib/_bootstrap.py:892:28932-28960) 在 _find_and_load_unlocked(../../../../../Library/Java/JavaVirtualMachines/graalvm-ee- 1.0.0-rc4/Contents/Home/jre/languages/python/lib-python/3/importlib/_bootstrap.py:953:31192-31213) 在 _find_and_load(../../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/python/lib- python/3/importlib/_bootstrap.py:968:31701-31738) 在 _gcd_import(../../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/内容/主页/jre/languages/python/lib-python/3/importlib/_bootstrap.py:985:32285-32317)在 导入(../../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/python/lib-python/3/importlib /_bootstrap.py:1066:35366-35382) 在 (../../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/语言/python/lib-graalpython/ builtins_patches.py:48:2220-2224) 在 org.graalvm.polyglot.Context.eval(Context.java:313) 在 bolt.tests.BoltEngineGraalTest.helloWorldPythonTest(BoltEngineGraalTest.java:66) 在 sun.reflect.NativeMethodAccessorImpl.invoke0( Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498)在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model。 FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.internal。runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java: 325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在 org.junit.runners.ParentRunner$3.run(ParentRunner. java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000 (ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal .junit4.runner。JUnit4TestReference.run(JUnit4TestReference.java:86) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:538) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java: 460) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) 原始内部错误:java.lang.SecurityException:不允许操作:/code/polyglot-test at com。 oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java:489) at com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) ...在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.run(TestExecution.java:38) 处运行(JUnit4TestReference.java:86)。 java:538) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) 原始内部错误:java.lang.SecurityException:不允许操作:com.oracle 的 /code/polyglot-test。 truffle.api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java:489) at com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) ...在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.run(TestExecution.java:38) 处运行(JUnit4TestReference.java:86)。 java:538) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) 原始内部错误:java.lang.SecurityException:不允许操作:com.oracle 的 /code/polyglot-test。 truffle.api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java:489) at com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) ...TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:760) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java: 206) 原始内部错误:java.lang.SecurityException:不允许操作:com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java:489) 的 /code/polyglot-test 在 com。 oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) ...TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:760) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java: 206) 原始内部错误:java.lang.SecurityException:不允许操作:com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java:489) 的 /code/polyglot-test 在 com。 oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) ...538) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) 在 org .eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)原始内部错误:java.lang.SecurityException:不允许操作:com.oracle.truffle的/code/polyglot-test。 com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) 处的 api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java:489) ...538) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) 在 org .eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)原始内部错误:java.lang.SecurityException:不允许操作:com.oracle.truffle的/code/polyglot-test。 com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) 处的 api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java:489) ...main(RemoteTestRunner.java:206)原始内部错误:java.lang.SecurityException:不允许操作:/code/polyglot-test at com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java :489) 在 com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) ...main(RemoteTestRunner.java:206)原始内部错误:java.lang.SecurityException:不允许操作:/code/polyglot-test at com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.forbidden(FileSystems.java :489) 在 com.oracle.truffle.api.vm.FileSystems$DeniedIOFileSystem.checkAccess(FileSystems.java:367) ...

标签: graalvm

解决方案


推荐阅读