Skip to content

Commit

Permalink
fix(ext/node): implement process.geteuid (#21151)
Browse files Browse the repository at this point in the history
Fixes #21097
  • Loading branch information
littledivy authored Nov 10, 2023
1 parent b78c713 commit df14835
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cli/tests/unit_node/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,14 @@ Deno.test("process.getuid", () => {
}
});

Deno.test("process.geteuid", () => {
if (Deno.build.os === "windows") {
assertEquals(process.geteuid, undefined);
} else {
assert(typeof process.geteuid?.() === "number");
}
});

Deno.test({
name: "process.exit",
async fn() {
Expand Down
1 change: 1 addition & 0 deletions ext/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ deno_core::extension!(deno_node,
ops::os::op_node_os_get_priority<P>,
ops::os::op_node_os_set_priority<P>,
ops::os::op_node_os_username<P>,
ops::os::op_geteuid<P>,
op_node_build_os,
op_is_any_arraybuffer,
op_node_is_promise_rejected,
Expand Down
19 changes: 19 additions & 0 deletions ext/node/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ where
Ok(deno_whoami::username())
}

#[op2(fast)]
pub fn op_geteuid<P>(state: &mut OpState) -> Result<u32, AnyError>
where
P: NodePermissions + 'static,
{
{
let permissions = state.borrow_mut::<P>();
permissions.check_sys("geteuid", "node:os.geteuid()")?;
}

#[cfg(windows)]
let euid = 0;
#[cfg(unix)]
// SAFETY: Call to libc geteuid.
let euid = unsafe { libc::geteuid() };

Ok(euid)
}

#[cfg(unix)]
mod priority {
use super::*;
Expand Down
7 changes: 7 additions & 0 deletions ext/node/polyfills/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const internals = globalThis.__bootstrap.internals;
const { core } = globalThis.__bootstrap;
const { ops } = core;
import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts";
import { EventEmitter } from "node:events";
import Module from "node:module";
Expand Down Expand Up @@ -652,6 +653,11 @@ class Process extends EventEmitter {
return Deno.uid()!;
}

/** This method is removed on Windows */
geteuid?(): number {
return ops.op_geteuid();
}

// TODO(kt3k): Implement this when we added -e option to node compat mode
_eval: string | undefined = undefined;

Expand Down Expand Up @@ -693,6 +699,7 @@ class Process extends EventEmitter {
if (isWindows) {
delete Process.prototype.getgid;
delete Process.prototype.getuid;
delete Process.prototype.geteuid;
}

/** https://nodejs.org/api/process.html#process_process */
Expand Down

0 comments on commit df14835

Please sign in to comment.