Repository navigation
fix(tx): count read-only POSTs as reads, from protocol or sandbox evidence - #2397
Conversation
…dence (#2389) Reads sent as POST used up the tap's single mutation, so the real save got 403, and with a relaxed budget the extra mutations made a correct save REFUTED (wire_evidence_incomplete, wire_target_mismatch, wire_collateral_mutation). Reproduced on a live Medplum through the merged engine: a batch read, a GraphQL query and a PUT save gave [200, 403, 403] and REFUTED; relaxed, [200, 200, 200] and REFUTED. Three general classes, none naming an app: - A GraphQL query on an operation-style route (/$graphql) is classified like one on /graphql. Mutations stay mutations. - A batch Bundle (batch or transaction) whose every entry is a GET or HEAD is a read. Any other entry, a resource body, or a missing method makes it a mutation. - Any other POST is a read only when it matches a ReadRoute declared in WirePolicy.read_routes: method, exact path, and every top-level body field that was constant across the observed samples, so a write on the same RPC route with a different operation field never matches. probe_read_routes learns those routes. It replays each POST route of a trace on a loopback sandbox, after a caller-supplied reset to the pre-trace state, and accepts a route only when the store was idle, every replay returned 2xx and a caller-supplied digest of the whole store did not change. Without the reset, replaying a recorded save writes the value the store already holds and looks like a read; a test pins that. Credential-like body fields are never stored in a route. Live on Medplum (every table in the app's database hashed): batch and GraphQL reads are reads, POST /Patient/_search is learned as a read, the PUT save is not, a wrapped save after three POST reads goes through with a second write still blocked, and the engine returns UNCERTAIN with only unqualified_observation_only. Signed-off-by: abrichr <richard.abrich@mldsai.com>
Independent adversarial review: REQUEST CHANGESFrom: Grok The static classes are fail-closed:
Pointer: openadapt-internal#211 |
Review finding: with an empty body_equals, all(()) matched any body on the route's path, so a write sent to a path that also served reads would be counted as a read and skip the mutation budget. _constant_fields returns () when every field varies or is a credential, which a form that both searches and saves produces. ReadRoute now rejects an empty body_equals, matches_read_route skips any such entry, and probe_read_routes refuses a route whose samples share no constant field. A test pins that a write on such a path still uses the budget. Signed-off-by: abrichr <richard.abrich@mldsai.com>
Independent adversarial review: APPROVEFrom: Grok The empty- The new tests cover constructor rejection and a probe that learns nothing. Residual only: the follow-up write in Do not merge: the request was review only. Pointer: openadapt-internal#211 |
# Conflicts: # CAPABILITIES.md # llms-full.txt
Independent adversarial review: APPROVEFrom: Grok Delta re-review after the approved head The only unique commit is a merge of Review only; I am not merging. |
# Conflicts: # CAPABILITIES.md # llms-full.txt
Independent adversarial review: APPROVEFrom: Claude (independent reviewer) No blockers.
|
# Conflicts: # CAPABILITIES.md # llms-full.txt
Independent adversarial review: APPROVEFrom: Claude (independent reviewer) The only new commit since the approved head
|
Independent adversarial review: APPROVEFrom: Gemini Independent cross-vendor adversarial review of Findings & fail-closed verification
Generality gate
|
# Conflicts: # CAPABILITIES.md # llms-full.txt
Independent adversarial review: APPROVEFrom: Gemini Verified clean merge with
|
#2518 split the page into capabilities/ row files from main as of 17:13 EST. Six later first-parent commits updated the stamped page and were kept on merge, but not the row files, so the stamp test and refresh workflow went red (#2562). Fold those edits into the sources and regenerate the stamp. CAPABILITIES.md is unchanged. - #2471: VERIFIED-run prose; fault-test and openIMIS measured-results notes - #2477: grade file/PDF/email checks by artifact provenance - #2472: api_recheck row - #2397: GraphQL / probe_read_routes text on the transaction-lens row - #2363 / #2368: second-factor row, including terminal/Windows console Fixes #2562
Fixes #2389.
Problem
Reads sent as
POST(batch reads, GraphQL queries, search forms, AJAX fragments) used up the tap's single mutation, so the real save got 403. With a relaxed budget the extra "mutations" made a correct saveREFUTED. Reproduced on a live Medplum through the merged engine, without model calls. The sequence was a batch read, a GraphQL query, then thePUTsave:max_mutations=1REFUTED(wire_target_mismatch,wire_collateral_mutation,wire_mutation_blocked)max_mutations=NoneREFUTED(wire_evidence_incomplete,wire_target_mismatchx2,wire_collateral_mutation)UNCERTAIN(unqualified_observation_onlyonly)Change
Three general classes. No engine code names an app.
GraphQL on an operation-style route.
/$graphqlis classified like/graphql. Mutations stay mutations, andgraphqlelsewhere in a path doesn't count.Read-only batches. A
Bundleof typebatchortransactionwhose every entry is aGETorHEADis a read. Another method, a resource body, a missing method or an empty batch makes it a mutation.Declared read routes, learned on a sandbox. Any other
POSTis a read only when it matches aReadRouteinWirePolicy.read_routes: the method, the exact path, and every top-level body field that was constant across the observed samples. A write on the same RPC route with a different operation field never matches.probe_read_routeslearns the routes as follows:POSTroute in a trace on a loopback sandbox.Without the reset, replaying a recorded save writes a value the store already holds and looks like a read. A test pins that.
Known limit (stated in the module docstring): a write route whose every recorded sample wrote values the pre-trace store already held can't be told from a read. That request changed nothing in the trace either, and the engine's read-back of the target still judges any write that reaches it.
Tests
tests/test_tx_post_reads.pyruns in CI against a local SQLite-backed server, so the digest is exact. It covers the static classes, read-route matching (body discriminator, forms, repeated names), and the probe. The probe learns search and RPC reads, refuses the save, refuses everything if the store isn't idle, refuses a non-loopback sandbox, and never stores a CSRF field. With the learned routes, the save goes through and an RPC write is still blocked by the budget.tests/test_tx_post_reads_live.pyis lab only. It needsOAFLOW_TX_LIVE_MEDPLUM_STATEand uses thebenchmark/fhir_medplumfixture; the digest hashes all 524 tables in the app's Postgres. All 5 pass:POST /fhir/R4/Patient/_searchis learned as a read, and the save is not probed as one;POSTreads goes through, a second write gets 403, and the stored value is the intended one;POSTcreate;wrap_actionreturnsUNCERTAINwith onlyunqualified_observation_only.test_o1_write_completeness_with_10k_rows_and_5_concurrent_writers, which also fails on unchanged code under load.Not in this PR
OpenEMR's form/AJAX POSTs have not been run live; they need a logged-in session, and the declared-route path is the same. Also not here: the RPC read template (#2390),
NULLversus''(#2391), and redirects (#2392).