rustwright-core 0.1.1

Rust CDP core for a Python Playwright-compatible automation API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
"""Durable, per-user state for persistent agent browser sessions."""

import errno
import fcntl
import hashlib
import json
import os
import re
import secrets
import stat
import tempfile
import time
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Dict, Iterator, Optional

from .errors import AgentError


_SESSION_NAME = re.compile(r"^[A-Za-z0-9_-]{1,64}$")
_STATE_FIELDS = {
    "schema",
    "session",
    "owner_pid",
    "endpoint",
    "control_token",
    "session_nonce",
    "active_target_id",
    "tabs",
    "next_tab_id",
    "next_ref_id",
    "dirty",
    "launch_config_hash",
}


def validate_session_name(name: str) -> str:
    if not isinstance(name, str) or _SESSION_NAME.fullmatch(name) is None:
        raise AgentError(
            "invalid_argument",
            "Session name must contain 1 to 64 letters, digits, underscores, or hyphens",
        )
    return name


def launch_config_hash(
    headed: bool,
    executable_path: Optional[str],
    browser_args: Any,
) -> str:
    value = {
        "headed": bool(headed),
        "executable_path": executable_path,
        "browser_args": list(browser_args or []),
    }
    encoded = json.dumps(value, separators=(",", ":"), sort_keys=True).encode("utf-8")
    return hashlib.sha256(encoded).hexdigest()


def _unsafe_path(message: str) -> AgentError:
    return AgentError("session_lost", message)


def _validate_directory(path: Path, require_private: bool = True) -> None:
    try:
        info = os.lstat(str(path))
    except OSError:
        raise _unsafe_path("The agent runtime directory is unavailable") from None
    if stat.S_ISLNK(info.st_mode) or not stat.S_ISDIR(info.st_mode):
        raise _unsafe_path("The agent runtime path must be a real directory")
    if info.st_uid != os.getuid():
        raise _unsafe_path("The agent runtime directory is not owned by the current user")
    if require_private and info.st_mode & 0o022:
        raise _unsafe_path("The agent runtime directory must not be group or world writable")


def _make_private_directory(path: Path) -> Path:
    try:
        os.makedirs(str(path), mode=0o700, exist_ok=True)
    except OSError:
        raise _unsafe_path("The agent runtime directory could not be created") from None
    _validate_directory(path)
    return path


def runtime_dir() -> Path:
    """Return a private runtime directory without accepting a symlink target."""

    configured = os.environ.get("RUSTWRIGHT_AGENT_RUNTIME_DIR")
    if configured:
        return _make_private_directory(Path(configured).expanduser())

    xdg_value = os.environ.get("XDG_RUNTIME_DIR")
    if xdg_value:
        xdg = Path(xdg_value).expanduser()
        try:
            _validate_directory(xdg)
        except AgentError:
            pass
        else:
            return _make_private_directory(xdg / "rustwright" / "agent")

    temporary = os.environ.get("TMPDIR") or tempfile.gettempdir() or "/tmp"
    return _make_private_directory(Path(temporary) / ("rustwright-agent-%d" % os.getuid()))


def session_dir(name: str) -> Path:
    validate_session_name(name)
    path = runtime_dir() / name
    try:
        os.mkdir(str(path), 0o700)
    except OSError as exc:
        if exc.errno != errno.EEXIST:
            raise _unsafe_path("The session directory could not be created") from None
    _validate_directory(path)
    return path


def state_path(name: str) -> Path:
    return session_dir(name) / "state.json"


def lock_path(name: str) -> Path:
    return session_dir(name) / "session.lock"


def stop_path(name: str) -> Path:
    return session_dir(name) / "stop.json"


def error_path(name: str) -> Path:
    return session_dir(name) / "error.json"


def bootstrap_ack_path(name: str) -> Path:
    return session_dir(name) / "bootstrap-ack.json"


def owner_lock_path(name: str) -> Path:
    return session_dir(name) / "owner.lock"


def _validate_regular_file(path: Path) -> os.stat_result:
    try:
        info = os.lstat(str(path))
    except OSError as exc:
        if exc.errno == errno.ENOENT:
            raise
        raise _unsafe_path("A session state file is unavailable") from None
    if stat.S_ISLNK(info.st_mode) or not stat.S_ISREG(info.st_mode):
        raise _unsafe_path("A session state path must be a regular file")
    if info.st_uid != os.getuid():
        raise _unsafe_path("A session state file is not owned by the current user")
    return info


def _fsync_directory(path: Path) -> None:
    flags = os.O_RDONLY
    if hasattr(os, "O_DIRECTORY"):
        flags |= os.O_DIRECTORY
    try:
        fd = os.open(str(path), flags)
    except OSError:
        return
    try:
        os.fsync(fd)
    finally:
        os.close(fd)


def atomic_write_json(path: Any, value: Dict[str, Any]) -> None:
    """Write compact JSON atomically using a private exclusive temp file."""

    target = Path(path)
    parent = target.parent
    _validate_directory(parent)
    try:
        _validate_regular_file(target)
    except OSError as exc:
        if exc.errno != errno.ENOENT:
            raise

    encoded = json.dumps(value, separators=(",", ":"), sort_keys=True).encode("utf-8") + b"\n"
    nofollow = getattr(os, "O_NOFOLLOW", 0)
    flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY | nofollow
    temporary = None  # type: Optional[Path]
    fd = None  # type: Optional[int]
    try:
        for _attempt in range(100):
            candidate = parent / (".%s.%s.tmp" % (target.name, secrets.token_hex(8)))
            try:
                fd = os.open(str(candidate), flags, 0o600)
            except OSError as exc:
                if exc.errno == errno.EEXIST:
                    continue
                raise
            temporary = candidate
            break
        if fd is None or temporary is None:
            raise OSError(errno.EEXIST, "could not allocate an atomic state file")
        offset = 0
        while offset < len(encoded):
            offset += os.write(fd, encoded[offset:])
        os.fsync(fd)
        os.close(fd)
        fd = None

        # Re-check an existing destination so a pre-existing symlink is refused
        # rather than silently replaced. os.replace itself never follows it.
        try:
            _validate_regular_file(target)
        except OSError as exc:
            if exc.errno != errno.ENOENT:
                raise
        os.replace(str(temporary), str(target))
        temporary = None
        _fsync_directory(parent)
    except AgentError:
        raise
    except OSError:
        raise _unsafe_path("The session state could not be written") from None
    finally:
        if fd is not None:
            try:
                os.close(fd)
            except OSError:
                pass
        if temporary is not None:
            try:
                os.unlink(str(temporary))
            except OSError:
                pass


def read_json(path: Any, missing_ok: bool = False) -> Optional[Dict[str, Any]]:
    target = Path(path)
    try:
        before = _validate_regular_file(target)
    except OSError as exc:
        if exc.errno == errno.ENOENT and missing_ok:
            return None
        if exc.errno == errno.ENOENT:
            raise _unsafe_path("The session state file does not exist") from None
        raise

    nofollow = getattr(os, "O_NOFOLLOW", 0)
    try:
        fd = os.open(str(target), os.O_RDONLY | nofollow)
        try:
            after = os.fstat(fd)
            if not stat.S_ISREG(after.st_mode) or after.st_uid != os.getuid():
                raise _unsafe_path("A session state path must be an owned regular file")
            if before.st_dev != after.st_dev or before.st_ino != after.st_ino:
                raise _unsafe_path("The session state changed while it was opened")
            chunks = []
            while True:
                chunk = os.read(fd, 65536)
                if not chunk:
                    break
                chunks.append(chunk)
                if sum(len(item) for item in chunks) > 4 * 1024 * 1024:
                    raise _unsafe_path("The session state file is too large")
        finally:
            os.close(fd)
        value = json.loads(b"".join(chunks).decode("utf-8"))
    except AgentError:
        raise
    except (OSError, UnicodeError, ValueError):
        raise _unsafe_path("The session state file is invalid") from None
    if not isinstance(value, dict):
        raise _unsafe_path("The session state file is invalid")
    return value


def _validate_state(value: Dict[str, Any], expected_session: str) -> Dict[str, Any]:
    if set(value) != _STATE_FIELDS or value.get("schema") != 1:
        raise _unsafe_path("The session state schema is unsupported")
    if value.get("session") != expected_session:
        raise _unsafe_path("The session state name does not match")
    if isinstance(value.get("owner_pid"), bool) or not isinstance(value.get("owner_pid"), int):
        raise _unsafe_path("The session owner pid is invalid")
    for field in ("endpoint", "control_token", "session_nonce", "launch_config_hash"):
        if not isinstance(value.get(field), str) or not value[field]:
            raise _unsafe_path("The session state is incomplete")
    active_target = value.get("active_target_id")
    if active_target is not None and not isinstance(active_target, str):
        raise _unsafe_path("The active tab metadata is invalid")
    tabs = value.get("tabs")
    if not isinstance(tabs, dict) or any(
        not isinstance(target, str) or not isinstance(tab, str) for target, tab in tabs.items()
    ):
        raise _unsafe_path("The tab metadata is invalid")
    for field in ("next_tab_id", "next_ref_id"):
        item = value.get(field)
        if isinstance(item, bool) or not isinstance(item, int) or item < 1:
            raise _unsafe_path("The session counter metadata is invalid")
    dirty = value.get("dirty")
    if dirty is not None and (not isinstance(dirty, str) or not dirty):
        raise _unsafe_path("The session journal marker is invalid")
    return value


def read_state(name: str) -> Optional[Dict[str, Any]]:
    validate_session_name(name)
    value = read_json(state_path(name), missing_ok=True)
    if value is None:
        return None
    return _validate_state(value, name)


def write_state(name: str, value: Dict[str, Any]) -> None:
    validate_session_name(name)
    _validate_state(value, name)
    atomic_write_json(state_path(name), value)


def mark_dirty(state: Dict[str, Any]) -> str:
    """Durably mark a state-changing command as in progress."""

    marker = "%d-%s" % (time.monotonic_ns(), secrets.token_hex(8))
    state["dirty"] = marker
    write_state(state["session"], state)
    return marker


def clear_dirty(state: Dict[str, Any]) -> None:
    state["dirty"] = None
    write_state(state["session"], state)


def _open_owned_lock(path: Path, message: str) -> int:
    nofollow = getattr(os, "O_NOFOLLOW", 0)
    fd = None  # type: Optional[int]
    try:
        fd = os.open(str(path), os.O_CREAT | os.O_RDWR | nofollow, 0o600)
        info = os.fstat(fd)
        if not stat.S_ISREG(info.st_mode) or info.st_uid != os.getuid():
            raise _unsafe_path(message)
        return fd
    except AgentError:
        if fd is not None:
            os.close(fd)
        raise
    except OSError:
        if fd is not None:
            try:
                os.close(fd)
            except OSError:
                pass
        raise _unsafe_path(message) from None


@contextmanager
def owner_lifetime_lock(name: str) -> Iterator[None]:
    """Hold the dedicated owner identity lock for the process lifetime."""

    validate_session_name(name)
    fd = _open_owned_lock(owner_lock_path(name), "The owner lock could not be opened")
    acquired = False
    try:
        try:
            fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
            acquired = True
        except OSError as exc:
            if exc.errno in (errno.EACCES, errno.EAGAIN):
                raise AgentError("session_busy", "A browser owner is already running") from None
            raise _unsafe_path("The owner lock failed") from None
        yield
    finally:
        if acquired:
            try:
                fcntl.flock(fd, fcntl.LOCK_UN)
            except OSError:
                pass
        os.close(fd)


def owner_lock_is_held(name: str) -> bool:
    """Return whether a live owner currently holds the identity lock."""

    validate_session_name(name)
    fd = _open_owned_lock(owner_lock_path(name), "The owner lock could not be opened")
    acquired = False
    try:
        try:
            fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
            acquired = True
            return False
        except OSError as exc:
            if exc.errno in (errno.EACCES, errno.EAGAIN):
                return True
            raise _unsafe_path("The owner lock failed") from None
    finally:
        if acquired:
            try:
                fcntl.flock(fd, fcntl.LOCK_UN)
            except OSError:
                pass
        os.close(fd)


@contextmanager
def session_lock(name: str, timeout: float = 30.0) -> Iterator[None]:
    validate_session_name(name)
    if isinstance(timeout, bool) or not isinstance(timeout, (int, float)) or timeout < 0:
        raise AgentError("invalid_argument", "Lock timeout must be a non-negative number")
    path = lock_path(name)
    nofollow = getattr(os, "O_NOFOLLOW", 0)
    try:
        fd = os.open(str(path), os.O_CREAT | os.O_RDWR | nofollow, 0o600)
        info = os.fstat(fd)
        if not stat.S_ISREG(info.st_mode) or info.st_uid != os.getuid():
            raise _unsafe_path("The session lock must be an owned regular file")
    except AgentError:
        raise
    except OSError:
        raise _unsafe_path("The session lock could not be opened") from None

    deadline = time.monotonic() + float(timeout)
    acquired = False
    try:
        while True:
            try:
                fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
                acquired = True
                break
            except OSError as exc:
                if exc.errno not in (errno.EACCES, errno.EAGAIN):
                    raise _unsafe_path("The session lock failed") from None
                if time.monotonic() >= deadline:
                    raise AgentError("session_busy", "The browser session is busy")
                time.sleep(0.025)
        yield
    finally:
        if acquired:
            try:
                fcntl.flock(fd, fcntl.LOCK_UN)
            except OSError:
                pass
        os.close(fd)


def remove_session_files(name: str, include_lock: bool = False) -> None:
    """Remove runtime control files without following attacker-supplied paths."""

    paths = [state_path(name), stop_path(name), error_path(name), bootstrap_ack_path(name)]
    if include_lock:
        paths.append(lock_path(name))
    for path in paths:
        try:
            os.unlink(str(path))
        except OSError as exc:
            if exc.errno != errno.ENOENT:
                raise _unsafe_path("The session runtime files could not be removed") from None
    _fsync_directory(session_dir(name))