From 37f0c64e6aff6827fdc60f3eb5c244954a4dcd4f Mon Sep 17 00:00:00 2001 From: "Daniel P. Clark" <6ftdan@gmail.com> Date: Wed, 21 Oct 2026 15:24:57 -0400 Subject: [PATCH] Conform the server and client to Gibson's final SQRL spec (for 0.1.2) Audit against the December 2019 GRC documents (On The Wire v1.07, Operating Details v1.01, Cryptography v1.04, Explained v1.03) and Steve Gibson's latest rulings in the grc.sqrl newsgroup, his later word superseding abandoned designs. sqrl-core - Return suk= in the reply to every command when the client sends opt=suk, and always for a disabled account or a previous-identity match (grc.sqrl:12289; On The Wire p.14). It was returned on query only. - A successful re-key replies with 0x01 set and 0x02 clear (the post-update state) and clears a disabled state (On The Wire p.17; grc.sqrl:20273, 22934). - disable in CPS mode returns url= like every other non-query command. - sqrlonly/hardlock are copied from every successful non-query command. - A superseded identity is refused with exactly 0x240 (grc.sqrl:23020). - An unknown, expired or reused nut draws a transient error whose fresh nut is not registered, so the retry fails identically, the client's cue to have the user refresh the page (grc.sqrl:23543). It used to "succeed" against an anonymous pending login no browser could complete. - Handler#status never reports a CPS login; PendingLogin#cps? is new. sqrl-rack - A login the client completed with opt=cps can no longer be completed through the browser's status poll or a token-less callback: the server "MUST NOT authenticate the browser session" in that case (On The Wire, url=). Only the browser the client redirects to the one-time token is signed in. sqrl-client - After a transient error the agent retries with the fresh nut and stops as soon as two successive replies carry the identical TIF, with GRC's "login page has expired" message (grc.sqrl:23543, 21887). - The conformance runner accepts both conforming stale-nut behaviours. New spec gems/sqrl-core/spec/gibson_rulings_spec.rb pins each rule to its citation. Docs and all six changelogs updated; the entries join the as yet unpublished 0.1.2 release. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01KbjY2qFzx6ywviQPTia9Ea --- docs/protocol.md | 39 +++- docs/security.md | 5 +- gems/devise-sqrl/CHANGELOG.md | 41 +++- gems/omniauth-sqrl/CHANGELOG.md | 40 +++- gems/sqrl-client/CHANGELOG.md | 37 +++- gems/sqrl-client/lib/sqrl/client/agent.rb | 23 ++- .../lib/sqrl/client/conformance/runner.rb | 19 +- gems/sqrl-client/spec/agent_spec.rb | 13 +- gems/sqrl-core/CHANGELOG.md | 63 +++++- gems/sqrl-core/lib/sqrl/server.rb | 64 +++++-- gems/sqrl-core/lib/sqrl/server/stores.rb | 6 + gems/sqrl-core/spec/flows_spec.rb | 17 +- gems/sqrl-core/spec/gibson_rulings_spec.rb | 181 ++++++++++++++++++ gems/sqrl-core/spec/server_spec.rb | 33 +++- gems/sqrl-rack/CHANGELOG.md | 40 +++- gems/sqrl-rack/lib/sqrl/rack/endpoint.rb | 8 +- gems/sqrl-rack/spec/middleware_spec.rb | 20 ++ gems/sqrl-rails/CHANGELOG.md | 40 +++- gems/sqrl-rails/spec/stores_spec.rb | 4 +- 19 files changed, 595 insertions(+), 98 deletions(-) create mode 100644 gems/sqrl-core/spec/gibson_rulings_spec.rb diff --git a/docs/protocol.md b/docs/protocol.md index 2989319..8c4e0e7 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -1,9 +1,11 @@ # SQRL protocol notes for Ruby A Ruby-oriented summary of the parts of SQRL that the gems implement. The authoritative -sources are GRC's specification documents (*SQRL Explained*, *Operating Details* v1.01, -*Cryptography* v1.04, *On The Wire* v1.07); where they are ambiguous the gems follow the -rulings collected in the Rust reference implementation. +sources are GRC's specification documents (*SQRL Explained* v1.03, *Operating Details* v1.01, +*Cryptography* v1.04, *On The Wire* v1.07, all October–December 2019); where they are +ambiguous the gems follow Steve Gibson's latest ruling in the `grc.sqrl` newsgroup +(news.grc.com, cited here as `grc.sqrl:
`), his later word superseding earlier, +abandoned designs. ## Keys @@ -23,7 +25,9 @@ rulings collected in the Rust reference implementation. `site_key` is the lower-cased host of the `sqrl://` URL (user info and port stripped), plus the first `x` characters of the path when the URL carries `x=`, plus `"\0" + alt_id` for an -Alt-ID. In Ruby: `SQRL::URL.parse(url).site_key_bytes(alt_id)`. +Alt-ID (the NUL separator is Gibson's April 2019 ruling, grc.sqrl:22238: `domain [+ext] +[+ 0x00 + Alt-ID]`; the *SQRL Explained* PDF only says "appended"). In Ruby: +`SQRL::URL.parse(url).site_key_bytes(alt_id)`. `EnHash` is 16 chained SHA-256 iterations XOR-ed together. `EnScrypt` is scrypt with `N = 512, r = 256, p = 1, dkLen = 32`, iterated with each output as the next salt and all @@ -66,6 +70,11 @@ and `SQRL::Protocol.parse_pairs` are the low-level pieces. `SQRL::Tif` wraps the value with predicate methods; any bit above 0x200 makes `Tif#defined?` false and the client aborts. +The bits describe the account *after* the command ran: the reply to the `ident` that re-keys +an account has 0x01 set and 0x02 clear. A superseded identity (one the site has already seen +as a `pidk`) is refused with exactly `0x240`. When 0x40 is set, no bit other than 0x80 +carries meaning. + ## Scheme `sqrl://` is the only scheme and it means HTTPS: the client POSTs to `https://` + the rest @@ -76,14 +85,26 @@ only in the explicit loopback testing mode described in `docs/security.md`. 1. The site renders `sqrl://host/path?nut=N` (`SQRL::Server::Handler#new_login_url`). 2. The client sends `query`. The reply says whether `idk` (or `pidk`) is known, whether - SQRL is disabled for the account, returns `suk` when asked, and may pose an `ask`. + SQRL is disabled for the account, and may pose an `ask` (only here, and only when 0x01 + is set). 3. The client sends `ident` (with `suk`/`vuk` for a new account, with `pidk`/`pids`/`urs` - and new `suk`/`vuk` to re-key, with `btn` to answer an `ask`). With `cps`, the reply - carries `url=`. + and new `suk`/`vuk` to re-key, with `btn` to answer an `ask`). A re-key also clears any + disabled state, since the `urs` carries the rescue code's authority. 4. `disable` needs only `ids`; `enable` and `remove` need `urs` under the URSK, which needs the IUK (rescue code), unless only a previous identity matched (then the previous IUK - signs). -5. `TRANSIENT_ERROR` (stale or unknown nut) means: send a fresh `query` with the new nut. + signs). Every successful non-query command copies the `sqrlonly`/`hardlock` state the + client sent (their absence clears the flags). +5. The reply to *every* command carries `suk=` when the client asked with `opt=suk` (GRC's + client always does), when the account is disabled, or when a previous identity matched + (Gibson, grc.sqrl:12289, January 2016; On The Wire v1.07). +6. With `cps`, the reply to every non-query command carries `url=`, and the server never + signs in the browser that showed the login page: only the browser the client redirects to + that one-time URL is signed in (`SQRL::Rack::Endpoint` refuses the poll for such logins). +7. `TRANSIENT_ERROR` (stale or unknown nut) means: retry once with the new nut. The gems' + server keeps its state by reference, so the retry draws the identical error; two identical + errors in a row tell the client that the login page has expired and must be refreshed + (Gibson, grc.sqrl:23543, November 2019). `SQRL::Client::Agent` raises `LoginFailed` with + that message. ## Client Provided Session diff --git a/docs/security.md b/docs/security.md index f8a4837..71e5d4e 100644 --- a/docs/security.md +++ b/docs/security.md @@ -52,7 +52,10 @@ Completing a login requires one of two things: - **CPS (same device).** The server hands the client a one-time, 32-byte random token in `url=`; the client sends the browser there. The token is claimed exactly once, expires after `cps_token_ttl` (60 s), and must belong to the pending login in *that browser's* - session. A token stolen from one browser cannot log in another. + session. A token stolen from one browser cannot log in another. A login the client flagged + `cps` is *never* completed through the poll below: the specification says the server "MUST + NOT authenticate the browser session" in that case, because the page that showed the login + link may be a spoof. Only the browser the client redirects to the token gets in. - **Poll (other device).** The browser's script polls a cookie-bound status endpoint. The pending login completes once, from the browser that owns it. The protocol's own IP check applies (`require_ip_match`: a client whose IP differs from the browser's is refused diff --git a/gems/devise-sqrl/CHANGELOG.md b/gems/devise-sqrl/CHANGELOG.md index 52b243f..7b9e663 100644 --- a/gems/devise-sqrl/CHANGELOG.md +++ b/gems/devise-sqrl/CHANGELOG.md @@ -1,18 +1,42 @@ # Changelog -## 0.1.2 — 2026-10-21 +All notable changes to `devise-sqrl` are documented in this file. -- No changes; released in lockstep with sqrl-core 0.1.2 (fixes binary S4 identity files whose last byte is whitespace being truncated on parse). +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this +project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). Every gem in +the family shares one version number and is released in lockstep. -## 0.1.1 — 2026-10-20 +## [Unreleased] -- No changes. Released with sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 - and 3.2; every gem in the family shares one version. +## [0.1.2] - 2026-10-21 -## 0.1.0 — 2026-10-20 +### Changed + +- Requires sqrl-rails 0.1.2, and through it sqrl-core and sqrl-rack 0.1.2. sqrl-core fixes + binary S4 identity files whose last byte is whitespace being truncated on parse and brings + the server into line with Steve Gibson's final specification (`suk=` on every command, the + post-update TIF after a re-key, `url=` on a CPS `disable`, and more). No code changes in + this gem. + +### Security + +- Through sqrl-rack 0.1.2, a login the SQRL client completed in CPS mode can no longer be + completed through the browser's status poll; only the browser holding the one-time + `url=` token is signed in, as the specification requires. + +## [0.1.1] - 2026-10-20 + +### Changed + +- Requires sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 and 3.2. No other + changes. + +## [0.1.0] - 2026-10-20 First release. +### Added + - `devise :sqrl_authenticatable`: routes under the mapping (`/users/sqrl`, `cli`, `status`, `callback`, `sqrl.js`, `refresh`, `identities`), a Warden strategy that signs in through Devise's own hooks (trackable, rememberable, activatable), a controller, views and a @@ -23,3 +47,8 @@ First release. - Identity management under the mapping; `ask` with deny button; 429/400 handling for rate-limit hooks and disallowed hosts. - Requires Ruby 3.2+, Devise 4.9 or 5.0, Rails 7.1 to 8.1, and `sqrl-rails`. + +[Unreleased]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.2...HEAD +[0.1.2]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.1...v0.1.2 +[0.1.1]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.0...v0.1.1 +[0.1.0]: https://github.com/danielpclark/sqrl-ruby/releases/tag/v0.1.0 diff --git a/gems/omniauth-sqrl/CHANGELOG.md b/gems/omniauth-sqrl/CHANGELOG.md index dd6e21d..751202d 100644 --- a/gems/omniauth-sqrl/CHANGELOG.md +++ b/gems/omniauth-sqrl/CHANGELOG.md @@ -1,18 +1,41 @@ # Changelog -## 0.1.2 — 2026-10-21 +All notable changes to `omniauth-sqrl` are documented in this file. -- No changes; released in lockstep with sqrl-core 0.1.2 (fixes binary S4 identity files whose last byte is whitespace being truncated on parse). +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this +project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). Every gem in +the family shares one version number and is released in lockstep. -## 0.1.1 — 2026-10-20 +## [Unreleased] -- No changes. Released with sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 - and 3.2; every gem in the family shares one version. +## [0.1.2] - 2026-10-21 -## 0.1.0 — 2026-10-20 +### Changed + +- Requires sqrl-core and sqrl-rack 0.1.2. sqrl-core fixes binary S4 identity files whose + last byte is whitespace being truncated on parse and brings the server into line with + Steve Gibson's final specification (`suk=` on every command, the post-update TIF after a + re-key, `url=` on a CPS `disable`, and more). No code changes in this gem. + +### Security + +- Through sqrl-rack 0.1.2, a login the SQRL client completed in CPS mode can no longer be + completed through the browser's status poll; only the browser holding the one-time + `url=` token is signed in, as the specification requires. + +## [0.1.1] - 2026-10-20 + +### Changed + +- Requires sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 and 3.2. No other + changes. + +## [0.1.0] - 2026-10-20 First release. +### Added + - `provider :sqrl`: request phase renders the login page; the strategy serves the SQRL endpoint, status poll, script and refresh routes itself; the callback builds an auth hash keyed by the identity key with `extra["sqrl"]` facts (created, re-keyed from, options, @@ -23,3 +46,8 @@ First release. testing mode, `poll_requires_ip_match` (off by default). - Works with `devise :omniauthable`. - Requires Ruby 3.1+, OmniAuth 2.x. + +[Unreleased]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.2...HEAD +[0.1.2]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.1...v0.1.2 +[0.1.1]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.0...v0.1.1 +[0.1.0]: https://github.com/danielpclark/sqrl-ruby/releases/tag/v0.1.0 diff --git a/gems/sqrl-client/CHANGELOG.md b/gems/sqrl-client/CHANGELOG.md index d577ca8..5656fbf 100644 --- a/gems/sqrl-client/CHANGELOG.md +++ b/gems/sqrl-client/CHANGELOG.md @@ -1,18 +1,38 @@ # Changelog -## 0.1.2 — 2026-10-21 +All notable changes to `sqrl-client` are documented in this file. -- No changes; released in lockstep with sqrl-core 0.1.2 (fixes binary S4 identity files whose last byte is whitespace being truncated on parse). +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this +project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). Every gem in +the family shares one version number and is released in lockstep. -## 0.1.1 — 2026-10-20 +## [Unreleased] -- No changes. Released with sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 - and 3.2; every gem in the family shares one version. +## [0.1.2] - 2026-10-21 -## 0.1.0 — 2026-10-20 +### Changed + +- After a transient error the agent retries with the fresh nut and stops as soon as two + successive replies carry the identical TIF, raising `LoginFailed` (reason `:transient`) + with GRC's "The SQRL login page has expired" message, as Gibson specified + (grc.sqrl:23543). `max_transient_retries` remains as an upper bound. +- The conformance runner's `transient_recovery` case accepts both conforming server + behaviours: recovery on retry, or the identical repeated transient error. +- Requires sqrl-core 0.1.2. + +## [0.1.1] - 2026-10-20 + +### Changed + +- Requires sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 and 3.2. No other + changes. + +## [0.1.0] - 2026-10-20 First release. +### Added + - `SQRL::Client::Agent`: login, query, disable, enable, remove against any SQRL site, with login-page discovery, previous-identity handling, `ask` and `sin` answering, transient retry and CPS handoff on the same cookie jar. @@ -24,3 +44,8 @@ First release. - A 23-case server conformance suite usable from Ruby, RSpec (`it_behaves_like "a SQRL server"`) and the command line, with JUnit output. - Requires Ruby 3.1+ and `http-cookie`. + +[Unreleased]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.2...HEAD +[0.1.2]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.1...v0.1.2 +[0.1.1]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.0...v0.1.1 +[0.1.0]: https://github.com/danielpclark/sqrl-ruby/releases/tag/v0.1.0 diff --git a/gems/sqrl-client/lib/sqrl/client/agent.rb b/gems/sqrl-client/lib/sqrl/client/agent.rb index f9c7cce..0cc1a91 100644 --- a/gems/sqrl-client/lib/sqrl/client/agent.rb +++ b/gems/sqrl-client/lib/sqrl/client/agent.rb @@ -88,6 +88,9 @@ def inspect class Agent # Seconds between status polls for non-CPS logins. DEFAULT_POLL_INTERVAL = 0.5 + # The message GRC's reference client shows when a login page has gone stale + # (Gibson, grc.sqrl:21887, 7 Feb 2019). + TRANSIENT_MESSAGE = "The SQRL login page has expired. Please refresh the page, then log in with SQRL." attr_reader :identity, :transport, :options, :alt_id, :logger attr_accessor :cps, :follow_cps, :unknown_site, :ask_handler, :max_transient_retries, :poll, :poll_interval, @@ -209,11 +212,10 @@ def run(target, command, rescue_code: nil) break unless session.state.transient? transient += 1 - next unless transient > @max_transient_retries + next unless transient > @max_transient_retries || repeated_error?(ctx) - raise LoginFailed.new("the server kept answering with a transient error", reason: :transient, - tif: session.last_response.tif, - state: session.state) + raise LoginFailed.new(TRANSIENT_MESSAGE, reason: :transient, tif: session.last_response.tif, + state: session.state) end finish(ctx, command) @@ -237,10 +239,10 @@ def query_until_settled(ctx) case state.kind when :transient transient += 1 - next if transient <= @max_transient_retries + next if transient <= @max_transient_retries && !repeated_error?(ctx) - raise LoginFailed.new("the server kept answering with a transient error", reason: :transient, - tif: session.last_response.tif, state: state) + raise LoginFailed.new(TRANSIENT_MESSAGE, reason: :transient, tif: session.last_response.tif, + state: state) when :unknown next if session.has_previous? && session.try_next_previous @@ -258,6 +260,13 @@ def query_until_settled(ctx) end end + # "In the case that two successive TIF errors exactly match, the SQRL client MUST abort the + # transaction and ask the user to please refresh their login page" (Gibson, grc.sqrl:23543, + # 23 Nov 2019; On The Wire v1.07, "Transient Error Details"). + def repeated_error?(ctx) + ctx.tifs.length >= 2 && ctx.tifs[-1] == ctx.tifs[-2] + end + def answer_ask(ctx) ask = ctx.session.pending_ask return unless ask diff --git a/gems/sqrl-client/lib/sqrl/client/conformance/runner.rb b/gems/sqrl-client/lib/sqrl/client/conformance/runner.rb index ddd60bd..19ea7fe 100644 --- a/gems/sqrl-client/lib/sqrl/client/conformance/runner.rb +++ b/gems/sqrl-client/lib/sqrl/client/conformance/runner.rb @@ -451,15 +451,26 @@ def case_undefined_tif_bit_aborts_client(_ctx) # ---- robustness -------------------------------------------------------------------- + # A stale nut must draw a transient error (0x20) with a fresh nut. What happens on the + # retry depends on the server (On The Wire v1.07, "Transient Error Details"): a server + # that can tie the stale nut back to its browser session lets the retry succeed; a + # server that keeps its state by reference answers with exactly the same error, which + # tells the client to have the user refresh the page. Both are conforming; anything else + # (a different error, or a "success" nobody's browser can complete) is not. def case_transient_recovery(ctx) id = identity browser = transport page = @site.login_page(browser) bogus = page.sqrl_url.sub(/nut=[^&]+/, "nut=conformance-stale") - result = agent(id, transport: browser, cps: false, poll: false).login(bogus) - ctx.transcript.concat(result.transcript) - check(result.tif_history.first.transient_error?, "the stale nut did not produce a transient error") - check(result.authenticated?, "login did not recover from the transient error") + begin + result = agent(id, transport: browser, cps: false, poll: false).login(bogus) + ctx.transcript.concat(result.transcript) + check(result.tif_history.first.transient_error?, "the stale nut did not produce a transient error") + check(result.authenticated?, "login did not recover from the transient error") + rescue LoginFailed => e + check(e.reason == :transient && e.tif&.transient_error?, + "the stale nut did not produce a transient error (#{e.message})") + end end def case_concurrent_logins(ctx) diff --git a/gems/sqrl-client/spec/agent_spec.rb b/gems/sqrl-client/spec/agent_spec.rb index 59699b4..3c10e40 100644 --- a/gems/sqrl-client/spec/agent_spec.rb +++ b/gems/sqrl-client/spec/agent_spec.rb @@ -155,14 +155,19 @@ end end - it "recovers from a transient error with a fresh query" do + it "retries a transient error once, then stops when the error repeats (stale login page)" do id = ephemeral("jack") page = SQRL::Client::Transport.new(allow_http: :loopback).get(site.login_url) url = page.body[/data-sqrl-url="([^"]+)"/, 1].gsub("&", "&") stale = url.sub(/nut=[^&]+/, "nut=bogus") - result = new_agent(id, poll: false).login(stale) - expect(result).to be_authenticated - expect(result.tif_history.first).to be_transient_error + # The test site keeps its state by reference, so the retry fails exactly like the first + # attempt; two identical TIFs mean the page has expired (Gibson, grc.sqrl:23543). + expect { new_agent(id, poll: false, max_transient_retries: 5).login(stale) } + .to raise_error(SQRL::Client::LoginFailed) do |e| + expect(e.reason).to eq(:transient) + expect(e.message).to include("refresh the page") + expect(e.tif).to be_transient_error + end end it "gives up after too many transient errors" do diff --git a/gems/sqrl-core/CHANGELOG.md b/gems/sqrl-core/CHANGELOG.md index 781dfcb..4587dec 100644 --- a/gems/sqrl-core/CHANGELOG.md +++ b/gems/sqrl-core/CHANGELOG.md @@ -1,23 +1,65 @@ # Changelog -## 0.1.2 — 2026-10-21 +All notable changes to `sqrl-core` are documented in this file. -- Fix: binary `sqrldata` files whose final byte happened to be an ASCII whitespace character +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this +project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). Every gem in +the family shares one version number and is released in lockstep. + +## [Unreleased] + +## [0.1.2] - 2026-10-21 + +The server side now follows Steve Gibson's final specification (*SQRL On The Wire* v1.07, +December 2019) and his latest rulings in the `grc.sqrl` newsgroup (cited as +`grc.sqrl:
`); see `docs/protocol.md`. + +### Changed + +- The server returns `suk=` in the reply to every command when the client sends `opt=suk`, + and always for a disabled account or a previous-identity match. It used to be returned on + `query` only (a MUST: grc.sqrl:12289). +- A successful re-key replies with 0x01 set and 0x02 clear (the post-update state) and + clears a disabled state along with the key update (grc.sqrl:20273; On The Wire p. 17). +- `disable` in CPS mode returns `url=` like every other non-query command. +- `sqrlonly` / `hardlock` are copied from every successful non-query command (`ident`, + `enable`, `disable`), not from `ident` alone. +- A superseded identity is refused with exactly `0x240`. +- An unknown, expired or reused nut draws a transient error whose fresh nut is not + registered, so the retry fails identically, which is the client's cue to have the user + refresh the page (grc.sqrl:23543). Previously the retry "succeeded" against an anonymous + pending login that no browser could ever complete. +- `PendingLogin#cps?` is new. + +### Fixed + +- 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 +### Security + +- `Handler#status` reports `authenticated: false` for a login the client completed in CPS + mode, so a polling page can no longer be signed in on the strength of a same-device + login: the specification requires that only the browser holding the one-time `url=` + token is signed in. + +## [0.1.1] - 2026-10-20 -- Fixed: Ed25519 and X25519 keys are built from their DER encodings when the openssl gem - has no `OpenSSL::PKey.new_raw_private_key` / `new_raw_public_key` (openssl 3.0 and 3.1, - bundled with Ruby 3.1 and 3.2). 0.1.0 raised `NoMethodError` on those Rubies as soon as - an identity was derived, signed or verified. +### Fixed -## 0.1.0 — 2026-10-20 +- Ed25519 and X25519 keys are built from their DER encodings when the openssl gem has no + `OpenSSL::PKey.new_raw_private_key` / `new_raw_public_key` (openssl 3.0 and 3.1, bundled + with Ruby 3.1 and 3.2). 0.1.0 raised `NoMethodError` on those Rubies as soon as an + identity was derived, signed or verified. + +## [0.1.0] - 2026-10-20 First release. +### Added + - Complete SQRL wire protocol: `query`, `ident`, `disable`, `enable`, `remove`, every TIF bit, `ask`/`btn` with a server-side accept-list, `sin`/`ins`/`pins`, previous identities and re-keying, `x=` path extension, Alt-IDs, `can=`, Client Provided Session. @@ -33,3 +75,8 @@ First release. `before_handle` hooks and `SQRL::Server::RateLimited`. - A shared store-contract spec for store implementations. - Requires Ruby 3.1 or newer. + +[Unreleased]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.2...HEAD +[0.1.2]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.1...v0.1.2 +[0.1.1]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.0...v0.1.1 +[0.1.0]: https://github.com/danielpclark/sqrl-ruby/releases/tag/v0.1.0 diff --git a/gems/sqrl-core/lib/sqrl/server.rb b/gems/sqrl-core/lib/sqrl/server.rb index d2fe560..9196859 100644 --- a/gems/sqrl-core/lib/sqrl/server.rb +++ b/gems/sqrl-core/lib/sqrl/server.rb @@ -95,6 +95,10 @@ def new_login_url(pending_login_id = nil, ip:, cancel_url: nil, return_to: nil, def status(pending_login_id) login = pending_login_id && @stores.pending_logins.find(pending_login_id) return { authenticated: false, idk: nil } if login.nil? || login.expired?(@config.now) + # A login the client flagged cps is completed only through the url= token the client + # hands its own browser: "the web server ... MUST NOT authenticate the browser session + # upon successful authentication" (On The Wire v1.07 p.14; Operating Details p.4). + return { authenticated: false, idk: nil } if login.cps? { authenticated: login.authenticated?, idk: login.authenticated_idk && Util.b64u_encode(login.authenticated_idk) } @@ -256,7 +260,11 @@ def run return fail_with(Tif::COMMAND_FAILED) end - return fail_with(Tif::IDENTITY_SUPERSEDED | Tif::COMMAND_FAILED) if @stores.identities.superseded?(idk) + if @stores.identities.superseded?(idk) + # "the server must immediately fail that client query by returning a TIF of 0x240" + @tif = 0 + return fail_with(Tif::IDENTITY_SUPERSEDED | Tif::COMMAND_FAILED) + end # --- which identity does the server know? --- current = @stores.identities.find(idk) @@ -291,22 +299,24 @@ def run ins_ok = @sin.nil? || previous.ins.nil? || Util.secure_compare(previous.ins, cget.call("pins").to_s) if suk_new && vuk_new && urs_ok.call(previous.vuk) && ins_ok current = @stores.identities.rekey(pidk, idk, suk: suk_new, vuk: vuk_new, ins: cget.call("ins")) + # The urs under the previous identity's VUK carries the same (rescue code) authority + # as an enable, so a disabled account comes back enabled under its new key, "so that + # the account can be immediately used under the new key" (Gibson, grc.sqrl:20273). + current = @stores.identities.update(idk, disabled: false) if current.disabled? @result.rekeyed_from = pidk @result.identity = current - matched = current id_match = true + # TIF bits describe the account AFTER the update: current-ID match on, previous-ID + # match off (On The Wire v1.07 p.16-17; Gibson, grc.sqrl:22934). @tif |= Tif::ID_MATCH + @tif &= ~(Tif::PREVIOUS_ID_MATCH | Tif::SQRL_DISABLED) else rekey_failed = true end end - suk_to_return = nil case cmd when Command::QUERY - if matched && (opts.suk? || matched.disabled? || prev_match) - suk_to_return = matched.suk - end # An ask is only valid in a query reply that recognises the current identity. if ask_pending && id_match @extra << ["ask", ask.to_wire] @@ -340,16 +350,21 @@ def run if rekey_failed || !id_match @tif |= Tif::COMMAND_FAILED else - current = @stores.identities.update(idk, disabled: true) + current = @stores.identities.update(idk, disabled: true, sqrlonly: opts.sqrlonly?, + hardlock: opts.hardlock?) @result.identity = current @tif |= Tif::SQRL_DISABLED + # "url= must be provided by the server in its response to any command, other than + # query, when the SQRL client includes opt=cps" (On The Wire v1.07 p.14). + issue_cps_url if opts.cps? end when Command::ENABLE if rekey_failed || !id_match || ask_refused @tif |= Tif::COMMAND_FAILED elsif prev_match || urs_ok.call(current.vuk) # after a re-key the urs was already verified against the old vuk - current = @stores.identities.update(idk, disabled: false, logins: current.logins.to_i + 1) + current = @stores.identities.update(idk, disabled: false, logins: current.logins.to_i + 1, + sqrlonly: opts.sqrlonly?, hardlock: opts.hardlock?) @tif &= ~Tif::SQRL_DISABLED authenticate!(current, cmd, opts) else @@ -369,7 +384,14 @@ def run end end - @extra << ["suk", Util.b64u_encode(suk_to_return)] if suk_to_return + # The stored SUK goes back whenever the client asked for it with opt=suk (a MUST: + # Gibson, grc.sqrl:12289, 9 Jan 2016) and whenever the client is likely to need it for + # a urs: the account is disabled, or it was recognised by a previous identity + # (On The Wire v1.07 p.14). It accompanies every command's reply, as GRC's server does. + identity_now = @result.identity + if identity_now && (opts.suk? || identity_now.disabled? || prev_match) + @extra << ["suk", Util.b64u_encode(identity_now.suk)] + end respond end @@ -441,17 +463,19 @@ def issue_cps_url @extra << ["url", url] end - # Unknown or reused nut: transient error, hand out a fresh nut bound to a new - # anonymous pending login so the client can retry. + # Unknown, expired or reused nut: transient error. This server keeps its state "by + # reference" (On The Wire, "Transient Error Details", second case), so the browser + # session behind a stale nut cannot be recovered. The reply carries a fresh nut, as every + # reply must, but that nut is deliberately not registered: the client's retry fails with + # the identical TIF, which is the client's cue to tell its user to refresh the login page + # (Gibson, grc.sqrl:23543, 23 Nov 2019; On The Wire v1.07 p.17). def transient_error @tif |= Tif::TRANSIENT_ERROR | Tif::COMMAND_FAILED - @login_id = @handler.new_id - @login = @stores.pending_logins.create(@login_id, ip: @ip, return_to: nil, - expires_at: @now + @config.nut_ttl, created_at: @now) - @result.pending_login_id = @login_id + @login_id = nil + @login = nil @bound = nil @sin = @config.sin_for(context) - respond + respond(register_nut: false) end def fail_with(bits) @@ -459,14 +483,16 @@ def fail_with(bits) respond end - def respond + def respond(register_nut: true) nut = @handler.new_nut text = "ver=#{Protocol::VERSION}\r\nnut=#{nut}\r\ntif=#{Tif.new(@tif).to_hex}\r\nqry=#{@config.path}?nut=#{nut}\r\n" text << "sin=#{@sin}\r\n" if @sin @extra.each { |k, v| text << "#{k}=#{v}\r\n" } body = Util.b64u_encode(text.b) - @stores.nuts.issue(nut: nut, pending_login_id: @login_id, expected_server: body, - ip: @record ? @record.ip : @ip, bound_idk: @bound, expires_at: @now + @config.nut_ttl) + if register_nut + @stores.nuts.issue(nut: nut, pending_login_id: @login_id, expected_server: body, + ip: @record ? @record.ip : @ip, bound_idk: @bound, expires_at: @now + @config.nut_ttl) + end @result.body = body @result.tif = Tif.new(@tif) @result.pending_login = @login diff --git a/gems/sqrl-core/lib/sqrl/server/stores.rb b/gems/sqrl-core/lib/sqrl/server/stores.rb index bfdb81f..ce8e0e1 100644 --- a/gems/sqrl-core/lib/sqrl/server/stores.rb +++ b/gems/sqrl-core/lib/sqrl/server/stores.rb @@ -62,6 +62,12 @@ def initialize(id:, ip: nil, return_to: nil, link_user_ref: nil, created_at: nil def authenticated? = !authenticated_idk.nil? def asked? = asked ? true : false + # Whether the SQRL client completed this login in CPS mode (a one-time +url=+ token was + # issued). Such a login may only be completed by the browser that presents that token, + # never through the poll. The token field stays set until the token is claimed, so an + # unclaimed or expired token keeps the poll closed, as the specification requires. + def cps? = !cps_token.nil? + def expired?(now) !expires_at.nil? && expires_at <= now end diff --git a/gems/sqrl-core/spec/flows_spec.rb b/gems/sqrl-core/spec/flows_spec.rb index 3bed5bf..18157cb 100644 --- a/gems/sqrl-core/spec/flows_spec.rb +++ b/gems/sqrl-core/spec/flows_spec.rb @@ -30,7 +30,10 @@ def b64(str) = SQRL::Util.b64u_encode(str) expect(r.identity.idk).to eq(s.idk) expect(s.state).to eq(SQRL::Session::State.new(kind: :known, disabled: false)) expect(handler.stores.pending_logins.find(sid).authenticated_idk).to eq(s.idk) - expect(handler.status(sid)).to eq(authenticated: true, idk: s.idk_b64) + # CPS: the browser that started the login is not signed in by the server; only the + # browser the client redirects to the url= token is. + expect(handler.stores.pending_logins.find(sid)).to be_cps + expect(handler.status(sid)).to eq(authenticated: false, idk: nil) cps = s.cps_url expect(cps).to eq(r.cps_url) expect(cps).to start_with("https://example.com/sqrl/callback?t=") @@ -140,6 +143,7 @@ def b64(str) = SQRL::Util.b64u_encode(str) expect(i.body).to include("&pids=", "&urs=") r = exchange(handler, s, i) expect(r.tif).to be_id_match + expect(r.tif).not_to be_previous_id_match # the TIF describes the account after the update expect(r.tif).not_to be_error expect(r.rekeyed_from).to eq(old_idk) expect(r).to be_rekeyed @@ -152,6 +156,7 @@ def b64(str) = SQRL::Util.b64u_encode(str) _, s = new_session(handler, old) r = exchange(handler, s, s.query) expect(r.tif).to be_identity_superseded + expect(r.tif.to_hex).to eq("240") # exactly, per "Handling Superseded Identities" expect(s.state).to eq(:superseded) # The new identity logs in normally from now on, without re-keying. @@ -342,20 +347,22 @@ def b64(str) = SQRL::Util.b64u_encode(str) expect(r).not_to be_authenticated end - it "recovers from a transient error with a fresh query" do + it "answers a stale nut with the same transient error twice (the client's cue to refresh)" do handler = new_handler id = identity(14) _, s = new_session(handler, id) q = s.query - # Replay the same request twice: the second use of the nut is a transient error. s.feed(post(handler, q).body) r2 = handler.handle(nut: "nope", body: q.body, ip: FlowHelpers::IP) expect(r2.tif).to be_transient_error s.feed(r2.body) expect(s.state).to eq(:transient) - # A new query built on that response works. + # This server keeps its state by reference, so the browser session behind the stale nut is + # gone: the retry with the fresh nut must fail with exactly the same TIF, never log the + # client in to a pending login no browser owns. r = exchange(handler, s, s.query) - expect(r.tif).not_to be_error + expect(r.tif).to eq(r2.tif) + expect(s.state).to eq(:transient) end it "treats a reused nut as a transient error" do diff --git a/gems/sqrl-core/spec/gibson_rulings_spec.rb b/gems/sqrl-core/spec/gibson_rulings_spec.rb new file mode 100644 index 0000000..91fef65 --- /dev/null +++ b/gems/sqrl-core/spec/gibson_rulings_spec.rb @@ -0,0 +1,181 @@ +# frozen_string_literal: true + +require_relative "support/flow_helpers" + +# Server behaviours pinned to Steve Gibson's final word: the December 2019 "SQRL On The Wire" +# v1.07 document and his later rulings in the grc.sqrl newsgroup (cited by article number). +RSpec.describe "SQRL::Server conformance to Gibson's rulings" do + let(:handler) { new_handler } + let(:iuk) { 7.chr * 32 } + let(:id) { identity(7) } + + def create!(handler, identity) + _, s = new_session(handler, identity) + exchange(handler, s, s.query) + exchange(handler, s, s.ident) + s + end + + describe "suk" do + # "Whenever 'suk' is present in the client's opt= value list, the server will return its saved + # SUK in its reply ... it *MUST* also always provide it when the client's query requests it." + # (grc.sqrl:12289, 9 Jan 2016; On The Wire v1.07 p.14.) GRC's reference client sends + # opt=cps~suk with every command and GRC's server answers every one of them with suk=. + it "is returned in the reply to every command the client requests it with, not only query" do + create!(handler, id) + _, s = new_session(handler, id, SQRL::ClientOptions.new(cps: true)) + exchange(handler, s, s.query) + expect(s.last_response.suk).not_to be_nil + exchange(handler, s, s.ident) + expect(s.last_response.suk).not_to be_nil, "ident reply lacks suk although opt=suk was sent" + + _, s = new_session(handler, id) + exchange(handler, s, s.query) + exchange(handler, s, s.disable) + expect(s.last_response.suk).not_to be_nil, "disable reply lacks suk" + + _, s = new_session(handler, id) + exchange(handler, s, s.query) + exchange(handler, s, s.enable(iuk)) + expect(s.last_response.suk).not_to be_nil, "enable reply lacks suk" + end + + it "is not returned to a client that did not ask for it on a healthy, current identity" do + create!(handler, id) + login = handler.new_login_url(ip: FlowHelpers::IP) + s = SQRL::Session.new(id, login.url) + # SQRL::Session always asks for the suk (like GRC's client); build the bare request by hand + q = s.query + client = SQRL::Util.b64u_decode(q.body[/client=([^&]+)/, 1]).sub("opt=suk\r\n", "") + client_b64 = SQRL::Util.b64u_encode(client) + server_b64 = q.body[/server=([^&]+)/, 1] + sig = SQRL::Crypto.ed25519_sign(id.site_seed(SQRL::URL.parse(login.url).site_key_bytes), client_b64 + server_b64) + body = "client=#{client_b64}&server=#{server_b64}&ids=#{SQRL::Util.b64u_encode(sig)}" + r = handler.handle(nut: nut_of(q), body: body, ip: FlowHelpers::IP) + expect(r.tif).to be_id_match + expect(SQRL::ServerResponse.parse(r.body).suk).to be_nil + end + + it "is always returned for a disabled account, even unasked" do + create!(handler, id) + _, s = new_session(handler, id) + exchange(handler, s, s.query) + exchange(handler, s, s.disable) + expect(s.last_response.tif).to be_sqrl_disabled + expect(s.last_response.suk).not_to be_nil + end + end + + describe "cps url=" do + # "This parameter must be provided by the server in its response to any command, other than + # query, when the SQRL client includes the opt=cps option" (On The Wire v1.07 p.14). The + # waiting browser is otherwise left with only the can= cancel redirect. + it "is returned for a disable issued in CPS mode" do + create!(handler, id) + _, s = new_session(handler, id, SQRL::ClientOptions.new(cps: true)) + exchange(handler, s, s.query) + r = exchange(handler, s, s.disable) + expect(r.tif).to be_sqrl_disabled + expect(r.cps_url).to start_with("https://example.com/sqrl/callback?t=") + expect(s.cps_url).to eq(r.cps_url) + # the callback token exists but the pending login is not authenticated: nobody gets logged in + claimed = handler.claim_cps(r.cps_url[/t=(.+)\z/, 1]) + expect(claimed).not_to be_nil + expect(claimed).not_to be_authenticated + end + end + + describe "re-keying" do + let(:old) { identity(4) } + let(:new) { identity(5, [4]) } + + # "The TIF to be returned is modified to reflect the post update state. So 'Previous ID + # Match' is turned off and 'Current ID Match' is turned on." (grc.sqrl:20273, 16 Aug 2018; + # On The Wire v1.07 p.17; grc.sqrl:22934, 6 Sep 2019.) + it "replies to the updating command with 0x01 set and 0x02 clear" do + create!(handler, old) + _, s = new_session(handler, new) + r = exchange(handler, s, s.query) + expect(r.tif.value & 0x03).to eq(0x02) + r = exchange(handler, s, s.ident) + expect(r.tif.value & 0x03).to eq(0x01) + expect(r).to be_rekeyed + end + + # "Any account disabled state (under the old key) is cleared so that the account can be + # immediately used under the new key." (grc.sqrl:20273.) The urs under the previous VUK + # carries the same rescue-code authority as an enable. + it "clears a disabled state along with the key update" do + create!(handler, old) + _, s = new_session(handler, old) + exchange(handler, s, s.query) + exchange(handler, s, s.disable) + expect(handler.stores.identities.find(s.idk)).to be_disabled + + _, s = new_session(handler, new) + r = exchange(handler, s, s.query) + expect(r.tif).to be_previous_id_match + expect(r.tif).to be_sqrl_disabled + r = exchange(handler, s, s.ident) + expect(r.tif).to be_id_match + expect(r.tif).not_to be_sqrl_disabled + expect(r.tif).not_to be_error + expect(r).to be_authenticated + expect(handler.stores.identities.find(s.idk)).not_to be_disabled + end + + # "the server must immediately fail that client query by returning a TIF of 0x240" + # (On The Wire v1.07 p.18; grc.sqrl:23020, 13 Sep 2019). + it "fails a superseded identity with exactly 0x240" do + create!(handler, old) + create!(handler, new) + _, s = new_session(handler, old) + r = exchange(handler, s, s.query) + expect(r.tif.to_hex).to eq("240") + r = exchange(handler, s, s.ident) + expect(r.tif.to_hex).to eq("240") + expect(r).not_to be_authenticated + end + end + + describe "sqrlonly / hardlock" do + # "Servers that support this option MUST retain and honor the most recently received state of + # this option as presented during the most recent successful non-query command. ... Its + # absence from any valid non-query command should immediately reset the flag" (On The Wire + # v1.07 p.7; grc.sqrl:15905, 4 Aug 2017). + it "copies the flags from every successful non-query command" do + _, s = new_session(handler, id, SQRL::ClientOptions.new(sqrlonly: true, hardlock: true)) + exchange(handler, s, s.query) + exchange(handler, s, s.ident) + expect(handler.stores.identities.find(s.idk)).to be_sqrlonly.and be_hardlock + + _, s = new_session(handler, id, SQRL::ClientOptions.new(sqrlonly: true)) + exchange(handler, s, s.query) + exchange(handler, s, s.disable) + stored = handler.stores.identities.find(s.idk) + expect(stored).to be_sqrlonly + expect(stored).not_to be_hardlock + + _, s = new_session(handler, id) + exchange(handler, s, s.query) + exchange(handler, s, s.enable(iuk)) + stored = handler.stores.identities.find(s.idk) + expect(stored).not_to be_sqrlonly + expect(stored).not_to be_hardlock + end + end + + describe "stale nuts" do + # "In the case that two successive TIF errors exactly match, the SQRL client MUST abort the + # transaction and ask the user to please refresh their login page" (grc.sqrl:23543, 23 Nov + # 2019). A by-reference server therefore answers the retry exactly as it answered the stale nut. + it "answers the retry of a stale nut with the identical transient error" do + s = SQRL::Session.new(id, "sqrl://example.com/cli.sqrl?nut=stale") + first = post(handler, s.query) + s.feed(first.body) + second = post(handler, s.query) + expect(first.tif).to be_transient_error + expect(second.tif).to eq(first.tif) + end + end +end diff --git a/gems/sqrl-core/spec/server_spec.rb b/gems/sqrl-core/spec/server_spec.rb index f9a6b5f..d77342a 100644 --- a/gems/sqrl-core/spec/server_spec.rb +++ b/gems/sqrl-core/spec/server_spec.rb @@ -99,12 +99,20 @@ def login!(handler, identity, cps: true) it "reports status for unknown, pending and authenticated logins" do expect(handler.status("nope")).to eq(authenticated: false, idk: nil) expect(handler.status(nil)).to eq(authenticated: false, idk: nil) - login, s, = login!(handler, id) + login, s, = login!(handler, id, cps: false) expect(handler.status(login.pending_login_id)).to eq(authenticated: true, idk: s.idk_b64) clock.now += 301 expect(handler.status(login.pending_login_id)).to eq(authenticated: false, idk: nil) end + it "never reports a CPS login to the browser poll (the url= token is the only way in)" do + login, = login!(handler, id, cps: true) + pending = handler.stores.pending_logins.find(login.pending_login_id) + expect(pending).to be_authenticated + expect(pending).to be_cps + expect(handler.status(login.pending_login_id)).to eq(authenticated: false, idk: nil) + end + it "mints a one-time CPS token that expires" do _, s, result = login!(handler, id) token = result.cps_url[/t=(.+)\z/, 1] @@ -136,15 +144,15 @@ def login!(handler, identity, cps: true) s = SQRL::Session.new(id, login2.url, options: SQRL::ClientOptions.new(cps: true)) exchange(handler, s, s.query) exchange(handler, s, s.ident) - expect(handler.status(login2.pending_login_id)[:authenticated]).to be(true) - # the suk only comes back in query replies, so a remove always follows a query - exchange(handler, s, s.query) + expect(handler.stores.pending_logins.find(login2.pending_login_id)).to be_authenticated + # the ident reply carried the suk (opt=suk), so the remove can follow it directly + expect(s.last_response.suk).not_to be_nil r = exchange(handler, s, s.remove(31.chr * 32)) expect(r.tif).not_to be_error expect(r.cps_url).not_to be_nil - expect(handler.status(login2.pending_login_id)).to eq(authenticated: false, idk: nil) + expect(handler.stores.pending_logins.find(login2.pending_login_id)).not_to be_authenticated expect(handler.claim_cps(r.cps_url[/t=(.+)\z/, 1])).not_to be_authenticated - expect(handler.status(login.pending_login_id)[:authenticated]).to be(true) + expect(handler.stores.pending_logins.find(login.pending_login_id)).to be_authenticated end it "expires nuts" do @@ -230,13 +238,20 @@ def login!(handler, identity, cps: true) expect(lines.join).not_to include(s.idk_b64) end - it "answers a transient error with a usable anonymous pending login" do + it "answers an unknown nut with a transient error whose retry fails identically" do r = handler.handle(nut: "unknown", body: "", ip: FlowHelpers::IP) expect(r.tif).to be_transient_error - expect(r.pending_login_id).not_to be_nil - expect(handler.stores.pending_logins.find(r.pending_login_id)).not_to be_nil + expect(r.tif.to_hex).to eq("60") + expect(r.pending_login_id).to be_nil expect(r.command).to be_nil expect(r.inspect).to include("tif=0x60") + # The reply carries a nut, as every reply must, but the browser session behind a stale + # nut cannot be recovered, so the retry gets exactly the same answer (the client's cue to + # ask its user to refresh the page) rather than a login nobody's browser can complete. + reply = SQRL::ServerResponse.parse(r.body) + retry_result = handler.handle(nut: reply.nut, body: "", ip: FlowHelpers::IP) + expect(retry_result.tif).to eq(r.tif) + expect(handler.stores.pending_logins.size).to eq(0) end it "serves the sin challenge in every response" do diff --git a/gems/sqrl-rack/CHANGELOG.md b/gems/sqrl-rack/CHANGELOG.md index 3d12dec..e17962b 100644 --- a/gems/sqrl-rack/CHANGELOG.md +++ b/gems/sqrl-rack/CHANGELOG.md @@ -1,18 +1,41 @@ # Changelog -## 0.1.2 — 2026-10-21 +All notable changes to `sqrl-rack` are documented in this file. -- No changes; released in lockstep with sqrl-core 0.1.2 (fixes binary S4 identity files whose last byte is whitespace being truncated on parse). +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this +project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). Every gem in +the family shares one version number and is released in lockstep. -## 0.1.1 — 2026-10-20 +## [Unreleased] -- No changes. Released with sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 - and 3.2; every gem in the family shares one version. +## [0.1.2] - 2026-10-21 -## 0.1.0 — 2026-10-20 +### Changed + +- Requires sqrl-core 0.1.2 (protocol conformance fixes and the S4 trailing-byte fix; see its + changelog). + +### Security + +- A login the SQRL client completed in CPS mode (`opt=cps`) can no longer be completed + through the browser's status poll or a token-less callback. The specification requires the + server not to sign in the browser that displayed the login page in that case, because that + page may be a spoof; only the browser the client redirects to the one-time `url=` token is + signed in. + +## [0.1.1] - 2026-10-20 + +### Changed + +- Requires sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 and 3.2. No other + changes. + +## [0.1.0] - 2026-10-20 First release. +### Added + - `SQRL::Rack::Middleware`: the SQRL endpoint, cookie-bound status poll, CPS and poll callbacks, `sqrl.js` and a refresh endpoint, with a request facade (`env["sqrl.server"]`). - `Sinatra::SQRL` extension and a Roda `plugin :sqrl` with the same helpers. @@ -23,3 +46,8 @@ First release. - Always issues `sqrl://`; `insecure_http: true` is a loopback-only testing mode. - `poll_requires_ip_match` (off by default) and 429 handling for rate-limit hooks. - Requires Ruby 3.1+, Rack 2.2 or 3.x, `rqrcode`. + +[Unreleased]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.2...HEAD +[0.1.2]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.1...v0.1.2 +[0.1.1]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.0...v0.1.1 +[0.1.0]: https://github.com/danielpclark/sqrl-ruby/releases/tag/v0.1.0 diff --git a/gems/sqrl-rack/lib/sqrl/rack/endpoint.rb b/gems/sqrl-rack/lib/sqrl/rack/endpoint.rb index c4fb30f..c49a297 100644 --- a/gems/sqrl-rack/lib/sqrl/rack/endpoint.rb +++ b/gems/sqrl-rack/lib/sqrl/rack/endpoint.rb @@ -174,8 +174,14 @@ def callback(request) Completion.new(identity: identity, pending_login: login, via: via) end - # Whether a pending login may be completed without a CPS token. + # Whether a pending login may be completed without a CPS token. Never when the SQRL + # client flagged +cps+: the server then "MUST NOT authenticate the browser session upon + # successful authentication" (On The Wire v1.07, +url=+); only the browser that presents + # the one-time token the client received may complete it. Otherwise a spoofed login + # page that keeps polling would be signed in, which is exactly what CPS exists to stop. def poll_completable?(login) + return false if login.cps? + !@poll_requires_ip_match || login.ip_matched == true end diff --git a/gems/sqrl-rack/spec/middleware_spec.rb b/gems/sqrl-rack/spec/middleware_spec.rb index 773771f..364557d 100644 --- a/gems/sqrl-rack/spec/middleware_spec.rb +++ b/gems/sqrl-rack/spec/middleware_spec.rb @@ -88,6 +88,26 @@ def visit_login(browser, extra = "") expect(browser.last_response.body).to eq(session.idk_b64) # still logged in end + it "is never completable through the browser poll (the url= token is the only way in)" do + browser = new_browser + url = visit_login(browser) + session = sqrl_login(new_client, url, cps: true) + expect(session.cps_url).not_to be_nil + # A spoofed login page that keeps polling must not be signed in: "the web server ... MUST + # NOT authenticate the browser session upon successful authentication" (On The Wire, url=). + browser.get("/sqrl/status") + expect(JSON.parse(browser.last_response.body)).to eq("authenticated" => false) + browser.get("/sqrl/callback") + expect(browser.last_response.status).to eq(403) + browser.get("/who") + expect(browser.last_response.body).to eq("anonymous") + # the token still works, once, in that browser + browser.get(session.cps_url.sub("https://example.com", "")) + expect(browser.last_response.status).to eq(303) + browser.get("/who") + expect(browser.last_response.body).to eq(session.idk_b64) + end + it "refuses a token claimed by a browser that did not start the login" do browser = new_browser url = visit_login(browser) diff --git a/gems/sqrl-rails/CHANGELOG.md b/gems/sqrl-rails/CHANGELOG.md index 011753c..2ce35bb 100644 --- a/gems/sqrl-rails/CHANGELOG.md +++ b/gems/sqrl-rails/CHANGELOG.md @@ -1,18 +1,41 @@ # Changelog -## 0.1.2 — 2026-10-21 +All notable changes to `sqrl-rails` are documented in this file. -- No changes; released in lockstep with sqrl-core 0.1.2 (fixes binary S4 identity files whose last byte is whitespace being truncated on parse). +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this +project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). Every gem in +the family shares one version number and is released in lockstep. -## 0.1.1 — 2026-10-20 +## [Unreleased] -- No changes. Released with sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 - and 3.2; every gem in the family shares one version. +## [0.1.2] - 2026-10-21 -## 0.1.0 — 2026-10-20 +### Changed + +- Requires sqrl-core and sqrl-rack 0.1.2. sqrl-core fixes binary S4 identity files whose + last byte is whitespace being truncated on parse and brings the server into line with + Steve Gibson's final specification (`suk=` on every command, the post-update TIF after a + re-key, `url=` on a CPS `disable`, and more). No code changes in this gem. + +### Security + +- Through sqrl-rack 0.1.2, a login the SQRL client completed in CPS mode can no longer be + completed through the browser's status poll; only the browser holding the one-time + `url=` token is signed in, as the specification requires. + +## [0.1.1] - 2026-10-20 + +### Changed + +- Requires sqrl-core 0.1.1, which fixes the `NoMethodError` on Ruby 3.1 and 3.2. No other + changes. + +## [0.1.0] - 2026-10-20 First release. +### Added + - Isolated engine serving the SQRL endpoint, status poll, CPS/poll callback, login page, script, refresh endpoint and identity-management pages. - ActiveRecord stores (`sqrl_identities`, `sqrl_nuts`, `sqrl_pending_logins`) with atomic @@ -25,3 +48,8 @@ First release. per-IP rate limiting; `ask` with deny button; `allowed_hosts`, hooks, `prune` task. - `insecure_http` testing mode; `poll_requires_ip_match` off by default. - Requires Ruby 3.2+ and Rails 7.1, 7.2, 8.0 or 8.1. + +[Unreleased]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.2...HEAD +[0.1.2]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.1...v0.1.2 +[0.1.1]: https://github.com/danielpclark/sqrl-ruby/compare/v0.1.0...v0.1.1 +[0.1.0]: https://github.com/danielpclark/sqrl-ruby/releases/tag/v0.1.0 diff --git a/gems/sqrl-rails/spec/stores_spec.rb b/gems/sqrl-rails/spec/stores_spec.rb index 472d37d..4bbc612 100644 --- a/gems/sqrl-rails/spec/stores_spec.rb +++ b/gems/sqrl-rails/spec/stores_spec.rb @@ -132,7 +132,9 @@ expect(result).to be_created session.feed(result.body) expect(session.cps_url).to start_with("https://example.com/sqrl/callback?t=") - expect(handler.status(login.pending_login_id)[:authenticated]).to be(true) + # a CPS login is authenticated on the pending login but never reported to the browser poll + expect(stores.pending_logins.find(login.pending_login_id)).to be_authenticated.and be_cps + expect(handler.status(login.pending_login_id)[:authenticated]).to be(false) token = session.cps_url[/t=(.+)/, 1] expect(handler.claim_cps(token).id).to eq(login.pending_login_id) expect(handler.claim_cps(token)).to be_nil