From de52ab4397e37cc5b7a4b69fe8c1bc5dd01fbf20 Mon Sep 17 00:00:00 2001 From: "Daniel P. Clark" <6ftdan@gmail.com> Date: Wed, 21 Oct 2026 05:04:00 +0000 Subject: [PATCH 1/2] Stop trimming trailing bytes from binary S4 identity files S4Identity.parse trimmed ASCII whitespace from both ends of its input before reading the header. The binary form ends with a random AES-GCM tag, so about one file in forty ended in a byte that counts as whitespace and came back one byte short ("block of type 2 claims 73 bytes but only 72 remain"), which is what made the client identity specs fail intermittently in CI. Only leading whitespace is skipped now; the SQRLDATA text form keeps its tolerant trimming. Adds regression specs for both forms. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01LquamEUPHNaNMzZW6tZz8r --- gems/sqrl-core/CHANGELOG.md | 7 +++++++ gems/sqrl-core/lib/sqrl/storage.rb | 13 ++++++++----- gems/sqrl-core/spec/storage_spec.rb | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) 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..936f590 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 From bfd0e40b2fff7a51cc0c42ee053375004c2a063c Mon Sep 17 00:00:00 2001 From: "Daniel P. Clark" <6ftdan@gmail.com> Date: Wed, 21 Oct 2026 05:04:50 +0000 Subject: [PATCH 2/2] Lint the new storage specs Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01LquamEUPHNaNMzZW6tZz8r --- gems/sqrl-core/spec/storage_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gems/sqrl-core/spec/storage_spec.rb b/gems/sqrl-core/spec/storage_spec.rb index 936f590..6e03c59 100644 --- a/gems/sqrl-core/spec/storage_spec.rb +++ b/gems/sqrl-core/spec/storage_spec.rb @@ -214,12 +214,12 @@ def fast_settings 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") + 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) + expect(SQRL::S4Identity.parse("\r\n#{bytes}").to_bytes).to eq(bytes) end it "still tolerates whitespace around the SQRLDATA text form" do