Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, expect, it } from "vitest";
import { windowsBackgroundSecurityScript } from "./windows-background-security.ts";

// The owner check runs on Windows only, but the script is a pure string builder, so its
// listener query is pinned here without PowerShell: on some Windows builds the OS reports a
// listening socket with a wrong or empty TCP state, and a `State = 2` predicate matched 0 rows
// even though the socket was listening (w01fb0ss/PiBot#15).
describe("windowsBackgroundSecurityScript owner check", () => {
const ownerScript = () =>
windowsBackgroundSecurityScript({
operation: "owner",
pid: 1234,
port: 56789,
executable: "C:\\Program Files\\Pi Bot\\Pi Bot.exe",
});

it("does not filter the listener query on the TCP state column", () => {
const script = ownerScript();
expect(script).not.toContain("State = 2");
expect(script).toContain(
"SELECT OwningProcess FROM MSFT_NetTCPConnection WHERE LocalAddress = '127.0.0.1' AND LocalPort = $port",
);
});

it("accepts the listener when any row for the port is owned by the process", () => {
const script = ownerScript();
expect(script).toContain(
"$owned = @($listener | Where-Object { [int]$_['OwningProcess'] -eq $processId })",
);
expect(script).toContain("if ($owned.Count -eq 0) { throw 'listener_identity_changed' }");
});

it("still reports a missing listener when the query returns no rows", () => {
expect(ownerScript()).toContain("if ($listener.Count -eq 0) { throw 'listener_missing' }");
});

it("checks ownership in order: rows exist, then one is owned by the process", () => {
// The script is one static program carrying every branch, so the order of the lines
// inside the owner block is what pins the logic.
const script = ownerScript();
const query = script.indexOf("SELECT OwningProcess FROM MSFT_NetTCPConnection");
const missing = script.indexOf("if ($listener.Count -eq 0) { throw 'listener_missing' }");
const owned = script.indexOf(
"$owned = @($listener | Where-Object { [int]$_['OwningProcess'] -eq $processId })",
);
const changed = script.indexOf("if ($owned.Count -eq 0) { throw 'listener_identity_changed' }");
expect(query).toBeGreaterThan(-1);
expect(missing).toBeGreaterThan(query);
expect(owned).toBeGreaterThan(missing);
expect(changed).toBeGreaterThan(owned);
});

it("rejects a non-integer pid", () => {
expect(() =>
windowsBackgroundSecurityScript({ operation: "owner", pid: 1.5, port: 1 }),
).toThrow("background_windows_input_invalid");
});
});
13 changes: 10 additions & 3 deletions apps/desktop/src/main/background/windows-background-security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,17 @@ export function windowsBackgroundSecurityScript(input: Input) {
" if ($newFile -and $ownerSid -ne $sid.Value) { $acl.SetOwner($sid); [IO.File]::SetAccessControl($path, $acl); Assert-Private $path }",
"}",
"if ($operation -eq 'owner') {",
// MSFT_NetTCPConnection.State 2 = Listen; the same CIM class Get-NetTCPConnection reads.
" $listener = @(([Management.ManagementObjectSearcher]::new('root\\StandardCimv2', \"SELECT OwningProcess FROM MSFT_NetTCPConnection WHERE State = 2 AND LocalAddress = '127.0.0.1' AND LocalPort = $port\")).Get())",
// State is deliberately not filtered: on some Windows builds the OS reports a listening
// socket with a wrong or empty State (0, 1, 6, or unmapped), so a `State = 2` predicate
// matches 0 rows even though the socket is listening and accepting connections. A row
// for this port owned by the process proves the bound socket; the State column is not
// identity evidence. Rows owned by anyone else (e.g. a TIME_WAIT leftover from a dead
// predecessor) are tolerated as long as one row is ours; the token-authenticated
// handshake in connectBackground remains the identity proof.
" $listener = @(([Management.ManagementObjectSearcher]::new('root\\StandardCimv2', \"SELECT OwningProcess FROM MSFT_NetTCPConnection WHERE LocalAddress = '127.0.0.1' AND LocalPort = $port\")).Get())",
" if ($listener.Count -eq 0) { throw 'listener_missing' }",
" if ($listener.Count -ne 1 -or [int]$listener[0]['OwningProcess'] -ne $processId) { throw 'listener_identity_changed' }",
" $owned = @($listener | Where-Object { [int]$_['OwningProcess'] -eq $processId })",
" if ($owned.Count -eq 0) { throw 'listener_identity_changed' }",
" $proc = [Management.ManagementObject]::new(\"root\\cimv2:Win32_Process.Handle='$processId'\")",
" try { $proc.Get() } catch { throw 'process_identity_changed' }",
" if ([string]$proc['ExecutablePath'] -ine $executable -or [int]$proc['SessionId'] -ne [Diagnostics.Process]::GetCurrentProcess().SessionId) { throw 'process_identity_changed' }",
Expand Down
Loading