Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe mode for Lua filters #8273

Open
jgm opened this issue Sep 2, 2022 · 5 comments
Open

Safe mode for Lua filters #8273

jgm opened this issue Sep 2, 2022 · 5 comments

Comments

@jgm
Copy link
Owner

jgm commented Sep 2, 2022

It would be nice to have something like a "sandboxed" mode in which Lua filters could run. This could be triggered by the existing --sandbox CLI option. What would have to be done to make this possible?

https://github.com/kikito/lua-sandbox gives a model for restricting unsafe functions.
We could modify to taste. In addition to what they exclude, we'd have to remove require, and we'd need special versions of many of the functions pandoc provides (which are basically hooks to code implemented in Haskell). This is a bit awkward, because Lua itself has to run in IO, but in principle we could use sandbox (T.P.Class) to lift pure operations into MonadIO. To avoid inefficiency, we'd probably need to change this function so that it takes an already-constructed FileTree rather than a list of FilePaths as argument. Then we could build the ersatz FileTree once and thread it through.

(Actually, thinking back, I wonder why I didn't implement the sandbox by using a new PandocSandboxed monad that is an instance of MonadIO but doesn't actually do any IO. That could be worth reconsidering.)

Anyway, I just wanted to put this up here so we can collect ideas, or at least catalog obstacles. This is getting more important the more people come to rely on Lua filters to extend pandoc.

PS. A safe mode for regular JSON filters would be impossible to implement; since these filters are separate programs, pandoc cannot control what they do in addition to spitting out JSON. But for Lua filters it does not seem impossible.

@jgm
Copy link
Owner Author

jgm commented Sep 2, 2022

I guess one approach could be to introduce a PandocLuaSandboxed monad, which defines sandboxed versions of all the standard functions, and use that instead of PandocLua in sandboxed contexts. That would take care of the Lua functions that wrap functions from T.P.Class. We'd have to deal with things like pipe separately.

@jgm
Copy link
Owner Author

jgm commented Sep 3, 2022

@tarleb What if in documentedModule we had defun and defunSafe? (Alternatively, moduleFunctions could be a list of pairs.) Then for each module we'd be keeping track of the safe functions, and the module initialization could simply exclude unsafe functions when in sandboxed mode?

That would take care of all the functions pandoc provides. It would only remain to remove the unsafe native Lua functions from the Lua environment.

@tarleb
Copy link
Collaborator

tarleb commented Sep 3, 2022

I like that. Maybe we could get rid of the liftPure functions that way, which would even simplify the interface.

@jgm
Copy link
Owner Author

jgm commented Oct 5, 2022

Is this worth pursuing for 3.0?

@tarleb
Copy link
Collaborator

tarleb commented Oct 5, 2022

I suspect that safe Lua is a bit too much work to get it done for 3.0. I can try to squeeze some work on that in, but I'd like to concentrate more on getting the Figure code up-to-date (and hopefully merged).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants