使用 Java 脚本 API 查找并销毁不良/恶意 Java 脚本代码

发布于 2024-08-07 23:48:53 字数 259 浏览 2 评论 0原文

我正在开发一个 servlet(在 tomcat 上运行),它接收包含 Java 脚本代码的请求,并使用 java 脚本 API 框架评估/运行代码并将答案返回给用户。

由于我们正在处理用户生成的代码,因此该代码可以是好代码,也可以是坏代码。错误代码的一个例子是 while(true);这将在服务器中无休止地循环,占用不必要的资源

我的问题

1)我如何发现错误的代码? 2)一旦被识别为坏/恶意代码,停止运行的最佳方法是什么?

谢谢

I am working on a servlet (runs on tomcat) which receives requests that contains Java Script code, and using the java scripting API framework evaluates/run the code and returns the answer to the user.

Since we are dealing with user generated code, the code can be a good code and it can be bad code. As an example for a bad code can be while(true); which will endlessly loop in the server taking unnecessary resources

my questions

1) how can i discover a bad code?
2) once identified as a bad/malicious code what is the best way to stop the run?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走过海棠暮 2024-08-14 23:48:53

我的问题是:什么算作糟糕的代码

如果您无法给出什么是坏代码的正式定义,您就不能指望能够检测到它。因为这可能是你的问题的真正含义,所以我将提出我的答案 - 没有办法做到这一点。

即使是看似微不足道的事情,例如程序是否会终止也无法提前确定,我希望任何坏代码的定义都是无法终止的。

因此,在我看来,你有一个主要选择:信任你的用户(或者不信任他们并且不运行任何东西)。

否则,可能工作的方法是在严格的沙箱中运行脚本,如果脚本尚未完成运行,则在适当的时间后终止它。可接受的程度在很大程度上取决于您的具体情况。

My question to you: what counts as bad code?

If you cannot come up with a formal definition of what counts as bad code, you cannot hope to be able to detect it. And since this is probably what your question really meant, I'll put forward my answer - there's no way to do it.

Even a seemingly trivial thing such as whether a program will terminate or not cannot be determined ahead of time, and I'd expect any definition of bad code would be something that couldn't terminate.

Thus to my mind you have one major option: trust your users (or alternatively don't trust them and don't run anything).

Something that might work otherwise is to run the script in a strict sandbox, and terminate it after an appropriate amount of time if it hasn't already finished running. It very much depends on your circumstances as to what is acceptable.

内心荒芜 2024-08-14 23:48:53

你真的是从兔子洞里跳下去了。无法提前确定代码是否属于资源密集型或具有恶意意图。即使是人类也很难做到这一点。话虽如此,您可以采取一些措施来保护自己。

  1. 使用 Rhino 而不是 Java 6 的内置 JS 脚本引擎,因为它为您提供了更多选择。
    • 实现监控指令计数的自定义上下文。这使您有机会中断无限循环的脚本。请参阅 Rhino 的 ContextFactory
    • 在单独的线程中运行脚本,以便您可以中断陷入等待状态而不会触发上下文的指令计数的脚本
    • 实施安全管理器:请参阅概述API。这将允许您将脚本限制为仅应与其交互的对象。

我已经在Myna中实现了1,2和3,欢迎您窃取代码

You are really jumping down the rabbit hole on this one. There is no way to determine in advance if code is resource intensive or has mailious intent. Even humans have a hard time with that. Having said that there are some things you can do to defend yourself.

  1. Use Rhino instead of Java 6's built-in JS scripting engine as it gives you more options.
    • Implement a custom context that monitors instruction count. This gives you an opportunity to interrupt scripts that are infinitely looping. See Rhino's ContextFactory class
    • run your scripts in a separate thread so that you can interrupt scripts stuck in in wait states that don't trigger the Context's intruction count
    • Implement a security manager: see Overview, API. This will allow you to restrict the script to just those objects it should be interacting with.

I have implemented 1,2, and 3 in Myna and you are welcome to steal code

葬花如无物 2024-08-14 23:48:53

已经有一个工具可以识别“坏”JavaScript,JSLint。显然,坏代码的定义是非常主观的,但 JSLint 提供了广泛的选项,因此您应该能够将其配置为非常接近您对坏代码的定义。

您可以通过上面链接的 Web 表单向 JSLint 提交代码(和配置选项)。还应该可以以编程方式向 JSLint 提交代码(和选项),但如果您打算定期这样做,则应该获得作者的许可。

There's already a tool that identifies 'bad' JavaScript, JSLint. Obviously the definition of bad code is highly subjective, but JSLint provides a wide range of options, so you should be able to configure it to conform fairly closely to your definition of bad.

You can submit code (and configuration options) to JSLint via the web form linked to above. It should also be possible to submit code (and options) to JSLint programatically, but you should get the author's permission if you plan to do this regularly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文