From 4e622767bb1f2c5396cc43c75efb9d108bc8261d Mon Sep 17 00:00:00 2001 From: Satchmo Date: Mon, 16 Nov 2026 19:18:41 +0000 Subject: [PATCH 1/2] BSVAuth and wallet crypto accept 16 MiB payloads, as the TS AuthFetch does AuthLimits.maximumPayloadBytes and BRC104Limits.maximumPayloadBytes defaulted to 1 MiB, so a wallet storage reply over 1 MiB was refused with BRC104HTTPFramingError.resourceLimit before it was read. After a 180 KB image inscription the harness saw createAction and beginActionBatch replies of 1,328,943 and 1,330,479 bytes. TS AuthFetch sends request frames up to 16 MiB (MAX_AUTH_HTTP_REQUEST_FRAME_BYTES), reads response bodies up to 16 MiB (DEFAULT_SIMPLIFIED_FETCH_MAX_RESPONSE_BYTES), and MAX_AUTH_MESSAGE_BYTES is 16 MiB. Both defaults are now 16 MiB. BRC-103 signs and verifies each general message's whole payload through the wallet, and WalletCryptoLimits capped signature data at 1 MiB, so a larger payload still failed with payloadTooLarge. The TS ProtoWallet sets no bound there; the default is now 16 MiB as well. --- Sources/BSVAuth/BRC103/AuthModels.swift | 12 ++- Sources/BSVAuth/BRC104/BRC104Payload.swift | 9 ++- .../BSVWallet/Values/WalletCryptoLimits.swift | 12 ++- .../BRC104/BRC104HTTPFramingTests.swift | 80 ++++++++++++++++++- .../BRC52CertificateBinaryTests.swift | 8 +- .../WalletIdentifierTests.swift | 4 +- 6 files changed, 111 insertions(+), 14 deletions(-) diff --git a/Sources/BSVAuth/BRC103/AuthModels.swift b/Sources/BSVAuth/BRC103/AuthModels.swift index da7ab87..20b6d7d 100644 --- a/Sources/BSVAuth/BRC103/AuthModels.swift +++ b/Sources/BSVAuth/BRC103/AuthModels.swift @@ -200,6 +200,14 @@ public struct AuthMessage: Equatable, Sendable, CustomStringConvertible, } } +/// Bounds on what a peer accepts. +/// +/// `maximumPayloadBytes` bounds a general message's payload, which over HTTP is the whole BRC-104 +/// request or response: its body plus a few hundred bytes of framing. The default is 16 MiB, the +/// TypeScript SDK's `MAX_AUTH_MESSAGE_BYTES` and `AuthFetch`'s request frame limit +/// (`MAX_AUTH_HTTP_REQUEST_FRAME_BYTES`), so a reply a TypeScript client reads is one this one +/// reads too. At 1 MiB, a wallet storage reply carrying a photo inscription's transaction was +/// refused before it was read. public struct AuthLimits: Equatable, Sendable { public static let maximumAllowedJSONBytes = 64 << 20 public static let maximumAllowedPayloadBytes = 16 << 20 @@ -227,7 +235,7 @@ public struct AuthLimits: Equatable, Sendable { public let certificateLimits: CertificateLimits public let handshakeTimeout: Duration public init( - maximumJSONBytes: Int = 8 << 20, maximumPayloadBytes: Int = 1 << 20, + maximumJSONBytes: Int = 8 << 20, maximumPayloadBytes: Int = 16 << 20, maximumSessions: Int = 1024, maximumSessionsPerIdentity: Int = 8, maximumPendingSessions: Int = 128, maximumMessages: Int = 4096, maximumCertificateCount: Int = 64, maximumCertificateCertifierCount: Int = 64, @@ -271,7 +279,7 @@ public struct AuthLimits: Equatable, Sendable { } private init(standard: Void) { maximumJSONBytes = 8 << 20 - maximumPayloadBytes = 1 << 20 + maximumPayloadBytes = 16 << 20 maximumSessions = 1024 maximumSessionsPerIdentity = 8 maximumPendingSessions = 128 diff --git a/Sources/BSVAuth/BRC104/BRC104Payload.swift b/Sources/BSVAuth/BRC104/BRC104Payload.swift index a67f4ba..8515ccd 100644 --- a/Sources/BSVAuth/BRC104/BRC104Payload.swift +++ b/Sources/BSVAuth/BRC104/BRC104Payload.swift @@ -86,6 +86,11 @@ public struct BRC104Header: Equatable, Sendable, CustomStringConvertible, } } +/// Bounds on a BRC-104 request or response payload. +/// +/// `maximumPayloadBytes` defaults to 16 MiB, matching `AuthLimits` and the TypeScript SDK's +/// `AuthFetch` (`MAX_AUTH_HTTP_REQUEST_FRAME_BYTES`, and `DEFAULT_SIMPLIFIED_FETCH_MAX_RESPONSE_BYTES` +/// for a response body). public struct BRC104Limits: Equatable, Sendable { public let maximumPayloadBytes: Int public let maximumHeaders: Int @@ -93,7 +98,7 @@ public struct BRC104Limits: Equatable, Sendable { public let maximumHeaderValueBytes: Int public let maximumHeaderBytes: Int public init( - maximumPayloadBytes: Int = 1 << 20, maximumHeaders: Int = 64, + maximumPayloadBytes: Int = 16 << 20, maximumHeaders: Int = 64, maximumHeaderNameBytes: Int = 256, maximumHeaderValueBytes: Int = 8 << 10, maximumHeaderBytes: Int = 64 << 10 ) throws { @@ -116,7 +121,7 @@ public struct BRC104Limits: Equatable, Sendable { maximumHeaderBytes = headerBytes } public static let standard = BRC104Limits( - validatedPayload: 1 << 20, headers: 64, headerName: 256, headerValue: 8 << 10, + validatedPayload: 16 << 20, headers: 64, headerName: 256, headerValue: 8 << 10, headerBytes: 64 << 10) } diff --git a/Sources/BSVWallet/Values/WalletCryptoLimits.swift b/Sources/BSVWallet/Values/WalletCryptoLimits.swift index 6ca4d83..4cdd4fb 100644 --- a/Sources/BSVWallet/Values/WalletCryptoLimits.swift +++ b/Sources/BSVWallet/Values/WalletCryptoLimits.swift @@ -1,7 +1,13 @@ +/// Bounds on what the wallet's own cryptography reads. +/// +/// `maximumPayloadByteCount` bounds the data signed, verified, HMACed or encrypted. The default is +/// 16 MiB: the TypeScript `ProtoWallet` sets no bound of its own, and BRC-103 signs each general +/// message's whole payload, which the TypeScript `AuthFetch` allows up to 16 MiB. At 1 MiB, an +/// authenticated request or response over 1 MiB could not be signed or verified. public struct WalletCryptoLimits: Hashable, Sendable { public static let standard = WalletCryptoLimits( - validatedPayload: 1_048_576, - ciphertext: 1_048_624, + validatedPayload: 16_777_216, + ciphertext: 16_777_264, json: 8_388_608 ) @@ -10,7 +16,7 @@ public struct WalletCryptoLimits: Hashable, Sendable { public let maximumJSONByteCount: Int public init( - maximumPayloadByteCount: Int = 1_048_576, + maximumPayloadByteCount: Int = 16_777_216, maximumJSONByteCount: Int = 8_388_608 ) throws { guard maximumPayloadByteCount >= 0 else { diff --git a/Tests/BSVAuthTests/BRC104/BRC104HTTPFramingTests.swift b/Tests/BSVAuthTests/BRC104/BRC104HTTPFramingTests.swift index bd411dd..73bae48 100644 --- a/Tests/BSVAuthTests/BRC104/BRC104HTTPFramingTests.swift +++ b/Tests/BSVAuthTests/BRC104/BRC104HTTPFramingTests.swift @@ -169,6 +169,77 @@ struct BRC104HTTPFramingTests { } } + /// The TypeScript `AuthFetch` sends a request frame of up to 16 MiB + /// (`MAX_AUTH_HTTP_REQUEST_FRAME_BYTES`) and reads a response body of up to 16 MiB + /// (`DEFAULT_SIMPLIFIED_FETCH_MAX_RESPONSE_BYTES`). A wallet storage reply carrying a 180 KB + /// image inscription as JSON numbers was 1,330,479 bytes, and at the old 1 MiB default it was + /// refused with `resourceLimit` before it was read. + @Test("payloads over 1 MiB and under 16 MiB are signed, framed and read") + func payloadsOverOneMebibyte() async throws { + #expect(AuthLimits.standard.maximumPayloadBytes == 16 << 20) + #expect(try AuthLimits().maximumPayloadBytes == 16 << 20) + #expect(BRC104Limits.standard.maximumPayloadBytes == 16 << 20) + #expect(try BRC104Limits().maximumPayloadBytes == 16 << 20) + #expect(BRC104HTTPFramingLimits.standard.payloadLimits.maximumPayloadBytes == 16 << 20) + + let requestBody = [UInt8](repeating: 0x5b, count: (1 << 20) + 1) + let responseBody = [UInt8](repeating: 0x31, count: 1_330_479) + let fixture = try await makeHTTPFixture( + requestBody: requestBody, responseBody: responseBody + ) + + let requestFrame = try BRC104HTTPFrameCodec.encodeRequest(fixture.requestMessage) + #expect(requestFrame.body == requestBody) + #expect(try BRC104HTTPFrameCodec.decodeRequest(requestFrame) == fixture.requestMessage) + + let responseFrame = try BRC104HTTPFrameCodec.encodeResponse(fixture.responseMessage) + #expect(responseFrame.body == responseBody) + let decoded = try BRC104HTTPFrameCodec.decodeResponse( + try BRC104HTTPResponseFrame( + status: responseFrame.status, headers: responseFrame.headers, + body: responseFrame.body + ), + expectedRequestID: fixture.requestID + ) + #expect(decoded == fixture.responseMessage) + let inner = try BRC104Codec.decodeResponse(try #require(decoded.payload)) + #expect(inner.body == responseBody) + } + + @Test("a response payload of exactly 16 MiB is read and one byte more is refused") + func sixteenMebibyteBoundary() throws { + let requestID = [UInt8](repeating: 9, count: 32) + let empty = try BRC104Codec.encode( + BRC104Response(requestID: requestID, status: 200, body: []) + ) + // The empty body's one-byte length prefix is five bytes for a body this large. + let overhead = empty.count - 1 + 5 + let largest = try BRC104Response( + requestID: requestID, status: 200, + body: [UInt8](repeating: 1, count: (16 << 20) - overhead) + ) + let encoded = try BRC104Codec.encode(largest) + #expect(encoded.count == 16 << 20) + #expect(try BRC104Codec.decodeResponse(encoded) == largest) + + #expect(throws: BRC104Error.resourceLimit) { + try BRC104Codec.encode( + BRC104Response( + requestID: requestID, status: 200, + body: [UInt8](repeating: 1, count: (16 << 20) - overhead + 1) + ) + ) + } + #expect(throws: BRC104Error.resourceLimit) { + try BRC104Codec.decodeResponse(encoded + [0]) + } + #expect(throws: BRC104HTTPFramingError.resourceLimit) { + try BRC104HTTPResponseFrame( + status: 200, body: [UInt8](repeating: 1, count: (16 << 20) + 1) + ) + } + } + @Test("frames and messages do not disclose HTTP or authentication data") func redaction() async throws { let fixture = try await makeHTTPFixture() @@ -214,7 +285,10 @@ private struct HTTPAuthFixture { let responseMessage: AuthMessage } -private func makeHTTPFixture() async throws -> HTTPAuthFixture { +private func makeHTTPFixture( + requestBody: [UInt8] = Array(#"{"secret":true}"#.utf8), + responseBody: [UInt8] = Array("accepted".utf8) +) async throws -> HTTPAuthFixture { let client = PeerAuthenticator(wallet: ProtoWallet(rootKey: try httpPrivateKey(3))) let server = PeerAuthenticator(wallet: ProtoWallet(rootKey: try httpPrivateKey(7))) let start = try await client.beginAuthentication(with: try httpPrivateKey(7).publicKey) @@ -232,7 +306,7 @@ private func makeHTTPFixture() async throws -> HTTPAuthFixture { .init(name: "content-type", value: "application/json"), .init(name: "x-bsv-trace", value: "1"), ], - body: Array(#"{"secret":true}"#.utf8) + body: requestBody ) let requestMessage = try sentHTTPMessage([ try await client.makeGeneralMessage( @@ -249,7 +323,7 @@ private func makeHTTPFixture() async throws -> HTTPAuthFixture { requestID: requestID, status: 201, headers: [.init(name: "x-bsv-result", value: "ok")], - body: Array("accepted".utf8) + body: responseBody ) let responseMessage = try sentHTTPMessage([ try await server.makeGeneralMessage( diff --git a/Tests/BSVWalletTests/Certificates/BRC52CertificateBinaryTests.swift b/Tests/BSVWalletTests/Certificates/BRC52CertificateBinaryTests.swift index 829c795..efe5eb4 100644 --- a/Tests/BSVWalletTests/Certificates/BRC52CertificateBinaryTests.swift +++ b/Tests/BSVWalletTests/Certificates/BRC52CertificateBinaryTests.swift @@ -402,9 +402,11 @@ final class BRC52CertificateBinaryTests: XCTestCase { limits: custom ) let unsigned = try certificate.binary(includingSignature: false, limits: custom) - XCTAssertGreaterThan(unsigned.count, WalletCryptoLimits.standard.maximumPayloadByteCount) + // A wallet bounded at 1 MiB, below the expanded preimage. + let walletLimits = try WalletCryptoLimits(maximumPayloadByteCount: 1_048_576) + XCTAssertGreaterThan(unsigned.count, walletLimits.maximumPayloadByteCount) let signed = try await certificate.signed( - using: ProtoWallet(rootKey: try key(9)), + using: ProtoWallet(rootKey: try key(9), limits: walletLimits), limits: custom ) let signatureValid = try await signed.verifySignature(limits: custom) @@ -416,7 +418,7 @@ final class BRC52CertificateBinaryTests: XCTestCase { ) await XCTAssertThrowsErrorAsync { _ = try await certificate.signed( - using: ProtoWallet(rootKey: try self.key(9)), + using: ProtoWallet(rootKey: try self.key(9), limits: walletLimits), limits: exactUnsignedOnly ) } diff --git a/Tests/BSVWalletTests/WalletIdentifierTests.swift b/Tests/BSVWalletTests/WalletIdentifierTests.swift index e761412..4128cfe 100644 --- a/Tests/BSVWalletTests/WalletIdentifierTests.swift +++ b/Tests/BSVWalletTests/WalletIdentifierTests.swift @@ -196,7 +196,9 @@ final class WalletIdentifierTests: XCTestCase { } func testCryptoLimitsValidation() throws { - XCTAssertEqual(WalletCryptoLimits.standard.maximumCiphertextByteCount, 1_048_624) + XCTAssertEqual(WalletCryptoLimits.standard.maximumPayloadByteCount, 16_777_216) + XCTAssertEqual(WalletCryptoLimits.standard.maximumCiphertextByteCount, 16_777_264) + XCTAssertEqual(try WalletCryptoLimits(), WalletCryptoLimits.standard) XCTAssertEqual(try WalletCryptoLimits(maximumPayloadByteCount: 0).maximumCiphertextByteCount, 48) XCTAssertThrowsError(try WalletCryptoLimits(maximumPayloadByteCount: -1)) XCTAssertThrowsError(try WalletCryptoLimits(maximumJSONByteCount: -1)) From fdeef0ed3261d72a6b86ce17b8e37d3053a0a9cc Mon Sep 17 00:00:00 2001 From: Satchmo Date: Mon, 16 Nov 2026 19:24:41 +0000 Subject: [PATCH 2/2] Rename the UHRP test's hash property so it builds on Darwin On Darwin an XCTestCase is an NSObject, whose hash is an Int, so a [UInt8] property named hash cannot override it. Linux builds it because there XCTestCase is not an NSObject. --- Tests/BSVStorageTests/UHRPAdvertisementTests.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Tests/BSVStorageTests/UHRPAdvertisementTests.swift b/Tests/BSVStorageTests/UHRPAdvertisementTests.swift index 3292bd4..0c6f616 100644 --- a/Tests/BSVStorageTests/UHRPAdvertisementTests.swift +++ b/Tests/BSVStorageTests/UHRPAdvertisementTests.swift @@ -15,14 +15,14 @@ import XCTest /// `[2, "uhrp advertisement"]`, key ID "1", counterparty anyone (lines 89-98). final class UHRPAdvertisementTests: XCTestCase { private let hostScalar: [UInt8] = Array(repeating: 0, count: 31) + [0x2a] - private let hash = [UInt8](repeating: 0xab, count: 32) + private let contentHash = [UInt8](repeating: 0xab, count: 32) private let url = "https://cdn.example/abc" func testDecodesHandBuiltTypeScriptAdvertisement() throws { let host = try PrivateKey(hostScalar) let expiry = CompactSize.encode(1_700_000_000) let size = CompactSize.encode(1_024) - let fields = [host.publicKey.compressedBytes, hash, Array(url.utf8), expiry, size] + let fields = [host.publicKey.compressedBytes, contentHash, Array(url.utf8), expiry, size] let lockingKey = try linkedPrivateKey(host) let signature = try lockingKey.sign(digest: BSVHashing.sha256(Array(fields.joined()))) @@ -40,7 +40,7 @@ final class UHRPAdvertisementTests: XCTestCase { let advertisement = try UHRPAdvertisement.decode(script) XCTAssertEqual(advertisement.hostIdentityKey, host.publicKey) - XCTAssertEqual(advertisement.hash.bytes, hash) + XCTAssertEqual(advertisement.hash.bytes, contentHash) XCTAssertEqual(advertisement.hostedFileLocation.absoluteString, url) XCTAssertEqual(advertisement.expiryTime, 1_700_000_000) XCTAssertEqual(advertisement.fileSize, 1_024) @@ -69,7 +69,7 @@ final class UHRPAdvertisementTests: XCTestCase { // requires six fields, so this shape is refused. let host = try PrivateKey(hostScalar) let fields = [ - hash, + contentHash, Array("XUT6PqWb3GP3LR7dmBMCJwZ3oo5g1iGCF3CrpzyuJCemkGu1WGoq".utf8), Array(url.utf8), CompactSize.encode(1_700_000_000), @@ -88,7 +88,7 @@ final class UHRPAdvertisementTests: XCTestCase { let host = try PrivateKey(hostScalar) let lockingKey = try linkedPrivateKey(host) let fields = [ - host.publicKey.compressedBytes, hash, Array(url.utf8), + host.publicKey.compressedBytes, contentHash, Array(url.utf8), CompactSize.encode(1_700_000_000), CompactSize.encode(1_024), ] let signature = try lockingKey.sign(digest: BSVHashing.sha256(Array(fields.joined()))) @@ -108,7 +108,7 @@ final class UHRPAdvertisementTests: XCTestCase { let host = try PrivateKey(hostScalar) let lockingKey = try linkedPrivateKey(host) let fields = [ - host.publicKey.compressedBytes, hash, Array(url.utf8), + host.publicKey.compressedBytes, contentHash, Array(url.utf8), CompactSize.encode(1_700_000_000), CompactSize.encode(1_024), ] let digest = BSVHashing.sha256(Array(fields.joined())) @@ -225,7 +225,7 @@ final class UHRPAdvertisementTests: XCTestCase { ) throws -> Script { let fields = [ hostKeyField ?? host.publicKey.compressedBytes, - hash ?? self.hash, + hash ?? contentHash, Array((url ?? self.url).utf8), expiry, size,