fix: preserve the delivery window for queued gift wraps

This commit is contained in:
Taksh committed 2026-09-30 22:33:42 +05:30
1 parent cdb1faaaba
commit 45b5eed2fe
3 files changed
+43 -17

No files matched your search

+9 -8
View File
@@ -230,17 +230,18 @@ enum TransportConfig {
/// keeps receding after send, so lookback − skew (85_500) still loses DMs
/// to a peer who comes online hours later. The two-hour margin reserves
/// room for that recession until #1706's wider subscribe is everywhere.
/// Accept stays at 48h+skew below so older Android wraps still clear.
/// Receive-side age limits independently allow queued older wraps.
/// The true send time lives on the inner rumor either way.
static let nostrGiftWrapTimestampRandomizationSeconds: TimeInterval = 79_200
// Outer gift-wrap age ceiling when *receiving*. Kept at the historical
// NIP-17 48h floor (+ skew) so wraps from peers that still randomize
// that deep (or that Android's 48h subscribe will deliver) are not
// dropped client-side after the relay hands them over. Independent of
// the send-side randomization above — do not derive one from the other.
static let nostrGiftWrapMaxAgeSeconds: TimeInterval =
172_800 + nostrDMMaxClockSkewSeconds
// Outer gift-wrap age ceiling. Android (and NIP-17-style clients) may
// randomize the wrap's created_at up to 48h into the past; relays that
// honor `since` will not deliver those wraps under a 24h filter. The
// envelope may then wait offline for the full inner-rumor delivery window.
// Keep that grace in the outer gate and relay filter; the inner timestamp
// still independently enforces message freshness after decryption.
static let nostrGiftWrapMaxAgeSeconds: TimeInterval = 172_800
+ nostrDMSubscribeLookbackSeconds + nostrDMMaxClockSkewSeconds
// A sampled chat message this recent means "a conversation is happening
// there" for the empty-timeline nearby-activity hint.
static let uiGeohashChatActivityWindowSeconds: TimeInterval = 900
@@ -570,12 +570,9 @@ final class NostrInboundPipeline {
}
extension NostrInboundPipeline {
/// Client-side mirror of the relay-side `since` filter on DM
/// subscriptions: a relay that ignores `since` — or replays archived
/// events — must not inject stale or future-dated DMs. The inner rumor
/// timestamp is the sender's true send time (only the outer gift wrap
/// is randomized per NIP-17), so the plausible window is the
/// subscription lookback plus tolerated clock skew on both ends.
/// Inner-message freshness, independent of the wider outer-wrap relay
/// filter. The rumor carries the sender's true send time, so accepting an
/// older randomized envelope must not admit stale or future-dated DMs.
/// Internal (not private) so tests can pin the window directly.
static func isPlausibleRumorTimestamp(_ ts: Int, now: Date = Date()) -> Bool {
let age = now.timeIntervalSince1970 - TimeInterval(ts)
@@ -585,8 +582,9 @@ extension NostrInboundPipeline {
}
/// Accept an outer gift-wrap `created_at` that is not in the far future
/// and not older than the 48h randomization ceiling plus skew. Checked
/// before decrypt so a hostile relay cannot force ECDH work with
/// and not older than the randomization ceiling plus the inner delivery
/// window and clock skew. Checked before decrypt so a hostile relay
/// cannot force ECDH work with
/// ancient or far-future wraps.
static func isAcceptableGiftWrapTimestamp(_ createdAt: Int, now: Date = Date()) -> Bool {
let age = now.timeIntervalSince1970 - TimeInterval(createdAt)
@@ -31,7 +31,7 @@ struct NostrInboundPipelineTimestampTests {
#expect(!NostrInboundPipeline.isPlausibleRumorTimestamp(nowSeconds - lookback - skew - 60, now: now))
}
@Test("Outer gift wraps inside the 48h-plus-skew window are accepted")
@Test("Outer gift wraps inside the randomization and delivery window are accepted")
func acceptsPlausibleGiftWrapTimestamps() {
#expect(NostrInboundPipeline.isAcceptableGiftWrapTimestamp(nowSeconds, now: now))
// Android randomizes wraps up to 48h into the past.
@@ -47,6 +47,33 @@ struct NostrInboundPipelineTimestampTests {
// the Android→iOS delivery gap this gate exists to keep open.
#expect(NostrInboundPipeline.isAcceptableGiftWrapTimestamp(nowSeconds - lookback - 3_600, now: now))
}
@Test("A wrap randomized 48h back remains deliverable after 23h offline")
func acceptsQueuedGiftWrapWithFreshRumor() {
// Fixed wire timestamps: created together 23h before receipt, with the
// outer timestamp moved another 48h back. Do not derive the vector
// from the policy constants: that would hide an undersized window.
let rumorCreatedAt = 1_699_917_200
let wrapCreatedAt = 1_699_744_400
#expect(NostrInboundPipeline.isPlausibleRumorTimestamp(rumorCreatedAt, now: now))
#expect(NostrInboundPipeline.isAcceptableGiftWrapTimestamp(wrapCreatedAt, now: now))
let subscriptionSince = now.addingTimeInterval(-TransportConfig.nostrGiftWrapMaxAgeSeconds)
#expect(Date(timeIntervalSince1970: TimeInterval(wrapCreatedAt)) >= subscriptionSince)
}
@Test("Delivery grace covers the inner boundary without extending rumor freshness")
func preservesIndependentTimestampBoundaries() {
// At this receive time, 24h plus 15min is the last valid inner rumor;
// its maximally randomized wrap is exactly 72h plus 15min old.
#expect(NostrInboundPipeline.isPlausibleRumorTimestamp(1_699_912_700, now: now))
#expect(NostrInboundPipeline.isAcceptableGiftWrapTimestamp(1_699_739_900, now: now))
#expect(!NostrInboundPipeline.isAcceptableGiftWrapTimestamp(1_699_739_899, now: now))
// A freshly rewrapped envelope cannot revive an expired inner rumor.
#expect(NostrInboundPipeline.isAcceptableGiftWrapTimestamp(nowSeconds, now: now))
#expect(!NostrInboundPipeline.isPlausibleRumorTimestamp(1_699_912_699, now: now))
#expect(!NostrInboundPipeline.isAcceptableGiftWrapTimestamp(1_700_000_901, now: now))
}
}
struct NostrEnvelopeTimestampRandomizationTests {