代码之家  ›  专栏  ›  技术社区  ›  noah

在没有安全策略文件的情况下阻止在groovy Web控制台中System.exit

  •  0
  • noah  · 技术社区  · 16 年前

    我们的应用程序中有一个管理调试控制台,允许您输入脚本 然后提交它以取回结果。我们不担心 恶意用户,但希望阻止像某人这样的基本内容 进入系统。退出(1)查看它的功能。不幸的是, 添加安全策略文件不是一个选项。有别的路吗 沙盒脚本?

    3 回复  |  直到 14 年前
        1
  •  3
  •   arturh    16 年前

    我查看了groovy的“noexitSecurityManager”,发现它确实在这样做:不允许System.exit()。这还不够。

    我的解决方案是创建自己的类“DisallowAllSecurityManager”,它扩展了SecurityManager,并通过在每次检查中抛出SecurityException来禁用脚本不需要的所有内容,如对文件系统的写访问或连接到其他主机。方法。

    但是:您不能在所有检查中抛出异常。方法。这是支票清单……您需要允许的方法:

    • 检查CreateClassLoader()。
    • 检查成员访问()
    • 检查包访问()
    • 检查权限()
    • 检查属性访问()
    • 检查读取()

    当您想要评估您的groovy脚本时,您可以按以下方式进行,只限制groovy脚本:

    SecurityManager securityManager = System.getSecurityManager();
    try {
     System.setSecurityManager(new DisallowAllSecurityManager());
     Object result = groovyShell.evaluate(expression);
            ...
    } catch (...) {
            ...
    } finally {
            System.setSecurityManager(securityManager);
    }
    
        2
  •  3
  •   ZZ Coder    16 年前

    groovy为此配备了专门的安全管理器。只需在运行脚本之前设置,

            System.setSecurityManager(new NoExitSecurityManager());