From 557dad7c918d64ed151eaaa0672a4915ee057a32 Mon Sep 17 00:00:00 2001 From: Peace-Maker Date: Wed, 14 Feb 2024 22:33:12 +0100 Subject: [PATCH 1/2] Fix readline omitting a trailing \n This is a regression from #2129 which was based on a bogus test which mixed `input` and `readline` calls. Only `input` (and `raw_input`) are documented to strip the newline, but `readline` itself should include it. Fixes #2342 --- pwnlib/term/readline.py | 9 +++++---- pwnlib/tubes/tube.py | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pwnlib/term/readline.py b/pwnlib/term/readline.py index b60b656de..72082eb89 100644 --- a/pwnlib/term/readline.py +++ b/pwnlib/term/readline.py @@ -5,6 +5,7 @@ import six import sys +import os from pwnlib.term import keyconsts as kc from pwnlib.term import keymap as km @@ -404,7 +405,7 @@ def readline(_size=-1, prompt='', float=True, priority=10): buffer = (buffer_left + buffer_right) if buffer: history.insert(0, buffer) - return force_to_bytes(buffer) + return force_to_bytes(buffer) + b'\n' except KeyboardInterrupt: control_c() finally: @@ -432,7 +433,7 @@ def raw_input(prompt='', float=True): float(bool): If set to `True`, prompt and input will float to the bottom of the screen when `term.term_mode` is enabled. """ - return readline(-1, prompt, float) + return readline(-1, prompt, float).rstrip(os.linesep.encode()) def str_input(prompt='', float=True): r"""str_input(prompt='', float=True) @@ -445,7 +446,7 @@ def str_input(prompt='', float=True): float(bool): If set to `True`, prompt and input will float to the bottom of the screen when `term.term_mode` is enabled. """ - return readline(-1, prompt, float).decode() + return readline(-1, prompt, float).decode().rstrip(os.linesep) def eval_input(prompt='', float=True): """eval_input(prompt='', float=True) @@ -471,7 +472,7 @@ def eval_input(prompt='', float=True): Favorite object? 20 """ from pwnlib.util import safeeval - return safeeval.const(readline(-1, prompt, float)) + return safeeval.const(readline(-1, prompt, float).rstrip(os.linesep.encode())) def init(): global safeeval diff --git a/pwnlib/tubes/tube.py b/pwnlib/tubes/tube.py index 0e5e9dab1..3d1eae975 100644 --- a/pwnlib/tubes/tube.py +++ b/pwnlib/tubes/tube.py @@ -900,8 +900,8 @@ def recv_thread(): while not go.isSet(): if term.term_mode: data = term.readline.readline(prompt = prompt, float = True) - if data: - data += self.newline + if data.endswith(b'\n') and self.newline != b'\n': + data = data[:-1] + self.newline else: stdin = getattr(sys.stdin, 'buffer', sys.stdin) data = stdin.read(1) From adb0631ee40fca99941e87f5ebc42e6e547f97ee Mon Sep 17 00:00:00 2001 From: Peace-Maker Date: Wed, 14 Feb 2024 23:31:41 +0100 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f835c23f..977389640 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,6 +90,11 @@ The table below shows which release corresponds to each branch, and what date th [2257]: https://github.com/Gallopsled/pwntools/pull/2257 [2225]: https://github.com/Gallopsled/pwntools/pull/2225 +## 4.11.2 +- [#2349][2349] Fix term.readline omitting a trailing \n + +[2349]: https://github.com/Gallopsled/pwntools/pull/2349 + ## 4.11.1 (`stable`) - [#2271][2271] FIX: Generated shebang with path to python invalid if path contains spaces