Skip to content
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

[Bug report] superuser detection brittleness #166

Open
2 tasks done
crgr opened this issue Feb 25, 2025 · 5 comments
Open
2 tasks done

[Bug report] superuser detection brittleness #166

crgr opened this issue Feb 25, 2025 · 5 comments

Comments

@crgr
Copy link

crgr commented Feb 25, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Update the issue title

  • I have updated the title

Expected Behavior

zsh-abbr loads normally

Actual Behavior

zsh-abbr fails silently

Steps To Reproduce

running exec zsh -xv reveals

  • zsh:86> [[ '' == \ # ]]
    zsh:86: parse error near `#'

line 86 reads:

86 if [[ ${(%):-%#} == '#' ]]; then

trying to escape the # character with a backslash \ works in some contexts in Zsh, however within the [[ ... ]] conditional expression and especially in combination with the parameter expansion flags, is causing a parse error. Zsh is misinterpreting # in this context.

Environment

~ # abbr profile
zsh-abbr version 6.2.1
zsh 5.9 (x86_64-debian-linux-gnu)
OSTYPE linux-gnu

Installation method

Manual

Installation method details

~ # echo $ABBR_SOURCE_PATH
/opt/zsh-abbr

Anything else?

replacing line 86 with

86 if [[ "%#" == '#' ]]; then

makes it work again

@olets
Copy link
Owner

olets commented Feb 25, 2025

Thanks for the report, and for looking through the -xv output.

I can't replicate the error: starting from a non-privileged user, I get

% exec zsh -xv
# snip
+/[snip]/zsh-abbr.zsh:86> [[ % == \# ]]
# no "parse error"
# snip

And the suggested change breaks support for distinct tempdirs for privileged and unprivileged users.

  • With 6.2.1 unchanged, I get

    % echo ${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr}%/}/
    # a path
    % echo ${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr-privileged-users}%/}/
    # a different path
    % echo $_abbr_tmpdir
    # same as the first path
    % sudo zsh
    % echo ${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr}%/}/
    # a path
    % echo ${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr-privileged-users}%/}/
    # a different path
    % echo $_abbr_tmpdir
    # same as the second path
  • With the suggested change, I get

    % echo ${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr}%/}/
    # a path
    % echo ${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr-privileged-users}%/}/
    # a different path
    % echo $_abbr_tmpdir
    # same as the first path
    % sudo zsh
    % echo ${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr}%/}/
    # a path
    % echo ${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr-privileged-users}%/}/
    # a different path
    % echo $_abbr_tmpdir
    # same as the FIRST path ⚠️ BAD

//

This makes me realize it'd be good to add a config var for the privileged user tmpdir

- ${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr-privileged-users}%/}/
+ ${${${ABBR_TMPDIR_PRIVILEGED_USERS:-ABBR_TMPDIR}:-${${TMPDIR:-/tmp}%/}/zsh-abbr-privileged-users}%/}/

//

This will take some more investigation.

  • What happens if you do

    % [[ ${(%):-%#} == '#' ]] && { echo y } || echo n
    % sudo zsh
    % [[ ${(%):-%#} == '#' ]] && { echo y } || echo n

    I get n and then y. Do you get n and then an error?

  • Tell me more about "zsh-abbr fails silently". What tipped you off that something was going wrong?

@olets olets added this to zsh-abbr Feb 25, 2025
@olets olets moved this to In Progress in zsh-abbr Feb 25, 2025
@crgr
Copy link
Author

crgr commented Feb 26, 2025

Thank you for the swift reply and for the wonderful piece of code that zsh-abbr is.

I got an n and then the y too, no errors.

What tipped me off that something went wrong was the red zsh prompt right after login. I have a very minimalistic .zshrc:

PS1='%F{blue}%~ %(?.%F{green}.%F{red})%#%f '
source "/opt/zsh-abbr/zsh-abbr.zsh"

I immediately echo $? and it was 1.

@olets
Copy link
Owner

olets commented Feb 27, 2025

I've pushed up a fix attempt in the new branch issues/166. If you have a chance to try it out, I'd be interested to hear the result!

@olets olets changed the title [Bug report] superuser detection in line 86 is broken [Bug report] superuser detection brittleness Feb 27, 2025
@crgr
Copy link
Author

crgr commented Feb 27, 2025

running issues/166 as root is fine but trying to run it as a non-privileged user is problematic:

_job_queue:push:add_job:7: permission denied: /tmp/zsh-job-queue/zsh-abbr/1740627352.4137823582--RV2hCM8cECJtrbGWfwKjxDTG7BAQoCWPk8fL
_job_queue:push:add_job:8: permission denied: /tmp/zsh-job-queue/zsh-abbr/1740627352.4137823582--RV2hCM8cECJtrbGWfwKjxDTG7BAQoCWPk8fL
_job_queue:push:wait_turn:6: bad math expression: operand expected at end of string
_abbr_load_user_abbreviations:load:42: permission denied: /tmp/zsh-abbr/regular-user-abbreviations
_abbr_load_user_abbreviations:load:43: permission denied: /tmp/zsh-abbr/global-user-abbreviations

@olets
Copy link
Owner

olets commented Feb 28, 2025

Thanks!

Bummer.

Might be a week and a half minimum before I have zsh-abbr time again. But we'll figure this out eventually…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

No branches or pull requests

2 participants