Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strip newline and carriage return when password is read from stdin #1191

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nico1510
Copy link

After reading https://www.fermyon.com/blog/spin-v09#:~:text=Running%20Spin%20Applications%20From%20Registries I tried to login to docker hub but wasn't successful. I traced it down to a newline character which is apparently added when reading from stdin with rust (see https://blog.v-gar.de/2019/04/rust-remove-trailing-newline-after-input).

This command failed:
echo $DOCKER_PAT | spin registry login registry-1.docker.io --username $user --password-stdin

I also had to manually create the registry-auth.json where spin tries to store the saved credentials but that's just a fyi and is not fixed in this PR.

@nico1510 nico1510 force-pushed the fix-newline-char-from-stdin-input branch from 526b32d to b8fd4f5 Compare February 22, 2023 20:11
Copy link
Contributor

@itowlson itowlson left a comment

Choose a reason for hiding this comment

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

Thanks for catching this and for taking the time to fix it. I've asked @radu-matei to review since he knows this code best, but apart from one (non-critical) question it looks fine to me.

@@ -143,6 +143,9 @@ impl Login {
let mut buf = String::new();
let mut stdin = std::io::stdin().lock();
stdin.read_to_string(&mut buf)?;
// trim trailing newlines and carriage returns (see https://blog.v-gar.de/2019/04/rust-remove-trailing-newline-after-input)
let len = buf.trim_end_matches(&['\r', '\n'][..]).len();
buf.truncate(len);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to use this length to truncate the original buf rather than return buf.trim_end_matches(...).to_owned()? That feels more readable to me and I'm not worried if it incurs an extra allocation. But I'm not familiar with this code and don't know the nuances of the string functions - I'll defer to you and Radu on this!

Copy link
Author

Choose a reason for hiding this comment

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

I think the only justification for the length variant would be to "fix" the faulty buf by mutating it. But yeah I agree that yours is much more readable. Pushed a change 👍

@nico1510 nico1510 force-pushed the fix-newline-char-from-stdin-input branch 2 times, most recently from 6581c45 to 4537f2f Compare February 22, 2023 20:45
@radu-matei
Copy link
Member

Hi, @nico1510!
Thanks for the fix!

I haven't been able to reproduce this locally, so I assume there is something OS / shell specific happening here?

Would you mind sharing your environment, please?

Thanks, and thank you for the fix, really appreciate taking the time!

@nico1510 nico1510 force-pushed the fix-newline-char-from-stdin-input branch from 4537f2f to 4b579bc Compare February 22, 2023 20:51
@nico1510
Copy link
Author

Would you mind sharing your environment, please?

Yep sure I'm on:
ProductName: macOS
ProductVersion: 13.2.1
BuildVersion: 22D68

zsh 5.8.1 (x86_64-apple-darwin22.0)

@nico1510 nico1510 force-pushed the fix-newline-char-from-stdin-input branch from 4b579bc to 4a36b24 Compare February 22, 2023 20:59
@itowlson
Copy link
Contributor

itowlson commented Mar 7, 2023

@radu-matei How do you want to proceed with this one? Is there any risk or harm in merging it even though it might be environment-specific and we can't reproduce the issue? Or do you want to try and qualify the issue further?

And @nico1510 my apologies for the long silence!

@itowlson
Copy link
Contributor

@radu-matei bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants