diff options
| author | William Boman <william@redwill.se> | 2021-08-15 02:09:50 +0200 |
|---|---|---|
| committer | William Boman <william@redwill.se> | 2021-08-15 02:51:33 +0200 |
| commit | 88a2a8557a92e69e683452e53a87b02b5d924412 (patch) | |
| tree | bd5314e4487be78c347e0cab41ef82bb65de7d8d /lua | |
| parent | fs: use native vim.fns (diff) | |
| download | mason-88a2a8557a92e69e683452e53a87b02b5d924412.tar mason-88a2a8557a92e69e683452e53a87b02b5d924412.tar.gz mason-88a2a8557a92e69e683452e53a87b02b5d924412.tar.bz2 mason-88a2a8557a92e69e683452e53a87b02b5d924412.tar.lz mason-88a2a8557a92e69e683452e53a87b02b5d924412.tar.xz mason-88a2a8557a92e69e683452e53a87b02b5d924412.tar.zst mason-88a2a8557a92e69e683452e53a87b02b5d924412.zip | |
fs: assert unsafe operations are run on safe paths only
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-lsp-installer/fs.lua | 22 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/path.lua | 6 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/server.lua | 2 |
3 files changed, 23 insertions, 7 deletions
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 diff --git a/lua/nvim-lsp-installer/path.lua b/lua/nvim-lsp-installer/path.lua index 97e3ed20..25498beb 100644 --- a/lua/nvim-lsp-installer/path.lua +++ b/lua/nvim-lsp-installer/path.lua @@ -35,4 +35,10 @@ function M.realpath(relpath, depth) return M.concat { vim.fn.fnamemodify(callsite_abs_path, ":h"), relpath } end +function M.is_subdirectory(root_path, path) + return path:find(root_path) == 1 +end + +M.SERVERS_ROOT_DIR = M.concat { vim.fn.stdpath "data", "lsp_servers" } + return M diff --git a/lua/nvim-lsp-installer/server.lua b/lua/nvim-lsp-installer/server.lua index 5e3c9e87..e921d2bc 100644 --- a/lua/nvim-lsp-installer/server.lua +++ b/lua/nvim-lsp-installer/server.lua @@ -5,7 +5,7 @@ local path = require "nvim-lsp-installer.path" local M = {} function M.get_server_root_path(server) - return path.concat { vim.fn.stdpath "data", "lsp_servers", server } + return path.concat { path.SERVERS_ROOT_DIR, server } end M.Server = {} |
