mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 04:25:21 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
41b7f5171e |
@@ -501,10 +501,6 @@ struct NostrEvent: Codable {
|
||||
throw NostrError.invalidEvent
|
||||
}
|
||||
|
||||
guard Self.isWithinInboundTagLimits(tags) else {
|
||||
throw NostrError.invalidEvent
|
||||
}
|
||||
|
||||
self.id = dict["id"] as? String ?? ""
|
||||
self.pubkey = pubkey
|
||||
self.created_at = createdAt
|
||||
@@ -513,17 +509,6 @@ struct NostrEvent: Codable {
|
||||
self.content = content
|
||||
self.sig = dict["sig"] as? String
|
||||
}
|
||||
|
||||
private static func isWithinInboundTagLimits(_ tags: [[String]]) -> Bool {
|
||||
guard tags.count <= TransportConfig.nostrMaxEventTags else { return false }
|
||||
|
||||
for tag in tags {
|
||||
guard tag.count <= TransportConfig.nostrMaxEventTagValues else { return false }
|
||||
guard tag.allSatisfy({ $0.utf8.count <= TransportConfig.nostrMaxEventTagValueBytes }) else { return false }
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func sign(with key: P256K.Schnorr.PrivateKey) throws -> NostrEvent {
|
||||
let (eventId, eventIdHash) = try calculateEventId()
|
||||
|
||||
@@ -963,7 +963,6 @@ private enum ParsedInbound {
|
||||
|
||||
init?(_ message: URLSessionWebSocketTask.Message) {
|
||||
guard let data = message.data,
|
||||
data.count <= TransportConfig.nostrMaxInboundMessageBytes,
|
||||
let array = try? JSONSerialization.jsonObject(with: data) as? [Any],
|
||||
array.count >= 2,
|
||||
let type = array[0] as? String else {
|
||||
|
||||
@@ -255,9 +255,8 @@ final class MessageFormattingEngine {
|
||||
isSelf: Bool,
|
||||
isMentioned: Bool
|
||||
) -> AttributedString {
|
||||
// For very long content without special tokens, use plain formatting
|
||||
let containsCashu = containsCashuToken(content)
|
||||
if (content.count > 4000 || content.hasVeryLongToken(threshold: 1024)) && !containsCashu {
|
||||
// For very long content, use plain formatting to avoid expensive regex and detector work.
|
||||
if content.isOversizedForRichFormatting() {
|
||||
return formatPlainContent(content, baseColor: baseColor, isSelf: isSelf)
|
||||
}
|
||||
|
||||
|
||||
@@ -129,12 +129,6 @@ enum TransportConfig {
|
||||
static let nostrGeohashSampleLimit: Int = 100
|
||||
static let nostrDMSubscribeLookbackSeconds: TimeInterval = 86400
|
||||
|
||||
// Nostr inbound event safety limits
|
||||
static let nostrMaxInboundMessageBytes: Int = 256 * 1024
|
||||
static let nostrMaxEventTags: Int = 64
|
||||
static let nostrMaxEventTagValues: Int = 16
|
||||
static let nostrMaxEventTagValueBytes: Int = 1024
|
||||
|
||||
// Nostr helpers
|
||||
static let nostrShortKeyDisplayLength: Int = 8
|
||||
static let nostrConvKeyPrefixLength: Int = 16
|
||||
|
||||
@@ -70,12 +70,7 @@ final class ChatMessageFormatter {
|
||||
let content = message.content
|
||||
let nsContent = content as NSString
|
||||
let nsLen = nsContent.length
|
||||
let containsCashuEarly: Bool = {
|
||||
let regex = Patterns.quickCashuPresence
|
||||
return regex.numberOfMatches(in: content, options: [], range: NSRange(location: 0, length: nsLen)) > 0
|
||||
}()
|
||||
|
||||
if (content.count > 4000 || content.hasVeryLongToken(threshold: 1024)) && !containsCashuEarly {
|
||||
if content.isOversizedForRichFormatting() {
|
||||
var plainStyle = AttributeContainer()
|
||||
plainStyle.foregroundColor = baseColor
|
||||
plainStyle.font = isSelf
|
||||
|
||||
@@ -262,10 +262,8 @@ final class ChatNostrCoordinator {
|
||||
if viewModel.deduplicationService.hasProcessedNostrEvent(event.id) { return }
|
||||
viewModel.deduplicationService.recordNostrEvent(event.id)
|
||||
|
||||
SecureLogger.debug(
|
||||
"GeoTeleport: recv pub=\(event.pubkey.prefix(8))… tagCount=\(event.tags.count)",
|
||||
category: .session
|
||||
)
|
||||
let tagSummary = event.tags.map { "[" + $0.joined(separator: ",") + "]" }.joined(separator: ",")
|
||||
SecureLogger.debug("GeoTeleport: recv pub=\(event.pubkey.prefix(8))… tags=\(tagSummary)", category: .session)
|
||||
|
||||
if viewModel.identityManager.isNostrBlocked(pubkeyHexLowercased: event.pubkey) {
|
||||
return
|
||||
|
||||
@@ -22,7 +22,7 @@ struct TextMessageView: View {
|
||||
let cashuLinks = message.content.extractCashuLinks()
|
||||
let lightningLinks = message.content.extractLightningLinks()
|
||||
HStack(alignment: .top, spacing: 0) {
|
||||
let isLong = (message.content.count > TransportConfig.uiLongMessageLengthThreshold || message.content.hasVeryLongToken(threshold: TransportConfig.uiVeryLongTokenThreshold)) && cashuLinks.isEmpty
|
||||
let isLong = message.content.isLongForDisplay()
|
||||
let isExpanded = expandedMessageIDs.contains(message.id)
|
||||
Text(conversationUIModel.formatMessage(message, colorScheme: colorScheme))
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
@@ -38,7 +38,7 @@ struct TextMessageView: View {
|
||||
}
|
||||
|
||||
// Expand/Collapse for very long messages
|
||||
if (message.content.count > TransportConfig.uiLongMessageLengthThreshold || message.content.hasVeryLongToken(threshold: TransportConfig.uiVeryLongTokenThreshold)) && cashuLinks.isEmpty {
|
||||
if message.content.isLongForDisplay() {
|
||||
let isExpanded = expandedMessageIDs.contains(message.id)
|
||||
let labelKey = isExpanded ? LocalizedStringKey("content.message.show_less") : LocalizedStringKey("content.message.show_more")
|
||||
Button(labelKey) {
|
||||
|
||||
@@ -21,6 +21,19 @@ extension String {
|
||||
return current >= threshold
|
||||
}
|
||||
|
||||
// Detect if message content should be collapsed to avoid expensive unbounded layout.
|
||||
func isLongForDisplay(
|
||||
lengthThreshold: Int = TransportConfig.uiLongMessageLengthThreshold,
|
||||
tokenThreshold: Int = TransportConfig.uiVeryLongTokenThreshold
|
||||
) -> Bool {
|
||||
count > lengthThreshold || hasVeryLongToken(threshold: tokenThreshold)
|
||||
}
|
||||
|
||||
// Detect if message content should use plain formatting to avoid expensive regex parsing.
|
||||
func isOversizedForRichFormatting(lengthThreshold: Int = 4000, tokenThreshold: Int = 1024) -> Bool {
|
||||
count > lengthThreshold || hasVeryLongToken(threshold: tokenThreshold)
|
||||
}
|
||||
|
||||
// Extract up to `max` Cashu tokens (cashuA/cashuB). Allow dot '.' and shorter lengths.
|
||||
func extractCashuLinks(max: Int = 3) -> [String] {
|
||||
let regex = MessageFormattingEngine.Patterns.cashu
|
||||
|
||||
@@ -606,6 +606,25 @@ struct ChatViewModelFormattingTests {
|
||||
|
||||
#expect(String(header.characters) == "<@Alice#a1b2> ")
|
||||
}
|
||||
|
||||
@Test @MainActor
|
||||
func formatMessageAsText_longCashuMessageUsesPlainFastPath() async {
|
||||
let (viewModel, _) = makeTestableViewModel()
|
||||
let cashu = "cashuA" + String(repeating: "a", count: 40)
|
||||
let longContent = "hi @bob " + cashu + " " + String(repeating: "x", count: 4_100)
|
||||
let message = BitchatMessage(
|
||||
id: "fmt-long-cashu",
|
||||
sender: "Alice#a1b2",
|
||||
content: longContent,
|
||||
timestamp: Date(timeIntervalSince1970: 1_700_010_123),
|
||||
isRelay: false,
|
||||
senderPeerID: PeerID(str: "00000000000000b3")
|
||||
)
|
||||
|
||||
let formatted = viewModel.formatMessageAsText(message, colorScheme: .light)
|
||||
|
||||
#expect(String(formatted.characters) == "<@Alice#a1b2> \(longContent) [\(message.formattedTimestamp)]")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Verification Tests
|
||||
|
||||
@@ -323,6 +323,32 @@ struct MessageFormattingEngineTests {
|
||||
// Exactly at threshold DOES trigger (uses >= comparison)
|
||||
#expect(content.hasVeryLongToken(threshold: 50))
|
||||
}
|
||||
|
||||
@Test func isLongForDisplay_doesNotIgnoreCashuLinks() {
|
||||
let cashu = "cashuA" + String(repeating: "a", count: 40)
|
||||
let content = String(repeating: "a", count: TransportConfig.uiLongMessageLengthThreshold + 1) + " " + cashu
|
||||
|
||||
#expect(content.extractCashuLinks().count == 1)
|
||||
#expect(content.isLongForDisplay())
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Test func formatMessage_longCashuMessageFallsBackToPlainContentPath() {
|
||||
let context = MockMessageFormattingContext(nickname: "carol")
|
||||
let cashu = "cashuA" + String(repeating: "a", count: 40)
|
||||
let longContent = "hi @bob " + cashu + " " + String(repeating: "x", count: 4_100)
|
||||
let message = BitchatMessage(
|
||||
id: "long-cashu",
|
||||
sender: "alice",
|
||||
content: longContent,
|
||||
timestamp: Date(timeIntervalSince1970: 1_700_000_999),
|
||||
isRelay: false
|
||||
)
|
||||
|
||||
let formatted = MessageFormattingEngine.formatMessage(message, context: context, colorScheme: .light)
|
||||
|
||||
#expect(String(formatted.characters) == "<@alice> \(longContent) [\(message.formattedTimestamp)]")
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
|
||||
@@ -241,51 +241,6 @@ struct NostrProtocolTests {
|
||||
#expect(!signed.isValidSignature())
|
||||
}
|
||||
|
||||
@Test func inboundNostrEventRejectsTooManyTags() throws {
|
||||
var eventDict = Self.validInboundEventDict()
|
||||
eventDict["tags"] = Array(
|
||||
repeating: ["g", "u4pruyd"],
|
||||
count: TransportConfig.nostrMaxEventTags + 1
|
||||
)
|
||||
|
||||
#expect(throws: NostrError.invalidEvent) {
|
||||
_ = try NostrEvent(from: eventDict)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func inboundNostrEventRejectsTooManyTagValues() throws {
|
||||
var eventDict = Self.validInboundEventDict()
|
||||
eventDict["tags"] = [Array(
|
||||
repeating: "value",
|
||||
count: TransportConfig.nostrMaxEventTagValues + 1
|
||||
)]
|
||||
|
||||
#expect(throws: NostrError.invalidEvent) {
|
||||
_ = try NostrEvent(from: eventDict)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func inboundNostrEventRejectsOversizedTagValues() throws {
|
||||
var eventDict = Self.validInboundEventDict()
|
||||
eventDict["tags"] = [[
|
||||
"g",
|
||||
String(repeating: "a", count: TransportConfig.nostrMaxEventTagValueBytes + 1)
|
||||
]]
|
||||
|
||||
#expect(throws: NostrError.invalidEvent) {
|
||||
_ = try NostrEvent(from: eventDict)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func inboundNostrEventAcceptsTagsWithinLimits() throws {
|
||||
var eventDict = Self.validInboundEventDict()
|
||||
eventDict["tags"] = [["g", "u4pruyd"], ["t", "teleport"]]
|
||||
|
||||
let event = try NostrEvent(from: eventDict)
|
||||
|
||||
#expect(event.tags.count == 2)
|
||||
}
|
||||
|
||||
@Test func geohashNotesSingleFilter_encodesExpectedTagShape() throws {
|
||||
let since = Date(timeIntervalSince1970: 1_234_567)
|
||||
let filter = NostrFilter.geohashNotes("u4pruyd", since: since, limit: 42)
|
||||
@@ -299,18 +254,6 @@ struct NostrProtocolTests {
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
private static func validInboundEventDict() -> [String: Any] {
|
||||
[
|
||||
"id": String(repeating: "0", count: 64),
|
||||
"pubkey": String(repeating: "1", count: 64),
|
||||
"created_at": 1_234_567,
|
||||
"kind": NostrProtocol.EventKind.ephemeralEvent.rawValue,
|
||||
"tags": [["g", "u4pruyd"]],
|
||||
"content": "hello",
|
||||
"sig": String(repeating: "2", count: 128)
|
||||
]
|
||||
}
|
||||
|
||||
private static func base64URLDecode(_ s: String) -> Data? {
|
||||
var str = s.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
|
||||
let rem = str.count % 4
|
||||
|
||||
Reference in New Issue
Block a user