diff --git a/dd.sql b/dd.sql index 4ed5605..8c1b16d 100644 --- a/dd.sql +++ b/dd.sql @@ -411,6 +411,11 @@ declare message_id bigint; frozen boolean; begin + -- A message is removed from this host only on purpose: a transaction that + -- sets fmsg.purge (SET LOCAL fmsg.purge = 'on') may delete its parts, so + -- the message can then be deleted. Once its row is gone, a reply to it is + -- rejected as parent not found. + if TG_OP='DELETE' and current_setting('fmsg.purge', true) = 'on' then return OLD; end if; -- Delivery and read receipts do not change a protocol recipient. if TG_OP='UPDATE' then if TG_TABLE_NAME='msg_to' then diff --git a/pkg/message/store_integration_test.go b/pkg/message/store_integration_test.go index 923969c..335f33d 100644 --- a/pkg/message/store_integration_test.go +++ b/pkg/message/store_integration_test.go @@ -137,6 +137,55 @@ func TestFinalizeAndImmutability(t *testing.T) { t.Fatal("committed sent message without hash") } } + +// A purge transaction may remove a sent message and its parts; nothing else may. +func TestPurgeRemovesSentMessage(t *testing.T) { + db, _ := testStore(t) + root := insertDraft(t, db, nil, "root") + hash := seal(t, db, root) + child := insertDraft(t, db, root, "child") + seal(t, db, child) + if _, err := db.Exec(`INSERT INTO msg_attachment(msg_id,filename,filesize,filepath) VALUES($1,'a.txt',0,'')`, root); err == nil { + t.Fatal("attachment added to a sent message") + } + if _, err := db.Exec(`DELETE FROM msg_to WHERE msg_id=$1`, root); err == nil { + t.Fatal("deleted parts of a sent message without purging") + } + + tx, err := db.Begin() + if err != nil { + t.Fatal(err) + } + defer tx.Rollback() + for _, q := range []string{ + `SET LOCAL fmsg.purge = 'on'`, + `UPDATE msg SET pid = NULL WHERE pid = ` + fmt.Sprint(root), + `DELETE FROM msg_attachment WHERE msg_id = ` + fmt.Sprint(root), + `DELETE FROM msg_to WHERE msg_id = ` + fmt.Sprint(root), + `DELETE FROM msg WHERE id = ` + fmt.Sprint(root), + } { + if _, err := tx.Exec(q); err != nil { + t.Fatalf("%s: %v", q, err) + } + } + if err := tx.Commit(); err != nil { + t.Fatal(err) + } + var n int + if err := db.QueryRow(`SELECT count(*) FROM msg WHERE sha256 = $1`, hash).Scan(&n); err != nil || n != 0 { + t.Fatalf("purged message still found by hash: %d, %v", n, err) + } + // The reply survives, keeping the hash of the parent it was sent to. + var parent []byte + if err := db.QueryRow(`SELECT psha256 FROM msg WHERE id = $1 AND pid IS NULL`, child).Scan(&parent); err != nil || !bytes.Equal(parent, hash) { + t.Fatalf("reply after purge: %x, %v", parent, err) + } + // Outside a purge, the reply's parts stay immutable. + if _, err := db.Exec(`DELETE FROM msg_to WHERE msg_id=$1`, child); err == nil { + t.Fatal("deleted parts of a sent message after a purge transaction ended") + } +} + func TestFinalizeDraftBatchesAndRollback(t *testing.T) { db, _ := testStore(t) root := insertDraft(t, db, nil, "root with draft batch")