Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CTRL + Z for interactive SQL REPL #18773

Merged
merged 1 commit into from Feb 5, 2024

Conversation

sjanusz-r7
Copy link
Contributor

This PR ensures that you can correctly CTRL + Z and CTRL + C out of an interactive REPL shell inside the PostgreSQL session type.

Verification

List the steps needed to make sure this thing works

  • Start msfconsole
  • get a postgres session using postgres_login
  • sessions -i -1
  • shell
  • Verify you can run SQL queries as expected
  • Verify multi-line queries work as expected
  • Try ctrl + z'ing
  • Try ctrl + c'ing
  • Try exiting by typing 'exit'
  • Verify the tests pass

@sempervictus
Copy link
Contributor

Thanks.
SIGTSTP/SIGINT handling has been the water-bear-grade gremlin around these parts since before my time - readline re-impls and so forth have emanated at least in part from this. Given the places where we're supposed to run correctly and possible session depths it can get a bit odd. I've been using native readline (-L) with trap "" 20 in the runner script for some years successfully, but as i understand its still a "mileage may vary" situation in other environments.

def _fallback
stop_words = %w[stop s exit e end quit q].freeze
line_buffer = []
while (line = ::Readline.readline(prompt = line_buffer.empty? ? 'SQL >> ' : 'SQL *> ', add_history = true))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a reminder for the future to use our history manager to ensure it's stored in a context-specific manner.

finished = false
begin
prompt_proc_before = ::Reline.prompt_proc
::Reline.prompt_proc = proc { |line_buffer| line_buffer.each_with_index.map { |_line, i| i > 0 ? 'SQL *> ' : 'SQL >> ' } }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib/rex/ui/text/input/readline is meant to be a reusable module shared by multiple modules, and used in multiple contexts. I don't think it makes sense to have any of this SQL-specific logic here - as it stops the method from be used in any other scenario other than SQL prompts 👀

finished || multiline_input.split.last&.end_with?(';')
end
rescue ::StandardError => e
elog('Failed to get multi-line SQL query from user', e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker; this error handling might happen a bit too transparently for the average user 🤔

@adfoster-r7 adfoster-r7 added the rn-fix release notes fix label Feb 5, 2024
@adfoster-r7 adfoster-r7 merged commit 5975d66 into rapid7:master Feb 5, 2024
36 checks passed
@adfoster-r7
Copy link
Contributor

Release Notes

Fixes an issue were Ctrl+Z and Ctrl+C when in the context of an interactive Postgres SQL shell prompt inside the PostgreSQL session type did work correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rn-fix release notes fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants