Merge c1fd702821e0ecb01d870f67c63f2be611247696 into 04995f68e470a18d1fc99f86fa326c2a87bab4f2

This commit is contained in:
Taksh Kothari 2026-11-05 12:21:14 +05:30 committed by GitHub
commit ef84a3f153
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 577 additions and 80 deletions

View File

@ -16184,187 +16184,187 @@
}
},
"content.delivery.reason.private_media_too_many_fragments" : {
"comment" : "Failure reason when private media exceeds the Android-compatible fragment limit",
"comment" : "Failure reason when private media exceeds the recipient's fragment limit; %lld is that limit",
"extractionState" : "manual",
"localizations" : {
"ar" : {
"stringUnit" : {
"state" : "translated",
"value" : "الملف كبير جدًا على تطبيق جهة الاتصال هذه (أكثر من 256 جزءًا عبر الشبكة المتداخلة)"
"value" : "الملف كبير جدًا على تطبيق جهة الاتصال هذه (أكثر من %lld جزءًا عبر الشبكة المتداخلة)"
}
},
"bn" : {
"stringUnit" : {
"state" : "translated",
"value" : "এই কন্টাক্টের ক্লায়েন্টের জন্য ফাইলটি খুব বড় (256টির বেশি মেশ ফ্র্যাগমেন্ট)"
"value" : "এই কন্টাক্টের ক্লায়েন্টের জন্য ফাইলটি খুব বড় (%lldটির বেশি মেশ ফ্র্যাগমেন্ট)"
}
},
"de" : {
"stringUnit" : {
"state" : "translated",
"value" : "Datei ist zu groß für den Client dieses Kontakts (mehr als 256 Mesh-Fragmente)"
"value" : "Datei ist zu groß für den Client dieses Kontakts (mehr als %lld Mesh-Fragmente)"
}
},
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "File is too large for this contact's client (more than 256 mesh fragments)"
"value" : "File is too large for this contact's client (more than %lld mesh fragments)"
}
},
"es" : {
"stringUnit" : {
"state" : "translated",
"value" : "El archivo es demasiado grande para el cliente de este contacto (más de 256 fragmentos de malla)"
"value" : "El archivo es demasiado grande para el cliente de este contacto (más de %lld fragmentos de malla)"
}
},
"fa" : {
"stringUnit" : {
"state" : "translated",
"value" : "فایل برای کلاینت این مخاطب خیلی بزرگ است (بیش از 256 قطعهٔ مش)"
"value" : "فایل برای کلاینت این مخاطب خیلی بزرگ است (بیش از %lld قطعهٔ مش)"
}
},
"fil" : {
"stringUnit" : {
"state" : "translated",
"value" : "Masyadong malaki ang file para sa client ng contact na ito (mahigit 256 mesh fragment)"
"value" : "Masyadong malaki ang file para sa client ng contact na ito (mahigit %lld mesh fragment)"
}
},
"fr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Le fichier est trop volumineux pour le client de ce contact (plus de 256 fragments mesh)"
"value" : "Le fichier est trop volumineux pour le client de ce contact (plus de %lld fragments mesh)"
}
},
"he" : {
"stringUnit" : {
"state" : "translated",
"value" : "הקובץ גדול מדי עבור הקליינט של איש הקשר הזה (יותר מ-256 מקטעי mesh)"
"value" : "הקובץ גדול מדי עבור הקליינט של איש הקשר הזה (יותר מ-%lld מקטעי mesh)"
}
},
"hi" : {
"stringUnit" : {
"state" : "translated",
"value" : "यह फ़ाइल इस संपर्क के क्लाइंट के लिए बहुत बड़ी है (256 से ज़्यादा मेश फ़्रैगमेंट)"
"value" : "यह फ़ाइल इस संपर्क के क्लाइंट के लिए बहुत बड़ी है (%lld से ज़्यादा मेश फ़्रैगमेंट)"
}
},
"id" : {
"stringUnit" : {
"state" : "translated",
"value" : "File terlalu besar untuk klien kontak ini (lebih dari 256 fragmen mesh)"
"value" : "File terlalu besar untuk klien kontak ini (lebih dari %lld fragmen mesh)"
}
},
"it" : {
"stringUnit" : {
"state" : "translated",
"value" : "Il file è troppo grande per il client di questo contatto (più di 256 frammenti mesh)"
"value" : "Il file è troppo grande per il client di questo contatto (più di %lld frammenti mesh)"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "このファイルはこの連絡先のクライアントには大きすぎます(メッシュフラグメントが256個を超えています)"
"value" : "このファイルはこの連絡先のクライアントには大きすぎます(メッシュフラグメントが%lld個を超えています)"
}
},
"ko" : {
"stringUnit" : {
"state" : "translated",
"value" : "이 연락처의 클라이언트에는 파일이 너무 커요(메시 조각 256개 초과)"
"value" : "이 연락처의 클라이언트에는 파일이 너무 커요(메시 조각 %lld개 초과)"
}
},
"ms" : {
"stringUnit" : {
"state" : "translated",
"value" : "Fail terlalu besar untuk klien kenalan ini (lebih daripada 256 fragmen mesh)"
"value" : "Fail terlalu besar untuk klien kenalan ini (lebih daripada %lld fragmen mesh)"
}
},
"ne" : {
"stringUnit" : {
"state" : "translated",
"value" : "यो सम्पर्कको क्लाइन्टका लागि फाइल धेरै ठूलो छ (256 भन्दा बढी मेश खण्डहरू)"
"value" : "यो सम्पर्कको क्लाइन्टका लागि फाइल धेरै ठूलो छ (%lld भन्दा बढी मेश खण्डहरू)"
}
},
"nl" : {
"stringUnit" : {
"state" : "translated",
"value" : "Het bestand is te groot voor de client van dit contact (meer dan 256 mesh-fragmenten)"
"value" : "Het bestand is te groot voor de client van dit contact (meer dan %lld mesh-fragmenten)"
}
},
"pl" : {
"stringUnit" : {
"state" : "translated",
"value" : "Plik jest za duży dla klienta tego kontaktu (ponad 256 fragmentów mesh)"
"value" : "Plik jest za duży dla klienta tego kontaktu (ponad %lld fragmentów mesh)"
}
},
"pt" : {
"stringUnit" : {
"state" : "translated",
"value" : "O ficheiro é demasiado grande para o cliente deste contacto (mais de 256 fragmentos da malha)"
"value" : "O ficheiro é demasiado grande para o cliente deste contacto (mais de %lld fragmentos da malha)"
}
},
"pt-BR" : {
"stringUnit" : {
"state" : "translated",
"value" : "O arquivo é grande demais para o cliente deste contato (mais de 256 fragmentos da malha)"
"value" : "O arquivo é grande demais para o cliente deste contato (mais de %lld fragmentos da malha)"
}
},
"ru" : {
"stringUnit" : {
"state" : "translated",
"value" : "Файл слишком большой для клиента этого контакта (больше 256 фрагментов mesh-сети)"
"value" : "Файл слишком большой для клиента этого контакта (больше %lld фрагментов mesh-сети)"
}
},
"sv" : {
"stringUnit" : {
"state" : "translated",
"value" : "Filen är för stor för den här kontaktens klient (fler än 256 mesh-fragment)"
"value" : "Filen är för stor för den här kontaktens klient (fler än %lld mesh-fragment)"
}
},
"ta" : {
"stringUnit" : {
"state" : "translated",
"value" : "இந்தத் தொடர்பின் கிளையண்டுக்கு இந்தக் கோப்பு மிகப் பெரியது (256-க்கும் மேற்பட்ட மெஷ் துண்டுகள்)"
"value" : "இந்தத் தொடர்பின் கிளையண்டுக்கு இந்தக் கோப்பு மிகப் பெரியது (%lld-க்கும் மேற்பட்ட மெஷ் துண்டுகள்)"
}
},
"th" : {
"stringUnit" : {
"state" : "translated",
"value" : "ไฟล์ใหญ่เกินไปสำหรับไคลเอ็นต์ของผู้ติดต่อรายนี้ (เกิน 256 ส่วนย่อยของ mesh)"
"value" : "ไฟล์ใหญ่เกินไปสำหรับไคลเอ็นต์ของผู้ติดต่อรายนี้ (เกิน %lld ส่วนย่อยของ mesh)"
}
},
"tr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Dosya bu kişinin istemcisi için çok büyük (256'dan fazla mesh parçası)"
"value" : "Dosya bu kişinin istemcisi için çok büyük (mesh parça sınırı: %lld)"
}
},
"uk" : {
"stringUnit" : {
"state" : "translated",
"value" : "Файл завеликий для клієнта цього контакту (понад 256 фрагментів mesh-мережі)"
"value" : "Файл завеликий для клієнта цього контакту (понад %lld фрагментів mesh-мережі)"
}
},
"ur" : {
"stringUnit" : {
"state" : "translated",
"value" : "یہ فائل اس رابطے کے کلائنٹ کے لیے بہت بڑی ہے (256 سے زیادہ میش ٹکڑے)"
"value" : "یہ فائل اس رابطے کے کلائنٹ کے لیے بہت بڑی ہے (%lld سے زیادہ میش ٹکڑے)"
}
},
"vi" : {
"stringUnit" : {
"state" : "translated",
"value" : "Tệp quá lớn so với ứng dụng của liên hệ này (hơn 256 phân mảnh mesh)"
"value" : "Tệp quá lớn so với ứng dụng của liên hệ này (hơn %lld phân mảnh mesh)"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "文件太大,这位联系人的客户端无法接收(超过 256 个网状网络分片)"
"value" : "文件太大,这位联系人的客户端无法接收(超过 %lld 个网状网络分片)"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "檔案太大,這位聯絡人的用戶端無法接收(超過 256 個網狀網路分段)"
"value" : "檔案太大,這位聯絡人的用戶端無法接收(超過 %lld 個網狀網路分段)"
}
}
}

View File

@ -165,26 +165,54 @@ struct AnnouncementPacket {
/// `[version=0x01][type][length][value]...`
/// - TLV `0x01`: canonical minimal little-endian `PeerCapabilities`
/// - TLV `0x02`: 32-byte Ed25519 signing public key
/// - TLV `0x03`: optional 2-byte big-endian reassembly fragment ceiling
///
/// Unknown TLVs are skipped for forward compatibility. Unknown versions,
/// duplicates, non-canonical capability fields, and malformed lengths are
/// rejected without changing authenticated state.
/// duplicate required fields, non-canonical capabilities, and malformed required
/// lengths are rejected. An unreadable optional ceiling preserves identity and
/// capabilities while selecting the conservative fragment limit.
struct AuthenticatedPeerStatePacket: Equatable {
static let currentVersion: UInt8 = 1
static let signingPublicKeyLength = 32
static let maxReassemblyFragmentsLength = 2
static let malformedReassemblyFragmentCeiling: UInt16 = 256
let capabilities: PeerCapabilities
let signingPublicKey: Data
/// How many BLE fragments this peer will reassemble for one packet, as it
/// reported inside the established Noise session.
///
/// The `.privateMedia` capability bit does not describe reassembly capacity.
/// A missing limit preserves existing sender behavior; this cannot protect
/// unadvertised encrypted receivers with a smaller fragment limit.
/// See `BLEFragmentCeilingPolicy` for the conservative malformed-field and
/// raw migration limits.
let maxReassemblyFragments: UInt16?
init(
capabilities: PeerCapabilities,
signingPublicKey: Data,
maxReassemblyFragments: UInt16? = nil
) {
self.capabilities = capabilities
self.signingPublicKey = signingPublicKey
self.maxReassemblyFragments = maxReassemblyFragments
}
private enum TLVType: UInt8 {
case capabilities = 0x01
case signingPublicKey = 0x02
case maxReassemblyFragments = 0x03
}
func encode() -> Data? {
guard signingPublicKey.count == Self.signingPublicKeyLength else { return nil }
let capabilityBytes = capabilities.encoded()
guard !capabilityBytes.isEmpty, capabilityBytes.count <= 8 else { return nil }
// Zero would advertise a peer that can reassemble nothing, which is
// indistinguishable in effect from refusing every fragmented packet.
// Reject zero rather than putting a meaningless number on the wire.
if let maxReassemblyFragments { guard maxReassemblyFragments > 0 else { return nil } }
var data = Data([Self.currentVersion])
data.append(TLVType.capabilities.rawValue)
@ -193,6 +221,11 @@ struct AuthenticatedPeerStatePacket: Equatable {
data.append(TLVType.signingPublicKey.rawValue)
data.append(UInt8(signingPublicKey.count))
data.append(signingPublicKey)
if let maxReassemblyFragments {
data.append(TLVType.maxReassemblyFragments.rawValue)
data.append(UInt8(Self.maxReassemblyFragmentsLength))
data.append(contentsOf: withUnsafeBytes(of: maxReassemblyFragments.bigEndian) { Data($0) })
}
return data
}
@ -202,13 +235,27 @@ struct AuthenticatedPeerStatePacket: Equatable {
var offset = 1
var capabilities: PeerCapabilities?
var signingPublicKey: Data?
var maxReassemblyFragments: UInt16?
var sawReassemblyCeiling = false
while offset < data.count {
guard offset + 2 <= data.count else { return nil }
guard offset + 2 <= data.count else {
if data[offset] == TLVType.maxReassemblyFragments.rawValue {
maxReassemblyFragments = malformedReassemblyFragmentCeiling
break
}
return nil
}
let typeRaw = data[offset]
let length = Int(data[offset + 1])
offset += 2
guard offset + length <= data.count else { return nil }
guard offset + length <= data.count else {
if typeRaw == TLVType.maxReassemblyFragments.rawValue {
maxReassemblyFragments = malformedReassemblyFragmentCeiling
break
}
return nil
}
let value = Data(data[offset..<(offset + length)])
offset += length
@ -228,13 +275,31 @@ struct AuthenticatedPeerStatePacket: Equatable {
guard signingPublicKey == nil,
value.count == Self.signingPublicKeyLength else { return nil }
signingPublicKey = value
case .maxReassemblyFragments:
// This optional extension must not discard the authenticated
// capabilities and signing key. Ambiguous or unreadable limits
// retain those fields and select the conservative 256 fallback.
guard !sawReassemblyCeiling else {
maxReassemblyFragments = malformedReassemblyFragmentCeiling
continue
}
sawReassemblyCeiling = true
guard value.count == Self.maxReassemblyFragmentsLength else {
maxReassemblyFragments = malformedReassemblyFragmentCeiling
continue
}
let decoded = (UInt16(value[value.startIndex]) << 8)
| UInt16(value[value.startIndex + 1])
maxReassemblyFragments = decoded > 0 ? decoded : malformedReassemblyFragmentCeiling
}
}
guard let capabilities, let signingPublicKey else { return nil }
return AuthenticatedPeerStatePacket(
capabilities: capabilities,
signingPublicKey: signingPublicKey
signingPublicKey: signingPublicKey,
maxReassemblyFragments: maxReassemblyFragments
)
}
}

View File

@ -35,7 +35,8 @@ struct BLEFragmentHeader: Equatable {
let index = Int((UInt16(packet.payload[8]) << 8) | UInt16(packet.payload[9]))
let total = Int((UInt16(packet.payload[10]) << 8) | UInt16(packet.payload[11]))
guard total > 0 && total <= 10_000 && index >= 0 && index < total else {
guard total > 0 && total <= BLEFragmentAssemblyBuffer.maxReassemblyFragments
&& index >= 0 && index < total else {
return nil
}
@ -54,6 +55,11 @@ struct BLEFragmentHeader: Equatable {
}
struct BLEFragmentAssemblyBuffer {
/// Header sanity bound, independent of the per-type byte budget below.
/// This is not an advertised capacity: 10,000 large fragments would exceed
/// that byte budget long before reaching the count limit.
static let maxReassemblyFragments = 10_000
enum AppendResult: Equatable {
case stored(header: BLEFragmentHeader, started: Bool)
case complete(header: BLEFragmentHeader, reassembledData: Data, started: Bool)

View File

@ -0,0 +1,63 @@
import BitFoundation
import Foundation
/// Applies authenticated peer fragment-count limits to outbound media.
/// A peer limit may lower either path, but may raise the migration ceiling only
/// on encrypted media. iOS does not advertise its own count: its reassembler
/// has a separate byte budget that cannot be expressed by this TLV.
enum BLEFragmentCeilingPolicy {
/// Why a particular ceiling was chosen. Carried so the caller can log and
/// explain a rejection, and so tests assert the reasoning rather than just
/// the number — two sources can agree on a value by coincidence.
enum Source: Equatable {
/// The recipient advertised its own limit in authenticated peer state.
case negotiated
/// No advertisement; the recipient is assumed to be a released client
/// on the directed raw-file migration path.
case migrationFallbackProxy
/// No advertisement and no reason to assume a small reassembler.
case localCeiling
}
struct Decision: Equatable {
let maxFragments: Int
let source: Source
func admits(fragmentCount: Int) -> Bool {
fragmentCount <= maxFragments
}
}
/// Preserve the existing fast path for encrypted peers without a limit.
static func requiresPreflight(packetType: UInt8, negotiatedCeiling: UInt16?) -> Bool {
packetType == MessageType.fileTransfer.rawValue || negotiatedCeiling != nil
}
/// The local clamp bounds the number of fragments we originate. It does
/// not describe another peer's byte budget or guarantee relay capacity.
static func decide(
packetType: UInt8,
isDirectedToPeer: Bool,
negotiatedCeiling: UInt16?,
localCeiling: Int = BLEFragmentAssemblyBuffer.maxReassemblyFragments,
migrationFallbackCeiling: Int = BLEOutboundFragmentPlanner.privateMediaV1MaxFragments
) -> Decision {
if isDirectedToPeer, let negotiatedCeiling, negotiatedCeiling > 0 {
let pathCeiling = packetType == MessageType.noiseEncrypted.rawValue
? localCeiling : min(localCeiling, migrationFallbackCeiling)
return Decision(
maxFragments: min(Int(negotiatedCeiling), pathCeiling),
source: .negotiated
)
}
if isDirectedToPeer, packetType == MessageType.fileTransfer.rawValue {
return Decision(
maxFragments: min(migrationFallbackCeiling, localCeiling),
source: .migrationFallbackProxy
)
}
return Decision(maxFragments: localCeiling, source: .localCeiling)
}
}

View File

@ -5,6 +5,22 @@ struct BLEAuthenticatedPeerStateObservation {
let fingerprint: String
let sessionGeneration: UUID
let capabilities: PeerCapabilities
/// The peer's own reassembly bound, if it advertised one. Bound to the
/// generation like every other field here: a replacement session must
/// re-state its ceiling rather than inherit the previous one.
let maxReassemblyFragments: UInt16?
init(
fingerprint: String,
sessionGeneration: UUID,
capabilities: PeerCapabilities,
maxReassemblyFragments: UInt16? = nil
) {
self.fingerprint = fingerprint
self.sessionGeneration = sessionGeneration
self.capabilities = capabilities
self.maxReassemblyFragments = maxReassemblyFragments
}
}
struct BLEPrivateMediaProofTimeoutMarker {
@ -163,14 +179,16 @@ final class BLEPrivateMediaSessionStore: @unchecked Sendable {
for peerID: PeerID,
fingerprint: String,
generation: UUID,
capabilities: PeerCapabilities
capabilities: PeerCapabilities,
maxReassemblyFragments: UInt16? = nil
) -> [@MainActor (PrivateMediaSendPolicy) -> Void]? {
lock.withLock {
guard sessionGenerations[peerID] == generation else { return nil }
authenticatedStates[peerID] = BLEAuthenticatedPeerStateObservation(
fingerprint: fingerprint,
sessionGeneration: generation,
capabilities: capabilities
capabilities: capabilities,
maxReassemblyFragments: maxReassemblyFragments
)
proofTimeoutMarkers.removeValue(forKey: peerID)
proofWatchdogs.removeValue(forKey: peerID)
@ -347,6 +365,22 @@ final class BLEPrivateMediaSessionStore: @unchecked Sendable {
}
extension BLEPrivateMediaSessionStore {
/// The recipient's advertised reassembly ceiling, or nil when it did not
/// advertise one.
///
/// Only honoured for an observation pinned to the peer's *current*
/// generation. A ceiling read off a superseded session is a statement by
/// whoever held that session, and after a rekey the counterpart may be a
/// different client entirely — falling back to the type proxy is the
/// conservative answer, not reusing a stale number.
func negotiatedFragmentCeiling(for peerID: PeerID) -> UInt16? {
let inputs = policyInputs(for: peerID)
guard let generation = inputs.sessionGeneration,
let authenticated = inputs.authenticatedState,
authenticated.sessionGeneration == generation else { return nil }
return authenticated.maxReassemblyFragments
}
/// The current generation iff its authenticated peer state proved the
/// private-media capability (and, when required, durable receipts).
func provenGeneration(for peerID: PeerID, requireReceipts: Bool) -> UUID? {

View File

@ -1996,26 +1996,24 @@ final class BLEService: NSObject {
// Encode once using a small per-type padding policy, then delegate by type
let padForBLE = BLEOutboundPacketPolicy.padsBLEFrame(for: packetToSend.type)
// The 256-fragment ceiling exists to protect *current Android*
// receivers, which only ever receive private media over the directed
// raw-file migration fallback (they do not implement the encrypted
// 0x20 path). Encrypted private media (`noiseEncrypted`) is sent only to
// peers that advertised the `.privateMedia` capability — modern clients
// that assemble up to the full receiver ceiling (see
// `BLEFragmentAssemblyBuffer`'s 10,000-fragment guard) — so forcing them
// down to Android's 256 cap would needlessly reject iOS→iOS photos in
// the ~120–512 KiB range that work today. Restrict the low cap to the
// migration fallback (directed `fileTransfer`); public media is
// unaffected. Run the same planner the scheduler will use, after route
// application, and reject before reserving a transfer slot or writing
// any fragment.
// TODO(#1434): negotiate an explicit per-peer fragment limit so a future
// Android client that adopts the encrypted 0x20 path but still caps its
// reassembler can advertise its own ceiling instead of relying on the
// capability/type proxy above.
// Android supports encrypted private media but still caps reassembly at
// 256 fragments. Capability support alone does not negotiate capacity.
// Apply authenticated limits after routing and before reserving a slot
// or writing fragments; raw migration traffic always keeps its low cap.
// Honor a peer's authenticated limit without planning every encrypted
// send twice. A missing limit preserves the existing encrypted path;
// raw migration sends always retain their preflight and 256 cap.
if let transferId,
let recipientPeerID = PeerID(hexData: packetToSend.recipientID),
packetToSend.type == MessageType.fileTransfer.rawValue {
BLEFragmentCeilingPolicy.requiresPreflight(
packetType: packetToSend.type,
negotiatedCeiling: privateMediaSessions.negotiatedFragmentCeiling(for: recipientPeerID.toShort())
) {
let ceiling = BLEFragmentCeilingPolicy.decide(
packetType: packetToSend.type,
isDirectedToPeer: true,
negotiatedCeiling: privateMediaSessions.negotiatedFragmentCeiling(for: recipientPeerID.toShort())
)
let compatibilityRequest = BLEOutboundFragmentTransferRequest(
packet: packetToSend,
pad: padForBLE,
@ -2027,17 +2025,20 @@ final class BLEService: NSObject {
for: compatibilityRequest,
defaultChunkSize: defaultFragmentSize,
bleMaxMTU: bleMaxMTU
), BLEOutboundFragmentPlanner.isPrivateMediaV1Compatible(plan) else {
), ceiling.admits(fragmentCount: plan.totalFragments) else {
SecureLogger.warning(
"Private media rejected: exceeds cross-platform 256-fragment limit",
"Private media rejected: \(String(describing: ceiling.source)) limit of \(ceiling.maxFragments) fragments",
category: .security
)
TransferProgressManager.shared.rejectBeforeStart(
id: transferId,
reason: String(
localized: "content.delivery.reason.private_media_too_many_fragments",
defaultValue: "File is too large for this contact's client (more than 256 mesh fragments)",
comment: "Failure reason when private media exceeds the Android-compatible fragment limit"
format: String(
localized: "content.delivery.reason.private_media_too_many_fragments",
defaultValue: "File is too large for this contact's client (more than %lld mesh fragments)",
comment: "Failure reason when private media exceeds the recipient's fragment limit; %lld is that limit"
),
ceiling.maxFragments
)
)
if requiresPrivateMediaAdmission {
@ -4234,6 +4235,8 @@ extension BLEService {
let capabilities = localIdentityState.snapshot().advertisedCapabilities
let state = AuthenticatedPeerStatePacket(
capabilities: capabilities,
// Do not advertise the header sanity bound as capacity. Our byte
// budget cannot promise a fragment count without a payload size.
signingPublicKey: noiseService.getSigningPublicKeyData()
)
guard let payload = BLENoisePayloadFactory.authenticatedPeerState(state) else {
@ -4308,7 +4311,8 @@ extension BLEService {
for: normalizedPeerID,
fingerprint: fingerprint,
generation: generation,
capabilities: state.capabilities
capabilities: state.capabilities,
maxReassemblyFragments: state.maxReassemblyFragments
) else {
return (false, [])
}

View File

@ -178,6 +178,81 @@ struct PacketsTests {
#expect(AuthenticatedPeerStatePacket.decode(from: Data([0x01]) + makeTLV(type: 0x01, value: Data(repeating: 0x01, count: 9)) + signing) == nil)
}
@Test
func authenticatedPeerStateRoundTripsTheReassemblyCeiling() throws {
let packet = AuthenticatedPeerStatePacket(
capabilities: [.privateMedia],
signingPublicKey: Data(repeating: 0xA5, count: 32),
maxReassemblyFragments: 256
)
let encoded = try #require(packet.encode())
#expect(encoded.suffix(4) == Data([0x03, 0x02, 0x01, 0x00]))
#expect(AuthenticatedPeerStatePacket.decode(from: encoded) == packet)
}
@Test
func authenticatedPeerStateOmitsAnAbsentCeilingAndDecodesLegacyPayloadsAsNil() throws {
let packet = AuthenticatedPeerStatePacket(
capabilities: [.privateMedia],
signingPublicKey: Data(repeating: 0xA5, count: 32)
)
let encoded = try #require(packet.encode())
// Every client released before TLV 0x03 emits exactly this — version
// byte, 2-byte capability TLV, 32-byte key TLV, nothing else — and it
// must decode as "did not advertise", not as a ceiling of 0.
#expect(encoded.count == 1 + (2 + 2) + (2 + 32))
let decoded = try #require(AuthenticatedPeerStatePacket.decode(from: encoded))
#expect(decoded.maxReassemblyFragments == nil)
}
@Test
func malformedOptionalCeilingsPreserveAuthenticatedIdentity() throws {
let key = Data(repeating: 0x44, count: 32)
let prefix = Data([0x01]) + makeTLV(type: 0x01, value: Data([0x00, 0x01]))
+ makeTLV(type: 0x02, value: key)
let valid = makeTLV(type: 0x03, value: Data([0x04, 0x00]))
let malformed: [Data] = [
makeTLV(type: 0x03, value: Data([0x00, 0x00])),
makeTLV(type: 0x03, value: Data([0x01])),
makeTLV(type: 0x03, value: Data([0x00, 0x00, 0x01, 0x00])),
valid + valid,
makeTLV(type: 0x03, value: Data([0x01])) + valid,
Data([0x03]),
Data([0x03, 0x02, 0x01])
]
for tail in malformed {
let decoded = try #require(AuthenticatedPeerStatePacket.decode(from: prefix + tail))
#expect(decoded.signingPublicKey == key)
#expect(decoded.capabilities.contains(.privateMedia))
#expect(decoded.maxReassemblyFragments == 256)
}
}
@Test
func malformedCeilingBeforeRequiredFieldsDoesNotHideThem() throws {
let key = Data(repeating: 0x44, count: 32)
let payload = Data([0x01]) + makeTLV(type: 0x03, value: Data([0x01]))
+ makeTLV(type: 0x01, value: Data([0x00, 0x01]))
+ makeTLV(type: 0x02, value: key)
let decoded = try #require(AuthenticatedPeerStatePacket.decode(from: payload))
#expect(decoded.signingPublicKey == key)
#expect(decoded.capabilities.contains(.privateMedia))
#expect(decoded.maxReassemblyFragments == 256)
}
@Test
func authenticatedPeerStateRefusesToEncodeAZeroCeiling() {
#expect(
AuthenticatedPeerStatePacket(
capabilities: [.privateMedia],
signingPublicKey: Data(repeating: 0xA5, count: 32),
maxReassemblyFragments: 0
).encode() == nil
)
}
@Test
func privateMessagePacketRejectsUnknownTypeAndTruncation() {
let unknownTLV = Data([0x7F, 0x01, 0x41])

View File

@ -0,0 +1,170 @@
import BitFoundation
import Foundation
import Testing
@testable import bitchat
struct BLEFragmentCeilingPolicyTests {
private let fileTransfer = MessageType.fileTransfer.rawValue
private let noiseEncrypted = MessageType.noiseEncrypted.rawValue
// MARK: - The proxy, unchanged where nothing was negotiated
@Test
func directedFileTransferWithoutAnAdvertisementKeepsTheMigrationCap() {
let decision = BLEFragmentCeilingPolicy.decide(
packetType: fileTransfer,
isDirectedToPeer: true,
negotiatedCeiling: nil
)
#expect(decision.source == .migrationFallbackProxy)
#expect(decision.maxFragments == BLEOutboundFragmentPlanner.privateMediaV1MaxFragments)
#expect(decision.admits(fragmentCount: 256))
#expect(!decision.admits(fragmentCount: 257))
}
@Test
func encryptedMediaWithoutAnAdvertisementKeepsTheFullLocalCeiling() {
let decision = BLEFragmentCeilingPolicy.decide(
packetType: noiseEncrypted,
isDirectedToPeer: true,
negotiatedCeiling: nil
)
#expect(decision.source == .localCeiling)
#expect(decision.maxFragments == BLEFragmentAssemblyBuffer.maxReassemblyFragments)
// The regression the 256 cap caused for iOS→iOS photos in the
// ~120–512 KiB range: those plans must still be admitted.
#expect(decision.admits(fragmentCount: 900))
}
@Test
func broadcastFileTransferIsNotTreatedAsTheDirectedMigrationPath() {
let decision = BLEFragmentCeilingPolicy.decide(
packetType: fileTransfer,
isDirectedToPeer: false,
negotiatedCeiling: nil
)
#expect(decision.source == .localCeiling)
#expect(decision.maxFragments == BLEFragmentAssemblyBuffer.maxReassemblyFragments)
}
// MARK: - An advertisement replaces the proxy
@Test
func anAdvertisedCeilingBelowTheMigrationCapIsHonoured() {
// The case the type proxy gets wrong in the silent direction: a client
// on the encrypted path whose reassembler is smaller than ours. Without
// the advertisement we would happily plan 900 fragments it will drop.
let decision = BLEFragmentCeilingPolicy.decide(
packetType: noiseEncrypted,
isDirectedToPeer: true,
negotiatedCeiling: 128
)
#expect(decision.source == .negotiated)
#expect(decision.maxFragments == 128)
#expect(decision.admits(fragmentCount: 128))
#expect(!decision.admits(fragmentCount: 129))
}
@Test
func anAdvertisedCeilingNeverLiftsTheRawMigrationCap() {
// An authenticated count is not permission to relax raw fallback;
// only encrypted media can use a value above the migration ceiling.
let decision = BLEFragmentCeilingPolicy.decide(
packetType: fileTransfer,
isDirectedToPeer: true,
negotiatedCeiling: 1024
)
#expect(decision.source == .negotiated)
#expect(decision.maxFragments == 256)
#expect(!decision.admits(fragmentCount: 257))
}
@Test
func anAdvertisedCeilingIsClampedToWhatWeWouldReassembleOurselves() {
let local = BLEFragmentAssemblyBuffer.maxReassemblyFragments
let decision = BLEFragmentCeilingPolicy.decide(
packetType: noiseEncrypted,
isDirectedToPeer: true,
negotiatedCeiling: UInt16.max
)
#expect(decision.source == .negotiated)
#expect(decision.maxFragments == local)
#expect(!decision.admits(fragmentCount: local + 1))
}
@Test
func aZeroAdvertisementFallsBackRatherThanBlockingEveryTransfer() {
// The decoder replaces a zero on the wire with 256, so this can only arrive from
// a future caller passing one through. Treat it as absent: refusing
// every fragment to that peer would be a worse failure than the proxy.
let decision = BLEFragmentCeilingPolicy.decide(
packetType: fileTransfer,
isDirectedToPeer: true,
negotiatedCeiling: 0
)
#expect(decision.source == .migrationFallbackProxy)
#expect(decision.maxFragments == BLEOutboundFragmentPlanner.privateMediaV1MaxFragments)
}
// MARK: - Invariants across the whole input space
@Test
func noDecisionEverExceedsTheLocalCeiling() {
let local = BLEFragmentAssemblyBuffer.maxReassemblyFragments
let ceilings: [UInt16?] = [nil, 1, 255, 256, 257, 9_999, 10_000, 10_001, UInt16.max]
let types: [UInt8] = [
fileTransfer,
noiseEncrypted,
MessageType.message.rawValue,
MessageType.announce.rawValue
]
for ceiling in ceilings {
for type in types {
for directed in [true, false] {
let decision = BLEFragmentCeilingPolicy.decide(
packetType: type,
isDirectedToPeer: directed,
negotiatedCeiling: ceiling
)
#expect(decision.maxFragments >= 1)
#expect(decision.maxFragments <= local)
}
}
}
}
@Test
func encryptedMediaCanUseAPeerLimitAbove256() {
let decision = BLEFragmentCeilingPolicy.decide(
packetType: noiseEncrypted, isDirectedToPeer: true, negotiatedCeiling: 1024
)
#expect(decision.maxFragments == 1024)
#expect(decision.admits(fragmentCount: 1024))
#expect(!decision.admits(fragmentCount: 1025))
}
@Test
func rawMediaStillHonorsALowerPeerLimit() {
let decision = BLEFragmentCeilingPolicy.decide(
packetType: fileTransfer, isDirectedToPeer: true, negotiatedCeiling: 1
)
#expect(decision.maxFragments == 1)
#expect(!decision.admits(fragmentCount: 2))
}
@Test
func preflightSkipsUnnegotiatedEncryptedMedia() {
#expect(!BLEFragmentCeilingPolicy.requiresPreflight(packetType: noiseEncrypted, negotiatedCeiling: nil))
#expect(BLEFragmentCeilingPolicy.requiresPreflight(packetType: noiseEncrypted, negotiatedCeiling: 256))
#expect(BLEFragmentCeilingPolicy.requiresPreflight(packetType: fileTransfer, negotiatedCeiling: nil))
}
}

View File

@ -189,4 +189,73 @@ struct BLEPrivateMediaSessionStoreTests {
)
#expect(store.markPeerStateSend(for: peer, echo: false))
}
// MARK: - Negotiated reassembly ceiling
@Test func negotiatedCeilingIsReadableOnlyForTheGenerationThatStatedIt() {
let store = BLEPrivateMediaSessionStore()
let generation = UUID()
_ = store.beginAuthenticatedGeneration(
for: peer, fingerprint: fingerprint, generation: generation
)
#expect(store.negotiatedFragmentCeiling(for: peer) == nil)
_ = store.applyAuthenticatedPeerState(
for: peer,
fingerprint: fingerprint,
generation: generation,
capabilities: [.privateMedia],
maxReassemblyFragments: 128
)
#expect(store.negotiatedFragmentCeiling(for: peer) == 128)
// A rekey replaces the counterpart's session state. The old ceiling
// was a statement by whoever held that session, and the replacement
// may be a different client entirely, so it must not be inherited.
//
// Two independent mechanisms enforce this: `beginAuthenticatedGeneration`
// drops the observation, and the accessor refuses one whose pinned
// generation is not current. Removing either alone still passes; this
// expectation fails once both are gone, which is the invariant it is
// here to hold rather than any single line of it.
_ = store.beginAuthenticatedGeneration(
for: peer, fingerprint: fingerprint, generation: UUID()
)
#expect(store.negotiatedFragmentCeiling(for: peer) == nil)
}
@Test func peerStateWithoutACeilingLeavesTheSenderOnTheTypeProxy() {
let store = BLEPrivateMediaSessionStore()
let generation = UUID()
_ = store.beginAuthenticatedGeneration(
for: peer, fingerprint: fingerprint, generation: generation
)
// Every client released before TLV 0x03 lands here.
_ = store.applyAuthenticatedPeerState(
for: peer,
fingerprint: fingerprint,
generation: generation,
capabilities: [.privateMedia]
)
#expect(store.negotiatedFragmentCeiling(for: peer) == nil)
}
@Test func clearingTheSessionDropsTheNegotiatedCeiling() {
let store = BLEPrivateMediaSessionStore()
let generation = UUID()
_ = store.beginAuthenticatedGeneration(
for: peer, fingerprint: fingerprint, generation: generation
)
_ = store.applyAuthenticatedPeerState(
for: peer,
fingerprint: fingerprint,
generation: generation,
capabilities: [.privateMedia],
maxReassemblyFragments: 512
)
#expect(store.negotiatedFragmentCeiling(for: peer) == 512)
_ = store.clearSession(for: peer)
#expect(store.negotiatedFragmentCeiling(for: peer) == nil)
}
}

View File

@ -173,7 +173,7 @@ proof = Ed25519-Sign(signingPrivateKey,
|| noiseStaticPublicKey (32 bytes))
```
Sent as a new TLV in `AuthenticatedPeerStatePacket`, whose existing structure already carries a version byte, a canonicality-checked capability TLV, and the 32-byte signing key. The receiver verifies:
Reserved as TLV `0x04` in `AuthenticatedPeerStatePacket` (`0x03` is the optional reassembly fragment-count ceiling), whose existing structure already carries a version byte, a canonicality-checked capability TLV, and the 32-byte signing key. The receiver verifies:
- **that the `noiseStaticPublicKey` inside the proof is byte-equal to the remote static key the Noise session actually established** — see below, this one is load-bearing, and
- the signature against the signing key in the same packet, **and**

View File

@ -20,8 +20,14 @@ peer's Noise session before BLE fragmentation.
type/length/value fields. Version 1 requires canonical TLV `0x01` (the
minimal little-endian `PeerCapabilities` bitfield, 1-8 bytes) and TLV `0x02`
(the 32-byte Ed25519 announcement signing key). Duplicate required fields,
non-minimal capabilities, malformed lengths, missing fields, and unknown
non-minimal capabilities, malformed required lengths, missing fields, and unknown
versions are ignored without changing state. Unknown TLVs are skipped.
Optional TLV `0x03` is a two-byte big-endian fragment-count ceiling (`1...65535`).
A zero, wrong width, duplicate, or truncated `0x03` preserves the required
capabilities/signing key and selects a conservative limit of 256. A truncated
required field still invalidates the packet. TLV `0x04` is reserved for the
peer-ID binding proof described in `PEER-ID-ROTATION.md`.
For example, the ceiling extension for 256 is `03 02 01 00`.
- The public `PeerCapabilities.privateMedia` announce bit is a discovery hint:
it starts a Noise handshake, but never selects encrypted sending or creates
a pin. A private transfer waits boundedly for the exact session's encrypted
@ -105,18 +111,23 @@ iOS bounds inbound file content at 1 MiB and applies the expanded allocation
budget only after a large Noise ciphertext authenticates to `0x20` or the
temporary `0x09` alias. Ordinary Noise messages retain their 64 KiB limit.
Current Android builds cap each reassembly at 256 fragments. Depending on the
negotiated BLE packet size and routing overhead, that is roughly 110-120 KiB,
well below iOS's absolute inbound ceiling. That cap only applies to those
receivers, which take private media exclusively over the directed raw-file
migration fallback (they do not implement the encrypted `0x20` path).
Private-media v1 therefore runs the actual route-aware BLE fragment planner
before a consented legacy send and rejects any plan above 256 fragments with a
visible failure. Encrypted sends go only to peers that advertised the
`privateMedia` capability — modern clients that reassemble up to the full
receiver ceiling (10,000 fragments) — so they are not held to Android's cap and
iOS→iOS photos in the ~120-512 KiB range keep working. This fragment-count
contract, rather than a guessed byte threshold, stays correct as route overhead
changes. A future Android client that adopts `0x20` but still caps its
reassembler would need to negotiate an explicit per-peer fragment limit
(tracked as a #1434 follow-up).
Android's 256-fragment bound is independent of its encrypted-media capability:
Android supports the `0x20` path (permissionlesstech/bitchat-android#728).
Capability bit 8 alone therefore
does not establish a peer's reassembly capacity. A directed raw migration send
retains the 256-fragment cap even if the peer advertises a larger value; a
smaller authenticated `0x03` limit lowers either path. Only encrypted media may
use an authenticated value above 256, clamped to our local count sanity bound.
Without an advertisement, encrypted sends keep their previous behavior. This
preserves existing iOS-to-iOS transfers but does not fix oversized sends to
released Android peers that advertise no ceiling. The companion negotiation
work is permissionlesstech/bitchat-android#879; this PR does not claim to solve that deployed-client gap.
The iOS reassembler also enforces a per-type byte budget. Its 10,000-fragment
header sanity check is not a promise to accept that many fragments of arbitrary
size. iOS therefore does not advertise a count in `0x03`; a future byte-budget
extension or an agreed maximum fragment payload is needed before it can state a
truthful capacity. The local sender clamp bounds fragment counts we originate,
not another device's memory budget. Unnegotiated encrypted sends skip the extra
fragment-planning preflight; raw migration sends and negotiated sends retain it.