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

TTY detection and handling #2

Closed
4 tasks done
str4d opened this issue Oct 8, 2019 · 4 comments · Fixed by #85
Closed
4 tasks done

TTY detection and handling #2

str4d opened this issue Oct 8, 2019 · 4 comments · Fixed by #85

Comments

@str4d
Copy link
Owner

str4d commented Oct 8, 2019

  • Prevent encrypted output from being sent to stdout if it is bound to a TTY.
  • Prevent long or unprintable decrypted output from being sent to stdout if it is bound to a TTY.
  • Require a TTY when using the --passphrase / -p flag.
  • Turn off TTY echo while reading the passphrase (where possible).
str4d added a commit that referenced this issue Oct 13, 2019
- Prevent passphrase from being echoed.
- Confirm passphrase entry when encrypting.
- Require that input files are passed as CLI arguments so they don't
  conflict with passphrase input.

Part of #2.
str4d added a commit that referenced this issue Oct 13, 2019
@str4d
Copy link
Owner Author

str4d commented Jan 7, 2020

age currently opens /dev/tty for passphrase querying. I think we should do the same on Unix, and then continue to require using -i/--input on Windows when using a passphrase.

@str4d
Copy link
Owner Author

str4d commented Jan 7, 2020

Oh, turns out that the console crate used by dialoguer::PasswordInput already opens /dev/tty on Unix by default, and returns an empty string if the terminal is not a TTY. So all we have to do is allow using a pipe input on Unix, and we are done!

@str4d
Copy link
Owner Author

str4d commented Jan 7, 2020

Hmm, turns out it isn't that simple. The expected diff would be:

diff --git a/src/bin/rage/error.rs b/src/bin/rage/error.rs
index c9d8dca..bf5b758 100644
--- a/src/bin/rage/error.rs
+++ b/src/bin/rage/error.rs
@@ -48,6 +48,7 @@ pub(crate) enum DecryptError {
     Io(io::Error),
     MissingIdentities(String),
     MixedIdentityAndPassphrase,
+    #[cfg(not(unix))]
     PassphraseWithoutFileArgument,
     RecipientFlag,
     UnsupportedKey(String, age::keys::UnsupportedKey),
@@ -89,6 +90,7 @@ impl fmt::Display for DecryptError {
             DecryptError::MixedIdentityAndPassphrase => {
                 write!(f, "-i/--identity can't be used with -p/--passphrase")
             }
+            #[cfg(not(unix))]
             DecryptError::PassphraseWithoutFileArgument => write!(
                 f,
                 "File to decrypt must be passed as an argument when using -p/--passphrase"
diff --git a/src/bin/rage/main.rs b/src/bin/rage/main.rs
index 130f9d6..164b097 100644
--- a/src/bin/rage/main.rs
+++ b/src/bin/rage/main.rs
@@ -267,8 +267,13 @@ fn decrypt(opts: AgeOptions) -> Result<(), error::DecryptError> {
             return Err(error::DecryptError::MixedIdentityAndPassphrase);
         }
 
-        if opts.input.is_none() {
-            return Err(error::DecryptError::PassphraseWithoutFileArgument);
+        // The `console` crate used by `dialoguer::PasswordInput` opens `/dev/tty`
+        // directly on Unix, so we don't have any conflict with stdin.
+        #[cfg(not(unix))]
+        {
+            if opts.input.is_none() {
+                return Err(error::DecryptError::PassphraseWithoutFileArgument);
+            }
         }
 
         match read_secret("Type passphrase", None) {

But with this, running cat test.age | cargo run -- -d -p in bash results in no output, and a corrupted terminal.

@str4d
Copy link
Owner Author

str4d commented Mar 15, 2020

Turns out this is caused by a problem somewhere in the console crate. Switching from dialoguer to rpassword fixes the problem.

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 a pull request may close this issue.

1 participant