-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: bound the work and traffic one peer can trigger through REQUEST_SYNC #885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Chessing234
wants to merge
4
commits into
permissionlesstech:main
Choose a base branch
from
Chessing234:fix/bound-request-sync-amplification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d6a1c3d
Bound the work a REQUEST_SYNC peer can trigger
Chessing234 48b85fb
Cover the sync budget and the honest-peer path
Chessing234 3c126b2
Budget REQUEST_SYNC by ingress link, not by sender ID
Chessing234 2d80548
Specify the REQUEST_SYNC responder budget
Chessing234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
app/src/test/kotlin/com/bitchat/android/sync/GossipSyncRequestBudgetTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| package com.bitchat.android.sync | ||
|
|
||
| import com.bitchat.android.model.RequestSyncPacket | ||
| import com.bitchat.android.protocol.BitchatPacket | ||
| import com.bitchat.android.protocol.MessageType | ||
| import com.bitchat.android.protocol.SpecialRecipients | ||
| import com.bitchat.android.util.AppConstants | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
|
|
||
| /** | ||
| * REQUEST_SYNC is a public packet handled straight off the wire, so an unauthenticated neighbor | ||
| * decides how often we decode a filter, hash every stored packet, and transmit what it claims to | ||
| * lack. These cover the budget that bounds that without changing what an honest peer receives. | ||
| */ | ||
| class GossipSyncRequestBudgetTest { | ||
|
|
||
| private companion object { | ||
| const val CAPACITY = 5 | ||
| const val MY_PEER = "aabbccddeeff0011" | ||
| const val REQUESTER = "1122334455667788" | ||
| } | ||
|
|
||
| /** A filter that claims to hold nothing, so every stored packet counts as missing. */ | ||
| private val emptyFilter = RequestSyncPacket(p = 1, m = 1L, data = ByteArray(0)) | ||
|
|
||
| private var clock = 1_000_000L | ||
| private val sentTo = mutableListOf<Pair<String, BitchatPacket>>() | ||
|
|
||
| private lateinit var manager: GossipSyncManager | ||
|
|
||
| private val delegate = object : GossipSyncManager.Delegate { | ||
| override fun sendPacket(packet: BitchatPacket) = Unit | ||
| override fun sendPacketToPeer(peerID: String, packet: BitchatPacket) { | ||
| sentTo.add(peerID to packet) | ||
| } | ||
|
|
||
| override fun signPacketForBroadcast(packet: BitchatPacket): BitchatPacket = packet | ||
| } | ||
|
|
||
| private val config = object : GossipSyncManager.ConfigProvider { | ||
| override fun seenCapacity(): Int = CAPACITY | ||
| override fun gcsMaxBytes(): Int = 400 | ||
| override fun gcsTargetFpr(): Double = 0.01 | ||
| } | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| sentTo.clear() | ||
| clock = 1_000_000L | ||
| manager = GossipSyncManager( | ||
| myPeerID = MY_PEER, | ||
| scope = CoroutineScope(Dispatchers.Unconfined), | ||
| configProvider = config, | ||
| nowMs = { clock } | ||
| ) | ||
| manager.delegate = delegate | ||
| } | ||
|
|
||
| private fun broadcastMessage(body: String): BitchatPacket = BitchatPacket( | ||
| type = MessageType.MESSAGE.value, | ||
| senderID = ByteArray(8) { 0x11 }, | ||
| recipientID = SpecialRecipients.BROADCAST, | ||
| timestamp = clock.toULong(), | ||
| payload = body.toByteArray(), | ||
| ttl = 3u | ||
| ) | ||
|
|
||
| private fun storeMessages(count: Int) { | ||
| repeat(count) { manager.onPublicPacketSeen(broadcastMessage("m$it")) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `an honest peer still receives every packet it is missing`() { | ||
| storeMessages(CAPACITY) | ||
|
|
||
| manager.handleRequestSync(REQUESTER, emptyFilter) | ||
|
|
||
| assertEquals(CAPACITY, sentTo.size) | ||
| assertTrue(sentTo.all { it.first == REQUESTER }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `back to back requests stop being serviced after the burst allowance`() { | ||
| storeMessages(CAPACITY) | ||
|
|
||
| // No time passes between these, so nothing refills. | ||
| repeat(100) { manager.handleRequestSync(REQUESTER, emptyFilter) } | ||
|
|
||
| // Before the fix every one of the 100 requests replayed all CAPACITY packets. | ||
| assertEquals(CAPACITY, sentTo.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a flood cannot pull more packets than the refill window allows`() { | ||
| storeMessages(CAPACITY) | ||
|
|
||
| // Hammer for a simulated minute, far faster than any honest peer syncs. | ||
| repeat(600) { | ||
| clock += 100L | ||
| manager.handleRequestSync(REQUESTER, emptyFilter) | ||
| } | ||
|
|
||
| // 60s of hammering earns two windows' worth of packets, not 600 replays. | ||
| val windows = 60_000L / AppConstants.Sync.RESPONSE_REFILL_WINDOW_MS | ||
| assertTrue( | ||
| "sent ${sentTo.size} packets, expected at most ${CAPACITY * (windows + 1)}", | ||
| sentTo.size <= CAPACITY * (windows + 1) | ||
| ) | ||
| assertTrue("honest sync must still make progress", sentTo.isNotEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `the budget refills so a peer can keep syncing`() { | ||
| storeMessages(CAPACITY) | ||
|
|
||
| manager.handleRequestSync(REQUESTER, emptyFilter) | ||
| assertEquals(CAPACITY, sentTo.size) | ||
|
|
||
| sentTo.clear() | ||
| clock += AppConstants.Sync.RESPONSE_REFILL_WINDOW_MS | ||
| manager.handleRequestSync(REQUESTER, emptyFilter) | ||
|
|
||
| assertEquals(CAPACITY, sentTo.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `one flooding peer does not consume another peer's budget`() { | ||
| storeMessages(CAPACITY) | ||
| repeat(50) { manager.handleRequestSync(REQUESTER, emptyFilter) } | ||
| sentTo.clear() | ||
|
|
||
| manager.handleRequestSync("99aabbccddeeff00", emptyFilter) | ||
|
|
||
| assertEquals(CAPACITY, sentTo.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `requester bookkeeping stays bounded when peer ids are spoofed`() { | ||
| storeMessages(1) | ||
|
|
||
| repeat(AppConstants.Sync.MAX_TRACKED_REQUESTERS * 4) { i -> | ||
| manager.handleRequestSync("%016x".format(i), emptyFilter) | ||
| } | ||
|
|
||
| assertEquals( | ||
| AppConstants.Sync.MAX_TRACKED_REQUESTERS, | ||
| manager.trackedRequesterCount() | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a backwards clock jump does not hand out free budget`() { | ||
| storeMessages(CAPACITY) | ||
|
|
||
| repeat(100) { | ||
| clock -= 5_000L | ||
| manager.handleRequestSync(REQUESTER, emptyFilter) | ||
| } | ||
|
|
||
| assertEquals(CAPACITY, sentTo.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `clearing sync state also clears the budgets`() { | ||
| storeMessages(CAPACITY) | ||
| manager.handleRequestSync(REQUESTER, emptyFilter) | ||
|
|
||
| manager.clear() | ||
|
|
||
| assertEquals(0, manager.trackedRequesterCount()) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
REQUEST_SYNCis intentionally outside the signed public-packet set,fromPeerIDhere is just the sender ID parsed from the packet, so any neighbor can mint fresh buckets by rotating spoofed IDs or spend another peer's request tokens by using that peer's ID. With the 256-entry LRU, a 257-ID cycle keeps each request admitted before decode/hashing, so the flood path this change is meant to close remains available; key the budget to a non-spoofable ingress/link identity or authenticate the claimed peer before granting a new bucket.AGENTS.md reference: AGENTS.md:L90-L91
Useful? React with 👍 / 👎.