secure_spec.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local Screen = require('test.functional.ui.screen')
  3. local eq = helpers.eq
  4. local clear = helpers.clear
  5. local command = helpers.command
  6. local pathsep = helpers.get_pathsep()
  7. local is_os = helpers.is_os
  8. local curbufmeths = helpers.curbufmeths
  9. local exec_lua = helpers.exec_lua
  10. local feed_command = helpers.feed_command
  11. local feed = helpers.feed
  12. local funcs = helpers.funcs
  13. local pcall_err = helpers.pcall_err
  14. local matches = helpers.matches
  15. describe('vim.secure', function()
  16. describe('read()', function()
  17. local xstate = 'Xstate'
  18. setup(function()
  19. clear{env={XDG_STATE_HOME=xstate}}
  20. helpers.mkdir_p(xstate .. pathsep .. (is_os('win') and 'nvim-data' or 'nvim'))
  21. helpers.write_file('Xfile', [[
  22. let g:foobar = 42
  23. ]])
  24. end)
  25. teardown(function()
  26. os.remove('Xfile')
  27. helpers.rmdir(xstate)
  28. end)
  29. it('works', function()
  30. local screen = Screen.new(80, 8)
  31. screen:attach()
  32. screen:set_default_attr_ids({
  33. [1] = {bold = true, foreground = Screen.colors.Blue1},
  34. [2] = {bold = true, reverse = true},
  35. [3] = {bold = true, foreground = Screen.colors.SeaGreen},
  36. [4] = {reverse = true},
  37. })
  38. --- XXX: screen:expect() may fail if this path is too long.
  39. local cwd = funcs.getcwd()
  40. -- Need to use feed_command instead of exec_lua because of the confirmation prompt
  41. feed_command([[lua vim.secure.read('Xfile')]])
  42. screen:expect{grid=[[
  43. |
  44. {1:~ }|
  45. {1:~ }|
  46. {1:~ }|
  47. {2: }|
  48. :lua vim.secure.read('Xfile') |
  49. {3:]] .. cwd .. pathsep .. [[Xfile is not trusted.}{MATCH:%s+}|
  50. {3:[i]gnore, (v)iew, (d)eny, (a)llow: }^ |
  51. ]]}
  52. feed('d')
  53. screen:expect{grid=[[
  54. ^ |
  55. {1:~ }|
  56. {1:~ }|
  57. {1:~ }|
  58. {1:~ }|
  59. {1:~ }|
  60. {1:~ }|
  61. |
  62. ]]}
  63. local trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  64. eq(string.format('! %s', cwd .. pathsep .. 'Xfile'), vim.trim(trust))
  65. eq(helpers.NIL, exec_lua([[return vim.secure.read('Xfile')]]))
  66. os.remove(funcs.stdpath('state') .. pathsep .. 'trust')
  67. feed_command([[lua vim.secure.read('Xfile')]])
  68. screen:expect{grid=[[
  69. |
  70. {1:~ }|
  71. {1:~ }|
  72. {1:~ }|
  73. {2: }|
  74. :lua vim.secure.read('Xfile') |
  75. {3:]] .. cwd .. pathsep .. [[Xfile is not trusted.}{MATCH:%s+}|
  76. {3:[i]gnore, (v)iew, (d)eny, (a)llow: }^ |
  77. ]]}
  78. feed('a')
  79. screen:expect{grid=[[
  80. ^ |
  81. {1:~ }|
  82. {1:~ }|
  83. {1:~ }|
  84. {1:~ }|
  85. {1:~ }|
  86. {1:~ }|
  87. |
  88. ]]}
  89. local hash = funcs.sha256(helpers.read_file('Xfile'))
  90. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  91. eq(string.format('%s %s', hash, cwd .. pathsep .. 'Xfile'), vim.trim(trust))
  92. eq(helpers.NIL, exec_lua([[vim.secure.read('Xfile')]]))
  93. os.remove(funcs.stdpath('state') .. pathsep .. 'trust')
  94. feed_command([[lua vim.secure.read('Xfile')]])
  95. screen:expect{grid=[[
  96. |
  97. {1:~ }|
  98. {1:~ }|
  99. {1:~ }|
  100. {2: }|
  101. :lua vim.secure.read('Xfile') |
  102. {3:]] .. cwd .. pathsep .. [[Xfile is not trusted.}{MATCH:%s+}|
  103. {3:[i]gnore, (v)iew, (d)eny, (a)llow: }^ |
  104. ]]}
  105. feed('i')
  106. screen:expect{grid=[[
  107. ^ |
  108. {1:~ }|
  109. {1:~ }|
  110. {1:~ }|
  111. {1:~ }|
  112. {1:~ }|
  113. {1:~ }|
  114. |
  115. ]]}
  116. -- Trust database is not updated
  117. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  118. eq(nil, trust)
  119. feed_command([[lua vim.secure.read('Xfile')]])
  120. screen:expect{grid=[[
  121. |
  122. {1:~ }|
  123. {1:~ }|
  124. {1:~ }|
  125. {2: }|
  126. :lua vim.secure.read('Xfile') |
  127. {3:]] .. cwd .. pathsep .. [[Xfile is not trusted.}{MATCH:%s+}|
  128. {3:[i]gnore, (v)iew, (d)eny, (a)llow: }^ |
  129. ]]}
  130. feed('v')
  131. screen:expect{grid=[[
  132. ^let g:foobar = 42 |
  133. {1:~ }|
  134. {1:~ }|
  135. {2:]] .. funcs.fnamemodify(cwd, ':~') .. pathsep .. [[Xfile [RO]{MATCH:%s+}}|
  136. |
  137. {1:~ }|
  138. {4:[No Name] }|
  139. |
  140. ]]}
  141. -- Trust database is not updated
  142. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  143. eq(nil, trust)
  144. -- Cannot write file
  145. pcall_err(command, 'write')
  146. eq(true, curbufmeths.get_option('readonly'))
  147. end)
  148. end)
  149. describe('trust()', function()
  150. local xstate = 'Xstate'
  151. setup(function()
  152. clear{env={XDG_STATE_HOME=xstate}}
  153. helpers.mkdir_p(xstate .. pathsep .. (is_os('win') and 'nvim-data' or 'nvim'))
  154. end)
  155. teardown(function()
  156. helpers.rmdir(xstate)
  157. end)
  158. before_each(function()
  159. helpers.write_file('test_file', 'test')
  160. end)
  161. after_each(function()
  162. os.remove('test_file')
  163. end)
  164. it('returns error when passing both path and bufnr', function()
  165. matches('"path" and "bufnr" are mutually exclusive',
  166. pcall_err(exec_lua, [[vim.secure.trust({action='deny', bufnr=0, path='test_file'})]]))
  167. end)
  168. it('returns error when passing neither path or bufnr', function()
  169. matches('one of "path" or "bufnr" is required',
  170. pcall_err(exec_lua, [[vim.secure.trust({action='deny'})]]))
  171. end)
  172. it('trust then deny then remove a file using bufnr', function()
  173. local cwd = funcs.getcwd()
  174. local hash = funcs.sha256(helpers.read_file('test_file'))
  175. local full_path = cwd .. pathsep .. 'test_file'
  176. command('edit test_file')
  177. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='allow', bufnr=0})}]]))
  178. local trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  179. eq(string.format('%s %s', hash, full_path), vim.trim(trust))
  180. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='deny', bufnr=0})}]]))
  181. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  182. eq(string.format('! %s', full_path), vim.trim(trust))
  183. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='remove', bufnr=0})}]]))
  184. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  185. eq('', vim.trim(trust))
  186. end)
  187. it('deny then trust then remove a file using bufnr', function()
  188. local cwd = funcs.getcwd()
  189. local hash = funcs.sha256(helpers.read_file('test_file'))
  190. local full_path = cwd .. pathsep .. 'test_file'
  191. command('edit test_file')
  192. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='deny', bufnr=0})}]]))
  193. local trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  194. eq(string.format('! %s', full_path), vim.trim(trust))
  195. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='allow', bufnr=0})}]]))
  196. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  197. eq(string.format('%s %s', hash, full_path), vim.trim(trust))
  198. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='remove', bufnr=0})}]]))
  199. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  200. eq('', vim.trim(trust))
  201. end)
  202. it('trust using bufnr then deny then remove a file using path', function()
  203. local cwd = funcs.getcwd()
  204. local hash = funcs.sha256(helpers.read_file('test_file'))
  205. local full_path = cwd .. pathsep .. 'test_file'
  206. command('edit test_file')
  207. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='allow', bufnr=0})}]]))
  208. local trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  209. eq(string.format('%s %s', hash, full_path), vim.trim(trust))
  210. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='deny', path='test_file'})}]]))
  211. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  212. eq(string.format('! %s', full_path), vim.trim(trust))
  213. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='remove', path='test_file'})}]]))
  214. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  215. eq('', vim.trim(trust))
  216. end)
  217. it('deny then trust then remove a file using bufnr', function()
  218. local cwd = funcs.getcwd()
  219. local hash = funcs.sha256(helpers.read_file('test_file'))
  220. local full_path = cwd .. pathsep .. 'test_file'
  221. command('edit test_file')
  222. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='deny', path='test_file'})}]]))
  223. local trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  224. eq(string.format('! %s', full_path), vim.trim(trust))
  225. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='allow', bufnr=0})}]]))
  226. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  227. eq(string.format('%s %s', hash, full_path), vim.trim(trust))
  228. eq({true, full_path}, exec_lua([[return {vim.secure.trust({action='remove', path='test_file'})}]]))
  229. trust = helpers.read_file(funcs.stdpath('state') .. pathsep .. 'trust')
  230. eq('', vim.trim(trust))
  231. end)
  232. it('trust returns error when buffer not associated to file', function()
  233. command('new')
  234. eq({false, 'buffer is not associated with a file'},
  235. exec_lua([[return {vim.secure.trust({action='allow', bufnr=0})}]]))
  236. end)
  237. end)
  238. end)