mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-11-22 08:17:28 +00:00
Merge 3eb2df004edf7668a7d3a54d467e1fc580084603 into 04995f68e470a18d1fc99f86fa326c2a87bab4f2
This commit is contained in:
commit
8644c26d64
@ -52221,6 +52221,192 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"content.delivery.reason.too_long" : {
|
||||
"comment" : "Failure reason when a private message exceeds the wire size limit",
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"ar" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "الرسالة طويلة جدًا"
|
||||
}
|
||||
},
|
||||
"bn" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "বার্তাটি খুব দীর্ঘ"
|
||||
}
|
||||
},
|
||||
"de" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "nachricht zu lang"
|
||||
}
|
||||
},
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "message too long"
|
||||
}
|
||||
},
|
||||
"es" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "mensaje demasiado largo"
|
||||
}
|
||||
},
|
||||
"fa" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "پیام بیش از حد طولانی است"
|
||||
}
|
||||
},
|
||||
"fil" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "masyadong mahaba ang mensahe"
|
||||
}
|
||||
},
|
||||
"fr" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "message trop long"
|
||||
}
|
||||
},
|
||||
"he" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "ההודעה ארוכה מדי"
|
||||
}
|
||||
},
|
||||
"hi" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "संदेश बहुत लंबा है"
|
||||
}
|
||||
},
|
||||
"id" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "pesan terlalu panjang"
|
||||
}
|
||||
},
|
||||
"it" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "messaggio troppo lungo"
|
||||
}
|
||||
},
|
||||
"ja" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "メッセージが長すぎます"
|
||||
}
|
||||
},
|
||||
"ko" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "메시지가 너무 깁니다"
|
||||
}
|
||||
},
|
||||
"ms" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "mesej terlalu panjang"
|
||||
}
|
||||
},
|
||||
"ne" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "सन्देश धेरै लामो छ"
|
||||
}
|
||||
},
|
||||
"nl" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "bericht te lang"
|
||||
}
|
||||
},
|
||||
"pl" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "wiadomość jest za długa"
|
||||
}
|
||||
},
|
||||
"pt" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "mensagem demasiado longa"
|
||||
}
|
||||
},
|
||||
"pt-BR" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "mensagem muito longa"
|
||||
}
|
||||
},
|
||||
"ru" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "сообщение слишком длинное"
|
||||
}
|
||||
},
|
||||
"sv" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "meddelandet är för långt"
|
||||
}
|
||||
},
|
||||
"ta" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "செய்தி மிகவும் நீளமாக உள்ளது"
|
||||
}
|
||||
},
|
||||
"th" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "ข้อความยาวเกินไป"
|
||||
}
|
||||
},
|
||||
"tr" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "mesaj çok uzun"
|
||||
}
|
||||
},
|
||||
"uk" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "повідомлення надто довге"
|
||||
}
|
||||
},
|
||||
"ur" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "پیغام بہت لمبا ہے"
|
||||
}
|
||||
},
|
||||
"vi" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "tin nhắn quá dài"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "消息过长"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "needs_review",
|
||||
"value" : "訊息過長"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"content.delivery.reason.unknown_recipient" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
|
||||
@ -240,6 +240,13 @@ struct AuthenticatedPeerStatePacket: Equatable {
|
||||
}
|
||||
|
||||
struct PrivateMessagePacket {
|
||||
/// Content TLV length is one byte. Longer private messages cannot be encoded.
|
||||
static let maxContentUTF8Bytes = 255
|
||||
|
||||
static func contentFitsOnWire(_ content: String) -> Bool {
|
||||
content.utf8.count <= maxContentUTF8Bytes
|
||||
}
|
||||
|
||||
let messageID: String
|
||||
let content: String
|
||||
|
||||
@ -259,7 +266,7 @@ struct PrivateMessagePacket {
|
||||
data.append(messageIDData)
|
||||
|
||||
// TLV for content
|
||||
guard let contentData = content.data(using: .utf8), contentData.count <= 255 else { return nil }
|
||||
guard let contentData = content.data(using: .utf8), contentData.count <= Self.maxContentUTF8Bytes else { return nil }
|
||||
data.append(TLVType.content.rawValue)
|
||||
data.append(UInt8(contentData.count))
|
||||
data.append(contentData)
|
||||
|
||||
@ -5245,6 +5245,17 @@ extension BLEService {
|
||||
let recipientID = recipientID.toShort()
|
||||
SecureLogger.debug("📨 Sending PM to \(recipientID.id.prefix(8))… id=\(messageID.prefix(8))… chars=\(content.count) bytes=\(content.utf8.count)", category: .session)
|
||||
|
||||
guard PrivateMessagePacket.contentFitsOnWire(content) else {
|
||||
SecureLogger.error("Private message is \(content.utf8.count) UTF-8 bytes; the wire limit is \(PrivateMessagePacket.maxContentUTF8Bytes)", category: .session)
|
||||
notifyUI { [weak self] in
|
||||
self?.deliverTransportEvent(.messageDeliveryStatusUpdated(
|
||||
messageID: messageID,
|
||||
status: .failed(reason: String(localized: "content.delivery.reason.send_error", comment: "Failure reason for a generic send error"))
|
||||
))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Check if we have an established Noise session
|
||||
if noiseService.hasEstablishedSession(with: recipientID) {
|
||||
// Encrypt and send
|
||||
@ -5375,7 +5386,12 @@ extension BLEService {
|
||||
// Use the same TLV format as normal sends to keep receiver decoding consistent
|
||||
guard let messagePayload = BLENoisePayloadFactory.privateMessage(content: message.content, messageID: message.messageID) else {
|
||||
SecureLogger.error("Failed to encode pending private message TLV")
|
||||
failedMessages.append(message)
|
||||
notifyUI { [weak self] in
|
||||
self?.deliverTransportEvent(.messageDeliveryStatusUpdated(
|
||||
messageID: message.messageID,
|
||||
status: .failed(reason: String(localized: "content.delivery.reason.send_error", comment: "Failure reason for a generic send error"))
|
||||
))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@ -530,6 +530,9 @@ final class CommandProcessor {
|
||||
let summary = info.displayAmount ?? "a cashu token"
|
||||
|
||||
if let peerID = contextProvider?.selectedPrivateChatPeer {
|
||||
guard PrivateMessagePacket.contentFitsOnWire(token) else {
|
||||
return .error(message: String(localized: "content.delivery.reason.too_long", defaultValue: "message too long", comment: "Failure reason when a private message exceeds the wire size limit"))
|
||||
}
|
||||
contextProvider?.sendPrivateMessage(token, to: peerID)
|
||||
return .success(message: String(format: String(localized: "command.pay.sent_private", defaultValue: "sent %@ — cashu is a bearer token; whoever redeems it first gets the funds", comment: "Confirmation after sending a cashu token in a private chat; placeholder is the amount summary"), locale: .current, summary))
|
||||
}
|
||||
|
||||
@ -355,6 +355,7 @@ final class ChatPrivateConversationCoordinator {
|
||||
?? "anon"
|
||||
|
||||
let messageID = UUID().uuidString
|
||||
let status = initialDeliveryStatus(for: content)
|
||||
let message = BitchatMessage(
|
||||
id: messageID,
|
||||
sender: context.nickname,
|
||||
@ -366,11 +367,12 @@ final class ChatPrivateConversationCoordinator {
|
||||
recipientNickname: recipientNickname,
|
||||
senderPeerID: context.myPeerID,
|
||||
mentions: nil,
|
||||
deliveryStatus: .sending
|
||||
deliveryStatus: status
|
||||
)
|
||||
|
||||
context.appendPrivateMessage(message, to: peerID)
|
||||
context.notifyUIChanged()
|
||||
guard status == .sending else { return }
|
||||
|
||||
// Always hand the message to the router — it owns delivery. A live
|
||||
// link sends now; an unreachable peer gets the retained-outbox path
|
||||
@ -393,6 +395,13 @@ final class ChatPrivateConversationCoordinator {
|
||||
// acks, or failed when the outbox TTL expires.
|
||||
}
|
||||
|
||||
private func initialDeliveryStatus(for content: String) -> DeliveryStatus {
|
||||
guard PrivateMessagePacket.contentFitsOnWire(content) else {
|
||||
return .failed(reason: String(localized: "content.delivery.reason.too_long", defaultValue: "message too long", comment: "Failure reason when a private message exceeds the wire size limit"))
|
||||
}
|
||||
return .sending
|
||||
}
|
||||
|
||||
func sendGeohashDM(_ content: String, to peerID: PeerID) {
|
||||
guard case .location(let channel) = context.activeChannel else {
|
||||
// The failure happened inside a geoDM thread — surface it there,
|
||||
@ -406,6 +415,7 @@ final class ChatPrivateConversationCoordinator {
|
||||
}
|
||||
|
||||
let messageID = UUID().uuidString
|
||||
let status = initialDeliveryStatus(for: content)
|
||||
let message = BitchatMessage(
|
||||
id: messageID,
|
||||
sender: context.nickname,
|
||||
@ -415,11 +425,12 @@ final class ChatPrivateConversationCoordinator {
|
||||
isPrivate: true,
|
||||
recipientNickname: context.nickname,
|
||||
senderPeerID: context.myPeerID,
|
||||
deliveryStatus: .sending
|
||||
deliveryStatus: status
|
||||
)
|
||||
|
||||
context.appendPrivateMessage(message, to: peerID)
|
||||
context.notifyUIChanged()
|
||||
guard status == .sending else { return }
|
||||
|
||||
guard let recipientHex = context.nostrKeyMapping[peerID] else {
|
||||
context.setPrivateDeliveryStatus(
|
||||
|
||||
@ -1233,4 +1233,40 @@ struct ChatPrivateConversationCoordinatorContextTests {
|
||||
#expect(context.routedPrivateMessages.map(\.content) == ["hello?"])
|
||||
#expect(context.privateChats[peerID]?.first?.deliveryStatus == .sending)
|
||||
}
|
||||
|
||||
@Test(arguments: [false, true]) @MainActor
|
||||
func oversizedPrivateMessageFailsWithoutRouting(isGeohash: Bool) {
|
||||
let context = MockChatPrivateConversationContext()
|
||||
let coordinator = ChatPrivateConversationCoordinator(context: context)
|
||||
let peerID: PeerID
|
||||
if isGeohash {
|
||||
let pubkey = String(repeating: "a", count: 64)
|
||||
peerID = PeerID(str: "nostr_" + String(pubkey.prefix(16)))
|
||||
context.activeChannel = .location(GeohashChannel(level: .region, geohash: "0000"))
|
||||
context.nostrKeyMapping[peerID] = pubkey
|
||||
} else {
|
||||
// An unreachable mesh peer would normally cause the router to retain the message.
|
||||
peerID = PeerID(str: "abcd1234abcd1234")
|
||||
}
|
||||
|
||||
let content = String(repeating: "é", count: 128)
|
||||
coordinator.sendPrivateMessage(content, to: peerID)
|
||||
|
||||
#expect(context.privateChats[peerID]?.map(\.content) == [content])
|
||||
#expect(context.privateChats[peerID]?.first?.deliveryStatus == .failed(
|
||||
reason: String(localized: "content.delivery.reason.too_long", defaultValue: "message too long")
|
||||
))
|
||||
#expect(context.routedPrivateMessages.isEmpty)
|
||||
#expect(context.geoPrivateMessages.isEmpty)
|
||||
|
||||
// The multibyte UTF-8 boundary still sends: 127 × 2 + 1 = 255 bytes.
|
||||
let boundaryContent = String(repeating: "é", count: 127) + "a"
|
||||
#expect(boundaryContent.utf8.count == PrivateMessagePacket.maxContentUTF8Bytes)
|
||||
coordinator.sendPrivateMessage(boundaryContent, to: peerID)
|
||||
|
||||
#expect(context.privateChats[peerID]?.map(\.content) == [content, boundaryContent])
|
||||
#expect(context.privateChats[peerID]?.last?.deliveryStatus == (isGeohash ? .sent : .sending))
|
||||
#expect(context.routedPrivateMessages.map(\.content) == (isGeohash ? [] : [boundaryContent]))
|
||||
#expect(context.geoPrivateMessages.map(\.content) == (isGeohash ? [boundaryContent] : []))
|
||||
}
|
||||
}
|
||||
|
||||
@ -410,16 +410,18 @@ struct CommandProcessorTests {
|
||||
let peerID = PeerID(str: "abcd1234abcd1234")
|
||||
context.selectedPrivateChatPeer = peerID
|
||||
let processor = makePayProcessor(context: context)
|
||||
let token = Self.cashuV3Token(mint: "https://mint.example.com", amounts: [16])
|
||||
#expect(token.utf8.count <= PrivateMessagePacket.maxContentUTF8Bytes)
|
||||
|
||||
// cashu: URI form must be normalized to the bare token before sending
|
||||
switch processor.process("/pay cashu:\(Self.validV3Token)") {
|
||||
switch processor.process("/pay cashu:\(token)") {
|
||||
case .success(let message):
|
||||
#expect(message?.contains("21 sat") == true)
|
||||
#expect(message?.contains("16 sat") == true)
|
||||
default:
|
||||
Issue.record("Expected success result")
|
||||
}
|
||||
#expect(context.sentPrivateMessages.count == 1)
|
||||
#expect(context.sentPrivateMessages.first?.content == Self.validV3Token)
|
||||
#expect(context.sentPrivateMessages.first?.content == token)
|
||||
#expect(context.sentPrivateMessages.first?.peerID == peerID)
|
||||
#expect(context.sentPublicMessages.isEmpty)
|
||||
}
|
||||
@ -486,12 +488,33 @@ struct CommandProcessorTests {
|
||||
#expect(context.sentPrivateMessages.first?.content == Self.validV4Token)
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Test(arguments: [false, true]) func payRefusesAPrivateTokenThatDoesNotFitTheWire(isGeohash: Bool) {
|
||||
let context = MockCommandContextProvider()
|
||||
context.selectedPrivateChatPeer = isGeohash
|
||||
? PeerID(nostr_: String(repeating: "a", count: 64))
|
||||
: PeerID(str: "abcd1234abcd1234")
|
||||
let processor = makePayProcessor(context: context)
|
||||
let token = Self.cashuV3Token(mint: "https://mint.example.com/" + String(repeating: "a", count: 240))
|
||||
#expect(token.utf8.count > PrivateMessagePacket.maxContentUTF8Bytes)
|
||||
|
||||
switch processor.process("/pay \(token)") {
|
||||
case .error(let message):
|
||||
#expect(message == String(localized: "content.delivery.reason.too_long", defaultValue: "message too long"))
|
||||
default:
|
||||
Issue.record("Expected the oversized token to be refused")
|
||||
}
|
||||
#expect(context.sentPrivateMessages.isEmpty)
|
||||
}
|
||||
|
||||
/// 21-sat single-mint V3 token (proofs of 1+4+16).
|
||||
private static let validV3Token: String = {
|
||||
private static let validV3Token = cashuV3Token(mint: "https://mint.example.com")
|
||||
|
||||
private static func cashuV3Token(mint: String, amounts: [Int] = [1, 4, 16]) -> String {
|
||||
let json: [String: Any] = [
|
||||
"token": [[
|
||||
"mint": "https://mint.example.com",
|
||||
"proofs": [1, 4, 16].map { ["amount": $0, "id": "009a1f293253e41e", "secret": "s", "C": "02c"] }
|
||||
"mint": mint,
|
||||
"proofs": amounts.map { ["amount": $0, "id": "009a1f293253e41e", "secret": "s", "C": "02c"] }
|
||||
]],
|
||||
"unit": "sat"
|
||||
]
|
||||
@ -501,7 +524,7 @@ struct CommandProcessorTests {
|
||||
.replacingOccurrences(of: "/", with: "_")
|
||||
.replacingOccurrences(of: "=", with: "")
|
||||
return "cashuA" + b64
|
||||
}()
|
||||
}
|
||||
|
||||
/// 21-sat single-mint definite-length V4 (CBOR) token (proofs of 1+4+16).
|
||||
private static let validV4Token: String = {
|
||||
|
||||
@ -35,6 +35,22 @@ struct PacketPayloadCapInteropTests {
|
||||
#expect(BinaryProtocol.decode(try inflatedFrame(type: .message)) == nil)
|
||||
}
|
||||
|
||||
@Test func privateMessageOver255UTF8BytesDoesNotEncode() {
|
||||
let within = String(repeating: "é", count: 127)
|
||||
#expect(within.utf8.count == 254)
|
||||
#expect(PrivateMessagePacket.contentFitsOnWire(within))
|
||||
#expect(PrivateMessagePacket(messageID: "m", content: within).encode() != nil)
|
||||
|
||||
let over = String(repeating: "é", count: 128)
|
||||
#expect(over.utf8.count == 256)
|
||||
#expect(PrivateMessagePacket.contentFitsOnWire(over) == false)
|
||||
#expect(PrivateMessagePacket(messageID: "m", content: over).encode() == nil)
|
||||
|
||||
let ascii = String(repeating: "x", count: PrivateMessagePacket.maxContentUTF8Bytes)
|
||||
#expect(PrivateMessagePacket(messageID: "m", content: ascii).encode() != nil)
|
||||
#expect(PrivateMessagePacket(messageID: "m", content: ascii + "y").encode() == nil)
|
||||
}
|
||||
|
||||
@Test func androidAndIOSSizedFragmentsStillDecode() throws {
|
||||
// Every byte value once, then zeros: the inner packet stays
|
||||
// uncompressed (its bytes are 100% unique) while the zero-filled
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user