-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
$includesUnsafeChars = false; | ||
$suggestions = array_map(function ($value) use (&$includesUnsafeChars) { | ||
$newValue = str_replace('\\', '\\\\', $value); | ||
$newValue = str_replace(' ', '\ ', $value); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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//\'/\\\'}")) |
There was a problem hiding this comment.
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
Closing in favor of #43665 |
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
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