aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-04-18 17:51:13 +0200
committerMarc André Tanner <mat@brain-dump.org>2016-04-18 17:54:10 +0200
commitb2ec2cdff86a022f1eb0e267fe36e807b2446d2b (patch)
tree10b924949fa91cb49bc31a397b2ff8b2eb76afd0
parent7939a5657320810a7802679d6c078f7c3f212152 (diff)
downloadvis-b2ec2cdff86a022f1eb0e267fe36e807b2446d2b.tar.gz
vis-b2ec2cdff86a022f1eb0e267fe36e807b2446d2b.tar.xz
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.
-rw-r--r--vis-lua.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/vis-lua.c b/vis-lua.c
index 4d9a61b..0c043fc 100644
--- 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