From 684bc07a5061c9010ebd357eac7a790640984b0a Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Sat, 24 Jun 2023 10:11:20 -0300 Subject: [PATCH 1/2] installShellFiles: ensure shell completion has more than 256 bytes I found an error in the completion script of tmuxp which were outputting an error message instead of a proper script so a sanity check in the installShellCompletion script is helpful to avoid this to happen in the future. After looking at possible ways to detect this, the only valid check I found was based on the generated file size as any completion script I could find had more than 256 bytes. Signed-off-by: Otavio Salvador --- .../setup-hooks/install-shell-files.sh | 19 ++++++++++++++++++- pkgs/test/install-shell-files/default.nix | 11 ++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/pkgs/build-support/setup-hooks/install-shell-files.sh b/pkgs/build-support/setup-hooks/install-shell-files.sh index 194b408b10501..2c5f00fc79bbd 100644 --- a/pkgs/build-support/setup-hooks/install-shell-files.sh +++ b/pkgs/build-support/setup-hooks/install-shell-files.sh @@ -96,7 +96,7 @@ installManPage() { # # If any argument is `--` the remaining arguments will be treated as paths. installShellCompletion() { - local shell='' name='' cmdname='' retval=0 parseArgs=1 arg + local shell='' name='' cmdname='' size_check=256 retval=0 parseArgs=1 arg while { arg=$1; shift; }; do # Parse arguments if (( parseArgs )); then @@ -122,6 +122,17 @@ installShellCompletion() { return 1 } continue;; + --size-check) + size_check=$1 + shift || { + echo 'installShellCompletion: error: --size-check flag expected an argument' >&2 + return 1 + } + continue;; + --size-check=*) + # treat `--size-check=foo` the same as `--size-check foo` + size_check=${arg#--size-check=} + continue;; --cmd=*) # treat `--cmd=foo` the same as `--cmd foo` cmdname=${arg#--cmd=} @@ -219,6 +230,12 @@ installShellCompletion() { else install -Dm644 -T "$arg" "$outPath" fi || return + # Sanity check file size to avoid assuming completion has succeed but it generate an invalid file. + local fileSize=$(stat -c%s "$outPath") + if (( fileSize < size_check )); then + echo "installShellCompletion: error: $outPath is too small ($fileSize only), use '--size-check $fileSize' to override" >&2 + return 1 + fi # Clear the per-path flags name= done diff --git a/pkgs/test/install-shell-files/default.nix b/pkgs/test/install-shell-files/default.nix index aef5acc1de6bf..9ac023d65df80 100644 --- a/pkgs/test/install-shell-files/default.nix +++ b/pkgs/test/install-shell-files/default.nix @@ -55,7 +55,7 @@ recurseIntoAttrs { echo qux > qux.zsh echo quux > quux - installShellCompletion --bash foo bar --zsh baz qux.zsh --fish quux + installShellCompletion --size-check 4 --bash foo bar --zsh baz qux.zsh --fish quux cmp foo $out/share/bash-completion/completions/foo cmp bar $out/share/bash-completion/completions/bar @@ -68,7 +68,7 @@ recurseIntoAttrs { } '' echo foo > foo - installShellCompletion --bash foo + installShellCompletion --size-check 4 --bash foo # assert it didn't go into $out [[ ! -f $out/share/bash-completion/completions/foo ]] @@ -82,7 +82,7 @@ recurseIntoAttrs { echo bar > bar echo baz > baz - installShellCompletion --bash --name foobar.bash foo --zsh --name _foobar bar --fish baz + installShellCompletion --size-check 4 --bash --name foobar.bash foo --zsh --name _foobar bar --fish baz cmp foo $out/share/bash-completion/completions/foobar.bash cmp bar $out/share/zsh/site-functions/_foobar @@ -93,7 +93,7 @@ recurseIntoAttrs { echo bar > bar.zsh echo baz > baz.fish - installShellCompletion foo.bash bar.zsh baz.fish + installShellCompletion --size-check 4 foo.bash bar.zsh baz.fish cmp foo.bash $out/share/bash-completion/completions/foo.bash cmp bar.zsh $out/share/zsh/site-functions/_bar @@ -105,7 +105,7 @@ recurseIntoAttrs { echo baz > baz.fish echo qux > qux.fish - installShellCompletion --cmd foobar --bash foo.bash --zsh bar.zsh --fish baz.fish --name qux qux.fish + installShellCompletion --size-check 4 --cmd foobar --bash foo.bash --zsh bar.zsh --fish baz.fish --name qux qux.fish cmp foo.bash $out/share/bash-completion/completions/foobar.bash cmp bar.zsh $out/share/zsh/site-functions/_foobar @@ -114,6 +114,7 @@ recurseIntoAttrs { ''; install-completion-fifo = runTest "install-completion-fifo" {} '' installShellCompletion \ + --size-check 4 \ --bash --name foo.bash <(echo foo) \ --zsh --name _foo <(echo bar) \ --fish --name foo.fish <(echo baz) From 32c275a517bca3e8e828322499b276bdc41977f9 Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Mon, 17 Jul 2023 09:49:38 -0300 Subject: [PATCH 2/2] terraform: override installShellCompletion check to allow 32 bytes Signed-off-by: Otavio Salvador --- pkgs/applications/networking/cluster/terraform/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/applications/networking/cluster/terraform/default.nix b/pkgs/applications/networking/cluster/terraform/default.nix index fe653013cf4d3..06aac978ed464 100644 --- a/pkgs/applications/networking/cluster/terraform/default.nix +++ b/pkgs/applications/networking/cluster/terraform/default.nix @@ -38,7 +38,7 @@ let postInstall = '' # https://github.com/posener/complete/blob/9a4745ac49b29530e07dc2581745a218b646b7a3/cmd/install/bash.go#L8 - installShellCompletion --bash --name terraform <(echo complete -C terraform terraform) + installShellCompletion --size-check 32 --bash --name terraform <(echo complete -C terraform terraform) ''; preCheck = ''