askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
notary.py · 13.2 KB · 351 lines
| 1 | """site_notary -- the public face of the notary. |
| 2 | |
| 3 | GET /notary what it is, and a box to check a digest |
| 4 | GET /notary/{digest} was this recorded here, and when |
| 5 | GET /notary/{digest}.json the same answer, machine-readable |
| 6 | |
| 7 | ## Why the check is public |
| 8 | |
| 9 | An attestation that only its submitter can look up is not an attestation, |
| 10 | it is a favour. The entire value of lodging a digest with an independent |
| 11 | party is that a third person -- a customer, an auditor, a court, the other |
| 12 | side of an argument -- can verify it without needing an account here, a |
| 13 | relationship with the operator, or anybody's permission. So this page |
| 14 | answers anybody, and it answers the same way every time. |
| 15 | |
| 16 | That is also why the answer for an unknown digest is a plain "not |
| 17 | recorded here" rather than a 404: a checker asking about a digest they |
| 18 | were given deserves a sentence explaining that absence proves nothing, |
| 19 | not a browser error page they will read as a fault. |
| 20 | |
| 21 | ## The file is hashed in the browser and never uploaded |
| 22 | |
| 23 | The page carries a drop box that computes a sha256 with SubtleCrypto, |
| 24 | locally, and fills the digest in. Nothing is sent. This is the one feature |
| 25 | here that is not strictly necessary, and it earns its place by making the |
| 26 | central claim visible rather than merely stated: a visitor can watch their |
| 27 | own file produce a digest without a request leaving the machine, and then |
| 28 | decide for themselves whether notarizing something confidential is safe. |
| 29 | A page that only ASSERTS "we never see your content" is asking to be |
| 30 | believed; one that demonstrates it is not. |
| 31 | |
| 32 | ## What it will not say |
| 33 | |
| 34 | Every answer renders object_notary.attestation() -- what the record |
| 35 | proves, what it does not, and what it rests on -- immediately beside the |
| 36 | result rather than in a footnote. The failure mode of a notary is not |
| 37 | technical. It is a reader taking "notarized" to mean verified, owned or |
| 38 | approved, and then relying on it. The wording lives in object_notary so |
| 39 | this page, the JSON and the submission receipt cannot drift into three |
| 40 | different strengths of the same promise. |
| 41 | """ |
| 42 | |
| 43 | import html |
| 44 | import json |
| 45 | import os |
| 46 | |
| 47 | import object_notary |
| 48 | import object_records |
| 49 | |
| 50 | COLLECTION = "notarizations" |
| 51 | SETTINGS_COLLECTION = "app_settings" |
| 52 | PUBLIC_SUBMISSION_KEY = "notary.public_submission" |
| 53 | |
| 54 | _STYLE = """ |
| 55 | .nt { max-width: 46rem; } |
| 56 | .nt h2 { font-size: 1.05rem; margin: 1.8rem 0 .4rem; } |
| 57 | .nt p, .nt li { line-height: 1.55; } |
| 58 | .nt .digest { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; |
| 59 | font-size: .82rem; word-break: break-all; } |
| 60 | .nt .card { border: 1px solid var(--line, #38384a); border-radius: 8px; |
| 61 | padding: .9rem 1.1rem; margin: 1rem 0; } |
| 62 | .nt .hit { border-color: var(--accent, #b5713a); } |
| 63 | .nt .when { font-size: 1.35rem; font-weight: 600; margin: .3rem 0 .1rem; } |
| 64 | .nt .note { border-left: 3px solid var(--line, #55556a); padding: .1rem 0 .1rem .7rem; |
| 65 | margin: .6rem 0 1.2rem; font-size: .88rem; opacity: .85; } |
| 66 | .nt .cols { display: grid; gap: 1rem; grid-template-columns: 1fr 1fr; } |
| 67 | @media (max-width: 40rem) { .nt .cols { grid-template-columns: 1fr; } } |
| 68 | .nt .cols h3 { font-size: .9rem; margin: 0 0 .4rem; text-transform: uppercase; |
| 69 | letter-spacing: .04em; opacity: .75; } |
| 70 | .nt .cols ul { margin: 0; padding-left: 1.1rem; font-size: .9rem; } |
| 71 | .nt input[type=text] { width: 100%; font-family: ui-monospace, monospace; |
| 72 | font-size: .85rem; padding: .5rem .6rem; box-sizing: border-box; } |
| 73 | .nt .drop { border: 1px dashed var(--line, #55556a); border-radius: 8px; |
| 74 | padding: 1.1rem; text-align: center; font-size: .9rem; margin: .6rem 0; } |
| 75 | .nt .drop.over { border-color: var(--accent, #b5713a); } |
| 76 | """ |
| 77 | |
| 78 | |
| 79 | def _esc(value): |
| 80 | return html.escape(str(value if value is not None else ""), quote=True) |
| 81 | |
| 82 | |
| 83 | def _base_dir(): |
| 84 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 85 | |
| 86 | |
| 87 | def _rows(base): |
| 88 | try: |
| 89 | return object_records.read_collection_records(COLLECTION, base_dir=base) |
| 90 | except Exception: |
| 91 | return [] |
| 92 | |
| 93 | |
| 94 | def _public_submission_allowed(base): |
| 95 | try: |
| 96 | rows = object_records.read_collection_records( |
| 97 | SETTINGS_COLLECTION, base_dir=base) |
| 98 | except Exception: |
| 99 | return False |
| 100 | for row in rows: |
| 101 | if str(row.get("key") or "").strip() == PUBLIC_SUBMISSION_KEY: |
| 102 | return str(row.get("value") or "").strip().lower() in { |
| 103 | "1", "true", "yes", "on"} |
| 104 | return False |
| 105 | |
| 106 | |
| 107 | def lookup(digest, *, base=None, algorithm=None): |
| 108 | """The answer, as a plain dict. One fold, three surfaces. |
| 109 | |
| 110 | Tries every supported algorithm when none is named, because a checker |
| 111 | handed a bare hex string knows what it is only by its length -- and |
| 112 | refusing to look because they did not say sha512 would be pedantry |
| 113 | standing between somebody and the fact they came for. |
| 114 | """ |
| 115 | base = _base_dir() if base is None else base |
| 116 | rows = _rows(base) |
| 117 | candidates = ([algorithm] if algorithm |
| 118 | else sorted(object_notary.ALGORITHMS)) |
| 119 | for name in candidates: |
| 120 | normalized = object_notary.normalize_digest(digest, name) |
| 121 | if not normalized: |
| 122 | continue |
| 123 | found = object_notary.first_seen(normalized, rows, name) |
| 124 | if found is not None: |
| 125 | return object_notary.receipt(found) |
| 126 | return object_notary.receipt(None, found=False) |
| 127 | |
| 128 | |
| 129 | # --- rendering --------------------------------------------------------------- |
| 130 | |
| 131 | def _attestation_html(attestation): |
| 132 | proves = "".join(f"<li>{_esc(line)}</li>" |
| 133 | for line in attestation.get("proves") or []) |
| 134 | denies = "".join(f"<li>{_esc(line)}</li>" |
| 135 | for line in attestation.get("does_not_prove") or []) |
| 136 | rests = "".join(f"<p class=\"note\">{_esc(line)}</p>" |
| 137 | for line in attestation.get("rests_on") or []) |
| 138 | proves_block = (f"<div><h3>What this proves</h3><ul>{proves}</ul></div>" |
| 139 | if proves else "") |
| 140 | return f""" |
| 141 | <div class="cols"> |
| 142 | {proves_block} |
| 143 | <div><h3>What it does not prove</h3><ul>{denies}</ul></div> |
| 144 | </div> |
| 145 | {rests} |
| 146 | """ |
| 147 | |
| 148 | |
| 149 | def _result_html(digest, answer): |
| 150 | shown = _esc(object_notary.normalize_digest(digest, "sha512") |
| 151 | or object_notary.normalize_digest(digest, "sha256") |
| 152 | or digest) |
| 153 | if not answer["found"]: |
| 154 | return f""" |
| 155 | <div class="breadcrumb"><a href="/">Home</a> / <a href="/notary">Notary</a> |
| 156 | / Check</div> |
| 157 | <h1>Not recorded here</h1> |
| 158 | <p class="digest">{shown}</p> |
| 159 | <div class="card"> |
| 160 | <p>This server has <strong>no record of that digest</strong>.</p> |
| 161 | <p>That is not evidence of anything. A digest is absent because nobody |
| 162 | lodged it here — which may mean it was lodged with a different notary, |
| 163 | lodged under a different algorithm, or never lodged at all. Absence from |
| 164 | this log proves nothing about the data, its age or its author.</p> |
| 165 | </div> |
| 166 | <p><a href="/notary">Check another, or lodge one</a></p> |
| 167 | """ |
| 168 | |
| 169 | return f""" |
| 170 | <div class="breadcrumb"><a href="/">Home</a> / <a href="/notary">Notary</a> |
| 171 | / Check</div> |
| 172 | <h1>Recorded</h1> |
| 173 | <p class="digest">{_esc(answer['digest'])}</p> |
| 174 | <div class="card hit"> |
| 175 | <p class="muted">First recorded by this server at</p> |
| 176 | <p class="when"><time datetime="{_esc(answer['first_seen_at'])}"> |
| 177 | {_esc(answer['first_seen_at'])}</time></p> |
| 178 | <p class="muted">{_esc(answer['first_seen_at'])} UTC — the canonical |
| 179 | value, shown because an attestation is evidence and evidence should not |
| 180 | change shape with who is reading it. The line above is the same instant in |
| 181 | your own timezone.</p> |
| 182 | <p class="muted">{_esc(answer['algorithm'])}{ |
| 183 | ' · ' + _esc(answer['label']) if answer.get('label') else ''}</p> |
| 184 | </div> |
| 185 | {_attestation_html(answer['attestation'])} |
| 186 | <p><a href="/notary/{_esc(answer['digest'])}.json">This answer as JSON</a> |
| 187 | · <a href="/notary">check another</a></p> |
| 188 | """ |
| 189 | |
| 190 | |
| 191 | def _index_html(base, count): |
| 192 | open_submission = _public_submission_allowed(base) |
| 193 | submit_line = ( |
| 194 | "<p>Anyone may lodge a digest with this server.</p>" |
| 195 | if open_submission else |
| 196 | "<p>Lodging a digest here requires an account or a service key. " |
| 197 | "Checking one does not, and never will.</p>") |
| 198 | return f""" |
| 199 | <div class="breadcrumb"><a href="/">Home</a> / Notary</div> |
| 200 | <h1>Notary</h1> |
| 201 | <p>This server keeps an <strong>append-only list of digests and the moment |
| 202 | each was first seen</strong>. Lodge one now, and at any point in the future |
| 203 | anybody can confirm that the data it was computed from already existed by |
| 204 | then.</p> |
| 205 | <p class="note">It holds <strong>{count:,}</strong> digest{ |
| 206 | '' if count == 1 else 's'} and <strong>no content whatsoever</strong>. |
| 207 | There is no field for a file, no code path that would accept one, and |
| 208 | nothing here worth stealing. That is the design, not a stage it will grow |
| 209 | out of.</p> |
| 210 | |
| 211 | <h2>Check a digest</h2> |
| 212 | <form id="check" onsubmit="return goCheck(event)"> |
| 213 | <p><input type="text" id="digest" name="digest" autocomplete="off" |
| 214 | spellcheck="false" placeholder="paste a sha256 or sha512 hex digest"></p> |
| 215 | <p><button type="submit">Check</button></p> |
| 216 | </form> |
| 217 | |
| 218 | <div class="drop" id="drop"> |
| 219 | <strong>Or drop a file here</strong> to hash it.<br> |
| 220 | <span class="muted">It is hashed by your browser and never uploaded — no |
| 221 | request leaves this machine, and this server never learns anything about |
| 222 | it.</span> |
| 223 | <div><input type="file" id="file"></div> |
| 224 | </div> |
| 225 | |
| 226 | <h2>Lodging one</h2> |
| 227 | {submit_line} |
| 228 | <pre><code>POST /objects/action_notarize |
| 229 | {{"digest": "<hex>", "algorithm": "sha256", "label": "optional note"}}</code></pre> |
| 230 | <p>Submitting the same digest twice returns the <strong>original</strong> |
| 231 | record and its original time. A later submission cannot make something |
| 232 | earlier, so nothing here can walk a timestamp forward.</p> |
| 233 | |
| 234 | <h2>What this is for</h2> |
| 235 | <ul> |
| 236 | <li>Anchoring the head of a ledger somewhere its own server cannot reach, |
| 237 | so rewriting history means also rewriting a record held elsewhere.</li> |
| 238 | <li>A contract, a photograph before an insurance claim, source code at a |
| 239 | release, experimental data before it is analysed.</li> |
| 240 | <li>Anywhere somebody may later need to say <em>this existed then, and here |
| 241 | is an independent party that saw it</em>.</li> |
| 242 | </ul> |
| 243 | |
| 244 | {_attestation_html(object_notary.attestation())} |
| 245 | |
| 246 | <script> |
| 247 | function goCheck(event) {{ |
| 248 | event.preventDefault(); |
| 249 | var value = document.getElementById('digest').value.trim(); |
| 250 | if (value) location.href = '/notary/' + encodeURIComponent(value); |
| 251 | return false; |
| 252 | }} |
| 253 | (function () {{ |
| 254 | var drop = document.getElementById('drop'); |
| 255 | var input = document.getElementById('file'); |
| 256 | var box = document.getElementById('digest'); |
| 257 | if (!drop || !window.crypto || !window.crypto.subtle) {{ |
| 258 | if (drop) drop.innerHTML = '<span class="muted">Your browser cannot ' + |
| 259 | 'hash files locally here (it needs a secure context), so paste a ' + |
| 260 | 'digest above instead. Nothing is uploaded either way.</span>'; |
| 261 | return; |
| 262 | }} |
| 263 | function hash(file) {{ |
| 264 | if (!file) return; |
| 265 | drop.classList.remove('over'); |
| 266 | var reader = new FileReader(); |
| 267 | reader.onload = function () {{ |
| 268 | crypto.subtle.digest('SHA-256', reader.result).then(function (buf) {{ |
| 269 | var hex = Array.prototype.map.call(new Uint8Array(buf), function (b) {{ |
| 270 | return ('00' + b.toString(16)).slice(-2); |
| 271 | }}).join(''); |
| 272 | box.value = hex; |
| 273 | box.focus(); |
| 274 | }}); |
| 275 | }}; |
| 276 | reader.readAsArrayBuffer(file); |
| 277 | }} |
| 278 | input.addEventListener('change', function () {{ hash(input.files[0]); }}); |
| 279 | ['dragenter', 'dragover'].forEach(function (name) {{ |
| 280 | drop.addEventListener(name, function (e) {{ |
| 281 | e.preventDefault(); drop.classList.add('over'); |
| 282 | }}); |
| 283 | }}); |
| 284 | drop.addEventListener('dragleave', function () {{ |
| 285 | drop.classList.remove('over'); |
| 286 | }}); |
| 287 | drop.addEventListener('drop', function (e) {{ |
| 288 | e.preventDefault(); |
| 289 | hash(e.dataTransfer.files && e.dataTransfer.files[0]); |
| 290 | }}); |
| 291 | }})(); |
| 292 | </script> |
| 293 | """ |
| 294 | |
| 295 | |
| 296 | def _page(title, body): |
| 297 | return { |
| 298 | "status": 200, |
| 299 | "content_type": "text/html; charset=utf-8", |
| 300 | "body": f"""<!doctype html> |
| 301 | <html lang="en"> |
| 302 | <head> |
| 303 | <meta charset="utf-8"> |
| 304 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 305 | <title>{_esc(title)}</title> |
| 306 | <link rel="stylesheet" href="/style"> |
| 307 | <style>{_STYLE}</style> |
| 308 | </head> |
| 309 | <body> |
| 310 | <div class="wrap nt"> |
| 311 | <header class="app"><h1><a href="/">DBBASIC</a></h1></header> |
| 312 | {body} |
| 313 | </div> |
| 314 | <script src="/nav"></script> |
| 315 | </body> |
| 316 | </html>""", |
| 317 | } |
| 318 | |
| 319 | |
| 320 | def GET(request): |
| 321 | request = request or {} |
| 322 | base = _base_dir() |
| 323 | digest = str(request.get("digest") or "").strip() |
| 324 | |
| 325 | wants_json = digest.lower().endswith(".json") |
| 326 | if wants_json: |
| 327 | digest = digest[:-len(".json")] |
| 328 | |
| 329 | if not digest: |
| 330 | return _page("Notary", _index_html(base, len(_rows(base)))) |
| 331 | |
| 332 | answer = lookup(digest, base=base) |
| 333 | if wants_json: |
| 334 | # 200 either way, including for a digest that is not here. "Not |
| 335 | # recorded" is a complete and correct answer to the question asked, |
| 336 | # and a monitor that has to distinguish a 404-meaning-absent from a |
| 337 | # 404-meaning-broken will eventually get it wrong. |
| 338 | return { |
| 339 | "status": 200, |
| 340 | "content_type": "application/json; charset=utf-8", |
| 341 | "body": json.dumps(answer, indent=2, sort_keys=True), |
| 342 | } |
| 343 | return _page("Notary", _result_html(digest, answer)) |
| 344 | |
| 345 | |
| 346 | def POST(request): |
| 347 | # Checking a digest is a read however it is spelled. Lodging one is |
| 348 | # action_notarize, deliberately a different object: a page that both |
| 349 | # answered questions and wrote rows would be one refactor away from |
| 350 | # recording a digest somebody only meant to look up. |
| 351 | return GET(request) |