Skip to content

tool_getpass: keep the last character when there is no newline - #23525

Closed
Marti2203 wants to merge 1 commit into
curl:masterfrom
Marti2203:getpass-no-newline
Closed

Marti2203 wants to merge 1 commit into
curl:masterfrom
Marti2203:getpass-no-newline

Conversation

@Marti2203

@Marti2203 Marti2203 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Without a terminal, curl reads the password from stdin, and getpass_r() always replaces the last byte read with the terminator, assuming it is the newline. So a password piped without a newline loses its last character:

$ printf pass | setsid curl -u alice http://localhost:8000/

sends alice:pas (printf 'pass\n' sends alice:pass).

This only removes a trailing newline, and reads at most buflen - 1 bytes so there is always room for the terminator (a full buffer also lost its last byte before).

There is no dedicated test: a stdin-only test is fragile, because curl reads the password from /dev/tty whenever it can open one.

This issue was found by an LLM-Enhanced Static Analysis Engine and verified manually.

When curl cannot open the terminal it reads the password from stdin,
and getpass_r() always replaced the last byte read with the null
terminator, assuming it was the newline from pressing enter. A password
piped without a trailing newline, as in 'printf pass | curl -u alice',
lost its last character.

Only remove a trailing newline, and read at most buflen - 1 bytes to
leave room for the terminator.
@dfandrich

dfandrich commented Oct 21, 2026 via email

Copy link
Copy Markdown
Contributor

@Marti2203

Copy link
Copy Markdown
Contributor Author

Thanks, that works. I added test 1696 (-u alice with secret on stdin, nonewline="yes"). It fails without the fix (alice:secre) and passes with it.

One catch: if the test suite runs with a controlling terminal, curl opens /dev/tty and reads the password from there instead of stdin, so the test would get an empty password, or wait for input. I added a precheck that skips the test when /dev/tty can be opened. It runs in CI and other runs without a terminal.

@github-actions github-actions Bot added the tests label Oct 21, 2026
Comment thread tests/data/test1696 Outdated
# only when curl cannot open the terminal
<precheck>
%PERL -e 'if(open(T, "/dev/tty")) {print "Test requires no controlling terminal"} elsif($^O =~ /MSWin32|msys/) {print "Test requires a POSIX getpass_r"} elsif(open(C, "../lib/curl_config.h")) {print "Test requires the getpass_r in src" if grep(/define HAVE_GETPASS_R 1/, readline(C))}'
</precheck>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like a maintenance headache and fragile in many ways.

I suggest replacing with something solid and universal. If not possible,
being an obscure issue, I think no dedicatedl test is also fine.

@Marti2203

Copy link
Copy Markdown
Contributor Author

Agreed, I dropped test 1696; the PR is now just the fix in src/tool_getpass.c.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused change corrects password truncation while safely terminating the buffer for the existing caller.

No open issues 🎉

What changed in this PR

Fixes password input handling so stdin input without a trailing newline retains its final character.

Changes:

  • Removes only a trailing newline.
  • Reserves space for the terminator and safely handles empty input and read errors.
File Description
src/​tool_getpass.c Preserves password bytes and ensures null termination.

Review effort: Balanced

@bagder bagder closed this in bcf0ec5 Oct 22, 2026
@bagder

bagder commented Oct 22, 2026

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants