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

0 downloads

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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
notarize.py · 6.9 KB · 175 lines
1"""action_notarize -- lodge a digest, get back the moment it was first seen.
2
3POST {digest, algorithm?, label?, source?}
4
5The one write in this package, and the only object anywhere that may
6append to `notarizations`.
7
8## Resubmitting is a lookup, not an error
9
10If the digest is already on the log, this returns THE EXISTING ROW with
11its original timestamp and writes nothing. Not a 409, not a duplicate,
12not a fresh stamp -- the same receipt the first submitter got.
13
14That is the opposite of the usual reflex, and it is the load-bearing rule
15of the whole service. A notarization claims "this existed BY then". A
16later submission cannot make a thing earlier, so accepting one and
17recording a new time would let anybody discard the only fact the log
18carries by simply submitting again. Everywhere else in this building
19idempotency exists to avoid a duplicate row (docs/logic-decisions.md #7);
20here it exists to stop a timestamp being walked forward, which is the
21only direction an attacker would ever want to walk it.
22
23## What it refuses, and why each refusal is the product
24
25**Content.** There is no field for it and no code path that would accept
26it. A submitter hashes their own data and sends the digest. This is not a
27limitation to be relaxed later: it is why notarizing something
28confidential is safe, and why this collection is not worth stealing.
29
30**Weak algorithms.** sha256 and sha512 only. A digest somebody can find a
31collision for lets them notarize one document today and produce a
32different one with the same hash tomorrow, which is precisely the attack
33a notary exists to make hard. md5 and sha1 are refused rather than
34unsupported, and the refusal says which.
35
36**Anonymous submission, unless the operator switched it on.** Default
37closed. `notary.public_submission = true` in app_settings opens it, which
38is what a notary run as a service for other people needs and what a notary
39run for one business's own ledger heads does not. The refusal names the
40setting, in the shape every capability boundary here uses: absent by
41default, a 409 saying exactly what to configure, never a stub that
42pretends to have worked.
43
44## What this object does NOT do
45
46**It does not sign the receipt.** A signed receipt would let a submitter
47prove what this server said even if this server later denied it. That is
48a real strengthening; it needs a key, a rotation story and a published
49public key, and it is in plan/notary-spec.md rather than here. Today the
50answer is weaker but not nothing: the record is PUBLIC, so anyone can
51check it at any moment, and a row that vanished would be visibly gone.
52
53**It does not verify anything about the data.** It cannot. Every surface
54renders object_notary.attestation() next to the answer so nobody reads
55"notarized" as "checked", "owned" or "approved" -- overstating what this
56means is the only way a service this simple can do harm.
57"""
58
59import os
60
61import object_notary
62import object_records
63
64ACTOR = "action_notarize"
65COLLECTION = "notarizations"
66SETTINGS_COLLECTION = "app_settings"
67PUBLIC_SUBMISSION_KEY = "notary.public_submission"
68
69TRUE_VALUES = {"1", "true", "yes", "on"}
70
71
72def _base_dir():
73 return os.environ.get("DBBASIC_DATA_DIR", "data")
74
75
76def _text(value):
77 return str(value if value is not None else "").strip()
78
79
80def _public_submission_allowed(base):
81 """Whether a caller with no identity may lodge a digest.
82
83 Read fresh from app_settings rather than cached, like every other
84 setting read on this box (docs/logic-decisions.md #4). A missing or
85 unreadable settings collection means NOT allowed, which is the safe
86 direction: the failure mode of guessing wrong the other way is an
87 open write endpoint on a public server.
88 """
89 try:
90 rows = object_records.read_collection_records(
91 SETTINGS_COLLECTION, base_dir=base)
92 except Exception:
93 return False
94 for row in rows:
95 if _text(row.get("key")) == PUBLIC_SUBMISSION_KEY:
96 return _text(row.get("value")).lower() in TRUE_VALUES
97 return False
98
99
100def POST(request):
101 request = request or {}
102 base = _base_dir()
103
104 identity = request.get("_identity") or {}
105 user_id = _text(identity.get("user_id"))
106
107 if not user_id and not _public_submission_allowed(base):
108 return {
109 "status": 409,
110 "error": (
111 "This notary does not accept anonymous submissions. Sign in, "
112 f"use a service key, or set {PUBLIC_SUBMISSION_KEY} to true "
113 "in app_settings to run it as a service for other people."),
114 "setting": PUBLIC_SUBMISSION_KEY,
115 }
116
117 algorithm = (object_notary.normalize_algorithm(request.get("algorithm"))
118 or _text(request.get("algorithm"))
119 or object_notary.DEFAULT_ALGORITHM)
120 found = object_notary.problems(request.get("digest"), algorithm)
121 if found:
122 return {"status": 400, "error": " ".join(found), "problems": found}
123
124 digest = object_notary.normalize_digest(request.get("digest"), algorithm)
125 algorithm = object_notary.normalize_algorithm(algorithm)
126
127 try:
128 rows = object_records.read_collection_records(COLLECTION, base_dir=base)
129 except Exception:
130 # The log cannot be read, so a first-seen row cannot be found, so a
131 # new row would be a SECOND stamp on a digest that may already have
132 # an earlier one -- exactly the thing this object exists to prevent.
133 # Refusing is the only safe direction: a submission refused is
134 # retried, a timestamp silently reset is gone.
135 return {
136 "status": 409,
137 "error": ("The notarization log cannot be read, so this digest "
138 "cannot be lodged without risking a second, later "
139 "stamp on something already recorded."),
140 }
141
142 existing = object_notary.first_seen(digest, rows, algorithm)
143 if existing is not None:
144 receipt = object_notary.receipt(existing)
145 receipt["already_recorded"] = True
146 receipt["note"] = (
147 "Already on the log. This is the ORIGINAL record and its original "
148 "time -- resubmitting cannot move a timestamp forward, because "
149 "the claim being made is that the data existed by then.")
150 return receipt
151
152 label = object_notary.normalize_label(request.get("label"))
153 source = object_notary.normalize_label(request.get("source"))
154
155 try:
156 stored = object_records.create_collection_record(
157 COLLECTION,
158 {
159 "digest": digest,
160 "algorithm": algorithm,
161 "label": label,
162 "submitter": user_id or "public",
163 "source": source or ACTOR,
164 },
165 base_dir=base,
166 actor=user_id or ACTOR,
167 )
168 except Exception as error:
169 return {"status": 500,
170 "error": f"The digest could not be recorded: {error}"}
171
172 receipt = object_notary.receipt(stored)
173 receipt["already_recorded"] = False
174 receipt["check_url"] = f"/notary/{digest}"
175 return receipt