Skip to content

plugin.lua sandbox #7

@ii14

Description

@ii14

Evaluating plugin.lua will probably look something like this:

local chunk = assert(loadfile('plugin.lua'))
local res = {}
setfenv(chunk, res)
assert(pcall(chunk))

print(vim.inspect(res))

I think it goes without saying that it probably shouldn't be evaluated with full unrestricted access to the global environment. The question is should it be evaluated with a completely clean environment, without access to anything, or with a subset of lua stdlib like for example this:

env
local env = {
  type = type,
  tonumber = tonumber,
  tostring = tostring,

  select = select,
  unpack = unpack,

  next = next,
  pairs = pairs,
  ipairs = ipairs,

  assert = assert,
  error = error,
  pcall = pcall,
  xpcall = xpcall,

  string = {
    byte = string.byte,
    char = string.char,
    -- no string.dump
    find = string.find,
    format = string.format,
    gmatch = string.gmatch,
    gsub = string.gsub,
    len = string.len,
    lower = string.lower,
    match = string.match,
    rep = string.rep,
    reverse = string.reverse,
    sub = string.sub,
    upper = string.upper,
  },

  table = {
    concat = table.concat,
    insert = table.insert,
    maxn = table.maxn,
    remove = table.remove,
    sort = table.sort,
  },

  math = {
    abs = math.abs,
    acos = math.acos,
    asin = math.asin,
    atan = math.atan,
    atan2 = math.atan2,
    ceil = math.ceil,
    cos = math.cos,
    cosh = math.cosh,
    deg = math.deg,
    exp = math.exp,
    floor = math.floor,
    fmod = math.fmod,
    frexp = math.frexp,
    huge = math.huge,
    ldexp = math.ldexp,
    log = math.log,
    log10 = math.log10,
    max = math.max,
    min = math.min,
    modf = math.modf,
    pi = math.pi,
    pow = math.pow,
    rad = math.rad,
    -- no math.random
    -- no math.randomseed
    sin = math.sin,
    sinh = math.sinh,
    sqrt = math.sqrt,
    tan = math.tan,
    tanh = math.tanh,
  }
}

cc @lewis6991

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions