Skip to content

Commit b09eeee

Browse files
Compare every bound-evidence digest; refuse duplicate evidence rows
- ct_scan_check_bound_evidence keyed its checks by the computed hash, so a terms snapshot byte-identical to the Target Summary overwrote the terms entry and the recorded terms hash / shown digest were never compared. Use a list of [computed, recorded] pairs so every comparison runs. - Refuse (ct_scan_evidence_duplicate) when an evidence key holds more than one meta row instead of silently using the first. - Accepted copy: compare the recorded terms hash case-insensitively, and re-check under the queue lock that the job is still live (queued, leased or running, authorization live) before claiming the send. - Tests: identical-bytes cases, duplicate rows at creation and while running, upper-case terms hash, and no copy for cancelled, revoked or edited-evidence jobs. - docs/WORDPRESS.md: document the above, list bound_required in schema 4, fix the missing blank line before Egress. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 9f92d26 commit b09eeee

4 files changed

Lines changed: 92 additions & 11 deletions

File tree

‎docs/WORDPRESS.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ the request closes). The form, the site recorder and this plugin render
196196
byte-identical text from that one function, so the limits the signer saw are the
197197
limits the job runs under. Any missing, oversized, mismatched, stale,
198198
non-canonical or partial piece refuses the job (`ct_scan_binding_mismatch` /
199-
`ct_scan_binding_required`), and the job row stores the summary digest, so a
199+
`ct_scan_binding_required`). Every recorded digest is compared, even when the
200+
terms and the Summary are byte-identical. An evidence key (the marker, the
201+
snapshot, the Summary or any of their digests) that holds more than one meta row
202+
is ambiguous and refuses the job (`ct_scan_evidence_duplicate`). The job row stores the summary digest, so a
200203
later edit of the retained evidence makes the authorization no longer live and
201204
cancels a queued or running job. A change of the profile's limits changes the
202205
text and therefore requires a new acceptance.
@@ -219,12 +222,15 @@ confirmation sends the verified contact their copy: the Target Summary, the
219222
hashes, the filled-in copy and, for a bound acceptance, the exact terms bytes
220223
as an attachment (written to a private temporary directory and attached only if
221224
it reproduces the recorded hash). The recipient must still be the confirmed,
222-
authorized address. It is sent at most once (the status is claimed by
225+
authorized address, and the job must still be live (queued, leased or running,
226+
not revoked, and its retained evidence unchanged), re-checked under the queue
227+
lock immediately before the send is claimed. It is sent at most once (the status is claimed by
223228
compare-and-set before mail is handed off), only to the verified address, and
224229
never to a referrer. An uncertain or failed hand-off is recorded as `unknown`
225230
and never resent automatically; `accepted` means transport acceptance, not
226231
receipt. A failure cannot cancel, delay or repeat a scan. The wording of that
227232
message has not been reviewed by the owner.
233+
228234
## Egress (activation requirement)
229235

230236
The admission guard, the unprivileged UID and the resource limits are
@@ -376,7 +382,8 @@ API stays refused, and the upgrade is not retried for five minutes (so a broken
376382
table cannot make every public request re-run it).
377383
Schema 3 adds `profile_id`, `http_admitted` and `last_admit_ms` to the jobs
378384
table (existing rows default to `r2-header-v1` with empty counters); schema 4
379-
adds `summary_sha256`, `accepted_copy_status` and `accepted_copy_at`. Upgrades
385+
adds `summary_sha256`, `bound_required` (the job's frozen evidence requirement;
386+
existing rows default to `0`), `accepted_copy_status` and `accepted_copy_at`. Upgrades
380387
run on `init` and the worker API refuses requests until they complete.
381388

382389
The healthcheck requires a successful authenticated exchange in the last ten

‎integrations/wordpress/ct-scan.php‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,13 @@ function ct_scan_meta_string( $post_id, $key ) {
370370
* unless the operator grandfathered an r2-or-earlier version as `legacy` in config `terms_policy`.
371371
*/
372372
function ct_scan_check_bound_evidence( $request_id, $terms_hash, $identity, $required, $profile ) {
373+
// get_post_meta( ..., true ) silently returns the first of several rows, so an evidence key that
374+
// holds more than one value is ambiguous: refuse rather than pick one.
375+
foreach ( array( '_ct_terms_binding', '_ct_terms_snapshot', '_ct_terms_sha256', '_ct_terms_shown_digest', '_ct_summary_text', '_ct_summary_sha256', '_ct_summary_shown_digest' ) as $key ) {
376+
if ( count( (array) get_post_meta( $request_id, $key, false ) ) > 1 ) {
377+
return new WP_Error( 'ct_scan_evidence_duplicate', 'The saved acceptance holds more than one value for a piece of evidence.' );
378+
}
379+
}
373380
$marker = ct_scan_meta_string( $request_id, '_ct_terms_binding' );
374381
$partial = false;
375382
foreach ( array( '_ct_terms_snapshot', '_ct_terms_shown_digest', '_ct_summary_text', '_ct_summary_sha256', '_ct_summary_shown_digest' ) as $key ) {
@@ -390,15 +397,18 @@ function ct_scan_check_bound_evidence( $request_id, $terms_hash, $identity, $req
390397
}
391398
$snapshot_hash = hash( 'sha256', $snapshot );
392399
$summary_hash = hash( 'sha256', $summary );
400+
// A list of [computed, recorded] pairs, never keyed by the computed hash: byte-identical terms and
401+
// Summary would share a key and silently drop the terms comparison.
393402
$checks = array(
394-
$snapshot_hash => array( $terms_hash, strtolower( ct_scan_meta_string( $request_id, '_ct_terms_shown_digest' ) ) ),
395-
$summary_hash => array( strtolower( ct_scan_meta_string( $request_id, '_ct_summary_sha256' ) ), strtolower( ct_scan_meta_string( $request_id, '_ct_summary_shown_digest' ) ) ),
403+
array( $snapshot_hash, $terms_hash ),
404+
array( $snapshot_hash, strtolower( ct_scan_meta_string( $request_id, '_ct_terms_shown_digest' ) ) ),
405+
array( $summary_hash, strtolower( ct_scan_meta_string( $request_id, '_ct_summary_sha256' ) ) ),
406+
array( $summary_hash, strtolower( ct_scan_meta_string( $request_id, '_ct_summary_shown_digest' ) ) ),
396407
);
397-
foreach ( $checks as $computed => $recorded ) {
398-
foreach ( $recorded as $value ) {
399-
if ( ! is_string( $value ) || ! preg_match( '/\A[a-f0-9]{64}\z/', $value ) || ! hash_equals( $computed, $value ) ) {
400-
return new WP_Error( 'ct_scan_binding_mismatch', 'The retained evidence does not match the digest the signer was shown.' );
401-
}
408+
foreach ( $checks as $check ) {
409+
list( $computed, $value ) = $check;
410+
if ( ! is_string( $value ) || ! preg_match( '/\A[a-f0-9]{64}\z/', $value ) || ! hash_equals( $computed, $value ) ) {
411+
return new WP_Error( 'ct_scan_binding_mismatch', 'The retained evidence does not match the digest the signer was shown.' );
402412
}
403413
}
404414
// The Target Summary must be exactly the canonical text of the authorized request (website, client,
@@ -916,9 +926,14 @@ function ct_scan_send_accepted_copy( $job_id ) {
916926
if ( ! $row || empty( $row->confirmed_at ) || ! is_email( $row->contact_email )
917927
|| ct_scan_meta_string( (int) $row->request_id, '_ct_scan_email' ) !== $row->contact_email
918928
|| ct_scan_meta_string( (int) $row->request_id, '_ct_terms_version' ) !== $row->terms_version
919-
|| ct_scan_meta_string( (int) $row->request_id, '_ct_terms_sha256' ) !== $row->terms_hash ) {
929+
|| strtolower( ct_scan_meta_string( (int) $row->request_id, '_ct_terms_sha256' ) ) !== $row->terms_hash ) {
920930
return new WP_Error( 'accepted_copy_unavailable', 'The accepted copy requires the confirmed, authorized recipient.' );
921931
}
932+
// Re-checked under the queue lock right before the claim: a job that was cancelled, stopped,
933+
// expired or whose evidence changed since confirmation gets no copy.
934+
if ( ! in_array( $row->status, array( 'queued', 'leased', 'running' ), true ) || ! ct_scan_authorization_is_live( $row ) ) {
935+
return new WP_Error( 'accepted_copy_unavailable', 'The accepted copy requires a live authorization.' );
936+
}
922937
$claimed = $wpdb->update(
923938
$table,
924939
array( 'accepted_copy_status' => 'unknown', 'accepted_copy_at' => ct_scan_utc_now() ),

‎integrations/wordpress/tests/evidence-strict.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,36 @@
7171
ct_test_assert( 'already_handled' === ct_scan_send_accepted_copy( $fevent['job_id'] ) && count( $mail ) === $count, 'An uncertain accepted copy was resent automatically.' );
7272
}
7373

74+
// The recorded terms hash is compared case-insensitively at send time (the job stores it lowercased).
75+
list( $upper_id, $upper_fields ) = ct_test_request( 'strict-upper.example.net' );
76+
$request_ids[] = $upper_id;
77+
$upper_meta = ct_test_bind( $upper_id, $upper_fields );
78+
update_post_meta( $upper_id, '_ct_terms_sha256', strtoupper( $upper_meta['_ct_terms_sha256'] ) );
79+
$upper_event = ct_scan_on_request_ready( $upper_id, $upper_fields );
80+
ct_test_assert( is_array( $upper_event ), 'A bound acceptance with an upper-case terms hash was refused.' );
81+
ct_test_queue_job( $upper_event['job_id'] );
82+
$count = count( $mail );
83+
ct_test_assert( 'sent' === ct_scan_send_accepted_copy( $upper_event['job_id'] ) && count( $mail ) === $count + 1, 'An upper-case recorded terms hash blocked the accepted copy.' );
84+
85+
// A job that is no longer live when the copy would be sent gets none, and its copy state is untouched.
86+
foreach ( array( 'cancelled', 'revoked', 'evidence_changed' ) as $how ) {
87+
list( $dead_id, $dead_fields ) = ct_test_request( 'strict-dead-' . str_replace( '_', '-', $how ) . '.example.net' );
88+
$request_ids[] = $dead_id;
89+
ct_test_bind( $dead_id, $dead_fields );
90+
$dead_event = ct_scan_on_request_ready( $dead_id, $dead_fields );
91+
ct_test_queue_job( $dead_event['job_id'] );
92+
if ( 'cancelled' === $how ) {
93+
$wpdb->update( $jobs, array( 'status' => 'cancelled', 'error_code' => 'customer_stop' ), array( 'job_id' => $dead_event['job_id'] ) );
94+
} elseif ( 'revoked' === $how ) {
95+
$wpdb->update( $jobs, array( 'revoked_at' => ct_scan_utc_now() ), array( 'job_id' => $dead_event['job_id'] ) );
96+
} else {
97+
update_post_meta( $dead_id, '_ct_summary_text', wp_slash( ct_test_summary( $dead_fields ) . "Note: edited afterwards\n" ) );
98+
}
99+
$count = count( $mail );
100+
$dead = ct_scan_send_accepted_copy( $dead_event['job_id'] );
101+
ct_test_assert( is_wp_error( $dead ) && count( $mail ) === $count && 'not_sent' === ct_scan_row( $dead_event['job_id'] )->accepted_copy_status, "An accepted copy was sent for a job that is no longer live ($how)." );
102+
}
103+
74104
// The requirement is frozen into each job. A job created before the version was listed as requiring
75105
// bound evidence keeps working when the configuration later requires it, and one created bound keeps
76106
// needing it: configuration can neither cancel nor relax an authorization already given.

‎integrations/wordpress/tests/evidence.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ function ct_test_refused_with( $request_id, $fields, $code, $message ) {
6363
'summary without the limits line' => array( array( 'summary_drop' => 'Limits' ), 'binding_mismatch' ),
6464
'summary with other limits' => array( array( 'summary' => ct_test_summary( array( 'target' => 'https://evidence-case.example.net/', 'name' => 'Fixture Reviewer', 'email' => 'fixture@example.com' ), array( 'Limits' => 'this website only; at most 2000 requests' ) ) ), 'binding_mismatch' ),
6565
'summary without the period line' => array( array( 'summary_drop' => 'Authorization' ), 'binding_mismatch' ),
66+
// Byte-identical terms and Summary share one computed hash; every recorded digest must still be compared.
67+
'snapshot equals summary, terms hash differs' => array( array( 'snapshot_is_summary' => true, 'meta' => array( '_ct_terms_sha256' => str_repeat( 'b', 64 ) ) ), 'binding_mismatch' ),
68+
'snapshot equals summary, stale terms digest' => array( array( 'snapshot_is_summary' => true, 'meta' => array( '_ct_terms_shown_digest' => str_repeat( 'a', 64 ) ) ), 'binding_mismatch' ),
6669
'bound pieces without the marker' => array( array( 'meta' => array( '_ct_terms_binding' => null ) ), 'binding_required' ),
6770
'marker with another value' => array( array( 'meta' => array( '_ct_terms_binding' => 'yes' ) ), 'binding_required' ),
6871
);
@@ -75,6 +78,9 @@ function ct_test_refused_with( $request_id, $fields, $code, $message ) {
7578
if ( isset( $mutations['summary_drop'] ) ) {
7679
$mutations = array( 'summary' => implode( "\n", array_filter( explode( "\n", ct_test_summary( $case_fields ) ), static function ( $line ) use ( $mutations ) { return '' !== $line && 0 !== strpos( $line, $mutations['summary_drop'] . ':' ); } ) ) . "\n" );
7780
}
81+
if ( ! empty( $mutations['snapshot_is_summary'] ) ) {
82+
$mutations['snapshot'] = ct_test_summary( $case_fields );
83+
}
7884
// Summaries built for "evidence-case" must name this request's website for the other checks to be the only fault.
7985
if ( isset( $mutations['summary'] ) ) {
8086
$mutations['summary'] = str_replace( 'https://evidence-case.example.net/', $case_fields['target'], $mutations['summary'] );
@@ -83,6 +89,29 @@ function ct_test_refused_with( $request_id, $fields, $code, $message ) {
8389
ct_test_refused_with( $case_id, $case_fields, $case[1], 'Case "' . $label . '"' );
8490
}
8591

92+
// Control: byte-identical terms and Summary with every digest consistent is accepted (the cases above fail only on the digest).
93+
list( $same_id, $same_fields ) = ct_test_request( 'evidence-same-bytes.example.net' );
94+
$request_ids[] = $same_id;
95+
ct_test_bind( $same_id, $same_fields, array( 'snapshot' => ct_test_summary( $same_fields ) ) );
96+
ct_test_assert( is_array( ct_scan_on_request_ready( $same_id, $same_fields ) ), 'Byte-identical but consistent terms and Summary were refused.' );
97+
98+
// An evidence key holding more than one row is ambiguous (get_post_meta( ..., true ) would pick the first): refused,
99+
// even when the extra row repeats the same value.
100+
foreach ( array( '_ct_terms_binding', '_ct_terms_snapshot', '_ct_terms_sha256', '_ct_terms_shown_digest', '_ct_summary_text', '_ct_summary_sha256', '_ct_summary_shown_digest' ) as $i => $key ) {
101+
list( $dup_id, $dup_fields ) = ct_test_request( 'evidence-duplicate-' . $i . '.example.net' );
102+
$request_ids[] = $dup_id;
103+
$dup_meta = ct_test_bind( $dup_id, $dup_fields );
104+
add_post_meta( $dup_id, $key, wp_slash( 0 === $i % 2 ? $dup_meta[ $key ] : $dup_meta[ $key ] . 'x' ) );
105+
ct_test_refused_with( $dup_id, $dup_fields, 'evidence_duplicate', 'Duplicate ' . $key );
106+
}
107+
// ...and a duplicate row added after the job exists ends the authorization.
108+
$dup_running = ct_test_running_job_for_bound( 'evidence-duplicate-running.example.net' );
109+
$request_ids[] = $dup_running['request_id'];
110+
ct_test_assert( 200 === ct_test_admit( $dup_running )->get_status(), 'A bound running job was not admitted (duplicate case).' );
111+
add_post_meta( $dup_running['request_id'], '_ct_summary_text', wp_slash( ct_scan_meta_string( $dup_running['request_id'], '_ct_summary_text' ) ) );
112+
ct_test_age_admission( $dup_running['job_id'] );
113+
$dup_admit = ct_test_admit( $dup_running );
114+
ct_test_assert( 410 === $dup_admit->get_status() && 'cancelled' === ct_scan_row( $dup_running['job_id'] )->status, 'A duplicate evidence row added after acceptance did not end the authorization.' );
86115

87116
// ---- Defaults: strict unless an r2-or-earlier version is explicitly grandfathered -------------------------------
88117
function ct_test_gate_env( $env ) {

0 commit comments

Comments
 (0)