Skip to content

Commit d94def3

Browse files
authored
HBASE-28250 Bump jruby to 9.4.8.0 to fix snakeyaml CVE (apache#6127) (apache#6146)
* Sync code as per irb 1.4.2 * Also provide option to try irb's new functionalities for colorize and autocomplete Signed-off-by: Duo Zhang <zhangduo@apache.org> Signed-off-by: Nick Dimiduk <ndimiduk@apache.org> (cherry picked from commit 6788ff4)
1 parent be762db commit d94def3

File tree

3 files changed

+63
-49
lines changed

3 files changed

+63
-49
lines changed

hbase-shell/src/main/ruby/irb/hirb.rb

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,21 @@ def initialize(workspace = nil, interactive = true, input_method = nil)
5353
$stdout = STDOUT
5454
end
5555

56-
def output_value
56+
def output_value(omit = false)
5757
# Suppress output if last_value is 'nil'
5858
# Otherwise, when user types help, get ugly 'nil'
5959
# after all output.
60-
super unless @context.last_value.nil?
60+
super(omit) unless @context.last_value.nil?
6161
end
6262

63-
# Copied from irb.rb and overrides the rescue Exception block so the
63+
# Copied from https://github.com/ruby/irb/blob/v1.4.2/lib/irb.rb
64+
# We override the rescue Exception block so the
6465
# Shell::exception_handler can deal with the exceptions.
6566
def eval_input
67+
exc = nil
68+
6669
@scanner.set_prompt do
67-
|ltype, indent, continue, line_no|
70+
|ltype, indent, continue, line_no|
6871
if ltype
6972
f = @context.prompt_s
7073
elsif continue
@@ -80,17 +83,19 @@ def eval_input
8083
else
8184
@context.io.prompt = p = ""
8285
end
83-
if @context.auto_indent_mode
86+
if @context.auto_indent_mode and !@context.io.respond_to?(:auto_indent)
8487
unless ltype
85-
ind = prompt(@context.prompt_i, ltype, indent, line_no)[/.*\z/].size +
88+
prompt_i = @context.prompt_i.nil? ? "" : @context.prompt_i
89+
ind = prompt(prompt_i, ltype, indent, line_no)[/.*\z/].size +
8690
indent * 2 - p.size
8791
ind += 2 if continue
8892
@context.io.prompt = p + " " * ind if ind > 0
8993
end
9094
end
95+
@context.io.prompt
9196
end
9297

93-
@scanner.set_input(@context.io) do
98+
@scanner.set_input(@context.io, context: @context) do
9499
signal_status(:IN_INPUT) do
95100
if l = @context.io.gets
96101
print l if @context.verbose?
@@ -101,24 +106,51 @@ def eval_input
101106
printf "Use \"exit\" to leave %s\n", @context.ap_name
102107
end
103108
else
104-
print "\n"
109+
print "\n" if @context.prompting?
105110
end
106111
end
107112
l
108113
end
109114
end
110115

116+
@scanner.set_auto_indent(@context) if @context.auto_indent_mode
117+
111118
@scanner.each_top_level_statement do |line, line_no|
112119
signal_status(:IN_EVAL) do
113120
begin
114-
line.untaint
115-
@context.evaluate(line, line_no)
116-
output_value if @context.echo?
117-
exc = nil
121+
line.untaint if RUBY_VERSION < '2.7'
122+
if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty?
123+
IRB.set_measure_callback
124+
end
125+
if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
126+
result = nil
127+
last_proc = proc{ result = @context.evaluate(line, line_no, exception: exc) }
128+
IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) { |chain, item|
129+
_name, callback, arg = item
130+
proc {
131+
callback.(@context, line, line_no, arg, exception: exc) do
132+
chain.call
133+
end
134+
}
135+
}.call
136+
@context.set_last_value(result)
137+
else
138+
@context.evaluate(line, line_no, exception: exc)
139+
end
140+
if @context.echo?
141+
if assignment_expression?(line)
142+
if @context.echo_on_assignment?
143+
output_value(@context.echo_on_assignment? == :truncate)
144+
end
145+
else
146+
output_value
147+
end
148+
end
118149
rescue Interrupt => exc
119150
rescue SystemExit, SignalException
120151
raise
121152
rescue SyntaxError => exc
153+
# HBASE-27726: Ignore SyntaxError to prevent exiting Shell on unexpected syntax.
122154
raise exc unless @interactive
123155
rescue NameError => exc
124156
raise exc unless @interactive
@@ -128,43 +160,13 @@ def eval_input
128160
# This modifies this copied method from JRuby so that the HBase shell can
129161
# manage the exception and set a proper exit code on the process.
130162
raise exc
163+
else
164+
exc = nil
165+
next
131166
end
132-
if exc
133-
if exc.backtrace && exc.backtrace[0] =~ /irb(2)?(\/.*|-.*|\.rb)?:/ && exc.class.to_s !~ /^IRB/ &&
134-
!(SyntaxError === exc)
135-
irb_bug = true
136-
else
137-
irb_bug = false
138-
end
139-
140-
messages = []
141-
lasts = []
142-
levels = 0
143-
if exc.backtrace
144-
count = 0
145-
exc.backtrace.each do |m|
146-
m = @context.workspace.filter_backtrace(m) or next unless irb_bug
147-
m = sprintf("%9d: from %s", (count += 1), m)
148-
if messages.size < @context.back_trace_limit
149-
messages.push(m)
150-
elsif lasts.size < @context.back_trace_limit
151-
lasts.push(m).shift
152-
levels += 1
153-
end
154-
end
155-
end
156-
attr = STDOUT.tty? ? ATTR_TTY : ATTR_PLAIN
157-
print "#{attr[1]}Traceback#{attr[]} (most recent call last):\n"
158-
unless lasts.empty?
159-
puts lasts.reverse
160-
printf "... %d levels...\n", levels if levels > 0
161-
end
162-
puts messages.reverse
163-
messages = exc.to_s.split(/\n/)
164-
print "#{attr[1]}#{exc.class} (#{attr[4]}#{messages.shift}#{attr[0, 1]})#{attr[]}\n"
165-
puts messages.map {|s| "#{attr[1]}#{s}#{attr[]}\n"}
166-
print "Maybe IRB bug!\n" if irb_bug
167-
end
167+
handle_exception(exc)
168+
@context.workspace.local_variable_set(:_, exc)
169+
exc = nil
168170
end
169171
end
170172
end

hbase-shell/src/main/ruby/jar-bootstrap.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
-h | --help This help.
6969
-n | --noninteractive Do not run within an IRB session and exit with non-zero
7070
status on first error.
71+
-c | --colorize Enable colorized output.
72+
-a | --autocomplete Enable auto-completion.
7173
--top-level-defs Compatibility flag to export HBase shell commands onto
7274
Ruby's main object
7375
-Dkey=value Pass hbase-*.xml Configuration overrides. For example, to
@@ -105,6 +107,8 @@ def add_to_configuration(c, arg)
105107
['--help', '-h', GetoptLong::NO_ARGUMENT],
106108
['--debug', '-d', GetoptLong::NO_ARGUMENT],
107109
['--noninteractive', '-n', GetoptLong::NO_ARGUMENT],
110+
['--colorize', '-c', GetoptLong::NO_ARGUMENT],
111+
['--autocomplete', '-a', GetoptLong::NO_ARGUMENT],
108112
['--top-level-defs', GetoptLong::NO_ARGUMENT],
109113
['-D', GetoptLong::REQUIRED_ARGUMENT],
110114
['--return-values', '-r', GetoptLong::NO_ARGUMENT]
@@ -115,6 +119,8 @@ def add_to_configuration(c, arg)
115119
log_level = 'ERROR'
116120
@shell_debug = false
117121
interactive = true
122+
colorize = false
123+
autocomplete = false
118124
full_backtrace = false
119125
top_level_definitions = false
120126

@@ -132,6 +138,10 @@ def add_to_configuration(c, arg)
132138
puts 'Setting DEBUG log level...'
133139
when '--noninteractive'
134140
interactive = false
141+
when '--colorize'
142+
colorize = true
143+
when '--autocomplete'
144+
autocomplete = true
135145
when '--return-values'
136146
warn '[INFO] the -r | --return-values option is ignored. we always behave '\
137147
'as though it was given.'
@@ -213,6 +223,8 @@ def debug?
213223
IRB.conf[:AP_NAME] = 'hbase'
214224
IRB.conf[:PROMPT_MODE] = :CUSTOM
215225
IRB.conf[:BACK_TRACE_LIMIT] = 0 unless full_backtrace
226+
IRB.conf[:USE_AUTOCOMPLETE] = autocomplete
227+
IRB.conf[:USE_COLORIZE] = colorize
216228

217229
# Create a workspace we'll use across sessions.
218230
workspace = @shell.get_workspace

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@
597597
<wx.rs.api.version>2.1.1</wx.rs.api.version>
598598
<glassfish.jsp.version>2.3.2</glassfish.jsp.version>
599599
<glassfish.el.version>3.0.1-b08</glassfish.el.version>
600-
<jruby.version>9.3.13.0</jruby.version>
600+
<jruby.version>9.4.8.0</jruby.version>
601601
<junit.version>4.13.2</junit.version>
602602
<hamcrest.version>1.3</hamcrest.version>
603603
<opentelemetry.version>1.15.0</opentelemetry.version>

0 commit comments

Comments
 (0)