Land #15887, Respect path expansion when performing path-based tab completion

This commit is contained in:
Grant Willcox 2021-11-18 12:24:49 -06:00
commit 3a9eb24bd3
No known key found for this signature in database
GPG Key ID: D35E05C0F2B81E83
2 changed files with 22 additions and 3 deletions

View File

@ -142,8 +142,7 @@ class File < Rex::Post::Meterpreter::Extensions::Stdapi::Fs::IO
# First check for ~
path_components = path.split(separator)
if path_components.length > 0 && path_components[0] == '~'
path_components[0] = '$HOME'
path = path_components.join(separator)
path = "$HOME#{path[1..-1]}"
end
# Now find the environment variables we'll need from the client

View File

@ -1073,7 +1073,10 @@ class Console::CommandDispatcher::Stdapi::Fs
def tab_complete_path(str, words, dir_only)
if client.commands.include?(COMMAND_ID_STDAPI_FS_LS)
results = client.fs.dir.match(str, dir_only) rescue []
expanded = str
expanded = client.fs.file.expand_path(expanded) if expanded =~ path_expand_regex
results = client.fs.dir.match(expanded, dir_only) rescue []
results = unexpand_path_for_suggestions(str, expanded, results)
if results.length == 1 && results[0] != str && results[0].end_with?(client.fs.file.separator)
# If Readline receives a single value from this function, it will assume we're done with the tab
# completing, and add an extra space at the end.
@ -1088,6 +1091,23 @@ class Console::CommandDispatcher::Stdapi::Fs
end
end
#
# After a path expansion followed by a tab completion suggestion set,
# unexpand the path back so that Readline is happy
#
def unexpand_path_for_suggestions(original_path, expanded_path, suggestions)
if original_path == expanded_path
suggestions
else
result = []
suggestions.each do |suggestion|
addition = suggestion[expanded_path.length..-1]
result.append("#{original_path}#{addition}")
end
result
end
end
end
end