gh-140006: Harden fish prompt against shadowed builtins#150936
Open
gaborbernat wants to merge 1 commit into
Open
gh-140006: Harden fish prompt against shadowed builtins#150936gaborbernat wants to merge 1 commit into
gaborbernat wants to merge 1 commit into
Conversation
A user function that shadows the `.`/`source` builtin hijacks the activate.fish prompt. fish resolves functions ahead of builtins, so the `echo "exit $status" | .` line that restores the exit status pipes into the user function instead of sourcing. Dot-style directory navigators redefine `.`, which made the prompt list the directory on every command and dropped the exit status handed to the original prompt. Route every builtin the prompt path uses (`source`, `echo`, `printf`, `set_color`, `functions`) through `builtin` so no user function can intercept them. These have all been fish builtins with a stable interface since fish 2.0.0, the version the script already required through `functions --copy`, so the minimum supported fish version does not change.
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.
The
activate.fishprompt override restores the previous command's exit status withecho "exit $old_status" | .. fish resolves a function ahead of a builtin, so a userfunction .(orfunction source) shadows thesourcebuiltin even inside a pipe. The override then pipesexit $old_statusinto that function instead of sourcing. A dot-style directory navigator that lists the directory turns this into a directory listing on every prompt, and the exit status handed to the original prompt is lost.This routes every builtin the prompt path uses through
builtin:source,echo,printf,set_color, and thefunctionscalls indeactivateand activation.builtinforces builtin resolution, so no user function can intercept them.Compatibility
Every command goes through a builtin present with a stable interface since fish 2.0.0 (May 2013):
printfbecame a builtin in fish 2.0.0 (fish issue Only trigger AppVeyor on code or config changes #611, commitfa090f2).builtin,functions(-c/-e/-q),set_color(hex +normal),echo, andsource -(stdin) all exist in fish 2.0.0 with the interface used here.The script already required fish 2.0.0 through
functions --copy, so the minimum supported fish version does not change. No version check is added.Testing
test_venv.BasicTest.test_fish_activate_shadowed_builtinscreates a venv, shadows./source/echo/printf/set_colorwith functions, renders the prompt, and asserts the exit status is restored and no shadowed function ran. It fails against the current script and passes with this change. Verified on fish 4.1.2 (the reported version) and 4.7.1.