Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions hbase-shell/src/main/ruby/irb/hirb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ def eval_input
else
exc = nil
next
ensure
# HBASE-28660: Prevent command shadowing by incorrectly parsed local variables
cmd_names = ::Shell.commands.keys.map(&:to_sym)
workspace_binding = @context.workspace.binding
shadowing_vars = workspace_binding.local_variables & cmd_names

if shadowing_vars.any?
shadowing_vars.each do |var|
puts "WARN: '#{var}' is a reserved HBase command. Local variable assignment ignored."
end

new_binding = @context.workspace.main.get_binding
(workspace_binding.local_variables - shadowing_vars).each do |var|
new_binding.local_variable_set(var, workspace_binding.local_variable_get(var))
end
@context.instance_variable_set(:@workspace, ::IRB::WorkSpace.new(new_binding))
end
end
handle_exception(exc)
@context.workspace.local_variable_set(:_, exc)
Expand Down
59 changes: 59 additions & 0 deletions hbase-shell/src/test/ruby/shell/general_test_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

require 'hbase_constants'
require 'hbase_shell'
require 'irb/hirb'

class ShellTest < Test::Unit::TestCase
include Hbase::TestHelpers
Expand Down Expand Up @@ -149,4 +150,62 @@ class TestException < RuntimeError; end
# create a table that exists
@shell.command('create', 'nothrow_table', 'family_1')
end

#-----------------------------------------------------------------------------

class MockInputMethod < IRB::InputMethod
def initialize(lines)
super()
@lines = lines
end
def gets
@lines.shift
end
def eof?
@lines.empty?
end
def encoding
Encoding::UTF_8
end
def readable_after_eof?
false
end
end

define_test 'Shell::Shell should prevent HBase commands from being shadowed by local variables (HBASE-28660)' do
workspace = @shell.get_workspace
IRB.setup(__FILE__) unless IRB.conf[:IRB_NAME]

lines = [
"list = 10\n",
"list_namespace, 'ns.*'\n",
"my_var = 5\n"
]

input_method = MockInputMethod.new(lines)
hirb = IRB::HIRB.new(workspace, true, input_method)

hirb.context.prompt_i = ""
hirb.context.prompt_s = ""
hirb.context.prompt_c = ""
hirb.context.prompt_n = ""
hirb.context.return_format = ""
hirb.context.echo = false

output = capture_stdout do
hirb.eval_input
end

final_workspace = hirb.context.workspace
final_vars = final_workspace.binding.local_variables

assert(final_vars.include?(:my_var), "Valid variables should be preserved")
assert_equal(5, final_workspace.binding.local_variable_get(:my_var))

assert(!final_vars.include?(:list), "Command 'list' should not be shadowed")
assert(!final_vars.include?(:list_namespace), "Command 'list_namespace' should not be shadowed")

assert_match(/WARN: 'list' is a reserved HBase command/, output)
assert_match(/WARN: 'list_namespace' is a reserved HBase command/, output)
end
end
Loading