CI & Build / Python lint (push) Successful in 3s
CI & Build / TypeScript typecheck (push) Successful in 8s
CI & Build / Python tests (push) Successful in 14s
Desktop (Tauri) / Tauri desktop (Linux) (push) Failing after 42s
CI & Build / Build & push image (push) Successful in 36s
Desktop (Tauri) / Windows installer (cross-compiled) (push) Successful in 1m34s
Version the sync WIRE PROTOCOL separately from either program's release
version, so a self-hosted server and the desktop app can sit on different
releases and still work out whether they can talk.
Each side declares two numbers — what it speaks, and the oldest counterpart
it accepts. Either side can therefore mark a change breaking without the
other shipping in step, which is the whole point: no app↔server lockstep.
Server advertises on the existing public /api/config (a client must be able
to ask "can I talk to you?" before it holds a device token, or even has an
account): sync_protocol_version, min_client_protocol_version, sync_features.
sync_features exists because a version number can only say newer/older. An
ADDITIVE change earns a capability name instead of a minimum bump, so a
newer client meeting an older server drops that one feature and syncs the
rest, rather than refusing. Raising a minimum is reserved for genuinely
breaking changes — it's the switch that hard-blocks the other side.
Client half is pure decision logic (sync/compat.rs), no I/O, so every branch
is unit-testable — there's no live-server lane in CI. Three outcomes: ok /
degraded{unavailable} / incompatible{reason, client_must_update}. The last
names which side can fix it, so the message is actionable. A server that
predates the handshake sends no protocol fields at all; that reads as
"update the server", deliberately not as a parse error, which would look to
the user like they mistyped the URL.
normalize_base_url defaults a bare host to https://, never http:// —
silently downgrading would put a long-lived device token on the wire in
cleartext because someone omitted five characters. Plain HTTP on a trusted
LAN stays supported; the user types http:// and thereby chooses it.
Transport (the actual fetch) lands next, separately: it needs an HTTP/TLS
stack, and that's a real risk to the Windows cross-compile lane, so it gets
its own CI run to bisect against rather than riding along with this.
No UI here by design — the link/settings surface it feeds is M10.7's, per
this task's own sequencing.
Policy documented in docs/sync.md.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SreJkbxB4gx8pPsu8QbLPi
114 lines
5.0 KiB
Python
114 lines
5.0 KiB
Python
from __future__ import annotations
|
|
|
|
import mimetypes
|
|
import os
|
|
import secrets
|
|
from datetime import timedelta
|
|
|
|
from quart import Quart, has_request_context, jsonify, request, send_from_directory
|
|
from quart.sessions import SecureCookieSessionInterface
|
|
|
|
from . import __version__
|
|
from .auth import bp as auth_bp
|
|
from .config import Config
|
|
from .db import session_scope
|
|
from .graph import bp as graph_bp
|
|
from .labels import bp as labels_bp
|
|
from .notes import bp as notes_bp
|
|
from .saved_filters import bp as saved_filters_bp
|
|
from .settings import get_public_config, get_setting, load_or_create_secret_key
|
|
from .settings_api import bp as settings_bp
|
|
from .sync import bp as sync_bp, protocol_advertisement
|
|
|
|
STATIC_DIR = os.path.join(os.path.dirname(__file__), "static")
|
|
|
|
# `.webmanifest` isn't in every base image's mime map; register it so the PWA
|
|
# manifest is served as application/manifest+json instead of octet-stream.
|
|
mimetypes.add_type("application/manifest+json", ".webmanifest")
|
|
|
|
|
|
class _AutoSecureSessionInterface(SecureCookieSessionInterface):
|
|
"""Mark the session cookie `Secure` whenever the request arrived over HTTPS —
|
|
directly, or via a TLS-terminating reverse proxy that sets X-Forwarded-Proto.
|
|
|
|
Auto-detecting per request (rather than a fixed SESSION_COOKIE_SECURE flag)
|
|
hardens the cookie on HTTPS deployments without breaking a plain-HTTP install on
|
|
a trusted LAN, where a hard-forced Secure flag would stop the browser from ever
|
|
sending the cookie back — i.e. silently break login. No configuration required.
|
|
"""
|
|
|
|
def get_cookie_secure(self, app: Quart) -> bool:
|
|
if not has_request_context():
|
|
return False
|
|
forwarded = request.headers.get("X-Forwarded-Proto", "").split(",")[0].strip().lower()
|
|
return forwarded == "https" or request.is_secure
|
|
|
|
|
|
def create_app() -> Quart:
|
|
# static_folder=None: the SPA catch-all below owns static serving.
|
|
app = Quart(__name__, static_folder=None)
|
|
# Ephemeral/env secret so the app (and DB-free unit tests) construct without a
|
|
# database. before_serving swaps in the real, DB-persisted key before serving.
|
|
app.secret_key = Config.secret_key_env() or secrets.token_urlsafe(48)
|
|
app.config["APP_VERSION"] = os.environ.get("APP_VERSION", __version__)
|
|
app.config["SESSION_COOKIE_HTTPONLY"] = True
|
|
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
|
|
# Auto-mark the session cookie Secure on HTTPS requests (see the interface above).
|
|
app.session_interface = _AutoSecureSessionInterface()
|
|
app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(days=30)
|
|
# Hard request-body ceiling (any-file attachments, import zips, sync push). The
|
|
# per-file attachment limit is the DB-backed `max_attachment_mb` setting, enforced
|
|
# in the upload handler; this must stay >= the largest value that allows.
|
|
app.config["MAX_CONTENT_LENGTH"] = 64 * 1024 * 1024
|
|
|
|
app.register_blueprint(auth_bp)
|
|
app.register_blueprint(notes_bp)
|
|
app.register_blueprint(labels_bp)
|
|
app.register_blueprint(graph_bp)
|
|
app.register_blueprint(settings_bp)
|
|
app.register_blueprint(sync_bp)
|
|
app.register_blueprint(saved_filters_bp)
|
|
|
|
@app.before_serving
|
|
async def _bootstrap() -> None:
|
|
# Load (or generate + persist) the real signing secret and the live session
|
|
# lifetime from the DB, before any request is served.
|
|
async with session_scope() as db:
|
|
app.secret_key = await load_or_create_secret_key(db)
|
|
try:
|
|
days = int(await get_setting(db, "session_ttl_days"))
|
|
app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(days=days)
|
|
except (ValueError, TypeError, KeyError):
|
|
pass
|
|
|
|
@app.get("/api/health")
|
|
async def health():
|
|
return jsonify({"status": "ok", "version": app.config["APP_VERSION"]})
|
|
|
|
@app.get("/api/config")
|
|
async def public_config():
|
|
# Public: the login/register screen reads site name + whether signups are open.
|
|
async with session_scope() as db:
|
|
data = await get_public_config(db)
|
|
data["version"] = app.config["APP_VERSION"]
|
|
# The sync-protocol handshake (M10.6). A native client reads this BEFORE
|
|
# linking — while it still has no token and possibly no account — to decide
|
|
# whether it can talk to this server, and which optional features to offer.
|
|
data.update(protocol_advertisement())
|
|
return jsonify(data)
|
|
|
|
@app.get("/", defaults={"path": ""})
|
|
@app.get("/<path:path>")
|
|
async def spa(path: str):
|
|
if path.startswith("api/"):
|
|
return jsonify({"error": "not found"}), 404
|
|
candidate = os.path.join(STATIC_DIR, path)
|
|
if path and os.path.isfile(candidate):
|
|
return await send_from_directory(STATIC_DIR, path)
|
|
index = os.path.join(STATIC_DIR, "index.html")
|
|
if os.path.isfile(index):
|
|
return await send_from_directory(STATIC_DIR, "index.html")
|
|
return jsonify({"error": "frontend not built"}), 404
|
|
|
|
return app
|