1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! Hidden (no-echo) line read for password/API-key prompts (UX-18).
//!
//! supercode's `login` prompt used to echo the pasted key straight back to
//! the terminal ("input hidden is not supported, paste carefully") — a real
//! shoulder-surfing / terminal-scrollback leak. This masks the input on a
//! real TTY by disabling `ECHO` via `termios` for the duration of the read
//! (keeping `ECHONL` so pressing Enter still moves the cursor down, exactly
//! like `sudo`/`ssh-keygen`/`git credential` prompts), and restores the
//! original terminal mode afterward via an RAII guard (see [`TermiosGuard`])
//! — so restoration happens on every exit path, not just the one explicit
//! call site a hand-written restore would cover.
//!
//! When stdin is not a TTY (piped input, e.g. `--api-key-stdin`), there is
//! no echo to suppress, so this falls back to a plain `read_line`.
//!
//! Known limitation (UX-18 ticket #2, documented not fixed): Ctrl-C during
//! the read. `ISIG` is deliberately left untouched (so `Ctrl-C` still works
//! at all), which means SIGINT invokes the default handler and kills the
//! process immediately — `Drop` does not run on death-by-signal, so
//! [`TermiosGuard`] cannot restore `ECHO` in that case, and the shell is
//! left with echo off until the user runs `stty sane`/`reset`. Fixing this
//! properly needs a `SIGINT` handler (the way `sudo`/`ssh` do it), which is
//! a materially bigger change that would also interact with UX-29's own
//! SIGINT handling — deliberately out of scope here. See `UX-18.md` →
//! "Known limitations".
//!
//! **Windows (UX-12).** Ported, not degraded: [`ConsoleGuard`] does the same
//! job as [`TermiosGuard`] via the Console API (`GetConsoleMode`/
//! `SetConsoleMode` clearing `ENABLE_ECHO_INPUT`, restored on drop). One
//! genuine behavior difference, called out here rather than left silent:
//! unlike Unix's `ECHONL` (which keeps the terminal driver echoing the
//! newline produced by Enter even with `ECHO` off), Windows has no
//! equivalent bit — suppressing `ENABLE_ECHO_INPUT` also suppresses the
//! Enter keypress's own line break, which would otherwise leave the cursor
//! sitting mid-line after the read. [`read_hidden_line_windows`] compensates
//! by writing the newline itself once the read completes, so the visible
//! result (masked input, cursor still lands on a fresh line after Enter) is
//! the same. `ISIG`'s Windows analog, `ENABLE_PROCESSED_INPUT`, is left
//! untouched for the same reason `ISIG` is on Unix: Ctrl-C must keep working.
use ;
use RawFd;
/// RAII guard that restores a terminal's original `termios` settings when
/// dropped. Constructed only after `ECHO` has actually been disabled, so its
/// existence is itself a witness that the terminal needs restoring.
///
/// Because restoration lives in `Drop`, it fires on *every* exit path out of
/// the scope that holds the guard — a normal return, an `Err`-propagating
/// `?`, an early `return`, or a panic unwind — without the caller having to
/// remember to call a matching "restore" function on each one.
/// Whether a hidden-input attempt actually ended up hiding the input.
/// Reported once, before the blocking read starts, so a caller printing a
/// prompt banner can word it honestly instead of unconditionally claiming
/// "input is hidden".
/// Read one line from stdin with terminal echo disabled when possible,
/// reporting the outcome via `on_status` — called exactly once, before the
/// blocking read begins — so a caller can print an accurate prompt first.
/// Returns the line with leading/trailing whitespace trimmed.
/// RAII guard that restores a console's original mode when dropped — the
/// Windows counterpart to [`TermiosGuard`]. See the module doc's "Windows"
/// section for why this ports cleanly (Console API mirrors `termios`
/// closely enough for this one bit) rather than needing to degrade.
/// Read one line from stdin with terminal echo disabled when possible.
/// Convenience wrapper over [`read_hidden_line_reporting`] for callers (like
/// `--api-key-stdin`) that don't print a status-dependent banner and so
/// don't need to know whether hiding actually happened.
// No unit tests here: the two that previously lived in this module didn't
// exercise production code (one re-implemented `str::trim` inline and
// claimed — incorrectly — to call `read_plain_line`; the other rebuilt the
// `ECHO`/`ECHONL` bit arithmetic inline instead of calling this module).
// The behavior they gestured at is covered for real by:
// - `crates/cli/tests/login_cli.rs`'s 4 integration tests, which spawn the
// actual built binary and exercise the true non-TTY `read_plain_line`
// path end-to-end (`--api-key-stdin`, reachability ping, save behavior).
// - A real-PTY manual check (see `UX-18.md`) that types a secret into an
// actual `openpty()` master/slave pair and asserts it never appears in
// the captured terminal bytes, then asserts the terminal's echo setting
// is restored afterward — the one behavior (echo hidden, then restored)
// that genuinely needs a real TTY and can't be faked with a pipe.
// A safe in-process unit test of the TTY branch isn't feasible: it would
// require repointing fd 0 at a pty for the duration of the test, which is
// process-global state shared with the rest of the (multi-threaded) test
// binary and every other test running concurrently in it.