This is my first time submitting a patch here; apologies in advance if I flub the process.

On windows, if you pipe data to psql, the password prompt correctly reads from and writes to the console, but the password text is echoed to the console. This is because echoing is disabled on the handle for stdin, but as part of a pipeline stdin doesn't refer to the console. I've attached a patch that gets a handle to the console's input buffer by opening CONIN$ instead, which corrects the problem.

I think the change is straightforward enough to apply directly, but there's another concern that might bear discussion: SetConsoleMode can fail, and when it does prompt input will be echoed (i.e. it's fail-open). Is it worth check for and reporting for an error there? Given that this is meant to be used interactively, I think the risk of someone not noticing the echo is low.

-Matt Stickney

From 27a0f9b2036680564ef6dfa54f3961af221cbc50 Mon Sep 17 00:00:00 2001
From: Matthew Stickney <mtstick...@gmail.com>
Date: Mon, 21 May 2018 23:24:34 -0400
Subject: [PATCH] Disable input echo on the console, not stdin.

When data is piped to psql, a handle to stdin will no longer be a handle
to the console; SetConsoleMode will fail, and prompt input will be echoed
to the screen.

Instead, use CreateFile to get a handle to the console's input buffer by
opening CONIN$, and set the console mode there. Prompt input is already
being read from CONIN$, so now prompt-related I/O modifications alter the
same buffer.
the same buffer.
consistently.
---
 src/port/sprompt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/port/sprompt.c b/src/port/sprompt.c
index 70dfa69d7b..f57b49bd3a 100644
--- a/src/port/sprompt.c
+++ b/src/port/sprompt.c
@@ -112,7 +112,7 @@ simple_prompt(const char *prompt, char *destination, size_t 
destlen, bool echo)
        if (!echo)
        {
                /* get a new handle to turn echo off */
-               t = GetStdHandle(STD_INPUT_HANDLE);
+               t = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, 
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
 
                /* save the old configuration first */
                GetConsoleMode(t, &t_orig);
@@ -164,6 +164,7 @@ simple_prompt(const char *prompt, char *destination, size_t 
destlen, bool echo)
        {
                /* reset to the original console mode */
                SetConsoleMode(t, t_orig);
+               CloseHandle(t);
                fputs("\n", termout);
                fflush(termout);
        }
-- 
2.16.2.windows.1

Reply via email to