| local M = {} |
|
|
| |
| |
| |
| local function read_trust() |
| local trust = {} |
| 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 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| local function compute_hash(fullpath, bufnr) |
| local contents |
| local hash |
| if vim.fn.isdirectory(fullpath) == 1 then |
| return true, 'directory' |
| end |
|
|
| if bufnr then |
| local newline = vim.bo[bufnr].fileformat == 'unix' and '\n' or '\r\n' |
| contents = |
| table.concat(vim.api.nvim_buf_get_lines(bufnr , 0, -1, false), newline) |
| if vim.bo[bufnr].endofline then |
| contents = contents .. newline |
| end |
| else |
| do |
| local f = io.open(fullpath, 'r') |
| if not f then |
| return nil, nil |
| end |
| contents = f:read('*a') |
| f:close() |
| end |
|
|
| if not contents then |
| return nil, nil |
| end |
| end |
|
|
| hash = vim.fn.sha256(contents) |
|
|
| return contents, hash |
| end |
|
|
| |
| |
| |
| |
| local function write_trust(trust) |
| vim.validate('trust', trust, 'table') |
| local f = assert(io.open(vim.fn.stdpath('state') .. '/trust', 'w')) |
|
|
| local t = {} |
| 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 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.read(path) |
| vim.validate('path', path, 'string') |
| local fullpath = vim.uv.fs_realpath(vim.fs.normalize(path)) |
| if not fullpath then |
| return nil |
| end |
|
|
| local trust = read_trust() |
|
|
| if trust[fullpath] == '!' then |
| |
| return nil |
| end |
|
|
| local contents, hash = compute_hash(fullpath, nil) |
| if not contents then |
| return nil |
| end |
|
|
| if trust[fullpath] == hash then |
| |
| return contents |
| end |
|
|
| local dir_msg = '' |
| if hash == 'directory' then |
| dir_msg = ' DIRECTORY trust is decided only by its name, not its contents.' |
| end |
|
|
| |
| local ok, result = pcall( |
| vim.fn.confirm, |
| string.format('%s is not trusted.%s', fullpath, dir_msg), |
| '&ignore\n&view\n&deny\n&allow', |
| 1 |
| ) |
|
|
| if not ok and result ~= 'Keyboard interrupt' then |
| error(result) |
| elseif not ok or result == 0 or result == 1 then |
| |
| return nil |
| elseif result == 2 then |
| |
| vim.cmd('sview ' .. fullpath) |
| return nil |
| elseif result == 3 then |
| |
| trust[fullpath] = '!' |
| contents = nil |
| elseif result == 4 then |
| |
| trust[fullpath] = hash |
| end |
|
|
| write_trust(trust) |
|
|
| return contents |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function M.trust(opts) |
| vim.validate('path', opts.path, 'string', true) |
| vim.validate('bufnr', opts.bufnr, 'number', true) |
| vim.validate('action', opts.action, function(m) |
| return m == 'allow' or m == 'deny' or m == 'remove' |
| end, [["allow" or "deny" or "remove"]]) |
|
|
| |
| local path = opts.path |
| local bufnr = opts.bufnr |
| local action = opts.action |
|
|
| 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"') |
| end |
|
|
| local fullpath |
| if path then |
| fullpath = vim.uv.fs_realpath(vim.fs.normalize(path)) |
| elseif bufnr then |
| local bufname = vim.api.nvim_buf_get_name(bufnr) |
| if bufname == '' then |
| return false, 'buffer is not associated with a file' |
| end |
| fullpath = vim.uv.fs_realpath(vim.fs.normalize(bufname)) |
| else |
| error('one of "path" or "bufnr" is required') |
| end |
|
|
| if not fullpath then |
| return false, string.format('invalid path: %s', path) |
| end |
|
|
| local trust = read_trust() |
|
|
| if action == 'allow' then |
| local contents, hash = compute_hash(fullpath, bufnr) |
| if not contents then |
| return false, string.format('could not read path: %s', fullpath) |
| end |
|
|
| trust[fullpath] = hash |
| elseif action == 'deny' then |
| trust[fullpath] = '!' |
| elseif action == 'remove' then |
| trust[fullpath] = nil |
| end |
|
|
| write_trust(trust) |
| return true, fullpath |
| end |
|
|
| return M |
|
|