Navigation Menu

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

[Console] Default hidden question to 1 attempt for non-tty session #36590

Merged
merged 1 commit into from May 4, 2020

Conversation

ostrolucky
Copy link
Contributor

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #36565
License MIT
Doc PR

Problem 1

validateAttempts() method repeats validation forever by default, until exception extending RuntimeException isn't thrown. This currently happens disregarding if user is in tty session where they can actually type input, or non-tty session. This presents a problem when user code throws custom exceptions for hidden questions -> loop doesn't stop. As far as I can tell this issue is in all Symfony versions, but it was uncovered only after we stopped marking interactive flag to false automatically ourselves. Actually, all 3 problems were already existing problems, just hidden until now.

Problem 2

Infinite loop problem is related to hidden questions, but this one isn't. If validation fails, another attempt to read & validate happens. This means user will get two prompts: 2x same question with 2 different error messages. One error message coming from validator, second error message about inability to read input (because this loop repeats until this kind of error happens, so last output will always be this error). As an example, output in practice would look like following


 What do you want to do:
 > 

                                                                                
 [ERROR] Action must not be empty.                                              
                                                                                

 What do you want to do:
 > 
            
  Aborted.  
            

So even if loop stops, output is more than expected.

Problem 3

This is purely cosmetic issue, but currently user gets stty: stdin isn't a terminal printed additionally when question helper tries to ask a hidden question without having tty. I have fixed this in same fashion as was already done for getShell() method.

More details

Well root of the first problem is that \Symfony\Component\Console\Helper\QuestionHelper::getHiddenResponse is inconsistent. In some cases it does throw MissingInputException (which extends RuntimeException), in others doesn't. This is because in others, shell_exec is used, which won't return false even in non-tty sessions. Initially I attempted to fix this and make them consistent by checking for empty result + isTty call, but during my testing I found that at least last, bash -c method returns \n as output both when passing empty input and when passing newline as input. This means we cannot differentiate with this technique when input is really empty, or at least I can't currently tell how, maybe someone does. I had also idea to use proc_open and check if STDERR cotains message about stdin not being a terminal, but I realized these functions might not be available. In future we should modernize this method to use less hacky techniques. Other solutions, eg. Inquirer.js or hoa/console have much more elegant solutions. Anyway, since I encountered this issue and additionally this doesn't solve Problem 2, I stopped trying to fix this on this level.

Alternative solution

Alternative solution to problem 1 and 3 would be to fallback to default in case of hidden questions when tty is missing. But this still doesn't solve problem 2 and I can't think about solution right now which would fix problem 2 separately. We also didn't really reach consensus if reading passwords via stdin is desired. I tried this in Inquirer.js and this library does read password from stdin

@chalasr chalasr added this to the 4.4 milestone Apr 27, 2020
@fabpot
Copy link
Member

fabpot commented May 4, 2020

Thank you @ostrolucky.

@fabpot fabpot merged commit 64e5a9d into symfony:4.4 May 4, 2020
This was referenced May 31, 2020
nicolas-grekas added a commit that referenced this pull request Jul 3, 2020
…tions (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[Console] always use stty when possible to ask hidden questions

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36565, replaces #36590
| License       | MIT
| Doc PR        | -

The current code doesn't make much sense: we check `hasSttyAvailable()`, and if the answer is `false`, we still use `stty` directly.

This PR relies on `stream_isatty` and equivalent fallback checks to decide if the password can be hidden or not.

Best reviewed [ignoring whitespaces](https://github.com/symfony/symfony/pull/37469/files?w=1).

Commits
-------

055b605 [Console] always use stty when possible to ask hidden questions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants