4 ms·
A warning: Don't think that this is a good idea. It's small, it's fast, it's elegant. But it's totally insecure. Everybody who had access to this format can ta
by rurban 6y ago
A warning: Don't think that this is a good idea. It's small, it's fast, it's elegant. But it's totally insecure.
Everybody who had access to this format can take it over. It can do far too much, that's why everybody uses JSON. It can only represent hashes, arrays, and some limited primitives. No links, no logic, no lib. But lua is far too powerful. Game over.
Lisp would be even better, btw. Much easier to parse, and much easier to avoid eval. But his idea is eval.
- memexy 6y agoYou can always sandbox the process if you don't trust the input. Ruby has taint checking built-in and most operating systems have lightweight sandboxing solutions. Docker is another way to go. You can just execute the script in a container and then grab standard output as the configuration.
- artifact_44 6y agobarf barf barf aaaaand barf.
- plorkyeran 6y agoA much simpler thing to do is to simply disable all of the standard library functions which allow i/o. Lua has functionality specifically intended for limiting the environment available to a script, and the standard library is small enough that doing this is actually practical.
- memexy 6y agoI remember playing around with Lua but didn't look too closely at sandboxing facilities but what you say makes sense.
- Dylan16807 6y ago> You can just execute the script in a container and then grab standard output as the configuration. In what format, that you then have to parse?
- memexy 6y agoYes. If you're passing it to another process then you would need to parse it. If you were using it in the same process and not crossing sandbox boundaries then you could just pass around whatever data structure was generated without serializing it into another format.
- cl3misch 6y agoThese are all valid points. But as OP's software is educational and runs locally (I guess), what would be the attack vector?
- ruslan 6y agoCan you please ellaborate more on why Lua is way better than JSON ? We use JSON a lot for configs and data exchange, cannot see how can we gain from switching to Lua. One of my developers suggested Lua, so I'm curious.
- anonymoushn 6y agoNot GP, but some typical gripes about json are that it does not allow comments or trailing commas. TFA is about adding loops to config files to generate big redundant configs, which may not be something you want.
- ufo 6y agoSome Lua features can make it more pleasant to write the config files by hand. For example, comments (!) and local variables. More powerful features can also be helpful if you have a large config file that would typically need to be generated by a separate program. One example of this is shown in the linked blog post, when they use a for loop to initialize a large array that has many similar objects. It can also help if the config file starts gaining special "PRAGMA" fields over the years. There is a certain tendency for configuration files to slowly evolve into an ad-hoc and poorly implemented scripting language and in those cases, switching to a proper programming language cuts the problem at the root. That said, for data exchange situations JSON has the advantage that it is more well-behaved in the case of malicious inputs.
- soapdog 6y agoWith Lua you can create environments and define exactly what functions and libraries that environment has access to, and then execute/load your configuration or data file into it. This way it can't have access to i/o or whatever you want to prevent it from accessing. This is a built-in feature since this is a very common use case for Lua. Lua is not far too powerful, Lua is as powerful as you want it to be.
- otikik 6y agoThere are a lot of cases were the input source is trusted. The author doesn't mention it explicitly, but I think that's his case. For his situation, I think it's a good use of the tool.
- fish45 6y agoon reading some of the comments here I've added some more security, but you're right that I'm not worried about security. The limitations I added should prevent your average prankster from sending an infinite loop to their teacher, but I don't think it would stop anyone more dedicated