From ba3f7d6e7d92ef2f640eaa6d288c3d1f143904e8 Mon Sep 17 00:00:00 2001 From: Taksh Date: Fri, 4 Sep 2026 22:51:55 +0530 Subject: [PATCH] Randomize seal and gift-wrap timestamps up to 48h into the past iOS used a +/-15min jitter around now for seal and gift-wrap created_at. Android samples uniformly from the past 48h, which is what forces the wider gift-wrap subscription window. Match that past-only distribution so iOS envelopes get the same timing-correlation resistance and stay compatible with receivers that already look back 48h. --- bitchat/Nostr/NostrProtocol.swift | 34 +++++++++---------- bitchat/Services/TransportConfig.swift | 8 ++++- .../NostrInboundPipelineTimestampTests.swift | 17 ++++++++++ 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/bitchat/Nostr/NostrProtocol.swift b/bitchat/Nostr/NostrProtocol.swift index 2e3e35d8..7e3864e3 100644 --- a/bitchat/Nostr/NostrProtocol.swift +++ b/bitchat/Nostr/NostrProtocol.swift @@ -745,25 +745,23 @@ struct NostrProtocol { return sharedSecretData } + /// Randomized `created_at` for seals and gift wraps. Samples uniformly + /// from `[now − maxPast, now]` (never future) so relays cannot correlate + /// envelope timing with the true send time carried on the inner rumor. + /// Matches Android `NostrCrypto.randomizeTimestampUpToPast`. Internal so + /// tests can pin `now` / `maxPast`. + static func randomizedEnvelopeTimestamp( + now: Date = Date(), + maxPast: TimeInterval = TransportConfig.nostrGiftWrapTimestampRandomizationSeconds + ) -> Date { + let clampedPast = max(0, maxPast) + // Inclusive of both endpoints, matching Android's nextInt(maxPast+1). + let offsetSeconds = Int.random(in: 0...Int(clampedPast)) + return now.addingTimeInterval(-TimeInterval(offsetSeconds)) + } + private static func randomizedTimestamp() -> Date { - // Add random offset to current time for privacy - // This prevents timing correlation attacks while the actual message timestamp - // is preserved in the encrypted rumor - let offset = TimeInterval.random(in: -900...900) // +/- 15 minutes - let now = Date() - let randomized = now.addingTimeInterval(offset) - - // Log with explicit UTC and local time for debugging - let formatter = DateFormatter() - // - formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" - formatter.timeZone = TimeZone(abbreviation: "UTC") - - formatter.timeZone = TimeZone.current - - // Timestamp randomized for privacy - - return randomized + randomizedEnvelopeTimestamp() } } diff --git a/bitchat/Services/TransportConfig.swift b/bitchat/Services/TransportConfig.swift index 8d17f9ea..bddd94ef 100644 --- a/bitchat/Services/TransportConfig.swift +++ b/bitchat/Services/TransportConfig.swift @@ -219,12 +219,18 @@ enum TransportConfig { static let nostrDMSubscribeLookbackSeconds: TimeInterval = 86400 // Tolerated clock skew for client-side Nostr DM timestamp windows. static let nostrDMMaxClockSkewSeconds: TimeInterval = 900 + /// Outer gift-wrap / seal `created_at` randomization floor. Matches + /// Android `NostrCrypto.randomizeTimestampUpToPast` (up to 48h into the + /// past). The actual message time lives on the inner rumor. + static let nostrGiftWrapTimestampRandomizationSeconds: TimeInterval = 172_800 + // 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 + static let nostrGiftWrapMaxAgeSeconds: TimeInterval = + nostrGiftWrapTimestampRandomizationSeconds + 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 diff --git a/bitchatTests/NostrInboundPipelineTimestampTests.swift b/bitchatTests/NostrInboundPipelineTimestampTests.swift index 7267a8cd..74bbd9bc 100644 --- a/bitchatTests/NostrInboundPipelineTimestampTests.swift +++ b/bitchatTests/NostrInboundPipelineTimestampTests.swift @@ -48,3 +48,20 @@ struct NostrInboundPipelineTimestampTests { #expect(NostrInboundPipeline.isAcceptableGiftWrapTimestamp(nowSeconds - lookback - 3_600, now: now)) } } + +struct NostrEnvelopeTimestampRandomizationTests { + @Test("Envelope timestamps stay in [now-maxPast, now] and never go future") + func samplesPastOnlyWindow() { + let now = Date(timeIntervalSince1970: 1_700_000_000) + let maxPast: TimeInterval = 172_800 + for _ in 0..<200 { + let ts = NostrProtocol.randomizedEnvelopeTimestamp(now: now, maxPast: maxPast) + #expect(ts <= now) + #expect(ts >= now.addingTimeInterval(-maxPast)) + } + // Zero past collapses to exactly now. + #expect(NostrProtocol.randomizedEnvelopeTimestamp(now: now, maxPast: 0) == now) + // Negative past is treated as zero. + #expect(NostrProtocol.randomizedEnvelopeTimestamp(now: now, maxPast: -10) == now) + } +}