askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
object_user_access.py · 3.1 KB · 86 lines
| 1 | """What a system or package object lets user code see and call. |
| 2 | |
| 3 | Two module-level flags, read statically from the source (AST, never executed, |
| 4 | the same posture as ``HANDLES`` in object_handlers): |
| 5 | |
| 6 | ``USER_CALLABLE = True`` |
| 7 | User code (a sandboxed ``u_<uid>_*`` object, whether it runs for an HTTP |
| 8 | request or for the daemon) may load and execute this object with |
| 9 | ``_runtime.load_object``. Without it the load is refused. Default: not |
| 10 | callable. The sandbox simulation (2026-12-01) showed user code running an |
| 11 | admin-created object with side effects, and posting to the daemon's queue |
| 12 | object; "system objects follow the usual permission rules" does not stop a |
| 13 | call that never comes in over HTTP. |
| 14 | ``LISTED = True`` |
| 15 | A non-admin user's ``GET /objects`` includes this system object. Without |
| 16 | it they see only their own ``u_<id>_*`` objects. Default: unlisted. This |
| 17 | is about discovery only, never access: an unlisted object is still |
| 18 | reachable by whoever may already reach it. |
| 19 | |
| 20 | Both defaults are the reversible choice: a package author who wants user code |
| 21 | to call an object adds one line; nothing is lost by starting closed. |
| 22 | |
| 23 | Opting in is a statement that the object treats its caller as untrusted: it |
| 24 | must check ``request["_identity"]`` itself and expose nothing it would not hand |
| 25 | to any signed-in customer. |
| 26 | """ |
| 27 | |
| 28 | from __future__ import annotations |
| 29 | |
| 30 | import ast |
| 31 | import os |
| 32 | from pathlib import Path |
| 33 | |
| 34 | USER_CALLABLE = "USER_CALLABLE" |
| 35 | LISTED = "LISTED" |
| 36 | FLAGS = (USER_CALLABLE, LISTED) |
| 37 | |
| 38 | _CACHE: dict[str, tuple[int, int, frozenset[str]]] = {} |
| 39 | _CACHE_MAX = 4096 |
| 40 | |
| 41 | |
| 42 | def declared_flags(path: Path | str) -> frozenset[str]: |
| 43 | """Flags a source file sets to a literal True at module level.""" |
| 44 | key = os.fspath(path) |
| 45 | try: |
| 46 | stat = os.stat(key) |
| 47 | except OSError: |
| 48 | return frozenset() |
| 49 | stamp = (stat.st_mtime_ns, stat.st_size) |
| 50 | cached = _CACHE.get(key) |
| 51 | if cached is not None and cached[:2] == stamp: |
| 52 | return cached[2] |
| 53 | try: |
| 54 | with open(key, encoding="utf-8") as handle: |
| 55 | tree = ast.parse(handle.read()) |
| 56 | except (OSError, SyntaxError, ValueError): |
| 57 | found: frozenset[str] = frozenset() |
| 58 | else: |
| 59 | names = set() |
| 60 | for node in tree.body: |
| 61 | if isinstance(node, ast.Assign): |
| 62 | targets, value = node.targets, node.value |
| 63 | elif isinstance(node, ast.AnnAssign) and node.value is not None: |
| 64 | targets, value = [node.target], node.value |
| 65 | else: |
| 66 | continue |
| 67 | truthy = isinstance(value, ast.Constant) and value.value is True |
| 68 | for target in targets: |
| 69 | if isinstance(target, ast.Name) and target.id in FLAGS: |
| 70 | if truthy: |
| 71 | names.add(target.id) |
| 72 | else: |
| 73 | names.discard(target.id) # a later non-True assignment wins |
| 74 | found = frozenset(names) |
| 75 | if len(_CACHE) >= _CACHE_MAX: |
| 76 | _CACHE.clear() |
| 77 | _CACHE[key] = (stamp[0], stamp[1], found) |
| 78 | return found |
| 79 | |
| 80 | |
| 81 | def is_user_callable(path: Path | str) -> bool: |
| 82 | return USER_CALLABLE in declared_flags(path) |
| 83 | |
| 84 | |
| 85 | def is_listed(path: Path | str) -> bool: |
| 86 | return LISTED in declared_flags(path) |