diff --git a/gems/sqrl-core/CHANGELOG.md b/gems/sqrl-core/CHANGELOG.md index def7f22..bd8a269 100644 --- a/gems/sqrl-core/CHANGELOG.md +++ b/gems/sqrl-core/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## Unreleased + +- Fix: binary `sqrldata` files whose final byte happened to be an ASCII whitespace character + (one in about forty, since the last block ends with a random AES-GCM tag) lost that byte on + parse and failed with "block ... claims N bytes but only N-1 remain". Only leading + whitespace is skipped before the header now; the `SQRLDATA` text form is still trimmed. + ## 0.1.1 — 2026-10-20 - Fixed: Ed25519 and X25519 keys are built from their DER encodings when the openssl gem diff --git a/gems/sqrl-core/lib/sqrl/storage.rb b/gems/sqrl-core/lib/sqrl/storage.rb index 6284054..a309546 100644 --- a/gems/sqrl-core/lib/sqrl/storage.rb +++ b/gems/sqrl-core/lib/sqrl/storage.rb @@ -407,17 +407,20 @@ def initialize(type1: nil, type2: nil, type3: nil, unknown: []) # Parse either the binary (+sqrldata+) or the textual base64url (+SQRLDATA+) form. def self.parse(input) - trimmed = Storage.trim_ascii(input) - raise FormatError, "identity data too short" if trimmed.bytesize < 8 + # Only leading whitespace is skipped before the header is read: the binary form ends + # with an AES-GCM tag whose last byte may itself be a whitespace character, so trimming + # the tail would truncate roughly one file in forty. + data = input.b.sub(/\A[ \t\r\n\f\v]+/, "") + raise FormatError, "identity data too short" if data.bytesize < 8 - header = trimmed.byteslice(0, 8) + header = data.byteslice(0, 8) if header == HEADER_TEXT - text = trimmed.byteslice(8..) + text = Storage.trim_ascii(data.byteslice(8..)) raise FormatError, "invalid SQRLDATA text" unless text.force_encoding("UTF-8").valid_encoding? return parse_blocks(Util.b64u_decode(text)) end - return parse_blocks(trimmed.byteslice(8..)) if header == HEADER_BINARY + return parse_blocks(data.byteslice(8..)) if header == HEADER_BINARY raise FormatError, "missing sqrldata header" end diff --git a/gems/sqrl-core/spec/storage_spec.rb b/gems/sqrl-core/spec/storage_spec.rb index 726cdfe..6e03c59 100644 --- a/gems/sqrl-core/spec/storage_spec.rb +++ b/gems/sqrl-core/spec/storage_spec.rb @@ -211,4 +211,22 @@ def fast_settings expect(s.log_n).to eq(9) expect(SQRL::S4Identity.new.settings).to eq(s) end + describe "binary files whose last byte is whitespace" do + it "parses them without truncating the final block" do + type2 = SQRL::Storage::Type2Block.new(salt: SQRL::Util.random_bytes(16), log_n: 9, iterations: 1, + enc_iuk: SQRL::Util.random_bytes(32), tag: "#{"\x00" * 15}\n") + bytes = SQRL::Storage::HEADER_BINARY + type2.to_bytes + expect(bytes.byteslice(-1)).to eq("\n") + parsed = SQRL::S4Identity.parse(bytes) + expect(parsed.to_bytes).to eq(bytes) + expect(SQRL::S4Identity.parse("\r\n#{bytes}").to_bytes).to eq(bytes) + end + + it "still tolerates whitespace around the SQRLDATA text form" do + type2 = SQRL::Storage::Type2Block.new(salt: SQRL::Util.random_bytes(16), log_n: 9, iterations: 1, + enc_iuk: SQRL::Util.random_bytes(32), tag: SQRL::Util.random_bytes(16)) + text = SQRL::Storage::HEADER_TEXT + SQRL::Util.b64u_encode(type2.to_bytes) + expect(SQRL::S4Identity.parse(" #{text}\n\n").to_bytes).to eq(SQRL::Storage::HEADER_BINARY + type2.to_bytes) + end + end end