robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
50c50ab119
master
1local M ={} 2 3local fileformat_newlines ={ 4unix ='\n' , 5dos ='\r\n' , 6mac ='\r' , 7} 8 9--- Reads trust database from $XDG_STATE_HOME/nvim/trust. 10--- 11---@return table<string, string> Contents of trust database, if it exists. Empty table otherwise. 12local function read_trust () 13local trust ={} ---@type table<string, string> 14local f =io . open ( vim . fn . stdpath ( 'state' ) .. '/trust' , 'r' ) 15if f then 16local contents =f : read ( '*a' ) 17if contents then 18for line in vim . gsplit ( contents , '\n' ) do 19local hash , file =string . match ( line , '^(%S+) (.+)$' ) 20if hash and file then 21trust [ file ] =hash 22end 23end 24end 25f : close () 26end 27return trust 28end 29 30--- If {fullpath} is a file, read the contents of {fullpath} (or the contents of {bufnr} 31--- if given) and returns the contents and a hash of the contents. 32--- 33--- If {fullpath} is a directory, then nothing is read from the filesystem, and 34--- `contents = true` and `hash = "directory"` is returned instead. 35--- 36---@param fullpath string Path to a file or directory to read. 37---@param bufnr integer? The number of the buffer. 38---@return string|boolean? contents the contents of the file, or true if it's a directory 39---@return string? hash the hash of the contents, or "directory" if it's a directory 40local function compute_hash ( fullpath , bufnr ) 41local contents ---@type string|boolean? 42local hash ---@type string 43if vim . fn . isdirectory ( fullpath ) == 1 then 44return true , 'directory' 45end 46 47if bufnr then 48local is_unchanged_empty =vim . api . nvim_buf_call ( bufnr , function () 49return not vim . bo [ bufnr ]. modified and vim . fn . line2byte ( 1 ) == - 1 50end ) 51if is_unchanged_empty then 52contents ='' 53else 54local newline =fileformat_newlines [ vim . bo [ bufnr ]. fileformat ] 55contents = 56table . concat ( vim . api . nvim_buf_get_lines ( bufnr --[[@as integer]] , 0 , - 1 , false ), newline ) 57if vim . bo [ bufnr ]. endofline then 58contents =contents .. newline 59end 60end 61else 62do 63local f =io . open ( fullpath , 'rb' ) 64if not f then 65return nil , nil 66end 67contents =f : read ( '*a' ) 68f : close () 69end 70 71if not contents then 72return nil , nil 73end 74end 75 76hash =vim . fn . sha256 ( contents ) 77 78return contents , hash 79end 80 81--- Writes provided {trust} table to trust database at 82--- $XDG_STATE_HOME/nvim/trust. 83--- 84---@param trust table<string, string> Trust table to write 85local function write_trust ( trust ) 86vim . validate ( 'trust' , trust , 'table' ) 87local f =assert ( io . open ( vim . fn . stdpath ( 'state' ) .. '/trust' , 'w' )) 88 89local t ={} ---@type string[] 90for p , h in pairs ( trust ) do 91t [ # t + 1 ] =string . format ( '%s %s\n' , h , p ) 92end 93f : write ( table . concat ( t )) 94f : close () 95end 96 97--- If {path} is a file: attempt to read the file, prompting the user if the file should be 98--- trusted. 99--- 100--- If {path} is a directory: return true if the directory is trusted (non-recursive), prompting 101--- the user as necessary. 102--- 103--- The user's choice is persisted in a trust database at 104--- $XDG_STATE_HOME/nvim/trust. 105--- 106---@since 11 107---@see |:trust| 108--- 109---@param path (string) Path to a file or directory to read. 110--- 111---@return (boolean|string|nil) If {path} is not trusted or does not exist, returns `nil`. Otherwise, 112--- returns the contents of {path} if it is a file, or true if {path} is a directory. 113function M . read ( path ) 114vim . validate ( 'path' , path , 'string' ) 115local fullpath =vim . uv . fs_realpath ( vim . fs . normalize ( path )) 116if not fullpath then 117return nil 118end 119fullpath =vim . fs . normalize ( fullpath ) -- Ensure "/" slashes, even on Windows. 120 121local trust =read_trust () 122 123if trust [ fullpath ] == '!' then 124-- File is denied 125return nil 126end 127 128local contents , hash =compute_hash ( fullpath , nil ) 129if not contents then 130return nil 131end 132 133if trust [ fullpath ] == hash then 134-- File already exists in trust database 135return contents 136end 137 138local msg2 =' To enable it, choose (v)iew then run `:trust`:' 139local choices ='&ignore\n&view\n&deny' 140if hash == 'directory' then 141msg2 =' DIRECTORY trust is decided only by name, not contents:' 142choices ='&ignore\n&view\n&deny\n&allow' 143end 144 145-- File either does not exist in trust database or the hash does not match 146local ok , result =pcall ( 147vim . fn . confirm , 148string . format ( 'exrc: Found untrusted code.%s\n%s' , msg2 , fullpath ), 149choices , 1501 151) 152 153if not ok and result ~= 'Keyboard interrupt' then 154error ( result ) 155elseif not ok or result == 0 or result == 1 then 156-- Cancelled or ignored 157return nil 158elseif result == 2 then 159-- View 160vim . cmd (( 'sview %s' ): format ( vim . fn . fnameescape ( fullpath ))) 161return nil 162elseif result == 3 then 163-- Deny 164trust [ fullpath ] ='!' 165contents =nil 166elseif hash == 'directory' and result == 4 then 167-- Allow 168trust [ fullpath ] =hash 169end 170 171write_trust ( trust ) 172 173return contents 174end 175 176--- @class vim.trust.opts 177--- @inlinedoc 178--- 179--- One of: 180--- - `'allow'` to add a file to the trust database and trust it, 181--- - `'deny'` to add a file to the trust database and deny it, 182--- - `'remove'` to remove file from the trust database 183--- @field action 'allow'|'deny'|'remove' 184--- 185--- Path to a file to update. Mutually exclusive with {bufnr}. 186--- @field path? string 187--- Buffer number to update. Mutually exclusive with {path}. 188--- @field bufnr? integer 189 190--- Manage the trust database. 191--- 192--- The trust database is located at |$XDG_STATE_HOME|/nvim/trust. 193--- 194---@since 11 195---@param opts vim.trust.opts 196---@return boolean success true if operation was successful 197---@return string msg full path if operation was successful, else error message 198function M . trust ( opts ) 199vim . validate ( 'path' , opts . path , 'string' , true ) 200vim . validate ( 'bufnr' , opts . bufnr , 'number' , true ) 201vim . validate ( 'action' , opts . action , function ( m ) 202return m == 'allow' or m == 'deny' or m == 'remove' 203end , [["allow" or "deny" or "remove"]] ) 204 205---@cast opts vim.trust.opts 206local path =opts . path 207local bufnr =opts . bufnr 208local action =opts . action 209 210assert ( not path or not bufnr , '"path" and "bufnr" are mutually exclusive' ) 211 212local fullpath ---@type string? 213if path then 214fullpath =vim . uv . fs_realpath ( vim . fs . normalize ( path )) 215elseif bufnr then 216local bufname =vim . api . nvim_buf_get_name ( bufnr ) 217if bufname == '' then 218return false , 'buffer is not associated with a file' 219end 220fullpath =vim . uv . fs_realpath ( vim . fs . normalize ( bufname )) 221else 222error ( 'one of "path" or "bufnr" is required' ) 223end 224 225if not fullpath then 226return false , string . format ( 'invalid path: %s' , path ) 227end 228fullpath =vim . fs . normalize ( fullpath ) -- Ensure "/" slashes, even on Windows. 229 230local trust =read_trust () 231 232if action == 'allow' then 233local contents , hash =compute_hash ( fullpath , bufnr ) 234if not contents then 235return false , string . format ( 'could not read path: %s' , fullpath ) 236end 237 238trust [ fullpath ] =hash 239elseif action == 'deny' then 240trust [ fullpath ] ='!' 241elseif action == 'remove' then 242trust [ fullpath ] =nil 243end 244 245write_trust ( trust ) 246return true , fullpath 247end 248 249return M