From 88a2a8557a92e69e683452e53a87b02b5d924412 Mon Sep 17 00:00:00 2001 From: William Boman Date: Sun, 15 Aug 2021 02:09:50 +0200 Subject: fs: assert unsafe operations are run on safe paths only --- lua/nvim-lsp-installer/fs.lua | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'lua/nvim-lsp-installer/fs.lua') diff --git a/lua/nvim-lsp-installer/fs.lua b/lua/nvim-lsp-installer/fs.lua index 55809767..0cfaceca 100644 --- a/lua/nvim-lsp-installer/fs.lua +++ b/lua/nvim-lsp-installer/fs.lua @@ -1,7 +1,23 @@ +local pathm = require("nvim-lsp-installer.path") + local uv = vim.loop local M = {} +local function assert_ownership(path) + if not pathm.is_subdirectory(pathm.SERVERS_ROOT_DIR, path) then + error(("Refusing to operate on path outside of the servers root dir (%s)."):format(pathm.SERVERS_ROOT_DIR)) + end +end + +function M.rmrf(path) + assert_ownership(path) + if vim.fn.delete(path, "rf") ~= 0 then + error(("rmrf: Could not remove directory %q."):format(path)) + end +end + function M.mkdirp(path) + assert_ownership(path) if vim.fn.mkdir(path, "p") ~= 1 then error(("mkdirp: Could not create directory %q."):format(path)) end @@ -28,10 +44,4 @@ function M.fstat(path) return assert(uv.fs_fstat(fd)) end -function M.rmrf(path) - if vim.fn.delete(path, "rf") ~= 0 then - error(("rmrf: Could not remove directory %q."):format(path)) - end -end - return M -- cgit v1.2.3-70-g09d2