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

9 downloads

Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 49 minutes ago History
notarize.py · 7.2 KB · 178 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 # access: box configuration
91 rows = object_records.read_collection_records(
92 SETTINGS_COLLECTION, base_dir=base)
93 except Exception:
94 return False
95 for row in rows:
96 if _text(row.get("key")) == PUBLIC_SUBMISSION_KEY:
97 return _text(row.get("value")).lower() in TRUE_VALUES
98 return False
99
100
101def POST(request):
102 request = request or {}
103 base = _base_dir()
104
105 identity = request.get("_identity") or {}
106 user_id = _text(identity.get("user_id"))
107
108 if not user_id and not _public_submission_allowed(base):
109 return {
110 "status": 409,
111 "error": (
112 "This notary does not accept anonymous submissions. Sign in, "
113 f"use a service key, or set {PUBLIC_SUBMISSION_KEY} to true "
114 "in app_settings to run it as a service for other people."),
115 "setting": PUBLIC_SUBMISSION_KEY,
116 }
117
118 algorithm = (object_notary.normalize_algorithm(request.get("algorithm"))
119 or _text(request.get("algorithm"))
120 or object_notary.DEFAULT_ALGORITHM)
121 found = object_notary.problems(request.get("digest"), algorithm)
122 if found:
123 return {"status": 400, "error": " ".join(found), "problems": found}
124
125 digest = object_notary.normalize_digest(request.get("digest"), algorithm)
126 algorithm = object_notary.normalize_algorithm(algorithm)
127
128 try:
129 # access: open by design: an append-only digest ledger; the submission gate is in this object (the action_notarize grant reason)
130 rows = object_records.read_collection_records(COLLECTION, base_dir=base)
131 except Exception:
132 # The log cannot be read, so a first-seen row cannot be found, so a
133 # new row would be a SECOND stamp on a digest that may already have
134 # an earlier one -- exactly the thing this object exists to prevent.
135 # Refusing is the only safe direction: a submission refused is
136 # retried, a timestamp silently reset is gone.
137 return {
138 "status": 409,
139 "error": ("The notarization log cannot be read, so this digest "
140 "cannot be lodged without risking a second, later "
141 "stamp on something already recorded."),
142 }
143
144 existing = object_notary.first_seen(digest, rows, algorithm)
145 if existing is not None:
146 receipt = object_notary.receipt(existing)
147 receipt["already_recorded"] = True
148 receipt["note"] = (
149 "Already on the log. This is the ORIGINAL record and its original "
150 "time -- resubmitting cannot move a timestamp forward, because "
151 "the claim being made is that the data existed by then.")
152 return receipt
153
154 label = object_notary.normalize_label(request.get("label"))
155 source = object_notary.normalize_label(request.get("source"))
156
157 try:
158 # access: open by design: an append-only digest ledger; the submission gate is in this object (the action_notarize grant reason)
159 stored = object_records.create_collection_record(
160 COLLECTION,
161 {
162 "digest": digest,
163 "algorithm": algorithm,
164 "label": label,
165 "submitter": user_id or "public",
166 "source": source or ACTOR,
167 },
168 base_dir=base,
169 actor=user_id or ACTOR,
170 )
171 except Exception as error:
172 return {"status": 500,
173 "error": f"The digest could not be recorded: {error}"}
174
175 receipt = object_notary.receipt(stored)
176 receipt["already_recorded"] = False
177 receipt["check_url"] = f"/notary/{digest}"
178 return receipt