Skip to content

Commit f9ee0a2

Browse files
authored
security: fix shell injection vulnerabilities for query inputs (#41)
Replace unsafe string.format() calls with vim.fn.shellescape() to prevent command injection when file paths contain special characters.
1 parent 4b31ac8 commit f9ee0a2

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

lua/fff/file_picker/image.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ end
158158
--- @return number|nil, number|nil Width and height in pixels
159159
function M.get_image_dimensions(file_path)
160160
-- Try file command first
161-
local cmd = string.format('file "%s"', file_path)
161+
local cmd = string.format('file %s', vim.fn.shellescape(file_path))
162162
local result = vim.fn.system(cmd)
163163

164164
if vim.v.shell_error == 0 then
@@ -167,7 +167,7 @@ function M.get_image_dimensions(file_path)
167167
end
168168

169169
-- Fallback to identify command (ImageMagick)
170-
cmd = string.format('identify -format "%%w %%h" "%s" 2>/dev/null', file_path)
170+
cmd = string.format('identify -format "%%w %%h" %s 2>/dev/null', vim.fn.shellescape(file_path))
171171
result = vim.fn.system(cmd)
172172

173173
if vim.v.shell_error == 0 then

lua/fff/file_picker/preview.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ end
323323
--- @return table|nil Lines of content, nil if failed
324324
function M.read_file_tail(file_path, tail_lines)
325325
-- Use system tail command for efficiency
326-
local cmd = string.format('tail -n %d "%s" 2>/dev/null', tail_lines, file_path)
326+
local cmd = string.format('tail -n %d %s 2>/dev/null', tail_lines, vim.fn.shellescape(file_path))
327327
local result = vim.fn.system(cmd)
328328

329329
if vim.v.shell_error ~= 0 then
@@ -488,7 +488,7 @@ function M.preview_binary_file(file_path, bufnr, info, file)
488488

489489
-- Try to get more information about the binary file
490490
if vim.fn.executable('file') == 1 then
491-
local cmd = string.format('file -b "%s"', file_path)
491+
local cmd = string.format('file -b %s', vim.fn.shellescape(file_path))
492492
local result = vim.fn.system(cmd)
493493
if vim.v.shell_error == 0 and result then
494494
result = result:gsub('\n', '')
@@ -502,7 +502,7 @@ function M.preview_binary_file(file_path, bufnr, info, file)
502502
table.insert(lines, 'Hex dump (first 1KB):')
503503
table.insert(lines, '')
504504

505-
local cmd = string.format('xxd -l 1024 "%s"', file_path)
505+
local cmd = string.format('xxd -l 1024 %s', vim.fn.shellescape(file_path))
506506
local hex_result = vim.fn.system(cmd)
507507
if vim.v.shell_error == 0 and hex_result then
508508
local hex_lines = vim.split(hex_result, '\n')

0 commit comments

Comments
 (0)