fix(trust): hash unchanged empty buffers as empty files #39027

Problem:
`vim.secure.trust()` hashes an unchanged empty buffer as
a newline, so trusting an empty file by buffer never works.

Solution:
Hash unchanged empty-buffers `''` so buffer-based
trust matches the on-disk empty file.
This commit is contained in:
Barrett Ruth
2026-04-23 15:01:37 -04:00
committed by GitHub
parent 398f2c108d
commit 0a8218a2b4
3 changed files with 39 additions and 5 deletions

View File

@@ -274,6 +274,7 @@ describe('vim.secure', function()
describe('trust()', function()
local xstate = 'Xstate_lua_secure'
local test_file = 'Xtest_functional_lua_secure'
local empty_file = 'Xtest_functional_lua_secure_empty'
local test_dir = 'Xtest_functional_lua_secure_dir'
setup(function()
@@ -283,11 +284,13 @@ describe('vim.secure', function()
before_each(function()
n.mkdir_p(xstate .. pathsep .. (is_os('win') and 'nvim-data' or 'nvim'))
t.write_file(test_file, 'test')
t.write_file(empty_file, '')
t.mkdir(test_dir)
end)
after_each(function()
os.remove(test_file)
os.remove(empty_file)
n.rmdir(test_dir)
n.rmdir(xstate)
end)
@@ -325,6 +328,17 @@ describe('vim.secure', function()
eq('', vim.trim(trust))
end)
it('trust an empty file using bufnr', function()
local cwd = fn.getcwd()
local hash = fn.sha256(assert(read_file(empty_file)))
local full_path = cwd .. pathsep .. empty_file
command('edit ' .. empty_file)
eq({ true, full_path }, exec_lua([[return {vim.secure.trust({action='allow', bufnr=0})}]]))
local trust = assert(read_file(stdpath('state') .. pathsep .. 'trust'))
eq(string.format('%s %s', hash, full_path), vim.trim(trust))
end)
it('deny then trust then remove a file using bufnr', function()
local cwd = fn.getcwd()
local hash = fn.sha256(assert(read_file(test_file)))