Skip to content

fix sudo handling and pkgx-under-/root reachability#84

Open
tannevaled wants to merge 8 commits into
pkgxdev:mainfrom
tannevaled:dev
Open

fix sudo handling and pkgx-under-/root reachability#84
tannevaled wants to merge 8 commits into
pkgxdev:mainfrom
tannevaled:dev

Conversation

@tannevaled
Copy link
Copy Markdown

Restore the privilege drop when running as root via sudo (so pkgx caches stay user-owned), but only when the resolved pkgx binary is actually reachable from $SUDO_USER. Falls back to running pkgx as root rather than crashing the inner sudo with "Permission denied" when pkgx lives under /root/.pkgx/ (#68).

  • Restore drop-privilege behaviour for sudo pkgm (fixes the regression flagged by jhheider on Refactor sudo handling for package installation #83).
  • Resolve an alternative pkgx under $SUDO_USER's home / /usr/local when the current path is unreachable to $SUDO_USER.
  • Override HOME so pkgx caches under the invoking user's tree.
  • Stop mutating args so the args[0] lookup at line 341 keeps working.
  • Avoid the Deno.env.get("USER")! non-null assertion crash.
  • Call install_prefix() once (it has filesystem side effects).
  • Keep the visible log surface unchanged: only the pre-existing "installing as root" warning fires by default; the new "pkgx unreachable" diagnostic is gated behind PKGM_DEBUG.

Restore the privilege drop when running as root via sudo (so pkgx caches
stay user-owned), but only when the resolved pkgx binary is actually
reachable from $SUDO_USER. Falls back to running pkgx as root rather
than crashing the inner sudo with "Permission denied" when pkgx lives
under /root/.pkgx/ (pkgxdev#68).

- Restore drop-privilege behaviour for `sudo pkgm` (fixes the
  regression flagged by jhheider on pkgxdev#83).
- Resolve an alternative pkgx under $SUDO_USER's home / /usr/local
  when the current path is unreachable to $SUDO_USER.
- Override HOME so pkgx caches under the invoking user's tree.
- Stop mutating `args` so the args[0] lookup at line 341 keeps working.
- Avoid the `Deno.env.get("USER")!` non-null assertion crash.
- Call install_prefix() once (it has filesystem side effects).
- Keep the visible log surface unchanged: only the pre-existing
  "installing as root" warning fires by default; the new
  "pkgx unreachable" diagnostic is gated behind PKGM_DEBUG.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 11, 2026 17:18
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates pkgm’s sudo/root execution flow when installing into the system prefix (/usr/local) to restore dropping privileges back to the invoking user (so caches remain user-owned), while avoiding failures when the resolved pkgx binary isn’t actually reachable by $SUDO_USER (eg when pkgx lives under root-owned private directories).

Changes:

  • Reworks query_pkgx() command selection so sudo -u $SUDO_USER is only used when running as root via sudo and pkgx is deemed reachable by that user.
  • Overrides HOME (when dropping privileges) to keep pkgx caches under the invoking user’s home.
  • Adds helper functions to discover a reachable pkgx path and to look up a user’s home directory.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkgm.ts Outdated
Comment thread pkgm.ts Outdated
Comment on lines +830 to +835
// Conservative heuristic: private home dirs are typically mode 700, so a
// path under another user's home is unreachable. System paths and the
// user's own home are assumed reachable.
if (p.startsWith("/root/")) return user === "root";
const m = p.match(/^\/home\/([^/]+)\//);
if (m) return m[1] === user;
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 17:25
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread pkgm.ts Outdated
Comment thread pkgm.ts
Comment on lines +809 to +820
if (existsSync(root)) {
let best: { v: SemVer; path: string } | undefined;
for (const entry of Deno.readDirSync(root)) {
if (!entry.isDirectory || !entry.name.startsWith("v")) continue;
try {
const v = new SemVer(entry.name.slice(1));
const path = join(root, entry.name, "bin/pkgx");
if (!existsSync(path)) continue;
if (!best || v.gt(best.v)) best = { v, path };
} catch { /* skip malformed version dir */ }
}
if (best) return best.path;
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 17:39
@tannevaled
Copy link
Copy Markdown
Author

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread pkgm.ts Outdated
Comment on lines +811 to +818
for (const entry of Deno.readDirSync(root)) {
if (!entry.isDirectory || !entry.name.startsWith("v")) continue;
try {
const v = new SemVer(entry.name.slice(1));
const path = join(root, entry.name, "bin/pkgx");
if (!existsSync(path)) continue;
if (!best || v.gt(best.v)) best = { v, path };
} catch { /* skip malformed version dir */ }
Comment thread pkgm.ts Outdated
@tannevaled
Copy link
Copy Markdown
Author

@copilot apply changes based on the comments in this thread

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 18:46
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread pkgm.ts Outdated
@tannevaled
Copy link
Copy Markdown
Author

@copilot apply changes based on the comments in this thread

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 19:30
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.

Comment thread pkgm.ts
Comment on lines +832 to +836
const local = join(home, ".local/bin/pkgx");
if (existsSync(local)) return local;
}
if (existsSync("/usr/local/bin/pkgx")) return "/usr/local/bin/pkgx";
return undefined;
Comment thread pkgm.ts
Comment on lines +839 to +846
function reachable_as(p: string, user: string): boolean {
// Conservative heuristic: private home dirs are typically mode 700, so a
// path under another user's home is unreachable. System paths and the
// user's own home are assumed reachable.
if (p.startsWith("/root/")) return user === "root";
const m = p.match(/^\/home\/([^/]+)\//);
if (m) return m[1] === user;
return true;
Comment thread pkgm.ts Outdated
Comment on lines +843 to +844
if (p.startsWith("/root/")) return user === "root";
const m = p.match(/^\/home\/([^/]+)\//);
Comment thread pkgm.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 20:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread pkgm.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 12, 2026 06:07
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread pkgm.ts Outdated
Comment thread pkgm.ts
Comment on lines +313 to +320
const reachable = pkgx_reachable_as(pkgx, sudoUser);
if (reachable) {
cmd = "/usr/bin/sudo";
cmd_args = ["-u", sudoUser, "--", reachable, ...args];
// Override HOME, or pkgx will cache back under /root/ where sudoUser
// can't reach it on the next invocation.
const home = user_home(sudoUser);
if (home) env.HOME = home;
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 12, 2026 06:16
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants