Fix syntax error in alias completion when alias contains a single quote - #2407
Open
Saadanjum0 wants to merge 1 commit into
Open
Saadanjum0 wants to merge 1 commit into
Saadanjum0 wants to merge 1 commit into
Conversation
aliases.completion.bash builds a wrapper completion function for each alias by splicing the alias' command and arguments directly into the generated source as unquoted text (e.g. prec_word='$alias_cmd $alias_args' and the COMP_LINE replacement). If the aliased command contains a single quote -- for example `alias foo="echo it's broken"` -- the generated file ends up with an odd number of unescaped quotes, and sourcing it fails with "unexpected EOF while looking for matching `''" (or a similar syntax error), breaking the user's whole shell startup (see Bash-it#2254). Run alias_cmd and alias_args through printf %q before embedding them, the same way the existing code already does for alias_arg_words, so arbitrary characters in the aliased command/arguments can't corrupt the generated wrapper. Added a regression test with an alias whose arguments contain a single quote.
seefood
approved these changes
Oct 2, 2026
seefood
left a comment
Contributor
There was a problem hiding this comment.
excellent catch! LGTM.
@akinomyoga want to have a look at well?
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
aliases.completion.bashbuilds a wrapper completion function for each alias by splicing the alias' command and arguments directly into the generated source as unquoted text, e.g.:If the aliased command contains a single quote, this produces invalid (or in some cases silently wrong) generated code. For example:
generates a snippet with an odd number of unescaped quotes, so sourcing it fails with something like:
which breaks the user's whole shell startup. This matches #2254, where a maintainer-adjacent contributor traced the problem to this same unquoted interpolation and confirmed it's still present on master.
Fix
Run
alias_cmdandalias_argsthroughprintf %qbefore embedding them in the generated wrapper, the same way the existing code already does foralias_arg_words. This keeps the generated source valid regardless of what characters the aliased command/arguments contain.Notes
COMP_LINEused to be rebuilt by splicing in the raw alias text, so any$VARin the alias got expanded (e.g.alias g2='cap -x "$HOME"'would show the expanded path inCOMP_LINE); after this fixCOMP_LINEkeeps the literal, unexpanded alias text instead, which is arguably more correct but is a real behavior change worth flagging.while read lineloop overalias -poutput doesn't use-r, so'\''sequences from some alias forms still get mangled into'''; completion still loads without crashing, but the resulting words aren't always exactly right. Could be a follow-up.Testing
Added a regression test (
test/completion/aliases.completion.bats) with an alias whose arguments contain a single quote, registering a completion trigger for the aliased command so the test actually exercises the vulnerable code path. Confirmed it fails with a syntax error on the old code (not necessarily the exact EOF message quoted above -- the specific error can vary) and passes with the fix. Ran the fullcompletionandaliasestest suites locally (63 tests, all passing).