diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-08-18 18:21:57 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-08-18 18:21:57 +0200 |
| commit | b86981f7b4ffe758136a527542ce256315823a46 (patch) | |
| tree | 0d06a1cb5ca6d946d73db8724faa89bb1bd17501 /packages/meshbay-node/tests/test_member_upload_policy.py | |
| parent | cd2e89f5f5cccdb116db4fcb82d00b6325972782 (diff) | |
| download | meshbay-b86981f7b4ffe758136a527542ce256315823a46.tar.gz | |
feat(node): the operator can close uploading to everyone but themselves
A group where every member may add files stays the default. Some groups want a
library the operator curates, and until now the only way to get one was to
designate no upload root at all — which refuses the operator too.
**The node enforces it; the interface merely stops offering it.** The Upload
button in the Files toolbar and the paperclip in the chat composer both
disappear, which is a courtesy to the people who are not trying. The control is
`_do_file_upload` refusing with `member_upload_off`, so a member on an old tab,
or one speaking MNP directly, gets the same answer. There is a test for each,
and the enforcement test is in the node package rather than beside the UI one so
nobody reads the hidden button as the mechanism.
**Changing it is a signed operator instruction** — `OP_MEMBER_UPLOAD`, on the
same path as removing a member. An unsigned one would let any member turn it
back on and make the setting a suggestion. The transcript's subject is `on` or
`off`: what the operator is shown before signing has to name the outcome, not
the operation.
**It lives on the node**, in a new `group_settings` table in `roster.db`. Not
the hub, which has no business deciding who may write to someone else's disk.
Not `node.toml` either: that file is hand-written and full of comments recording
decisions, `ops.py` appends to it rather than round-tripping it through a
writer, and a setting toggled from a panel must not rewrite the operator's file
or need a restart. The value is cached in the group context because the upload
path is synchronous, and the signed operation updates both — storing it without
applying it would make the panel say one thing while the node did another.
**Absent means allowed**, at every layer: no row in the table, no key in the
context, no field in `handshake_ack`. An older node and an older client both
behave exactly as before, and upgrading never silently closes a group. Each of
those three has its own test, because they fail independently.
The operator is always exempt — otherwise turning it off locks them out of their
own node with a config file and a restart as the only way back. `is_node_admin`
was being computed in two places by then and is now one function, since two
copies of "is this the operator" is how the ack and the gate come to disagree.
A change reaches everyone already connected via `member_upload_ack`, so the
button goes without a reconnection. That message is both a broadcast and the
reply to the request that caused it, which is why the client does not return
early on it.
Docs updated for a cold start: draft-v6 §2.1b and change 9, a new "Where Phase
13 stands" section in CLAUDE.md recording what is built, deployed and still
missing, the module map row, and desktop-client-v1 §10b on the Settings tab and
where group settings live.
883 tests pass.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-node/tests/test_member_upload_policy.py')
| -rw-r--r-- | packages/meshbay-node/tests/test_member_upload_policy.py | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/packages/meshbay-node/tests/test_member_upload_policy.py b/packages/meshbay-node/tests/test_member_upload_policy.py new file mode 100644 index 0000000..b1dc0cb --- /dev/null +++ b/packages/meshbay-node/tests/test_member_upload_policy.py @@ -0,0 +1,176 @@ +""" +The operator can close uploading to everyone but themselves. + +The point of these tests is the difference between a hidden button and a closed +door. The interface stops offering the control, which is a courtesy to the +people who are not trying; **the node refuses the upload**, which is the part +that holds against someone who is. A member who kept an old tab open, or who +speaks MNP directly, gets the same answer as everyone else. + +Two further things are worth holding: + +* the setting is changed by a **signed** operator instruction. A node that took + it from an unsigned message would let any member turn it back on, and the + control would be a suggestion; +* it is stored on the **node**, not the hub. A hub that could decide who may + write to the operator's disk is a hub with authority over the node, which is + the thing this whole design is arranged to avoid. +""" + +import base64 +from pathlib import Path + +import pytest +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey + +from meshbay_common.adminop import OP_MEMBER_UPLOAD +from meshbay_node.indexer.group_index import GroupIndex +from meshbay_node.roster import Roster +from meshbay_node.transport.webrtc_server import WebRTCPeerSession + +from conftest import one_root + +pytestmark = pytest.mark.asyncio + + +def _session(tmp_path: Path, user_id: str, *, member_upload: bool, + operator: str | None = None) -> WebRTCPeerSession: + shared_root = tmp_path / "shared" + shared_root.mkdir(exist_ok=True) + index = GroupIndex(group_id="g" * 32, sk_node=Ed25519PrivateKey.generate()) + ctx = { + "roots": one_root(shared_root), + "index": index, + "sk_node": index.sk_node, + "member_upload": member_upload, + "node_user_id": operator, + } + session = WebRTCPeerSession.__new__(WebRTCPeerSession) + session._ctx = ctx + session._group_id = None + session._user_id = user_id + session._pk_user = "" + session._uploads = {} + session.sent = [] + session._send = session.sent.append + session._audit = lambda *a, **k: None + return session + + +def _upload(session, filename="clip.mp4", body=b"bytes"): + session._do_file_upload({ + "filename": filename, "chunk_index": 0, "total_chunks": 1, + "data": base64.b64encode(body).decode(), + }) + + +def _uploads_dir(session) -> Path: + return session._ctx["roots"].upload_root.path / "uploads" + + +# ── The door, not the button ──────────────────────────────────────────────── + +async def test_a_member_cannot_upload_when_it_is_turned_off(tmp_path): + session = _session(tmp_path, "member-1", member_upload=False, + operator="the-operator") + _upload(session) + + assert not (_uploads_dir(session) / "clip.mp4").exists(), ( + "the file was written even though uploading is off — the setting is " + "decorative and the hidden button was the whole control") + refusal = [m for m in session.sent if m.get("type") == "error"] + assert refusal and refusal[0].get("code") == "member_upload_off" + + +async def test_the_operator_can_still_upload(tmp_path): + """Otherwise turning it off locks the operator out of their own node, and + the only way back is a config file and a restart.""" + session = _session(tmp_path, "the-operator", member_upload=False, + operator="the-operator") + _upload(session) + + assert (_uploads_dir(session) / "clip.mp4").read_bytes() == b"bytes" + + +async def test_members_upload_normally_when_it_is_on(tmp_path): + session = _session(tmp_path, "member-1", member_upload=True, + operator="the-operator") + _upload(session) + + assert (_uploads_dir(session) / "clip.mp4").read_bytes() == b"bytes" + + +async def test_a_node_that_never_heard_of_the_setting_still_accepts_uploads(tmp_path): + """An existing node's context has no such key. The absence must read as + "allowed", or upgrading the node silently closes every group.""" + session = _session(tmp_path, "member-1", member_upload=True, + operator="the-operator") + del session._ctx["member_upload"] + _upload(session) + + assert (_uploads_dir(session) / "clip.mp4").read_bytes() == b"bytes" + + +# ── Who may change it ─────────────────────────────────────────────────────── + +async def test_changing_it_needs_a_signature(tmp_path): + """ + The request only ever produces a challenge. Nothing is applied until a + signature over the transcript verifies — the same path as removing a member. + """ + session = _session(tmp_path, "member-1", member_upload=True, + operator="the-operator") + session._has_admin_authority = lambda: True + issued = [] + session._issue_admin_challenge = lambda op, subject: issued.append((op, subject)) + + session._do_member_upload({"allowed": False}) + + assert issued == [(OP_MEMBER_UPLOAD, "off")] + assert session._ctx["member_upload"] is True, "applied before it was signed" + + +async def test_the_subject_names_the_outcome_not_the_operation(tmp_path): + """The operator is shown the subject before signing. "member_upload" tells + them nothing; "off" tells them what they are about to do.""" + session = _session(tmp_path, "op", member_upload=False, operator="op") + session._has_admin_authority = lambda: True + issued = [] + session._issue_admin_challenge = lambda op, subject: issued.append((op, subject)) + + session._do_member_upload({"allowed": True}) + + assert issued == [(OP_MEMBER_UPLOAD, "on")] + + +async def test_a_request_with_nobody_to_authorize_it_is_refused(tmp_path): + session = _session(tmp_path, "member-1", member_upload=True, + operator="the-operator") + session._has_admin_authority = lambda: False + + session._do_member_upload({"allowed": False}) + + assert [m for m in session.sent if m.get("type") == "error"] + + +# ── Where it is stored ────────────────────────────────────────────────────── + +async def test_the_setting_lives_on_the_node_and_survives_a_restart(tmp_path): + roster = Roster(db_path=tmp_path / "roster.db") + await roster.open() + try: + assert await roster.member_upload_allowed("g1") is True, ( + "absent must mean allowed, or an upgrade closes every group") + await roster.set_member_upload("g1", False, set_by="op") + assert await roster.member_upload_allowed("g1") is False + finally: + await roster.close() + + reopened = Roster(db_path=tmp_path / "roster.db") + await reopened.open() + try: + assert await reopened.member_upload_allowed("g1") is False + assert await reopened.member_upload_allowed("g2") is True, ( + "one group's setting must not answer for another") + finally: + await reopened.close() |