Skip to content

[Console] Escape completion suggestions with special chars #43664

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

Closed

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Oct 22, 2021

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

This fixes shell completion whenever a special char (backslash and whitespace at the moment) are present. In these cases, all suggestions are put in quotes (to avoid double-double escaping) and the special chars are escaped.

This bug was found in #43598

$includesUnsafeChars = false;
$suggestions = array_map(function ($value) use (&$includesUnsafeChars) {
$newValue = str_replace('\\', '\\\\', $value);
$newValue = str_replace(' ', '\ ', $value);
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this do the replacement in $newValue ? Otherwise, the previous line is useless code.

Copy link
Member

Choose a reason for hiding this comment

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

btw, you could do it as a single str_replace using arrays as arguments

COMPREPLY=($(compgen -W "$sfcomplete" -- "$cur"))
# $sfcomplete is escaped already, escape the input as well
local escapedcur=${cur//\\/\\\\}
COMPREPLY=($(compgen -W "${sfcomplete}" -- "${escapedcur//\'/\\\'}"))
Copy link
Member

@GromNaN GromNaN Oct 22, 2021

Choose a reason for hiding this comment

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

I was working on this issue and found that printf "%q" "$cur" escapes for using as shell input.

%q causes printf to output the corresponding argument in a format that can be reused as shell input.

My version: #43665

@wouterj
Copy link
Member Author

wouterj commented Oct 28, 2021

Closing in favor of #43665

@wouterj wouterj closed this Oct 28, 2021
@wouterj wouterj deleted the bugfix/shell-completion-backslash branch October 28, 2021 09:57
fabpot added a commit that referenced this pull request Oct 30, 2021
This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[Console] Fix backslash escaping in bash completion

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #43664
| License       | MIT
| Doc PR        | -

Fully tested code with #43598

- `printf` options `%b` and `%q` are handy to escape string using in shell input.
- Backslashes must to be escaped (doubled) in normal input (`bin/console debug:form App\\Form\\CommentType`). Otherwise they are dropped when PHP receive the argument (`App\Form\CommentType` become `AppFormCommentType` in `$SERVER['argv']`). This is not necessary for quoted arguments (`bin/console debug:form "App\Form\CommentType"` is OK). In the context of a command stored in a variable, like in the bash script, escaping is not interpreted: we must replace `\\` by `\` using `printf '%b'`.
- Completion does not process escaping. If `App\\Form\\` is typed, the suggestions must contain a `\\` to match. Since these values are provided to shell, double backslash must be escaped: `\` becomes `\\\\` in suggestion output.
- I choose to detect when quotes are typed to allow them in completion and add quotes to suggestions.
- Suggestion with spaces are still not properly working.

https://user-images.githubusercontent.com/400034/138536102-e282ee75-56c8-414b-8b16-7c3c290276d7.mov

Commits
-------

e9e0c07 [Console] Fix backslash escaping in bash completion
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.

4 participants