2

I am using Lua embedded. In my case, I'd like to load files from a fake in memory file system (based on a .zip file), not the real file system.

To accomplish this, I think I need to to redirect/override all of the internal fopen calls to something that only looks inside the .zip file. That doesn't seem possible. Is it?

How can I create a secure Lua sandbox?

Here's a quick example:

This code will open a text file and read the contents into a local variable. Internally, it seems to use fopen(), which means that it will use the real filesystem only.

local f = io.open('filename.txt')
local t = f:read("*all")
f:close()

Instead, I want this to internally call into my "fake" filesystem and I'll return the contents of the file from the fake filesystem.

I have already been able to accomplish something like this for loading .lua files only. That worked by adding a custom searcher/loader which loaded files from the .zip file. Now, require("something.lua") only loads from the fake filesystem, which is perfect.

However, load(), dofile() and loadfile() still work and use fopen(). I am currently nil'ing them out until I can find a better solution.

load = nil
dofile = nil
loadfile = nil

At some level, I think I really want to redirect all the filesystem calls somewhere else.

Regardless, I narrowed the scope for this SO question to just ask about io.open. Should I be replacing/redirecting io.open in lua with my own so that it loads from my fake filesystem. I guess by extension, would I then re-define the io namespace?

101010
  • 14,866
  • 30
  • 95
  • 172

0 Answers0