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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ public function write(CompletionSuggestions $suggestions, OutputInterface $outpu
}
$output->write(implode(' ', $options));

$output->writeln(implode(' ', $suggestions->getValueSuggestions()));
$output->writeln($this->normalizeSuggestions($suggestions->getValueSuggestions()));
}

/**
* Escapes special chars (e.g. backslash or space) and puts quotes around
* the suggestions whenever escaping was needed.
*/
private function normalizeSuggestions(array $suggestions): string
{
$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

$includesUnsafeChars = $includesUnsafeChars || $newValue !== $value;

return $newValue;
}, $suggestions);

return $includesUnsafeChars ? "\\'".implode("\\' \\'", $suggestions)."\\'" : implode(' ', $suggestions);
}
}
4 changes: 3 additions & 1 deletion src/Symfony/Component/Console/Resources/completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ _sf_{{ COMMAND_NAME }}() {

local sfcomplete
if sfcomplete=$(${completecmd[@]} 2>&1); then
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

__ltrim_colon_completions "$cur"
else
if [[ "$sfcomplete" != *"Command \"_complete\" is not defined."* ]]; then
Expand Down