Skip to content

[BUG] Install via Git fails when running with sudo (no access to SSH_AUTH_SOCK) #44

Open
@okdana

Description

@okdana

What / Why

(Sorry for any bad assumptions i'm making here, i know very little about NPM or pacote)

When pacote detects that it's running as root, but the directory it's trying to clone into is owned by another user, it runs git with that user's UID. This seems to work as far as making the permissions on the clone consistent, but, when using Git+SSH, the privilege-dropped OpenSSH process is then prevented from accessing the agent auth socket (because it's still owned by root). This causes git to fail.

The obvious work-around (besides not running NPM as root in the first place, which is of course my long-term goal) is to pass -H or -i to sudo, which should avoid the privilege drop in most cases. And, if reconciling the ownerships of the clone directory and auth socket is too irritating, maybe that should be the 'official' solution — i think pip has a similar requirement.

But one big difference between pip and pacote is that pip actually tells you what's wrong and how to fix it — without spend a whole bunch of time troubleshooting it, the pacote issue simply looks like the Git clone is failing for no reason at all.

When

Always, in this configuration/scenario:

  • Ubuntu 20.04 Focal
  • npm 6.14.4
  • pacote 9.5.12
  • npm running as root
  • Clone directory not owned by root (e.g. using sudo without -i or -H)
  • Installing package via Git+SSH
  • SSH agent needed for auth (i.e. OpenSSH can't just fall back to a default key)

Where

Using a private repository in this case, but i assume this can occur any time pacote deals with Git via SSH

How

Current Behavior

git commands fail in the above scenario, with no good explanation as to why

Steps to Reproduce

% mkdir /tmp/pacote-bug && cd /tmp/pacote-bug
% sudo sh -c '
  eval "$(ssh-agent)";
  ssh-add -q /path/to/necessary/key;
  ls -ld -- "$SSH_AUTH_SOCK";
  GIT_SSH_COMMAND="id >&2; ssh -v" npm install "git+ssh://[email protected]/foo/bar.git"
'
Agent pid 1456457
srw------- 1 root root 0 Jun 17 01:50 /tmp/ssh-W9IypxzdTXFs/agent.1456456
npm ERR! code 128
npm ERR! Command failed: git clone --mirror -q ssh://[email protected]/foo/bar.git /home/dana/.npm/_cacache/tmp/git-clone-0b3c15e8/.git
npm ERR! warning: templates not found in /tmp/pacote-git-template-tmp/git-clone-576ad45a
npm ERR! uid=1001(dana) gid=1001(dana) groups=1001(dana)
...
npm ERR! debug1: pubkey_prepare: ssh_get_authentication_socket: Permission denied
...
npm ERR! [email protected]: Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
...

(The clone succeeds if i change cwdOwner() and mkOpts() in git.js so that they don't try to de-escalate)

Expected Behavior

imo, pacote should either:

  • figure out how to perform the clone without breaking the SSH agent, or
  • alert the user that the way they're running it may cause problems, and ideally explain what to do instead

Who

Me!

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions