2 ms·
If you're LISP has first class environments, then this becomes much easier, and safer. (eval (read (get-some-user-string)) (let [ (post-
by shakna 10y ago
If you're LISP has first class environments, then this becomes much easier, and safer.
(eval (read (get-some-user-string))
(let [
(post-title post-title)
(post-date post-date)
(post-author post-author)
(fmt string-append)
]
(null-environment)))
eval is only executed in the context of some environment, and the only things it has access to are some strings, and a string concatenation procedure.
It doesn't have access to lambda, quasiliterals or quotes, or anything of the like.
I do have code like this in production, because eval only looks up within it's environment... Which is empty apart from the bound let. No closure access.
- swift 10y agoLua has this feature as well and it's fantastically useful for this kind of use-case, or more generally anywhere where you want to allow scripting.
- shakna 10y agoThe more I use Lua, the more it seems like the sanest of the scripting languages out there.
- kazinator 10y agoMy Lisp has search through a first-class environnment which falls back on a dynamic/global environment. A way could be provided to suppress the fallback, like a special environment node indicating "do not search for bindings past me, stop here". If we have a completely empty environment, then we cannot eval the (sys:quasi ...) form itself; the sys:quasi symbol itself has no binding. A nice idea might be to have a filter object in the environment stack which says "only if you're looking for one of these specific symbols, can you proceed to the global environment". Then we can easily arrange for select global bindings to be visible.
- shakna 10y agoYou don't want the sys:quasi form to evaluate: It can be used as an escape hatch. If you're binding to the null-environment, you can provide everything a user can use. In my above example, a user can do things like: (fmt post.title post.date) (fmt post.title "@" post.date) (fmt (fmt post.title "@" post.date) "by" post.author) But they can't escape the sandbox and do anything else. To make a list, they need access to list, or quote, otherwise they just have number and strings, and anything else you give them. If you give them anything more than a null-environment, you begin to lose the safety of a safe evaluation, which might be something you want to do yourself, but not something that you want a user to have access to, or they'll find an escape hatch, like sys:quasi, and gain access to areas they shouldn't. Which is as bad as any SQL injection, if not worse. So, a safe eval, can be created, without the need for any further analysis, if eval is handed an environment, and only uses it. If the eval then searches the global environment, then the eval is unsafe, and you should never hand it user input, unless you have first ensured it's safe... Which defeats all the niceness provided by first class environments. Eval is only not evil, if the programmer controls all the bindings to eval's environment. REPL's usually use an unsafe eval and environment, and that makes sense, as you want to be able to do anything you want in code. But a templating language doesn't need, and shouldn't use, all the unsafeness of a Turing Complete language, that might have access to your system.