我REPL
为scala
.
它在 linux 环境中运行并scala
在对话框中处理编写的代码并给出结果:
例如
user| 1+1
bot | res0: Int = 2
user| res0 + 3
bot | res1: Int = 5
...
为此,我使用了scala 解释器 API 。
string
以scala代码给出的处理代码:
private val settings = new Settings
settings.processArgumentString(
"""
|-deprecation
|-feature
|-Xfatal-warnings
|-Xlint
|-usejavacp
|""".stripMargin)
def run(code: String, id: Long): (Result, String) = {
stream.reset()
try {
val intp = intpMap.getOrElseUpdate(id, new IMain(settings, new PrintWriter(stream, true)))
timedRun(maxWorkTime)(intp.interpret(code)) -> stream.toString
} catch {
case e: TimeoutException => (Error, s"Долго считать - иди в пень")
}
}
但是在以下情况下会出现问题:如果用户尝试访问系统文件怎么办?例如会写字符串:
scala.sys.process.stringToProcess("ls /").!!
bot 将允许访问系统文件。我还在https://scastie.scala-lang.org/中尝试了这个代码片段并获得了对系统文件的访问权限。但我认为他们在 docker 容器中运行 REPL 并且没有问题。
有什么方法可以限制运行我的机器人的 jvm 实例访问系统文件,或者我可以限制 REPL API 配置中的文件访问?
此时我正在为“scala.sys”或“java.io”子字符串的给定字符串进行分析,但我认为它不可靠。
还有其他安全漏洞吗?