fix: preserve the delivery window for queued gift wraps

This commit is contained in:
Taksh 2026-09-30 22:21:22 +05:30
parent 108a9eb00f
commit cf93872d21
3 changed files with 39 additions and 12 deletions

View File

@ -227,9 +227,11 @@ enum TransportConfig {
// 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
// accept window is 48h plus skew so a wrap at the randomization floor
// still clears a mildly slow clock.
static let nostrGiftWrapMaxAgeSeconds: TimeInterval = 172_800 + 900
// 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

View File

@ -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)

View File

@ -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,4 +47,31 @@ 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))
}
}