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
test_notary.py · 16.8 KB · 382 lines
| 1 | """The notary: an independent party's record that a digest already existed. |
| 2 | |
| 3 | Two claims carry this file, and everything else here is in service of one |
| 4 | or the other. |
| 5 | |
| 6 | THE TIMESTAMP CANNOT BE WALKED FORWARD. A notarization claims "this |
| 7 | existed BY then", so a second submission of the same digest must return |
| 8 | the FIRST record rather than making a new one. Everywhere else in this |
| 9 | building idempotency exists to avoid a duplicate row; here it exists |
| 10 | because the only direction an attacker would want to move a timestamp is |
| 11 | later, and there must be no way to do it. |
| 12 | |
| 13 | THE SERVICE NEVER LEARNS WHAT IT ATTESTS TO. There is no field for |
| 14 | content and no code path that would accept it, which is what makes |
| 15 | notarizing something confidential safe and what makes this collection not |
| 16 | worth stealing. And because the record proves so much less than the word |
| 17 | "notarized" suggests, every surface has to say so -- the failure mode of |
| 18 | a notary is not a bug, it is a reader taking it for verification. |
| 19 | """ |
| 20 | |
| 21 | import pathlib |
| 22 | |
| 23 | import pytest |
| 24 | from conftest import stage_collection |
| 25 | |
| 26 | import object_execution |
| 27 | import object_notary |
| 28 | import object_records |
| 29 | import python_object_runtime |
| 30 | |
| 31 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 32 | PACKAGES = REPO_ROOT / "packages" |
| 33 | NOTARY_OBJECTS = PACKAGES / "app-notary" / "objects" |
| 34 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 35 | |
| 36 | # sha256 of b"the quick brown fox", computed by hashlib in the fixture |
| 37 | # rather than pasted, so the constant cannot rot away from the algorithm. |
| 38 | SHA256_A = object_notary.digest_of(b"the quick brown fox") |
| 39 | SHA256_B = object_notary.digest_of(b"a different thing entirely") |
| 40 | SHA512_A = object_notary.digest_of(b"the quick brown fox", "sha512") |
| 41 | |
| 42 | |
| 43 | # --- the pure module, with no data directory in sight ------------------------- |
| 44 | |
| 45 | def test_a_digest_is_normalised_rather_than_refused_over_its_spelling(): |
| 46 | """A hex digest is the same value upper or lower cased, and tools hand |
| 47 | them over with '0x' and 'sha256:' prefixes all the time. Losing a |
| 48 | lookup to a capital letter would be a policy nobody chose.""" |
| 49 | assert object_notary.normalize_digest(SHA256_A.upper()) == SHA256_A |
| 50 | assert object_notary.normalize_digest(" " + SHA256_A + " ") == SHA256_A |
| 51 | assert object_notary.normalize_digest("sha256:" + SHA256_A) == SHA256_A |
| 52 | assert object_notary.normalize_digest("0x" + SHA256_A) == SHA256_A |
| 53 | |
| 54 | |
| 55 | def test_weak_algorithms_are_refused_and_the_refusal_says_which_are_not(): |
| 56 | """md5 and sha1 are not merely unimplemented. A digest somebody can |
| 57 | collide lets them notarize one document today and produce a different |
| 58 | one with the same hash tomorrow -- which is the entire attack this |
| 59 | exists to make hard, so accepting one would be storing nothing while |
| 60 | appearing to store something.""" |
| 61 | problems = object_notary.problems("d41d8cd98f00b204e9800998ecf8427e", "md5") |
| 62 | assert problems |
| 63 | joined = " ".join(problems) |
| 64 | assert "md5" in joined |
| 65 | assert "sha256" in joined and "sha512" in joined |
| 66 | assert "collided" in joined or "collision" in joined |
| 67 | assert object_notary.normalize_algorithm("sha1") == "" |
| 68 | |
| 69 | |
| 70 | def test_a_wrong_length_digest_is_told_what_length_it_should_be(): |
| 71 | """'Invalid digest' is the sentence whose commonest cause is somebody |
| 72 | having hashed with sha1 and having no way to find that out.""" |
| 73 | problems = object_notary.problems("abc123", "sha256") |
| 74 | assert len(problems) == 1 |
| 75 | assert "64" in problems[0] and "6" in problems[0] |
| 76 | |
| 77 | not_hex = object_notary.problems("z" * 64, "sha256") |
| 78 | assert "not hex" in " ".join(not_hex).lower() |
| 79 | |
| 80 | |
| 81 | def test_the_earliest_record_wins_and_a_later_one_cannot_replace_it(): |
| 82 | """THE load-bearing rule. A notarization says the data existed BY the |
| 83 | stamped moment; a later submission cannot make it earlier, so the |
| 84 | first record is the only one that carries information. If the latest |
| 85 | won, anybody could quietly discard the single fact the log holds by |
| 86 | submitting again.""" |
| 87 | rows = [ |
| 88 | {"digest": SHA256_A, "algorithm": "sha256", |
| 89 | "created_at": "2026-07-26T10:00:00"}, |
| 90 | {"digest": SHA256_A, "algorithm": "sha256", |
| 91 | "created_at": "2026-01-02T09:00:00"}, |
| 92 | {"digest": SHA256_A, "algorithm": "sha256", |
| 93 | "created_at": "2026-12-31T23:59:59"}, |
| 94 | ] |
| 95 | found = object_notary.first_seen(SHA256_A, rows) |
| 96 | assert found["created_at"] == "2026-01-02T09:00:00" |
| 97 | |
| 98 | |
| 99 | def test_the_same_hex_under_a_different_algorithm_is_a_different_record(): |
| 100 | """A sha256 and a sha512 digest cannot collide by length, but the |
| 101 | lookup still keys on both -- otherwise a row's attestation would be |
| 102 | about an algorithm nobody checked.""" |
| 103 | rows = [{"digest": SHA512_A, "algorithm": "sha512", |
| 104 | "created_at": "2026-01-01T00:00:00"}] |
| 105 | assert object_notary.first_seen(SHA512_A, rows, "sha512") is not None |
| 106 | assert object_notary.first_seen(SHA512_A, rows, "sha256") is None |
| 107 | |
| 108 | |
| 109 | def test_the_attestation_names_what_it_does_not_prove(): |
| 110 | """Overstating this is the only way a service this simple can do harm. |
| 111 | The wording lives in one function so the page, the JSON and the |
| 112 | submission receipt cannot drift into three different strengths of the |
| 113 | same promise.""" |
| 114 | said = object_notary.attestation({"created_at": "2026-07-26T10:00:00"}) |
| 115 | denied = " ".join(said["does_not_prove"]).lower() |
| 116 | assert "who created" in denied |
| 117 | assert "owned" in denied |
| 118 | assert "earlier" in denied # it is an upper bound on age |
| 119 | assert "content" in denied |
| 120 | assert "clock" in " ".join(said["rests_on"]).lower() |
| 121 | |
| 122 | |
| 123 | def test_a_missing_digest_denies_rather_than_proves_absence(): |
| 124 | """A checker who is told '404' reads a fault. A checker who is told |
| 125 | 'not recorded here' has the right answer, and the record has to say |
| 126 | that absence is not evidence.""" |
| 127 | answer = object_notary.receipt(None, found=False) |
| 128 | assert answer["found"] is False |
| 129 | assert not answer["attestation"]["proves"] |
| 130 | assert "not evidence" in " ".join( |
| 131 | answer["attestation"]["does_not_prove"]).lower() |
| 132 | |
| 133 | |
| 134 | def test_a_label_cannot_break_the_row_it_is_written_into(): |
| 135 | """A tab ends a field and a newline ends a record. Refusing the |
| 136 | submission over whitespace would lose a notarization to a formatting |
| 137 | detail, so the label is collapsed instead.""" |
| 138 | label = object_notary.normalize_label("payroll\thead\nfor July 2026 ") |
| 139 | assert "\t" not in label and "\n" not in label |
| 140 | assert label == "payroll head for July 2026" |
| 141 | assert len(object_notary.normalize_label("x" * 500)) == object_notary.MAX_LABEL |
| 142 | |
| 143 | |
| 144 | # --- through the objects, on a real data directory ---------------------------- |
| 145 | |
| 146 | @pytest.fixture |
| 147 | def data_dir(tmp_path, monkeypatch): |
| 148 | base = tmp_path / "data" |
| 149 | stage_collection(base, "app-notary", "notarizations") |
| 150 | stage_collection(base, "app-settings", "app_settings") |
| 151 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(base)) |
| 152 | return base |
| 153 | |
| 154 | |
| 155 | def run(object_id, payload, *, method="POST"): |
| 156 | return object_execution.execute_object( |
| 157 | RUNTIME, |
| 158 | object_execution.ObjectExecutionRequest(object_id, method=method, |
| 159 | payload=payload), |
| 160 | roots=[NOTARY_OBJECTS]).result |
| 161 | |
| 162 | |
| 163 | def notarize(**payload): |
| 164 | payload.setdefault("_identity", {"user_id": "dan"}) |
| 165 | return run("action_notarize", payload) |
| 166 | |
| 167 | |
| 168 | def setting(data_dir, key, value): |
| 169 | return object_records.create_collection_record( |
| 170 | "app_settings", {"key": key, "value": value}, base_dir=data_dir) |
| 171 | |
| 172 | |
| 173 | def test_lodging_a_digest_records_the_moment_and_not_the_content(data_dir): |
| 174 | receipt = notarize(digest=SHA256_A, label="ledger head") |
| 175 | assert receipt["found"] is True |
| 176 | assert receipt["already_recorded"] is False |
| 177 | assert receipt["digest"] == SHA256_A |
| 178 | assert receipt["first_seen_at"] |
| 179 | |
| 180 | rows = object_records.read_collection_records("notarizations", |
| 181 | base_dir=data_dir) |
| 182 | assert len(rows) == 1 |
| 183 | stored = rows[0] |
| 184 | assert stored["digest"] == SHA256_A |
| 185 | assert stored["submitter"] == "dan" |
| 186 | # The whole safety argument in one assertion: there is nowhere in this |
| 187 | # row for the thing that was hashed, so a stolen copy of the log tells |
| 188 | # a thief which digests exist and nothing else at all. |
| 189 | assert "the quick brown fox" not in "\t".join(stored.values()) |
| 190 | assert set(stored) <= {"id", "digest", "algorithm", "label", "submitter", |
| 191 | "source", "created_at", "_op", "rev"} |
| 192 | |
| 193 | |
| 194 | def test_resubmitting_returns_the_original_record_and_writes_nothing(data_dir): |
| 195 | """The property that makes the whole service worth anything. If a |
| 196 | resubmission wrote a new row, the earliest fact would still be in the |
| 197 | log -- but a checker reading the newest would be told a later time, |
| 198 | and the attestation would have quietly weakened itself.""" |
| 199 | first = notarize(digest=SHA256_A, label="original") |
| 200 | again = notarize(digest=SHA256_A, label="a different label entirely") |
| 201 | |
| 202 | assert again["already_recorded"] is True |
| 203 | assert again["first_seen_at"] == first["first_seen_at"] |
| 204 | assert again["label"] == "original" # the later label is discarded |
| 205 | assert "cannot move a timestamp forward" in again["note"] |
| 206 | |
| 207 | rows = object_records.read_collection_records("notarizations", |
| 208 | base_dir=data_dir) |
| 209 | assert len(rows) == 1 |
| 210 | |
| 211 | |
| 212 | def test_resubmitting_upper_cased_is_still_the_same_digest(data_dir): |
| 213 | first = notarize(digest=SHA256_A) |
| 214 | again = notarize(digest=SHA256_A.upper()) |
| 215 | assert again["already_recorded"] is True |
| 216 | assert again["first_seen_at"] == first["first_seen_at"] |
| 217 | assert len(object_records.read_collection_records( |
| 218 | "notarizations", base_dir=data_dir)) == 1 |
| 219 | |
| 220 | |
| 221 | def test_a_weak_algorithm_is_refused_at_the_door(data_dir): |
| 222 | refused = notarize(digest="d41d8cd98f00b204e9800998ecf8427e", |
| 223 | algorithm="md5") |
| 224 | assert refused["status"] == 400 |
| 225 | assert not object_records.read_collection_records("notarizations", |
| 226 | base_dir=data_dir) |
| 227 | |
| 228 | |
| 229 | def test_anonymous_submission_is_closed_until_the_operator_opens_it(data_dir): |
| 230 | """A capability boundary in the house shape: absent by default, and a |
| 231 | 409 naming exactly what to configure rather than a stub that pretends |
| 232 | to have worked. A notary run as a service for other people must accept |
| 233 | strangers; one run for a single business's own ledger heads must not, |
| 234 | and those two deployments are one setting apart.""" |
| 235 | refused = run("action_notarize", {"digest": SHA256_A}) |
| 236 | assert refused["status"] == 409 |
| 237 | assert refused["setting"] == "notary.public_submission" |
| 238 | assert "notary.public_submission" in refused["error"] |
| 239 | assert not object_records.read_collection_records("notarizations", |
| 240 | base_dir=data_dir) |
| 241 | |
| 242 | setting(data_dir, "notary.public_submission", "true") |
| 243 | accepted = run("action_notarize", {"digest": SHA256_A}) |
| 244 | assert accepted["found"] is True |
| 245 | assert accepted["digest"] == SHA256_A |
| 246 | rows = object_records.read_collection_records("notarizations", |
| 247 | base_dir=data_dir) |
| 248 | assert rows[0]["submitter"] == "public" |
| 249 | |
| 250 | |
| 251 | def test_an_unreadable_log_refuses_rather_than_stamping_a_second_time(tmp_path, |
| 252 | monkeypatch): |
| 253 | """If the log cannot be read, a first-seen row cannot be found, so |
| 254 | appending would risk a SECOND and later stamp on something already |
| 255 | recorded -- exactly the failure this object exists to prevent. A |
| 256 | submission refused gets retried; a timestamp silently reset is gone.""" |
| 257 | base = tmp_path / "empty" |
| 258 | base.mkdir() |
| 259 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(base)) |
| 260 | refused = run("action_notarize", |
| 261 | {"digest": SHA256_A, "_identity": {"user_id": "dan"}}) |
| 262 | assert refused["status"] == 409 |
| 263 | assert "second" in refused["error"] and "later stamp" in refused["error"] |
| 264 | |
| 265 | |
| 266 | # --- the public check --------------------------------------------------------- |
| 267 | |
| 268 | def check(digest, *, method="GET"): |
| 269 | return object_execution.execute_object( |
| 270 | RUNTIME, |
| 271 | object_execution.ObjectExecutionRequest("site_notary", method=method, |
| 272 | payload={"digest": digest}), |
| 273 | roots=[NOTARY_OBJECTS]).result |
| 274 | |
| 275 | |
| 276 | def test_the_check_is_public_and_answers_with_the_time_it_first_saw_it(data_dir): |
| 277 | receipt = notarize(digest=SHA256_A, label="ledger head 26 July") |
| 278 | page = check(SHA256_A) |
| 279 | assert page["status"] == 200 |
| 280 | body = page["body"] |
| 281 | assert SHA256_A in body |
| 282 | assert receipt["first_seen_at"] in body |
| 283 | assert "ledger head 26 July" in body |
| 284 | # And the caveats travel with the answer rather than living in a |
| 285 | # footnote somebody scrolls past. |
| 286 | assert "does not prove" in body |
| 287 | assert "Anybody can notarize anybody" in body |
| 288 | |
| 289 | |
| 290 | def test_the_public_answer_does_not_say_who_lodged_it(data_dir): |
| 291 | """What a digest is worth as evidence does not depend on who filed it, |
| 292 | and publishing who anchored what would leak the shape of somebody's |
| 293 | records without adding anything to the attestation.""" |
| 294 | import json |
| 295 | run("action_notarize", {"digest": SHA256_A, "label": "ledger head", |
| 296 | "_identity": {"user_id": "accounts-payable-clerk"}}) |
| 297 | |
| 298 | body = check(SHA256_A)["body"] |
| 299 | assert SHA256_A in body # the answer IS there |
| 300 | assert "accounts-payable-clerk" not in body |
| 301 | |
| 302 | parsed = json.loads(check(SHA256_A + ".json")["body"]) |
| 303 | assert parsed["found"] is True |
| 304 | assert "submitter" not in parsed |
| 305 | assert "accounts-payable-clerk" not in json.dumps(parsed) |
| 306 | |
| 307 | # And the operator can still see it, because "not on the public page" |
| 308 | # is not the same as "not recorded". |
| 309 | stored = object_records.read_collection_records("notarizations", |
| 310 | base_dir=data_dir) |
| 311 | assert stored[0]["submitter"] == "accounts-payable-clerk" |
| 312 | |
| 313 | |
| 314 | def test_an_unknown_digest_is_answered_rather_than_404ed(data_dir): |
| 315 | page = check(SHA256_B) |
| 316 | assert page["status"] == 200 |
| 317 | assert "Not recorded here" in page["body"] |
| 318 | assert "not evidence of anything" in page["body"] |
| 319 | |
| 320 | answer = check(SHA256_B + ".json") |
| 321 | assert answer["status"] == 200 # a complete answer, not a fault |
| 322 | import json |
| 323 | assert json.loads(answer["body"])["found"] is False |
| 324 | |
| 325 | |
| 326 | def test_the_json_and_the_page_are_the_same_fold(data_dir): |
| 327 | import json |
| 328 | notarize(digest=SHA256_A, label="head") |
| 329 | parsed = json.loads(check(SHA256_A + ".json")["body"]) |
| 330 | assert parsed["found"] is True |
| 331 | assert parsed["digest"] == SHA256_A |
| 332 | assert parsed["first_seen_at"] in check(SHA256_A)["body"] |
| 333 | |
| 334 | |
| 335 | def test_the_index_says_it_holds_no_content_and_offers_a_local_hash(data_dir): |
| 336 | """The drop box hashes in the browser with SubtleCrypto and uploads |
| 337 | nothing. It earns its place by demonstrating the central claim instead |
| 338 | of asking to be believed -- a visitor can watch a file produce a digest |
| 339 | with no request leaving the machine.""" |
| 340 | page = object_execution.execute_object( |
| 341 | RUNTIME, |
| 342 | object_execution.ObjectExecutionRequest("site_notary", method="GET", |
| 343 | payload={}), |
| 344 | roots=[NOTARY_OBJECTS]).result |
| 345 | body = page["body"] |
| 346 | assert "no content whatsoever" in body |
| 347 | assert "crypto.subtle" in body |
| 348 | assert "never uploaded" in body |
| 349 | # The submission door is described as closed, because it is. |
| 350 | assert "requires an account or a service key" in body |
| 351 | |
| 352 | |
| 353 | def test_nothing_in_the_package_offers_a_way_to_delete_a_notarization(): |
| 354 | """A notary with an edit path is a notary whose operator can be leaned |
| 355 | on. This is a grep rather than a behaviour test on purpose: the risk |
| 356 | is not that today's code deletes a row, it is that somebody later adds |
| 357 | a tidy-up that does, and a test naming the whole package is what |
| 358 | stands in the way.""" |
| 359 | rules = (PACKAGES / "app-notary" / "permissions" / "rules.json").read_text() |
| 360 | assert '"delete"' not in rules |
| 361 | assert '"update"' not in rules |
| 362 | |
| 363 | for path in (PACKAGES / "app-notary").rglob("*.py"): |
| 364 | source = path.read_text() |
| 365 | assert "delete_collection_record" not in source, path |
| 366 | assert "update_collection_record" not in source, path |
| 367 | assert "prune_collection_records" not in source, path |
| 368 | |
| 369 | |
| 370 | def test_the_schema_stores_the_digest_append_only_and_holds_no_content_field(): |
| 371 | import json |
| 372 | schema = json.loads( |
| 373 | (PACKAGES / "app-notary" / "schemas" / "notarizations.json").read_text()) |
| 374 | assert schema["storage"] == "append" |
| 375 | names = {field["name"] for field in schema["fields"]} |
| 376 | assert names == {"id", "digest", "algorithm", "label", "submitter", |
| 377 | "source", "created_at"} |
| 378 | # Named explicitly: if somebody adds one of these the assertion above |
| 379 | # already fails, but the failure should read as a decision being |
| 380 | # reversed rather than a list needing updating. |
| 381 | assert not names & {"content", "body", "data", "file", "filename", |
| 382 | "bytes", "payload"} |