mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-11-29 08:29:22 +00:00
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.
This commit is contained in:
1 parent
4a555b9d83
commit
ba3f7d6e7d
3 files changed
+40
-19
No files matched your search
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user