Is there a way to guarantee that a script is pure?...
# scripting
r
Is there a way to guarantee that a script is pure? So it has zero side effects and just returns a value? My idea is that Kotlin is expressive enough to write typesafe configuration files directly in it - so an application could specify a data class or interface representing its config, and then configurers can write a
.kts
file that returns an instance of that type, and by setting up their IDE to have the classpath available then they will find out when configuring it whether or not their configuration will compile, and the application can gain compilation validation of its config by evaluating the file. But that means evaluating code that could theoretically do anything. I’ve implemented it using JSR-223 & a
SecurityManager
to try and prevent unwanted execution here: https://gist.github.com/Mahoney/8979e520b7477c9a9771fe3e1da3472a but it feels a bit clunky, and the
SecurityManager
is deprecated in Java 17 for removal in future Java releases. As I’ve just found out that Kotlin has its own scripting API, I wondered if it could be run in such a way?
v
I have no idea whether it will work, but maybe you could have some kotlin compiler plugin that will then verify only allowed APIs are used.
🙏 1
i
Unfortunately there is no explicit compiler-enforceable pureness that you can reuse in scripting yet. We have thought about similar scenarios and there are plans to add something like precise import control to the scripting API, that would allow some sandboxing, but it is not implemented yet. And also whether it will be sufficient for secure sandboxing - remains to be investigated too. But there is some control that you have right now, so you can experiment with it, if you like. With scripting API you can control the script compilation classpath (including stdlib and reflect) and also evaluation classloaders, so this may give you some sandboxing already.
🙏 1
👍 1
e
@Rob Elliot Did you go further with this? I'm also interested and I'd like to hear your experience with it, if any
r
Nope, not yet.