robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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