Skip to content

Commit e78404d

Browse files
committed
Add functions to windows-utils for checking named pipe ownership
1 parent 5ba594d commit e78404d

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

desktop/packages/windows-utils/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ path = "windows-utils-rs/lib.rs"
1919
neon = "1"
2020
windows = { version = "0.59.0", features = ["Win32", "Win32_UI", "Win32_UI_Shell", "Win32_System", "Win32_System_Com", "Win32_Storage_FileSystem"] }
2121
thiserror = { workspace = true }
22+
23+
talpid-windows = { path = "../../../talpid-windows" }

desktop/packages/windows-utils/src/index.cts

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as addon from './load.cjs';
77
// which otherwise by default are `any`.
88
declare module './load.cjs' {
99
function readShortcut(linkPath: string): string | null;
10+
function pipeIsAdminOwned(pipePath: string): boolean;
1011
}
1112

1213
/**
@@ -16,3 +17,11 @@ declare module './load.cjs' {
1617
export function readShortcut(linkPath: string): string | null {
1718
return addon.readShortcut(linkPath);
1819
}
20+
21+
/**
22+
* Return whether a named pipe is owned by the admin or SYSTEM account.
23+
* @param pipePath path to a named pipe.
24+
*/
25+
export function pipeIsAdminOwned(pipePath: string): boolean {
26+
return addon.pipeIsAdminOwned(pipePath);
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::io;
2+
use std::path::Path;
3+
4+
use neon::prelude::{Context, FunctionContext};
5+
use neon::result::JsResult;
6+
use neon::types::{JsString, JsValue, Value};
7+
8+
#[derive(thiserror::Error, Debug)]
9+
enum Error {
10+
/// Failed to open the provided file
11+
#[error("Failed to open named pipe")]
12+
OpenPipe(#[source] io::Error),
13+
14+
/// Failed to check pipe ownership (GetSecurityInfo)
15+
#[error("Failed to check named pipe ownership (GetSecurityInfo failed)")]
16+
CheckPermissions(#[source] io::Error),
17+
}
18+
19+
pub fn pipe_is_admin_owned(mut cx: FunctionContext<'_>) -> JsResult<'_, JsValue> {
20+
let link_path = cx.argument::<JsString>(0)?.value(&mut cx);
21+
22+
match pipe_is_admin_owned_inner(link_path) {
23+
Ok(is_admin_owned) => Ok(cx.boolean(is_admin_owned).as_value(&mut cx)),
24+
Err(err) => cx.throw_error(format!("Failed to get pipe ownership: {err}")),
25+
}
26+
}
27+
28+
fn pipe_is_admin_owned_inner<P: AsRef<Path>>(path: P) -> Result<bool, Error> {
29+
let client = std::fs::File::options()
30+
.read(true)
31+
.open(path)
32+
.map_err(Error::OpenPipe)?;
33+
34+
talpid_windows::fs::is_admin_owned(client).map_err(Error::CheckPermissions)
35+
}

desktop/packages/windows-utils/windows-utils-rs/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
use neon::{prelude::ModuleContext, result::NeonResult};
44

5+
mod fs;
56
mod shortcut;
67

78
#[neon::main]
89
fn main(mut cx: ModuleContext<'_>) -> NeonResult<()> {
910
cx.export_function("readShortcut", shortcut::read_shortcut)?;
11+
cx.export_function("pipeIsAdminOwned", fs::pipe_is_admin_owned)?;
1012

1113
Ok(())
1214
}

0 commit comments

Comments
 (0)