Skip to content

fix: '*' attribute dispatch only exposes public methods; bound obj_store - #23

Merged
thorwhalen merged 3 commits into
masterfrom
py2http-restrict-star-dispatch
Oct 13, 2026
Merged

thorwhalen merged 3 commits into
masterfrom
py2http-restrict-star-dispatch

Conversation

@thorwhalen

Copy link
Copy Markdown
Member

Closes #18 (item 2; item 1 landed in #20).

What changes

  • attr_names='*' now means public_attr: a name is dispatchable only if it does not start with _ and is a plain Python function, staticmethod, classmethod or property defined on the object's class (or a base class) and not shadowed by the instance. For a module endpoint, only functions the module defines (or lists in __all__), never names it imports, nor plain data.
    Refused now: _private methods, dunders (__dict__, __init__, ...), instance state (__dict__ entries, __slots__, dataclass and namedtuple fields), callables merely assigned as class attributes (opener = open), builtin method descriptors (dict.clear on a dict subclass), and a module's imports (from os import listdir).
  • Explicit name lists are unchanged (they may list dunders on purpose, as examples/object_dispatch.py does).
  • attr_names also accepts a predicate (obj, attr_name) -> bool.
  • Class-endpoint objects now live in a per-handler LRUObjStore (at most DFLT_OBJ_STORE_MAX_SIZE = 1000, least recently used evicted) instead of an unbounded module-global dict. A handler can pass its own obj_store= mapping. The module-global obj_store is gone; nothing in the fleet imported it.
  • Smaller fixes: a falsy object (e.g. an empty list) is no longer reported as missing; a non-string _attr_name / _obj_id gives an InputError instead of a 500; a dispatched method may have parameters named obj / attr_name.

Migration (one line)

To get the old "every attribute" behaviour back, for trusted clients only:

from py2http.service import unsafe_any_attr
dict(endpoint=obj, attr_names=unsafe_any_attr)   # was: attr_names='*'

For an unbounded object store (old behaviour): dict(endpoint=MyClass, attr_names=..., obj_store={}).

Note that '*' still exposes every public method, inherited ones included (e.g. MutableMapping's clear/pop, pydantic's model_dump); for stores or objects holding secrets, list the names explicitly. This is documented in public_attr and pinned by a test.

Adversarial review

A fresh Opus reviewer read the diff and the issue and tried to break it. First pass: SHIP-WITH-FIXES. It confirmed that the first version still leaked instance state through slots, dataclass and namedtuple fields and shadowed names; let a module endpoint reach anything it imported (it listed a directory through from os import listdir) and its plain data; dispatched callable class attributes; and returned a 500 on unhashable names. All of these are fixed in 59bcae2, with tests. Second pass: SHIP. Its two low residuals are fixed in 6956169: dispatch internals are now positional-only, and the check reads the real instance __dict__. It deferred ownership of the class-endpoint objects and size bounds, now filed as #22.

Checks

  • pytest py2http + doctests: 55 passed.
  • Dependents gate (priv test-dependents py2http --transitive): py2http, extrude both pass.

Follow-ups filed: #21 (JWT middleware options/algorithms), #22 (object ownership/size), i2mint/http2py#19 (client-side unpickling).

🤖 Generated with Claude Code

thorwhalen and others added 3 commits October 13, 2026 02:03
…bound obj_store

attr_names='*' now means `public_attr`: a name is dispatchable only if it does
not start with '_' and is defined on the object's class (or, for a module, in
the module). Private helpers, dunders (__dict__, __init__, ...) and per-instance
state are no longer reachable over HTTP. Explicit name lists are unchanged.
attr_names also accepts a `(obj, attr_name) -> bool` predicate; the old
behaviour is the explicit opt-in `attr_names=unsafe_any_attr`.

Objects created through a class endpoint now live in a per-handler
LRUObjStore (DFLT_OBJ_STORE_MAX_SIZE entries) instead of an unbounded
module-global dict; a handler can pass its own `obj_store=` mapping.
Also: an object that is falsy (e.g. an empty list) is no longer reported as
missing.

Refs #18 (item 2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed functions

Refuses instance state that public_attr still let through (slots, dataclass and
namedtuple fields, class defaults overwritten per instance, instance attributes
shadowing methods), callables merely assigned as class attributes, and, on
module endpoints, imported names and plain data (honours __all__). Non-string
_attr_name/_obj_id now give InputError instead of a 500.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dict__

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thorwhalen
thorwhalen merged commit fbbf0f2 into master Oct 13, 2026
6 checks passed
@thorwhalen
thorwhalen deleted the py2http-restrict-star-dispatch branch October 13, 2026 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restrict binary (pickle) input and '*' attribute dispatch

1 participant