2022-11-05 12:37:05 -07:00
|
|
|
local M = {}
|
|
|
|
|
2022-11-28 12:23:04 -07:00
|
|
|
--- Reads trust database from $XDG_STATE_HOME/nvim/trust.
|
|
|
|
---
|
2023-09-20 19:03:40 -07:00
|
|
|
---@return table<string, string> Contents of trust database, if it exists. Empty table otherwise.
|
2022-11-28 12:23:04 -07:00
|
|
|
local function read_trust()
|
2023-09-20 19:03:40 -07:00
|
|
|
local trust = {} ---@type table<string, string>
|
2022-11-28 12:23:04 -07:00
|
|
|
local f = io.open(vim.fn.stdpath('state') .. '/trust', 'r')
|
|
|
|
if f then
|
|
|
|
local contents = f:read('*a')
|
|
|
|
if contents then
|
|
|
|
for line in vim.gsplit(contents, '\n') do
|
|
|
|
local hash, file = string.match(line, '^(%S+) (.+)$')
|
|
|
|
if hash and file then
|
|
|
|
trust[file] = hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
return trust
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Writes provided {trust} table to trust database at
|
|
|
|
--- $XDG_STATE_HOME/nvim/trust.
|
|
|
|
---
|
2023-09-20 19:03:40 -07:00
|
|
|
---@param trust table<string, string> Trust table to write
|
2022-11-28 12:23:04 -07:00
|
|
|
local function write_trust(trust)
|
2024-10-16 09:03:48 -07:00
|
|
|
vim.validate('trust', trust, 'table')
|
2022-11-28 12:23:04 -07:00
|
|
|
local f = assert(io.open(vim.fn.stdpath('state') .. '/trust', 'w'))
|
|
|
|
|
2023-09-20 19:03:40 -07:00
|
|
|
local t = {} ---@type string[]
|
2022-11-28 12:23:04 -07:00
|
|
|
for p, h in pairs(trust) do
|
|
|
|
t[#t + 1] = string.format('%s %s\n', h, p)
|
|
|
|
end
|
|
|
|
f:write(table.concat(t))
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
2022-11-05 12:37:05 -07:00
|
|
|
--- Attempt to read the file at {path} prompting the user if the file should be
|
|
|
|
--- trusted. The user's choice is persisted in a trust database at
|
|
|
|
--- $XDG_STATE_HOME/nvim/trust.
|
|
|
|
---
|
2024-10-04 02:13:31 -07:00
|
|
|
---@since 11
|
2022-11-28 12:23:04 -07:00
|
|
|
---@see |:trust|
|
|
|
|
---
|
2022-11-05 12:37:05 -07:00
|
|
|
---@param path (string) Path to a file to read.
|
|
|
|
---
|
|
|
|
---@return (string|nil) The contents of the given file if it exists and is
|
|
|
|
--- trusted, or nil otherwise.
|
|
|
|
function M.read(path)
|
2024-10-16 09:03:48 -07:00
|
|
|
vim.validate('path', path, 'string')
|
2023-06-03 03:06:00 -07:00
|
|
|
local fullpath = vim.uv.fs_realpath(vim.fs.normalize(path))
|
2022-11-05 12:37:05 -07:00
|
|
|
if not fullpath then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2022-11-28 12:23:04 -07:00
|
|
|
local trust = read_trust()
|
2022-11-05 12:37:05 -07:00
|
|
|
|
|
|
|
if trust[fullpath] == '!' then
|
|
|
|
-- File is denied
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2023-09-20 19:03:40 -07:00
|
|
|
local contents ---@type string?
|
2022-11-05 12:37:05 -07:00
|
|
|
do
|
|
|
|
local f = io.open(fullpath, 'r')
|
|
|
|
if not f then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
contents = f:read('*a')
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
local hash = vim.fn.sha256(contents)
|
|
|
|
if trust[fullpath] == hash then
|
|
|
|
-- File already exists in trust database
|
|
|
|
return contents
|
|
|
|
end
|
|
|
|
|
|
|
|
-- File either does not exist in trust database or the hash does not match
|
2022-12-05 11:59:04 -07:00
|
|
|
local ok, result = pcall(
|
|
|
|
vim.fn.confirm,
|
2022-11-05 12:37:05 -07:00
|
|
|
string.format('%s is not trusted.', fullpath),
|
|
|
|
'&ignore\n&view\n&deny\n&allow',
|
|
|
|
1
|
|
|
|
)
|
|
|
|
|
2022-12-05 11:59:04 -07:00
|
|
|
if not ok and result ~= 'Keyboard interrupt' then
|
|
|
|
error(result)
|
|
|
|
elseif not ok or result == 0 or result == 1 then
|
2022-11-05 12:37:05 -07:00
|
|
|
-- Cancelled or ignored
|
|
|
|
return nil
|
2022-12-05 11:59:04 -07:00
|
|
|
elseif result == 2 then
|
2022-11-05 12:37:05 -07:00
|
|
|
-- View
|
2022-12-05 11:59:04 -07:00
|
|
|
vim.cmd('sview ' .. fullpath)
|
2022-11-05 12:37:05 -07:00
|
|
|
return nil
|
2022-12-05 11:59:04 -07:00
|
|
|
elseif result == 3 then
|
2022-11-05 12:37:05 -07:00
|
|
|
-- Deny
|
|
|
|
trust[fullpath] = '!'
|
|
|
|
contents = nil
|
2022-12-05 11:59:04 -07:00
|
|
|
elseif result == 4 then
|
2022-11-05 12:37:05 -07:00
|
|
|
-- Allow
|
|
|
|
trust[fullpath] = hash
|
|
|
|
end
|
|
|
|
|
2022-11-28 12:23:04 -07:00
|
|
|
write_trust(trust)
|
|
|
|
|
|
|
|
return contents
|
|
|
|
end
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @class vim.trust.opts
|
|
|
|
--- @inlinedoc
|
|
|
|
---
|
|
|
|
--- - `'allow'` to add a file to the trust database and trust it,
|
|
|
|
--- - `'deny'` to add a file to the trust database and deny it,
|
|
|
|
--- - `'remove'` to remove file from the trust database
|
|
|
|
--- @field action 'allow'|'deny'|'remove'
|
|
|
|
---
|
|
|
|
--- Path to a file to update. Mutually exclusive with {bufnr}.
|
|
|
|
--- Cannot be used when {action} is "allow".
|
|
|
|
--- @field path? string
|
|
|
|
--- Buffer number to update. Mutually exclusive with {path}.
|
|
|
|
--- @field bufnr? integer
|
2023-09-20 19:03:40 -07:00
|
|
|
|
2022-11-28 12:23:04 -07:00
|
|
|
--- Manage the trust database.
|
|
|
|
---
|
|
|
|
--- The trust database is located at |$XDG_STATE_HOME|/nvim/trust.
|
|
|
|
---
|
2024-10-04 02:13:31 -07:00
|
|
|
---@since 11
|
2024-03-16 10:11:42 -07:00
|
|
|
---@param opts vim.trust.opts
|
2023-08-09 02:06:13 -07:00
|
|
|
---@return boolean success true if operation was successful
|
|
|
|
---@return string msg full path if operation was successful, else error message
|
2022-11-28 12:23:04 -07:00
|
|
|
function M.trust(opts)
|
|
|
|
vim.validate({
|
|
|
|
path = { opts.path, 's', true },
|
|
|
|
bufnr = { opts.bufnr, 'n', true },
|
|
|
|
action = {
|
|
|
|
opts.action,
|
|
|
|
function(m)
|
|
|
|
return m == 'allow' or m == 'deny' or m == 'remove'
|
|
|
|
end,
|
|
|
|
[["allow" or "deny" or "remove"]],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-09-20 19:03:40 -07:00
|
|
|
---@cast opts vim.trust.opts
|
2022-11-28 12:23:04 -07:00
|
|
|
local path = opts.path
|
|
|
|
local bufnr = opts.bufnr
|
|
|
|
local action = opts.action
|
|
|
|
|
2022-11-28 15:40:50 -07:00
|
|
|
assert(not path or not bufnr, '"path" and "bufnr" are mutually exclusive')
|
|
|
|
|
|
|
|
if action == 'allow' then
|
|
|
|
assert(not path, '"path" is not valid when action is "allow"')
|
2022-11-28 12:23:04 -07:00
|
|
|
end
|
|
|
|
|
2023-09-20 19:03:40 -07:00
|
|
|
local fullpath ---@type string?
|
2022-11-28 12:23:04 -07:00
|
|
|
if path then
|
2023-06-03 03:06:00 -07:00
|
|
|
fullpath = vim.uv.fs_realpath(vim.fs.normalize(path))
|
2022-11-28 15:40:50 -07:00
|
|
|
elseif bufnr then
|
2022-11-28 12:23:04 -07:00
|
|
|
local bufname = vim.api.nvim_buf_get_name(bufnr)
|
|
|
|
if bufname == '' then
|
|
|
|
return false, 'buffer is not associated with a file'
|
2022-11-05 12:37:05 -07:00
|
|
|
end
|
2023-06-03 03:06:00 -07:00
|
|
|
fullpath = vim.uv.fs_realpath(vim.fs.normalize(bufname))
|
2022-11-28 15:40:50 -07:00
|
|
|
else
|
|
|
|
error('one of "path" or "bufnr" is required')
|
2022-11-28 12:23:04 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if not fullpath then
|
|
|
|
return false, string.format('invalid path: %s', path)
|
|
|
|
end
|
|
|
|
|
|
|
|
local trust = read_trust()
|
|
|
|
|
|
|
|
if action == 'allow' then
|
|
|
|
local newline = vim.bo[bufnr].fileformat == 'unix' and '\n' or '\r\n'
|
2023-09-20 19:03:40 -07:00
|
|
|
local contents =
|
|
|
|
table.concat(vim.api.nvim_buf_get_lines(bufnr --[[@as integer]], 0, -1, false), newline)
|
2022-11-28 12:23:04 -07:00
|
|
|
if vim.bo[bufnr].endofline then
|
|
|
|
contents = contents .. newline
|
2022-11-05 12:37:05 -07:00
|
|
|
end
|
2022-11-28 12:23:04 -07:00
|
|
|
local hash = vim.fn.sha256(contents)
|
|
|
|
|
|
|
|
trust[fullpath] = hash
|
|
|
|
elseif action == 'deny' then
|
|
|
|
trust[fullpath] = '!'
|
|
|
|
elseif action == 'remove' then
|
|
|
|
trust[fullpath] = nil
|
2022-11-05 12:37:05 -07:00
|
|
|
end
|
|
|
|
|
2022-11-28 12:23:04 -07:00
|
|
|
write_trust(trust)
|
|
|
|
return true, fullpath
|
2022-11-05 12:37:05 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|