Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/object_user_access.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 7 minutes ago History
object_user_access.py · 3.1 KB · 86 lines
1"""What a system or package object lets user code see and call.
2
3Two module-level flags, read statically from the source (AST, never executed,
4the 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
20Both defaults are the reversible choice: a package author who wants user code
21to call an object adds one line; nothing is lost by starting closed.
22
23Opting in is a statement that the object treats its caller as untrusted: it
24must check ``request["_identity"]`` itself and expose nothing it would not hand
25to any signed-in customer.
26"""
27
28from __future__ import annotations
29
30import ast
31import os
32from pathlib import Path
33
34USER_CALLABLE = "USER_CALLABLE"
35LISTED = "LISTED"
36FLAGS = (USER_CALLABLE, LISTED)
37
38_CACHE: dict[str, tuple[int, int, frozenset[str]]] = {}
39_CACHE_MAX = 4096
40
41
42def 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
81def is_user_callable(path: Path | str) -> bool:
82 return USER_CALLABLE in declared_flags(path)
83
84
85def is_listed(path: Path | str) -> bool:
86 return LISTED in declared_flags(path)