Execute lua string as lua code

I want to share lua modules with co-workers. To get the latest version of the shared modules, I want to store and retrieve them using a webserver.

My questions:

Can lua code be loaded directly from http request or string?

I want to achieve something like this:

module = [[
    local sharedModule = {}
    function sharedModule.greet(name) print("hello " .. name) end  
    return sharedModule
]]
greeter = require (module)
greeter.greet("john")

      

Perhaps this is the wrong thing. Is there a better approach than this one?

+3


source to share


1 answer


There's a whole section in Program in Lua dedicated to this. Your needs will be met directly with loadstring

.

I would carefully check the code you are actually executing. At least the version (working with the wrong version is likely to end up in all kinds of problems if the code being executed depends on the environment being in a particular state). Optimize the checksum and sign the code and check the signature before doing anything. If your environment is not secure, this is essentially a huge backdoor opening.



You can also use the library rings

to isolate the code you use in the Lua environment itself. It might not be sealed from a security standpoint, but should at least prevent the resulting code from breaking your application if / when it goes wrong.

+2


source







All Articles