vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 503151c0a9c38602dc6ccf8cefcfbf41893c5d13
parent facc6852f012f329b6807af8a23a8bd0a11f5017
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Mon, 18 Apr 2016 17:51:13 +0200

vis-lua: strip relative paths from package.{path,cpath}

Allthough the default paths should take precedence we do not want
to execute arbitrary code from the current working directory.

Diffstat:
Mvis-lua.c | 41+++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+), 0 deletions(-)

diff --git a/vis-lua.c b/vis-lua.c @@ -916,6 +916,44 @@ static void vis_lua_event(Vis *vis, const char *name) { lua_remove(L, -2); } +static bool vis_lua_path_strip(Vis *vis) { + lua_State *L = vis->lua; + lua_getglobal(L, "package"); + + for (const char **var = (const char*[]){ "path", "cpath", NULL }; *var; var++) { + + lua_getfield(L, -1, *var); + const char *path = lua_tostring(L, -1); + lua_pop(L, 1); + if (!path) + return false; + + char *copy = strdup(path), *stripped = calloc(1, strlen(path)+2); + if (!copy || !stripped) { + free(copy); + free(stripped); + return false; + } + + for (char *elem = copy, *stripped_elem = stripped, *next; elem; elem = next) { + if ((next = strstr(elem, ";"))) + *next++ = '\0'; + if (strstr(elem, "./")) + continue; /* skip relative path entries */ + stripped_elem += sprintf(stripped_elem, "%s;", elem); + } + + lua_pushstring(L, stripped); + lua_setfield(L, -2, *var); + + free(copy); + free(stripped); + } + + lua_pop(L, 1); /* package */ + return true; +} + static bool vis_lua_path_add(Vis *vis, const char *path) { if (!path) return false; @@ -939,6 +977,9 @@ void vis_lua_start(Vis *vis) { vis->lua = L; luaL_openlibs(L); + /* remove any relative paths from lua's default package.path */ + vis_lua_path_strip(vis); + /* extends lua's package.path with: * - $VIS_PATH/{,lexers} * - {,lexers} relative to the binary location