Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gems/sqrl-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 8 additions & 5 deletions gems/sqrl-core/lib/sqrl/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions gems/sqrl-core/spec/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading