robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each , after_each , setup , finally = 6t . describe , t . it , t . before_each , t . after_each , t . setup , t . finally 7local eq =t . eq 8local clear =n . clear 9local command =n . command 10local is_os =t . is_os 11local api =n . api 12local exec_lua =n . exec_lua 13local feed_command =n . feed_command 14local feed =n . feed 15local fn =n . fn 16local stdpath =fn . stdpath 17local pcall_err =t . pcall_err 18local matches =t . matches 19local read_file =t . read_file 20 21describe ( 'vim.secure' , function () 22local function assert_trust_entry ( expected ) 23local trust =assert ( read_file ( vim . fs . joinpath ( stdpath ( 'state' ), 'trust' ))) 24eq ( expected , vim . trim ( trust )) 25end 26 27describe ( 'read()' , function () 28local xstate ='Xstate_lua_secure' 29local screen ---@type test.functional.ui.screen 30 31before_each ( function () 32clear { env ={ XDG_STATE_HOME =xstate } } 33n . mkdir_p ( vim . fs . joinpath ( xstate , is_os ( 'win' ) and 'nvim-data' or 'nvim' )) 34 35t . mkdir ( 'Xdir' ) 36t . mkdir ( 'Xdir/Xsubdir' ) 37t . write_file ( 'Xdir/Xfile.txt' , [[hello, world]] ) 38 39t . write_file ( 40'Xfile' , 41[[ 42let g:foobar = 42 43]] 44) 45screen =Screen . new ( 500 , 8 ) 46end ) 47 48after_each ( function () 49screen : detach () 50os . remove ( 'Xfile' ) 51n . rmdir ( 'Xdir' ) 52n . rmdir ( xstate ) 53end ) 54 55it ( 'regular file' , function () 56local cwd =fn . getcwd () 57local msg ='exrc: Found untrusted code. To enable it, choose (v)iew then run `:trust`:' 58local path =vim . fs . joinpath ( cwd , 'Xfile' ) 59 60-- Need to use feed_command instead of exec_lua because of the confirmation prompt 61feed_command ( [[lua vim.secure.read('Xfile')]] ) 62screen : expect ( [[ 63{MATCH: +}| 64{1:~{MATCH: +}}|*2 65{3:{MATCH: +}}| 66:lua vim.secure.read('Xfile'){MATCH: +}| 67{6:]] .. msg .. [[}{MATCH: *}| 68{6:]] .. path .. [[}{MATCH: *}| 69{6:[i]gnore, (v)iew, (d)eny: }^{MATCH: +}| 70]] ) 71feed ( 'd' ) 72screen : expect ( [[ 73^{MATCH: +}| 74{1:~{MATCH: +}}|*6 75{MATCH: +}| 76]] ) 77 78assert_trust_entry (( '! %s' ): format ( vim . fs . joinpath ( cwd , 'Xfile' ))) 79eq ( vim . NIL , exec_lua ( [[return vim.secure.read('Xfile')]] )) 80 81os . remove ( vim . fs . joinpath ( stdpath ( 'state' ), 'trust' )) 82 83feed_command ( [[lua vim.secure.read('Xfile')]] ) 84screen : expect ( [[ 85{MATCH: +}| 86{1:~{MATCH: +}}|*2 87{3:{MATCH: +}}| 88:lua vim.secure.read('Xfile'){MATCH: +}| 89{6:]] .. msg .. [[}{MATCH: *}| 90{6:]] .. path .. [[}{MATCH: *}| 91{6:[i]gnore, (v)iew, (d)eny: }^{MATCH: +}| 92]] ) 93feed ( 'v' ) 94feed ( ':trust<CR>' ) 95screen : expect ( [[ 96^let g:foobar = 42{MATCH: +}| 97{1:~{MATCH: +}}|*2 98{3:]] .. vim . fs . joinpath ( fn . fnamemodify ( cwd , ':~' ), 'Xfile' ) .. [[ [RO]{MATCH: +}}| 99{MATCH: +}| 100{1:~{MATCH: +}}| 101{2:[No Name]{MATCH: +}}| 102Allowed in trust database: "]] .. vim . fs . joinpath ( cwd , 'Xfile' ) .. [["{MATCH: +}| 103]] ) 104-- close the split for the next test below. 105feed ( ':q<CR>' ) 106 107local hash =fn . sha256 ( assert ( read_file ( 'Xfile' ))) 108assert_trust_entry (( '%s %s' ): format ( hash , vim . fs . joinpath ( cwd , 'Xfile' ))) 109eq ( 'let g:foobar = 42\n' , exec_lua ( [[return vim.secure.read('Xfile')]] )) 110 111os . remove ( vim . fs . joinpath ( stdpath ( 'state' ), 'trust' )) 112 113feed_command ( [[lua vim.secure.read('Xfile')]] ) 114screen : expect ( [[ 115{MATCH: +}| 116{1:~{MATCH: +}}|*2 117{3:{MATCH: +}}| 118:lua vim.secure.read('Xfile'){MATCH: +}| 119{6:]] .. msg .. [[}{MATCH: *}| 120{6:]] .. path .. [[}{MATCH: *}| 121{6:[i]gnore, (v)iew, (d)eny: }^{MATCH: +}| 122]] ) 123feed ( 'i' ) 124screen : expect ( [[ 125^{MATCH: +}| 126{1:~{MATCH: +}}|*6 127{MATCH: +}| 128]] ) 129 130-- Trust database is not updated 131eq ( nil , read_file ( vim . fs . joinpath ( stdpath ( 'state' ), 'trust' ))) 132 133feed_command ( [[lua vim.secure.read('Xfile')]] ) 134screen : expect ( [[ 135{MATCH: +}| 136{1:~{MATCH: +}}|*2 137{3:{MATCH: +}}| 138:lua vim.secure.read('Xfile'){MATCH: +}| 139{6:]] .. msg .. [[}{MATCH: +}| 140{6:]] .. path .. [[}{MATCH: *}| 141{6:[i]gnore, (v)iew, (d)eny: }^{MATCH: +}| 142]] ) 143feed ( 'v' ) 144screen : expect ( [[ 145^let g:foobar = 42{MATCH: +}| 146{1:~{MATCH: +}}|*2 147{3:]] .. vim . fs . joinpath ( fn . fnamemodify ( cwd , ':~' ), 'Xfile' ) .. [[ [RO]{MATCH: +}}| 148{MATCH: +}| 149{1:~{MATCH: +}}| 150{2:[No Name]{MATCH: +}}| 151{MATCH: +}| 152]] ) 153 154-- Trust database is not updated 155eq ( nil , read_file ( vim . fs . joinpath ( stdpath ( 'state' ), 'trust' ))) 156 157-- Cannot write file 158pcall_err ( command , 'write' ) 159eq ( true , api . nvim_get_option_value ( 'readonly' , {} )) 160end ) 161 162it ( 'directory' , function () 163local cwd =fn . getcwd () 164local msg = 165'exrc: Found untrusted code. DIRECTORY trust is decided only by name, not contents:' 166local path =vim . fs . joinpath ( cwd , 'Xdir' ) 167 168-- Need to use feed_command instead of exec_lua because of the confirmation prompt 169feed_command ( [[lua vim.secure.read('Xdir')]] ) 170screen : expect ( [[ 171{MATCH: +}| 172{1:~{MATCH: +}}|*2 173{3:{MATCH: +}}| 174:lua vim.secure.read('Xdir'){MATCH: +}| 175{6:]] .. msg .. [[}{MATCH: +}| 176{6:]] .. path .. [[}{MATCH: +}| 177{6:[i]gnore, (v)iew, (d)eny, (a)llow: }^{MATCH: +}| 178]] ) 179feed ( 'd' ) 180screen : expect ( [[ 181^{MATCH: +}| 182{1:~{MATCH: +}}|*6 183{MATCH: +}| 184]] ) 185 186assert_trust_entry (( '! %s' ): format ( vim . fs . joinpath ( cwd , 'Xdir' ))) 187eq ( vim . NIL , exec_lua ( [[return vim.secure.read('Xdir')]] )) 188 189os . remove ( vim . fs . joinpath ( stdpath ( 'state' ), 'trust' )) 190 191feed_command ( [[lua vim.secure.read('Xdir')]] ) 192screen : expect ( [[ 193{MATCH: +}| 194{1:~{MATCH: +}}|*2 195{3:{MATCH: +}}| 196:lua vim.secure.read('Xdir'){MATCH: +}| 197{6:]] .. msg .. [[}{MATCH: +}| 198{6:]] .. path .. [[}{MATCH: +}| 199{6:[i]gnore, (v)iew, (d)eny, (a)llow: }^{MATCH: +}| 200]] ) 201feed ( 'a' ) 202screen : expect ( [[ 203^{MATCH: +}| 204{1:~{MATCH: +}}|*6 205{MATCH: +}| 206]] ) 207 208-- Directories aren't hashed in the trust database, instead a slug ("directory") is stored 209-- instead. 210assert_trust_entry (( 'directory %s' ): format ( vim . fs . joinpath ( cwd , 'Xdir' ))) 211eq ( true , exec_lua ( [[return vim.secure.read('Xdir')]] )) 212 213os . remove ( vim . fs . joinpath ( stdpath ( 'state' ), 'trust' )) 214 215feed_command ( [[lua vim.secure.read('Xdir')]] ) 216screen : expect ( [[ 217{MATCH: +}| 218{1:~{MATCH: +}}|*2 219{3:{MATCH: +}}| 220:lua vim.secure.read('Xdir'){MATCH: +}| 221{6:]] .. msg .. [[}{MATCH: +}| 222{6:]] .. path .. [[}{MATCH: +}| 223{6:[i]gnore, (v)iew, (d)eny, (a)llow: }^{MATCH: +}| 224]] ) 225feed ( 'i' ) 226screen : expect ( [[ 227^{MATCH: +}| 228{1:~{MATCH: +}}|*6 229{MATCH: +}| 230]] ) 231 232-- Trust database is not updated 233eq ( nil , read_file ( vim . fs . joinpath ( stdpath ( 'state' ), 'trust' ))) 234 235feed_command ( [[lua vim.secure.read('Xdir')]] ) 236screen : expect ( [[ 237{MATCH: +}| 238{1:~{MATCH: +}}|*2 239{3:{MATCH: +}}| 240:lua vim.secure.read('Xdir'){MATCH: +}| 241{6:]] .. msg .. [[}{MATCH: +}| 242{6:]] .. path .. [[}{MATCH: +}| 243{6:[i]gnore, (v)iew, (d)eny, (a)llow: }^{MATCH: +}| 244]] ) 245feed ( 'v' ) 246screen : expect ( [[ 247^{MATCH: +}| 248{1:~{MATCH: +}}|*2 249{3:]] .. vim . fs . joinpath ( fn . fnamemodify ( cwd , ':~' ), 'Xdir' ) .. [[ [RO]{MATCH: +}}| 250{MATCH: +}| 251{1:~{MATCH: +}}| 252{2:[No Name]{MATCH: +}}| 253{MATCH: +}| 254]] ) 255 256-- Trust database is not updated 257eq ( nil , read_file ( vim . fs . joinpath ( stdpath ( 'state' ), 'trust' ))) 258end ) 259 260it ( '(v)iew action does not execute malicious filename #39914' , function () 261if t . skip ( t . is_os ( 'win' ), 'N/A: filename cannot have "|" char' ) then 262return 263end 264 265local evil ='Xfile|let g:secure_poc=42' 266t . write_file ( evil , 'pwned\n' ) 267finally ( function () 268os . remove ( evil ) 269end ) 270 271eq ( 272nil , 273exec_lua ( function ( path ) 274vim . fn . confirm =function () 275return 2 -- View 276end 277return vim . secure . read ( path ) 278end , evil ) 279) 280 281-- Malicious injected `:let` did NOT execute. 282eq ( 0 , fn . exists ( 'g:secure_poc' )) 283-- The file is opened in a [RO] split with its literal name. 284eq ( true , api . nvim_get_option_value ( 'readonly' , {} )) 285eq ( evil , vim . fs . basename ( api . nvim_buf_get_name ( 0 ))) 286end ) 287end ) 288 289describe ( 'trust()' , function () 290local xstate ='Xstate_lua_secure' 291local test_file ='Xtest_functional_lua_secure' 292local empty_file ='Xtest_functional_lua_secure_empty' 293local test_dir ='Xtest_functional_lua_secure_dir' 294 295setup ( function () 296clear { env ={ XDG_STATE_HOME =xstate } } 297end ) 298 299before_each ( function () 300n . mkdir_p ( vim . fs . joinpath ( xstate , is_os ( 'win' ) and 'nvim-data' or 'nvim' )) 301t . write_file ( test_file , 'test' ) 302t . write_file ( empty_file , '' ) 303t . mkdir ( test_dir ) 304end ) 305 306after_each ( function () 307os . remove ( test_file ) 308os . remove ( empty_file ) 309n . rmdir ( test_dir ) 310n . rmdir ( xstate ) 311end ) 312 313it ( 'returns error when passing both path and bufnr' , function () 314matches ( 315'"path" and "bufnr" are mutually exclusive' , 316pcall_err ( exec_lua , [[vim.secure.trust({action='deny', bufnr=0, path=...})]] , test_file ) 317) 318end ) 319 320it ( 'returns error when passing neither path or bufnr' , function () 321matches ( 322'one of "path" or "bufnr" is required' , 323pcall_err ( exec_lua , [[vim.secure.trust({action='deny'})]] ) 324) 325end ) 326 327it ( 'trust then deny then remove a file using bufnr' , function () 328local cwd =fn . getcwd () 329local hash =fn . sha256 ( assert ( read_file ( test_file ))) 330local full_path =vim . fs . joinpath ( cwd , test_file ) 331 332command ( 'edit ' .. test_file ) 333eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='allow', bufnr=0})}]] )) 334assert_trust_entry (( '%s %s' ): format ( hash , full_path )) 335 336eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='deny', bufnr=0})}]] )) 337assert_trust_entry (( '! %s' ): format ( full_path )) 338 339eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='remove', bufnr=0})}]] )) 340assert_trust_entry ( '' ) 341end ) 342 343it ( 'trust an empty file using bufnr' , function () 344local cwd =fn . getcwd () 345local hash =fn . sha256 ( assert ( read_file ( empty_file ))) 346local full_path =vim . fs . joinpath ( cwd , empty_file ) 347 348command ( 'edit ' .. empty_file ) 349eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='allow', bufnr=0})}]] )) 350assert_trust_entry (( '%s %s' ): format ( hash , full_path )) 351end ) 352 353it ( 'deny then trust then remove a file using bufnr' , function () 354local cwd =fn . getcwd () 355local hash =fn . sha256 ( assert ( read_file ( test_file ))) 356local full_path =vim . fs . joinpath ( cwd , test_file ) 357 358command ( 'edit ' .. test_file ) 359eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='deny', bufnr=0})}]] )) 360assert_trust_entry (( '! %s' ): format ( full_path )) 361 362eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='allow', bufnr=0})}]] )) 363assert_trust_entry (( '%s %s' ): format ( hash , full_path )) 364 365eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='remove', bufnr=0})}]] )) 366assert_trust_entry ( '' ) 367end ) 368 369it ( 'trust using bufnr then deny then remove a file using path' , function () 370local cwd =fn . getcwd () 371local hash =fn . sha256 ( assert ( read_file ( test_file ))) 372local full_path =vim . fs . joinpath ( cwd , test_file ) 373 374command ( 'edit ' .. test_file ) 375eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='allow', bufnr=0})}]] )) 376assert_trust_entry (( '%s %s' ): format ( hash , full_path )) 377 378eq ( 379{ true , full_path } , 380exec_lua ( [[return {vim.secure.trust({action='deny', path=...})}]] , test_file ) 381) 382assert_trust_entry (( '! %s' ): format ( full_path )) 383 384eq ( 385{ true , full_path } , 386exec_lua ( [[return {vim.secure.trust({action='remove', path=...})}]] , test_file ) 387) 388assert_trust_entry ( '' ) 389end ) 390 391it ( 'trust then deny then remove a file using path' , function () 392local cwd =fn . getcwd () 393local hash =fn . sha256 ( assert ( read_file ( test_file ))) 394local full_path =vim . fs . joinpath ( cwd , test_file ) 395 396eq ( 397{ true , full_path } , 398exec_lua ( [[return {vim.secure.trust({action='allow', path=...})}]] , test_file ) 399) 400assert_trust_entry (( '%s %s' ): format ( hash , full_path )) 401 402eq ( 403{ true , full_path } , 404exec_lua ( [[return {vim.secure.trust({action='deny', path=...})}]] , test_file ) 405) 406assert_trust_entry (( '! %s' ): format ( full_path )) 407 408eq ( 409{ true , full_path } , 410exec_lua ( [[return {vim.secure.trust({action='remove', path=...})}]] , test_file ) 411) 412assert_trust_entry ( '' ) 413end ) 414 415it ( 'deny then trust then remove a file using bufnr' , function () 416local cwd =fn . getcwd () 417local hash =fn . sha256 ( assert ( read_file ( test_file ))) 418local full_path =vim . fs . joinpath ( cwd , test_file ) 419 420command ( 'edit ' .. test_file ) 421eq ( 422{ true , full_path } , 423exec_lua ( [[return {vim.secure.trust({action='deny', path=...})}]] , test_file ) 424) 425assert_trust_entry (( '! %s' ): format ( full_path )) 426 427eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='allow', bufnr=0})}]] )) 428assert_trust_entry (( '%s %s' ): format ( hash , full_path )) 429 430eq ( 431{ true , full_path } , 432exec_lua ( [[return {vim.secure.trust({action='remove', path=...})}]] , test_file ) 433) 434assert_trust_entry ( '' ) 435end ) 436 437it ( 'trust returns error when buffer not associated to file' , function () 438command ( 'new' ) 439eq ( 440{ false , 'buffer is not associated with a file' } , 441exec_lua ( [[return {vim.secure.trust({action='allow', bufnr=0})}]] ) 442) 443end ) 444 445it ( 'trust then deny then remove a directory using bufnr' , function () 446local cwd =fn . getcwd () 447local full_path =vim . fs . joinpath ( cwd , test_dir ) 448command ( 'edit ' .. test_dir ) 449 450eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='allow', bufnr=0})}]] )) 451assert_trust_entry (( 'directory %s' ): format ( full_path )) 452 453eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='deny', bufnr=0})}]] )) 454assert_trust_entry (( '! %s' ): format ( full_path )) 455 456eq ( { true , full_path } , exec_lua ( [[return {vim.secure.trust({action='remove', bufnr=0})}]] )) 457assert_trust_entry ( '' ) 458end ) 459end ) 460end )