pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matthew Stickney <mtstickney@gmail.com>
To: pgsql-hackers@lists.postgresql.org
Subject: [PATCH] (Windows) psql echoes password when reading from pipe
Date: Tue, 22 May 2018 00:27:48 -0400
Message-ID: <502a1fff-862b-da52-1031-f68df6ed5a2d@gmail.com> (raw)

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

Attachments:

0001-Disable-input-echo-on-the-console-not-stdin.patch (text/plain, 1.5K) download From 27a0f9b2036680564ef6dfa54f3961af221cbc50 Mon Sep 17 00:00:00 2001 From: Matthew Stickney <mtstickney@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

view thread (9+ messages)  latest in thread

Message-ID: <502a1fff-862b-da52-1031-f68df6ed5a2d@gmail.com>
Permalink:  ../502a1fff-862b-da52-1031-f68df6ed5a2d@gmail.com/
Also on:    postgresql.org/message-id/502a1fff-862b-da52-1031-f68df6ed5a2d@gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: mtstickney@gmail.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: [PATCH] (Windows) psql echoes password when reading from pipe
  In-Reply-To: <502a1fff-862b-da52-1031-f68df6ed5a2d@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox