Skip to content
Merged
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
10 changes: 10 additions & 0 deletions packages/desktop/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# CHANGELOG


## v0.31.1 (2026-11-22)

### Bug Fixes

- Exception cards are checked on Windows as on macOS and Linux: a link or junction is
refused, and a card must be private to this user by its access control list (only this
user, SYSTEM and Administrators). Answers are written only where they come out private,
and card and answer files are read and written as raw bytes.
- Update the `url` crate to 2.5.9 (RUSTSEC-2026-0592).

## v0.31.0 (2026-11-21)

### Features
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/openadapt_desktop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
+-- backends/ Storage backend plugins
"""

__version__ = "0.31.0"
__version__ = "0.31.1"
40 changes: 29 additions & 11 deletions packages/desktop/openadapt_desktop/approver_anchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,13 @@ def _read_windows(path: Path) -> bytes: # pragma: no cover - Windows only
return data


def _windows_security(target: Path) -> tuple[str | None, list[Ace] | None]: # pragma: no cover
def _windows_security( # pragma: no cover - Windows only
target: Path, *, handle: int | None = None
) -> tuple[str | None, list[Ace] | None]:
"""The owner SID and DACL entries of ``target``, or of the open file
``handle`` (a Win32 handle) when one is given, so the answer is about the
file already opened and not whatever the name points to now."""

import ctypes
from ctypes import wintypes

Expand All @@ -480,16 +486,28 @@ def _windows_security(target: Path) -> tuple[str | None, list[Ace] | None]: # p
dacl = ctypes.c_void_p()
descriptor = ctypes.c_void_p()
owner_info, dacl_info = 0x1, 0x4
status = advapi.GetNamedSecurityInfoW(
wintypes.LPCWSTR(str(target)),
1, # SE_FILE_OBJECT
owner_info | dacl_info,
ctypes.byref(owner),
None,
ctypes.byref(dacl),
None,
ctypes.byref(descriptor),
)
if handle is None:
status = advapi.GetNamedSecurityInfoW(
wintypes.LPCWSTR(str(target)),
1, # SE_FILE_OBJECT
owner_info | dacl_info,
ctypes.byref(owner),
None,
ctypes.byref(dacl),
None,
ctypes.byref(descriptor),
)
else:
status = advapi.GetSecurityInfo(
wintypes.HANDLE(handle),
1, # SE_FILE_OBJECT
owner_info | dacl_info,
ctypes.byref(owner),
None,
ctypes.byref(dacl),
None,
ctypes.byref(descriptor),
)
if status != 0:
raise AnchorError(
"approver_anchor_unreadable", f"Can't read the security of {target} ({status})."
Expand Down
167 changes: 156 additions & 11 deletions packages/desktop/openadapt_desktop/exception_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
this user, readable by no one else (no group or other permission bits), under
the size cap. A card that fails any check isn't shown.

Windows has no permission bits or ``O_NOFOLLOW``, so there the same rules are
checked another way: a card that is a link or any other reparse point (a
symlink, a junction) is refused before it is opened, the opened file must be
the one that was checked, and its owner and access control list are read from
the open handle. The owner must be this user, and only this user, SYSTEM and
Administrators (what a user's profile folder grants by default) may hold any
right on it (OWNER RIGHTS, which means the owner, too). When that can't be
read, no card is shown.

An answer is the closed answer signed with the approver key Desktop already
uses for the stop queue (Touch ID on a Mac, the portal's key for a phone).
The waiting run checks the signature against its pinned approver roster, so
Expand Down Expand Up @@ -61,6 +70,12 @@
_RUN_REF = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$")
_CARD_FILE = re.compile(r"^card-([0-9a-f]{64})\.json$")
_SUMMARY_MAX = 200
_FILE_ATTRIBUTE_REPARSE_POINT = 0x400
#: Who besides this user may hold rights on a card on Windows: SYSTEM and
#: Administrators, which a user's profile folder grants by default, and OWNER
#: RIGHTS (S-1-3-4), which names the file's owner, already required to be
#: this user.
_WINDOWS_TRUSTED_SIDS = frozenset({"S-1-5-18", "S-1-5-32-544", "S-1-3-4"})


def _canonical(value: Any) -> bytes:
Expand All @@ -72,8 +87,124 @@ def registry_root() -> Path:
return (Path(home) if home else Path.home() / ".openadapt") / "exceptions"


def _binary(flags: int) -> int:
"""Raw bytes on Windows too (no newline or Ctrl-Z translation)."""

return flags | getattr(os, "O_BINARY", 0)


def _is_link(info: os.stat_result) -> bool:
"""A symlink, or on Windows any reparse point (a junction too)."""

reparse = getattr(info, "st_file_attributes", 0) & _FILE_ATTRIBUTE_REPARSE_POINT
return stat.S_ISLNK(info.st_mode) or bool(reparse)


def windows_private_reason(
owner_sid: str | None, aces: list[Any] | None, *, me: frozenset[str]
) -> str | None:
"""Why a Windows file isn't private to this user, or ``None`` when it is.

``me`` is this user's SIDs: the process token's user and the owner it
gives new files. ``aces is None`` is a NULL DACL (everyone has full
control). Deny entries only take rights away; an entry of a type this
check doesn't know refuses the file.
"""

from openadapt_desktop.approver_anchor import (
_ALLOW_ACE_TYPES,
_DENY_ACE_TYPES,
_INHERIT_ONLY_ACE,
)

if owner_sid not in me:
return f"it is owned by {owner_sid}, not this user"
if aces is None:
return "it has no access control list, so everyone can open it"
allowed = me | _WINDOWS_TRUSTED_SIDS
for ace in aces:
if ace.ace_type in _DENY_ACE_TYPES:
continue
if ace.ace_type not in _ALLOW_ACE_TYPES:
return f"it has an access entry of a type this check doesn't know ({ace.ace_type})"
if ace.flags & _INHERIT_ONLY_ACE:
continue
if ace.mask and ace.sid not in allowed:
return f"{ace.sid} has access to it"
return None


def _windows_me() -> frozenset[str]: # pragma: no cover - Windows only
"""This process's user SID and the owner SID it gives new files."""

import ctypes
from ctypes import wintypes

from openadapt_desktop.approver_anchor import _sid_string, _windll

advapi = _windll("advapi32", use_last_error=True)
kernel = _windll("kernel32", use_last_error=True)
kernel.GetCurrentProcess.restype = wintypes.HANDLE
advapi.OpenProcessToken.argtypes = [
wintypes.HANDLE,
wintypes.DWORD,
ctypes.POINTER(wintypes.HANDLE),
]
advapi.GetTokenInformation.argtypes = [
wintypes.HANDLE,
ctypes.c_int,
ctypes.c_void_p,
wintypes.DWORD,
ctypes.POINTER(wintypes.DWORD),
]
token = wintypes.HANDLE()
if not advapi.OpenProcessToken(kernel.GetCurrentProcess(), 0x0008, ctypes.byref(token)):
raise OSError("can't open this process's token")
try:
sids: set[str] = set()
for info_class in (1, 4): # TokenUser, TokenOwner: a SID pointer first
buffer = ctypes.create_string_buffer(1024)
needed = wintypes.DWORD()
if not advapi.GetTokenInformation(
token, info_class, buffer, len(buffer), ctypes.byref(needed)
):
raise OSError("can't read this process's token")
sid = ctypes.c_void_p.from_buffer(buffer).value
if not sid:
raise OSError("this process's token has no SID")
sids.add(_sid_string(advapi, kernel, ctypes.c_void_p(sid)))
return frozenset(sids)
finally:
kernel.CloseHandle(token)


def _windows_private(fd: int, path: Path) -> bool: # pragma: no cover - Windows only
"""The open file is private to this user. Any failure to tell is ``False``."""

try:
import msvcrt

from openadapt_desktop.approver_anchor import _windows_security

owner, aces = _windows_security(path, handle=msvcrt.get_osfhandle(fd))
return windows_private_reason(owner, aces, me=_windows_me()) is None
except Exception:
return False


def _read(path: Path) -> bytes | None:
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_CLOEXEC", 0)
flags = _binary(os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_CLOEXEC", 0))
windows = os.name == "nt"
before = None
if windows:
# No O_NOFOLLOW here: refuse a link before opening, and below require
# that the file opened is the one checked.
try:
before = os.lstat(path)
except OSError:
return None
if _is_link(before) or not stat.S_ISREG(before.st_mode):
return None
try:
fd = os.open(path, flags)
except OSError:
Expand All @@ -86,6 +217,15 @@ def _read(path: Path) -> bytes | None:
return None
if os.name == "posix" and info.st_mode & 0o077:
return None # another user could read or change it
if windows:
if (
before is None
or not info.st_ino
or (info.st_dev, info.st_ino) != (before.st_dev, before.st_ino)
):
return None # swapped between the check and the open
if not _windows_private(fd, path):
return None # another user could read or change it
return os.read(fd, _MAX_BYTES + 1)[:_MAX_BYTES]
finally:
os.close(fd)
Expand Down Expand Up @@ -138,7 +278,7 @@ def open_cards(root: Path | None = None) -> list[dict[str, Any]]:
cards: list[dict[str, Any]] = []
for folder in folders:
try:
if stat.S_ISLNK(os.lstat(folder).st_mode) or not folder.is_dir():
if _is_link(os.lstat(folder)) or not folder.is_dir():
continue
names = sorted(os.listdir(folder))
except OSError:
Expand Down Expand Up @@ -205,16 +345,20 @@ def message(unsigned: dict[str, Any]) -> bytes:

def _write_exclusive(path: Path, payload: bytes) -> None:
tmp = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp")
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0)
fd = os.open(tmp, flags, 0o600)
try:
view = memoryview(payload)
while view:
view = view[os.write(fd, view) :]
os.fsync(fd)
finally:
os.close(fd)
flags = _binary(os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0))
try:
fd = os.open(tmp, flags, 0o600)
try:
# 0o600 means nothing on Windows: the file takes its folder's ACL.
# Write the answer only where it comes out private, as a card must.
if os.name == "nt" and not _windows_private(fd, tmp):
raise PermissionError(f"{tmp} would not be private to this user")
view = memoryview(payload)
while view:
view = view[os.write(fd, view) :]
os.fsync(fd)
finally:
os.close(fd)
os.link(tmp, path)
finally:
try:
Expand Down Expand Up @@ -307,6 +451,7 @@ def _after(a: str, b: str) -> bool:
"registry_root",
"unsigned_answer",
"valid_card",
"windows_private_reason",
]


Expand Down
4 changes: 2 additions & 2 deletions packages/desktop/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/desktop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openadapt-desktop",
"version": "0.31.0",
"version": "0.31.1",
"description": "Installed companion for OpenAdapt authoring, teaching, and local pairing",
"private": true,
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "openadapt-desktop"
version = "0.31.0"
version = "0.31.1"
description = "The OpenAdapt desktop app and local engine: record a data-entry task, run it on a list, and check each saved record"
readme = "README.md"
requires-python = ">=3.11"
Expand Down
10 changes: 5 additions & 5 deletions packages/desktop/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openadapt-desktop"
version = "0.31.0"
version = "0.31.1"
description = "Installed companion for OpenAdapt authoring, teaching, and local pairing"
authors = ["OpenAdapt AI"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/nicoverbruggen/tauri-v2-schema/main/schema.json",
"productName": "OpenAdapt Desktop",
"version": "0.31.0",
"version": "0.31.1",
"identifier": "ai.openadapt.desktop",
"build": {
"frontendDist": "../dist",
Expand Down
Loading
Loading