mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-28 21:29:17 +01:00
Compare commits
5 Commits
47f2d239c4
...
6a40f7530b
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a40f7530b | |||
| edd0f7a788 | |||
| ba58ac41e9 | |||
| 90286f6cb2 | |||
| 9102c4b6ff |
57
crdt-sync/crdt_sync/_e2e_cache.py
Normal file
57
crdt-sync/crdt_sync/_e2e_cache.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
# Copyright (c) 2026 Krzysztof Rudnicki
|
||||||
|
"""On-disk E2E state for short-lived daemons (one process per tick).
|
||||||
|
|
||||||
|
The derived key is cached ``0600`` beside the app's other state, so Argon2id
|
||||||
|
runs once per machine rather than once a minute; it is no more sensitive
|
||||||
|
than ``~/.config/crdt-sync/password``, which it is derived from. The copy
|
||||||
|
memo records the old prefix's revisions last copied, so an unchanged old
|
||||||
|
prefix is not re-scanned every tick.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
_MASTER = 32
|
||||||
|
|
||||||
|
|
||||||
|
def _slug(old_prefix: str) -> str:
|
||||||
|
return hashlib.sha256(old_prefix.encode()).hexdigest()[:16]
|
||||||
|
|
||||||
|
|
||||||
|
def read_cached_key(state_dir: Path, old_prefix: str) -> bytes | None:
|
||||||
|
"""Return the cached 32-byte master key, or None."""
|
||||||
|
path = state_dir / f"e2e-{_slug(old_prefix)}.key"
|
||||||
|
if not path.exists():
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
master = bytes.fromhex(path.read_text(encoding="utf-8").strip())
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
return master if len(master) == _MASTER else None
|
||||||
|
|
||||||
|
|
||||||
|
def write_cached_key(state_dir: Path, old_prefix: str, master: bytes) -> None:
|
||||||
|
"""Store the master key owner-only."""
|
||||||
|
state_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
path = state_dir / f"e2e-{_slug(old_prefix)}.key"
|
||||||
|
path.touch(mode=0o600, exist_ok=True)
|
||||||
|
path.chmod(0o600)
|
||||||
|
path.write_text(master.hex(), encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def read_copy_memo(state_dir: Path, old_prefix: str) -> str | None:
|
||||||
|
"""Return the old-prefix fingerprint last copied, or None."""
|
||||||
|
path = state_dir / f"e2e-{_slug(old_prefix)}.copied"
|
||||||
|
return path.read_text(encoding="utf-8") if path.exists() else None
|
||||||
|
|
||||||
|
|
||||||
|
def write_copy_memo(state_dir: Path, old_prefix: str, fingerprint: str) -> None:
|
||||||
|
"""Record that the old prefix was copied at ``fingerprint``."""
|
||||||
|
state_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
path = state_dir / f"e2e-{_slug(old_prefix)}.copied"
|
||||||
|
path.write_text(fingerprint, encoding="utf-8")
|
||||||
@ -12,12 +12,19 @@ from dataclasses import dataclass
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from crdt_sync._e2e_cache import (
|
||||||
|
read_cached_key,
|
||||||
|
read_copy_memo,
|
||||||
|
write_cached_key,
|
||||||
|
write_copy_memo,
|
||||||
|
)
|
||||||
from crdt_sync._e2e_cipher import E2eDecryptError, open_log_text, seal_log_text
|
from crdt_sync._e2e_cipher import E2eDecryptError, open_log_text, seal_log_text
|
||||||
from crdt_sync._e2e_key import (
|
from crdt_sync._e2e_key import (
|
||||||
Argon2Params,
|
Argon2Params,
|
||||||
E2eHeaderError,
|
E2eHeaderError,
|
||||||
E2eKey,
|
E2eKey,
|
||||||
E2eWrongPassphraseError,
|
E2eWrongPassphraseError,
|
||||||
|
key_matches,
|
||||||
unlock_e2e,
|
unlock_e2e,
|
||||||
)
|
)
|
||||||
from crdt_sync._log import merge_logs
|
from crdt_sync._log import merge_logs
|
||||||
@ -25,6 +32,8 @@ from crdt_sync._store import dump_log, load_log
|
|||||||
from crdt_sync._sync import default_revs_path
|
from crdt_sync._sync import default_revs_path
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from crdt_sync._remote import RemoteStore
|
from crdt_sync._remote import RemoteStore
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
@ -150,9 +159,16 @@ def copy_to_encrypted(
|
|||||||
except ValueError, KeyError, TypeError:
|
except ValueError, KeyError, TypeError:
|
||||||
_logger.warning("Unreadable old log %s/%s not copied", old_prefix, rel)
|
_logger.warning("Unreadable old log %s/%s not copied", old_prefix, rel)
|
||||||
continue
|
continue
|
||||||
|
if not source:
|
||||||
|
continue
|
||||||
path = f"{layout.prefix}/{rel}"
|
path = f"{layout.prefix}/{rel}"
|
||||||
existing = dest.get_file_text(path)
|
existing = dest.get_file_text(path)
|
||||||
current = load_log(existing) if existing is not None else {}
|
try:
|
||||||
|
current = load_log(existing) if existing is not None else {}
|
||||||
|
except ValueError, KeyError, TypeError:
|
||||||
|
# Not a sync log (an app's own blob): never overwrite it.
|
||||||
|
_logger.warning("Non-log blob at %s left as is", path)
|
||||||
|
continue
|
||||||
merged = merge_logs(current, source)
|
merged = merge_logs(current, source)
|
||||||
if existing is not None and dump_log(merged) == dump_log(current):
|
if existing is not None and dump_log(merged) == dump_log(current):
|
||||||
continue
|
continue
|
||||||
@ -177,34 +193,75 @@ def _fingerprint(store: RemoteStore, old_prefix: str) -> str:
|
|||||||
return ";".join(f"{k}={revs[k]}" for k in sorted(revs))
|
return ";".join(f"{k}={revs[k]}" for k in sorted(revs))
|
||||||
|
|
||||||
|
|
||||||
def open_encrypted_client(
|
@dataclass(frozen=True)
|
||||||
store: RemoteStore,
|
class _OpenRequest:
|
||||||
old_prefix: str,
|
old_prefix: str
|
||||||
*,
|
layout: E2eLayout
|
||||||
passphrase: str,
|
passphrase: str
|
||||||
params: Argon2Params | None = None,
|
params: Argon2Params | None
|
||||||
) -> tuple[EncryptingStore, E2eLayout]:
|
state_dir: Path | None
|
||||||
"""Unlock, copy the old prefix when it changed, and return the store.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
E2eLockedError: No passphrase, a wrong one, or an unreadable header.
|
def _unlock(store: RemoteStore, req: _OpenRequest) -> E2eKey:
|
||||||
"""
|
old_prefix, layout, state_dir = req.old_prefix, req.layout, req.state_dir
|
||||||
layout = E2eLayout.of(old_prefix)
|
if state_dir is not None:
|
||||||
if not passphrase:
|
cached = read_cached_key(state_dir, old_prefix)
|
||||||
|
if cached is not None:
|
||||||
|
key = E2eKey.from_bytes(cached)
|
||||||
|
if key_matches(key, store, layout.e2e_path):
|
||||||
|
return key
|
||||||
|
if not req.passphrase:
|
||||||
msg = "no crdt-sync passphrase on this device"
|
msg = "no crdt-sync passphrase on this device"
|
||||||
raise E2eLockedError(msg)
|
raise E2eLockedError(msg)
|
||||||
try:
|
try:
|
||||||
key = unlock_e2e(
|
key = unlock_e2e(
|
||||||
store,
|
store,
|
||||||
path_prefix=layout.prefix,
|
path_prefix=layout.prefix,
|
||||||
passphrase=passphrase,
|
passphrase=req.passphrase,
|
||||||
params=params,
|
params=req.params,
|
||||||
e2e_path=layout.e2e_path,
|
e2e_path=layout.e2e_path,
|
||||||
)
|
)
|
||||||
except (E2eWrongPassphraseError, E2eHeaderError) as exc:
|
except (E2eWrongPassphraseError, E2eHeaderError) as exc:
|
||||||
raise E2eLockedError(str(exc) or type(exc).__name__) from exc
|
raise E2eLockedError(str(exc) or type(exc).__name__) from exc
|
||||||
|
if state_dir is not None:
|
||||||
|
write_cached_key(state_dir, old_prefix, key.master)
|
||||||
|
return key
|
||||||
|
|
||||||
|
|
||||||
|
def open_encrypted_client(
|
||||||
|
store: RemoteStore,
|
||||||
|
old_prefix: str,
|
||||||
|
*,
|
||||||
|
passphrase: str,
|
||||||
|
params: Argon2Params | None = None,
|
||||||
|
state_dir: Path | None = None,
|
||||||
|
) -> tuple[EncryptingStore, E2eLayout]:
|
||||||
|
"""Unlock, copy the old prefix when it changed, and return the store.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
store: The outermost remote (above any mirror).
|
||||||
|
old_prefix: The app's former plaintext prefix.
|
||||||
|
passphrase: The shared crdt-sync password.
|
||||||
|
params: Argon2id cost for a header created now; tests lower it.
|
||||||
|
state_dir: Where a short-lived daemon keeps its derived key (0600)
|
||||||
|
and copy memo between runs; None keeps both in memory only.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
E2eLockedError: No passphrase, a wrong one, or an unreadable header.
|
||||||
|
"""
|
||||||
|
layout = E2eLayout.of(old_prefix)
|
||||||
|
key = _unlock(
|
||||||
|
store, _OpenRequest(old_prefix, layout, passphrase, params, state_dir)
|
||||||
|
)
|
||||||
fingerprint = _fingerprint(store, old_prefix)
|
fingerprint = _fingerprint(store, old_prefix)
|
||||||
if _copied.get(old_prefix) != fingerprint:
|
last = (
|
||||||
|
read_copy_memo(state_dir, old_prefix)
|
||||||
|
if state_dir is not None
|
||||||
|
else _copied.get(old_prefix)
|
||||||
|
)
|
||||||
|
if last != fingerprint:
|
||||||
copy_to_encrypted(store, key, old_prefix, layout)
|
copy_to_encrypted(store, key, old_prefix, layout)
|
||||||
_copied[old_prefix] = fingerprint
|
_copied[old_prefix] = fingerprint
|
||||||
|
if state_dir is not None:
|
||||||
|
write_copy_memo(state_dir, old_prefix, fingerprint)
|
||||||
return EncryptingStore(store, key, layout), layout
|
return EncryptingStore(store, key, layout), layout
|
||||||
|
|||||||
101
crdt-sync/crdt_sync/tests/test_e2e_state_dir.py
Normal file
101
crdt-sync/crdt_sync/tests/test_e2e_state_dir.py
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
# Copyright (c) 2026 Krzysztof Rudnicki
|
||||||
|
"""A per-minute daemon must not pay Argon2id or a full copy every tick."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from crdt_sync import Argon2Params, E2eLockedError, open_encrypted_client
|
||||||
|
from crdt_sync import _e2e_key as key_mod
|
||||||
|
from crdt_sync import _e2e_store as store_mod
|
||||||
|
from crdt_sync.tests.conftest import FakeStore
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from crdt_sync import E2eKey, RemoteStore
|
||||||
|
|
||||||
|
CHEAP = Argon2Params(8, 1, 1)
|
||||||
|
PHRASE = "pw"
|
||||||
|
WRONG = PHRASE + "x"
|
||||||
|
LOG = json.dumps(
|
||||||
|
{
|
||||||
|
"a": {
|
||||||
|
"id": "a",
|
||||||
|
"fields": {"t": ["x", "2026-01-01T00:00:00.000Z-0000-a"]},
|
||||||
|
"deleted": False,
|
||||||
|
"deleted_hlc": None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_state_dir_caches_key_and_copy_across_processes(
|
||||||
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||||
|
) -> None:
|
||||||
|
remote = FakeStore({"app/devices/pc/log.json": LOG, "app/revs/pc": "r1"})
|
||||||
|
derives: list[int] = []
|
||||||
|
real = key_mod.unlock_e2e
|
||||||
|
|
||||||
|
def counting(
|
||||||
|
remote: RemoteStore,
|
||||||
|
*,
|
||||||
|
path_prefix: str,
|
||||||
|
passphrase: str,
|
||||||
|
params: Argon2Params | None = None,
|
||||||
|
e2e_path: str | None = None,
|
||||||
|
) -> E2eKey:
|
||||||
|
derives.append(1)
|
||||||
|
return real(
|
||||||
|
remote,
|
||||||
|
path_prefix=path_prefix,
|
||||||
|
passphrase=passphrase,
|
||||||
|
params=params,
|
||||||
|
e2e_path=e2e_path,
|
||||||
|
)
|
||||||
|
|
||||||
|
monkeypatch.setattr(store_mod, "unlock_e2e", counting)
|
||||||
|
for _ in range(3):
|
||||||
|
store_mod.reset_e2e_copy_memo() # a fresh process each tick
|
||||||
|
remote.writes.clear()
|
||||||
|
open_encrypted_client(
|
||||||
|
remote, "app/devices", passphrase=PHRASE, params=CHEAP, state_dir=tmp_path
|
||||||
|
)
|
||||||
|
assert derives == [1]
|
||||||
|
assert remote.writes == []
|
||||||
|
key_files = list(tmp_path.glob("e2e-*.key"))
|
||||||
|
assert len(key_files) == 1
|
||||||
|
assert key_files[0].stat().st_mode & 0o777 == 0o600
|
||||||
|
|
||||||
|
|
||||||
|
def test_stale_cached_key_rederives(tmp_path: Path) -> None:
|
||||||
|
remote = FakeStore()
|
||||||
|
open_encrypted_client(
|
||||||
|
remote, "app/devices", passphrase=PHRASE, params=CHEAP, state_dir=tmp_path
|
||||||
|
)
|
||||||
|
for key_file in tmp_path.glob("e2e-*.key"):
|
||||||
|
key_file.write_text("00" * 32, encoding="utf-8")
|
||||||
|
store, _ = open_encrypted_client(
|
||||||
|
remote, "app/devices", passphrase=PHRASE, params=CHEAP, state_dir=tmp_path
|
||||||
|
)
|
||||||
|
assert next(tmp_path.glob("e2e-*.key")).read_text(encoding="utf-8") == (
|
||||||
|
store.key.master.hex()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_garbage_cached_key_is_ignored_and_wrong_passphrase_locks(
|
||||||
|
tmp_path: Path,
|
||||||
|
) -> None:
|
||||||
|
remote = FakeStore()
|
||||||
|
open_encrypted_client(
|
||||||
|
remote, "app/devices", passphrase=PHRASE, params=CHEAP, state_dir=tmp_path
|
||||||
|
)
|
||||||
|
for key_file in tmp_path.glob("e2e-*.key"):
|
||||||
|
key_file.write_text("not hex", encoding="utf-8")
|
||||||
|
with pytest.raises(E2eLockedError):
|
||||||
|
open_encrypted_client(
|
||||||
|
remote, "app/devices", passphrase=WRONG, params=CHEAP, state_dir=tmp_path
|
||||||
|
)
|
||||||
@ -24,7 +24,9 @@ from crdt_sync._e2e_store import _blobs, _fingerprint
|
|||||||
from crdt_sync.tests.conftest import FakeStore, FakeStoreWithoutBulkRead
|
from crdt_sync.tests.conftest import FakeStore, FakeStoreWithoutBulkRead
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from crdt_sync import E2eKey
|
from collections.abc import Callable
|
||||||
|
|
||||||
|
from crdt_sync import E2eKey, Hlc
|
||||||
|
|
||||||
PHRASE = "pw"
|
PHRASE = "pw"
|
||||||
CHEAP = Argon2Params(8, 1, 1)
|
CHEAP = Argon2Params(8, 1, 1)
|
||||||
@ -163,3 +165,53 @@ def test_unreadable_header_locks_the_client() -> None:
|
|||||||
remote = FakeStore({LAYOUT.e2e_path: "not json"})
|
remote = FakeStore({LAYOUT.e2e_path: "not json"})
|
||||||
with pytest.raises(E2eLockedError, match="not JSON"):
|
with pytest.raises(E2eLockedError, match="not JSON"):
|
||||||
open_encrypted_client(remote, OLD, passphrase=PHRASE, params=CHEAP)
|
open_encrypted_client(remote, OLD, passphrase=PHRASE, params=CHEAP)
|
||||||
|
|
||||||
|
|
||||||
|
def test_copy_never_overwrites_non_log_and_skips_empty() -> None:
|
||||||
|
layout = E2eLayout.of("app/devices")
|
||||||
|
remote = FakeStore(
|
||||||
|
{
|
||||||
|
"app/devices/pc/current.json": "{}",
|
||||||
|
"app/devices-e2e/pc/current.json": '{"photos":["a"]}',
|
||||||
|
"app/devices/tab/current.json": "{}",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
key = unlock_e2e(
|
||||||
|
remote,
|
||||||
|
path_prefix=layout.prefix,
|
||||||
|
passphrase="x" * 8,
|
||||||
|
params=Argon2Params(8, 1, 1),
|
||||||
|
e2e_path=layout.e2e_path,
|
||||||
|
)
|
||||||
|
assert copy_to_encrypted(remote, key, "app/devices", layout) == 0
|
||||||
|
assert remote.files["app/devices-e2e/pc/current.json"] == '{"photos":["a"]}'
|
||||||
|
assert "app/devices-e2e/tab/current.json" not in remote.files
|
||||||
|
|
||||||
|
|
||||||
|
def test_copy_leaves_a_non_log_destination_alone(
|
||||||
|
make_hlc: Callable[..., Hlc],
|
||||||
|
) -> None:
|
||||||
|
layout = E2eLayout.of("app/devices")
|
||||||
|
log = {
|
||||||
|
"n": {
|
||||||
|
"id": "n",
|
||||||
|
"fields": {"t": ["x", make_hlc(1).to_str()]},
|
||||||
|
"deleted": False,
|
||||||
|
"deleted_hlc": None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
remote = FakeStore(
|
||||||
|
{
|
||||||
|
"app/devices/pc/current.json": json.dumps(log),
|
||||||
|
"app/devices-e2e/pc/current.json": '{"photos":["a"]}',
|
||||||
|
}
|
||||||
|
)
|
||||||
|
key = unlock_e2e(
|
||||||
|
remote,
|
||||||
|
path_prefix=layout.prefix,
|
||||||
|
passphrase="x" * 8,
|
||||||
|
params=Argon2Params(8, 1, 1),
|
||||||
|
e2e_path=layout.e2e_path,
|
||||||
|
)
|
||||||
|
assert copy_to_encrypted(remote, key, "app/devices", layout) == 0
|
||||||
|
assert remote.files["app/devices-e2e/pc/current.json"] == '{"photos":["a"]}'
|
||||||
|
|||||||
62
crdt-sync/crdt_sync/tests/test_tool_e2e_audit.py
Normal file
62
crdt-sync/crdt_sync/tests/test_tool_e2e_audit.py
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
# Copyright (c) 2026 Krzysztof Rudnicki
|
||||||
|
"""The RTDB plaintext audit behind the E2E done check."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import io
|
||||||
|
import json
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from crdt_sync import E2eCipher, seal_log_text
|
||||||
|
from tool import e2e_audit
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
LOG = json.dumps(
|
||||||
|
{
|
||||||
|
"n": {
|
||||||
|
"id": "n",
|
||||||
|
"fields": {"title": ["x", "2026-01-01T00:00:00.000Z-0000-a"]},
|
||||||
|
"deleted": False,
|
||||||
|
"deleted_hlc": None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _sealed() -> str:
|
||||||
|
return seal_log_text(LOG, E2eCipher(bytes(32)), path="a/devices-e2e/p/log.json")
|
||||||
|
|
||||||
|
|
||||||
|
def test_sealed_tree_is_clean() -> None:
|
||||||
|
tree = {
|
||||||
|
"devices-e2e": {"p": {"log~2Ejson": _sealed()}},
|
||||||
|
"revs-e2e": {"p": "ab" * 32},
|
||||||
|
"e2e": '{"v":1,"kdf":"argon2id"}',
|
||||||
|
}
|
||||||
|
assert e2e_audit.plaintext_paths(tree) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_plaintext_leaf_is_reported_with_decoded_key() -> None:
|
||||||
|
tree = {"devices": {"p": {"log~2Ejson": LOG}}, "settings": {"k": '{"a":1}'}}
|
||||||
|
assert e2e_audit.plaintext_paths(tree) == [
|
||||||
|
"devices/p/log.json",
|
||||||
|
"settings/k",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_raw_json_scalars_count_as_plaintext() -> None:
|
||||||
|
assert e2e_audit.plaintext_paths({"a": 5, "b": True, "c": None}) == ["a", "b"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_main_reads_stdin_and_sets_exit_code(
|
||||||
|
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
||||||
|
) -> None:
|
||||||
|
monkeypatch.setattr("sys.stdin", io.StringIO(json.dumps({"x": LOG})))
|
||||||
|
assert e2e_audit.main(["todo-sync"]) == 1
|
||||||
|
assert "todo-sync/x" in capsys.readouterr().out
|
||||||
|
monkeypatch.setattr("sys.stdin", io.StringIO("null"))
|
||||||
|
assert e2e_audit.main(["todo-sync"]) == 0
|
||||||
|
monkeypatch.setattr("sys.stdin", io.StringIO(json.dumps({"x": _sealed()})))
|
||||||
|
assert e2e_audit.main([]) == 0
|
||||||
@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "crdt-sync"
|
name = "crdt-sync"
|
||||||
version = "0.10.0"
|
version = "0.11.0"
|
||||||
description = "Shared CRDT merge scheme + GitHub-Contents-API sync transport for cross-device apps"
|
description = "Shared CRDT merge scheme + GitHub-Contents-API sync transport for cross-device apps"
|
||||||
requires-python = ">=3.14"
|
requires-python = ">=3.14"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
|||||||
63
crdt-sync/tool/e2e_audit.py
Normal file
63
crdt-sync/tool/e2e_audit.py
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
# Copyright (c) 2026 Krzysztof Rudnicki
|
||||||
|
"""Read-only E2E done check: list plaintext leaves in an RTDB JSON dump.
|
||||||
|
|
||||||
|
Feed it ``firebase database:get /<root>`` on stdin::
|
||||||
|
|
||||||
|
firebase database:get /todo-sync | python3 -m tool.e2e_audit todo-sync
|
||||||
|
|
||||||
|
Exit 0 when every leaf is sealed (or is the clear Argon2id header or a keyed
|
||||||
|
revision), 1 when any leaf is plaintext. A plain ``grep '"title"'`` misses
|
||||||
|
plaintext because RTDB serves each blob as an escaped JSON string; this
|
||||||
|
decodes the tree instead.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from crdt_sync import is_e2e_envelope
|
||||||
|
|
||||||
|
_HEX64 = re.compile(r"[0-9a-f]{64}")
|
||||||
|
|
||||||
|
|
||||||
|
def _decode_key(key: str) -> str:
|
||||||
|
return re.sub(r"~([0-9A-F]{2})", lambda m: chr(int(m.group(1), 16)), key)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_clear_leaf(value: str) -> bool:
|
||||||
|
# Envelopes and the Argon2id header both carry an integer "v"; keyed
|
||||||
|
# revisions are 64 hex chars; a bare E2eCipher blob is base64, not JSON.
|
||||||
|
if is_e2e_envelope(value) or _HEX64.fullmatch(value):
|
||||||
|
return True
|
||||||
|
return not value.lstrip().startswith(("{", "["))
|
||||||
|
|
||||||
|
|
||||||
|
def plaintext_paths(tree: object, prefix: str = "") -> list[str]:
|
||||||
|
"""Return the decoded paths of every plaintext leaf under ``tree``."""
|
||||||
|
found: list[str] = []
|
||||||
|
if isinstance(tree, dict):
|
||||||
|
for key, value in tree.items():
|
||||||
|
path = f"{prefix}/{_decode_key(key)}" if prefix else _decode_key(key)
|
||||||
|
found.extend(plaintext_paths(value, path))
|
||||||
|
elif isinstance(tree, str):
|
||||||
|
if not _is_clear_leaf(tree):
|
||||||
|
found.append(prefix)
|
||||||
|
elif tree is not None:
|
||||||
|
found.append(prefix)
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str]) -> int:
|
||||||
|
"""Audit the dump on stdin; ``argv[0]`` names the root for the report."""
|
||||||
|
root = argv[0] if argv else ""
|
||||||
|
paths = plaintext_paths(json.load(sys.stdin), root)
|
||||||
|
sys.stdout.write(f"plaintext leaves under /{root}: {len(paths)}\n")
|
||||||
|
for path in paths:
|
||||||
|
sys.stdout.write(f" {path}\n")
|
||||||
|
return 1 if paths else 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main(sys.argv[1:]))
|
||||||
@ -155,9 +155,12 @@ Future<MigrationReport> copyToEncrypted({
|
|||||||
skipped.add(rel);
|
skipped.add(rel);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (source.isEmpty) continue;
|
||||||
final path = '${layout.prefix}/$rel';
|
final path = '${layout.prefix}/$rel';
|
||||||
final existing = await dest.getFileText(path);
|
final existing = await dest.getFileText(path);
|
||||||
final current = existing == null ? null : _parse(existing);
|
final current = existing == null ? null : _parse(existing);
|
||||||
|
// Not a sync log (an app's own blob): never overwrite it.
|
||||||
|
if (existing != null && current == null) continue;
|
||||||
final merged = mergeLogs(current ?? <String, Record>{}, source);
|
final merged = mergeLogs(current ?? <String, Record>{}, source);
|
||||||
if (current != null && _signature(merged) == _signature(current)) continue;
|
if (current != null && _signature(merged) == _signature(current)) continue;
|
||||||
await dest.putFileText(path, logToJson(merged), message: 'e2e copy');
|
await dest.putFileText(path, logToJson(merged), message: 'e2e copy');
|
||||||
|
|||||||
@ -2,7 +2,7 @@ name: crdt_sync
|
|||||||
description: >-
|
description: >-
|
||||||
Shared CRDT merge scheme + GitHub-Contents-API sync transport for
|
Shared CRDT merge scheme + GitHub-Contents-API sync transport for
|
||||||
cross-device apps. Pure Dart, no Flutter dependency.
|
cross-device apps. Pure Dart, no Flutter dependency.
|
||||||
version: 0.14.0
|
version: 0.14.1
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
|
|||||||
@ -200,4 +200,22 @@ void main() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('never overwrites a non-log blob and skips empty sources', () async {
|
||||||
|
final remote = MemoryRemote({
|
||||||
|
'app/notes/pc/current.json': '{}',
|
||||||
|
'app/notes-e2e/pc/current.json': '{"photos":["a"]}',
|
||||||
|
'app/notes/tab/current.json': '{}',
|
||||||
|
});
|
||||||
|
final key = await _key(remote, layout);
|
||||||
|
final report = await copyToEncrypted(
|
||||||
|
store: remote,
|
||||||
|
key: key,
|
||||||
|
fromPrefix: _old,
|
||||||
|
layout: layout,
|
||||||
|
);
|
||||||
|
expect(report.copied, 0);
|
||||||
|
expect(remote.files['app/notes-e2e/pc/current.json'], '{"photos":["a"]}');
|
||||||
|
expect(remote.files.containsKey('app/notes-e2e/tab/current.json'), isFalse);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -112,7 +112,7 @@ repos:
|
|||||||
- pytest
|
- pytest
|
||||||
# Kept in step with pyproject.toml, so pylint resolves the crdt_sync
|
# Kept in step with pyproject.toml, so pylint resolves the crdt_sync
|
||||||
# imports instead of scoring every one of them as import-error.
|
# imports instead of scoring every one of them as import-error.
|
||||||
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync
|
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync
|
||||||
|
|
||||||
- repo: https://github.com/PyCQA/bandit
|
- repo: https://github.com/PyCQA/bandit
|
||||||
rev: 1.9.4
|
rev: 1.9.4
|
||||||
|
|||||||
@ -112,7 +112,7 @@ repos:
|
|||||||
- pytest
|
- pytest
|
||||||
# Kept in step with pyproject.toml, so pylint resolves the crdt_sync
|
# Kept in step with pyproject.toml, so pylint resolves the crdt_sync
|
||||||
# imports instead of scoring every one of them as import-error.
|
# imports instead of scoring every one of them as import-error.
|
||||||
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync
|
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync
|
||||||
|
|
||||||
- repo: https://github.com/PyCQA/bandit
|
- repo: https://github.com/PyCQA/bandit
|
||||||
rev: 1.9.4
|
rev: 1.9.4
|
||||||
|
|||||||
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "freedays"
|
name = "freedays"
|
||||||
version = "0.4.0"
|
version = "0.4.2"
|
||||||
description = "Shared pre-declared free-day pool: one global list of days on which every gate app stands down"
|
description = "Shared pre-declared free-day pool: one global list of days on which every gate app stands down"
|
||||||
requires-python = ">=3.14"
|
requires-python = ">=3.14"
|
||||||
# Deliberately NOT gatelock. Its __init__ imports tkinter for the lock-window
|
# Deliberately NOT gatelock. Its __init__ imports tkinter for the lock-window
|
||||||
@ -12,7 +12,7 @@ requires-python = ">=3.14"
|
|||||||
# on a GUI toolkit -- `import freedays` then fails anywhere without Tk. The
|
# on a GUI toolkit -- `import freedays` then fails anywhere without Tk. The
|
||||||
# ~15 lines of HMAC that were wanted live in freedays/_hmac.py instead.
|
# ~15 lines of HMAC that were wanted live in freedays/_hmac.py instead.
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync",
|
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
|
|||||||
@ -112,7 +112,7 @@ repos:
|
|||||||
- pytest
|
- pytest
|
||||||
# Kept in step with pyproject.toml, so pylint resolves the crdt_sync
|
# Kept in step with pyproject.toml, so pylint resolves the crdt_sync
|
||||||
# imports instead of scoring every one of them as import-error.
|
# imports instead of scoring every one of them as import-error.
|
||||||
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync
|
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync
|
||||||
|
|
||||||
- repo: https://github.com/PyCQA/bandit
|
- repo: https://github.com/PyCQA/bandit
|
||||||
rev: 1.9.4
|
rev: 1.9.4
|
||||||
|
|||||||
@ -125,7 +125,7 @@ repos:
|
|||||||
- pytest
|
- pytest
|
||||||
- requests
|
- requests
|
||||||
- genanki
|
- genanki
|
||||||
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync
|
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync
|
||||||
|
|
||||||
- repo: https://github.com/PyCQA/bandit
|
- repo: https://github.com/PyCQA/bandit
|
||||||
rev: 1.9.4
|
rev: 1.9.4
|
||||||
|
|||||||
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "leetcode-coach"
|
name = "leetcode-coach"
|
||||||
version = "0.1.1"
|
version = "0.1.3"
|
||||||
description = "The /leetcode coach as a library: phase gate, leak and stop gates, session log, grading, cards, relay"
|
description = "The /leetcode coach as a library: phase gate, leak and stop gates, session log, grading, cards, relay"
|
||||||
requires-python = ">=3.14"
|
requires-python = ">=3.14"
|
||||||
# Everything below loads lazily (rtdb, make_cards, relay.__main__): importing
|
# Everything below loads lazily (rtdb, make_cards, relay.__main__): importing
|
||||||
@ -12,7 +12,7 @@ requires-python = ">=3.14"
|
|||||||
# leetcode_guard is NOT a dependency (guard_bridge imports it on demand, so the
|
# leetcode_guard is NOT a dependency (guard_bridge imports it on demand, so the
|
||||||
# package never pulls the gate's ledger code into a tutor server).
|
# package never pulls the gate's ledger code into a tutor server).
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync",
|
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync",
|
||||||
"genanki==0.13.1",
|
"genanki==0.13.1",
|
||||||
"requests==2.34.2",
|
"requests==2.34.2",
|
||||||
]
|
]
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
bandit[toml]==1.9.4
|
bandit[toml]==1.9.4
|
||||||
codespell==2.4.3
|
codespell==2.4.3
|
||||||
coverage==7.17.2
|
coverage==7.17.2
|
||||||
crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync
|
crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync
|
||||||
genanki==0.13.1
|
genanki==0.13.1
|
||||||
mypy==2.4.1
|
mypy==2.4.1
|
||||||
pre-commit==4.7.0
|
pre-commit==4.7.0
|
||||||
|
|||||||
@ -112,7 +112,7 @@ repos:
|
|||||||
- pytest
|
- pytest
|
||||||
# Kept in step with pyproject.toml, so pylint resolves the crdt_sync
|
# Kept in step with pyproject.toml, so pylint resolves the crdt_sync
|
||||||
# imports instead of scoring every one of them as import-error.
|
# imports instead of scoring every one of them as import-error.
|
||||||
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync
|
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync
|
||||||
|
|
||||||
- repo: https://github.com/PyCQA/bandit
|
- repo: https://github.com/PyCQA/bandit
|
||||||
rev: 1.9.4
|
rev: 1.9.4
|
||||||
|
|||||||
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "sickdays"
|
name = "sickdays"
|
||||||
version = "0.5.0"
|
version = "0.5.3"
|
||||||
description = "Shared sick-day pool: per-date, uncapped, synced; every gate respects it"
|
description = "Shared sick-day pool: per-date, uncapped, synced; every gate respects it"
|
||||||
requires-python = ">=3.14"
|
requires-python = ">=3.14"
|
||||||
# Deliberately NOT gatelock (its __init__ imports tkinter); the HMAC lives in
|
# Deliberately NOT gatelock (its __init__ imports tkinter); the HMAC lives in
|
||||||
@ -12,8 +12,8 @@ requires-python = ">=3.14"
|
|||||||
# cross-pool check only, and is imported lazily by name (sickdays/_conflict.py)
|
# cross-pool check only, and is imported lazily by name (sickdays/_conflict.py)
|
||||||
# so the two packages can never form an import cycle.
|
# so the two packages can never form an import cycle.
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync",
|
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync",
|
||||||
"freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.0#subdirectory=freedays",
|
"freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.2#subdirectory=freedays",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user