Skip to content

Commit

Permalink
better handling of prefix injection
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar committed Aug 3, 2024
1 parent 311f023 commit 43d1a51
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/foreman/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def initialize(options={})
@env = {}
@mutex = Mutex.new
@names = {}
@prefixed = {}
@processes = []
@running = {}
@readers = {}
Expand Down Expand Up @@ -378,6 +379,7 @@ def spawn_processes
writer.puts "unknown command: #{process.command}"
end
@buffers[reader] = Buffer.new
@prefixed[reader] = false
@running[pid] = [process, n]
@readers[pid] = reader
end
Expand Down Expand Up @@ -414,22 +416,26 @@ def handle_io(readers)
def handle_io_interactive(reader)
done = false
name = name_for(@readers.invert[reader])

output_partial prefix(name)
indent = prefix(name).gsub(ANSI_TOKEN, "").length

loop do
@buffers[reader].write(reader.read_nonblock(10))

@buffers[reader].each_token do |token|
case token
when /^\e\[(\d+)G$/
output_partial "\e[#{::Regexp.last_match(1).to_i + prefix(name).gsub(ANSI_TOKEN, "").length}G"
output_partial "\e[#{Regexp.last_match(1).to_i + indent}G"
when ANSI_TOKEN
output_partial token
when "\n"
output_partial token
output_partial prefix(name)
@prefixed[reader] = false
else
unless @prefixed[reader]
output_partial "\e[1G"
output_partial prefix(name)
@prefixed[reader] = true
end
output_partial token
end
done = (token == "\n")
Expand All @@ -450,6 +456,11 @@ def handle_io_noninteractive(reader)
end
end

def output_prefix(reader)
output_partial prefix(name_for(@readers.invert[reader]))
@prefixed[reader] = true
end

def watch_for_output
Thread.new do
begin
Expand Down

0 comments on commit 43d1a51

Please sign in to comment.