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
39 changes: 30 additions & 9 deletions docs/protocol.md
Original file line number Diff line number Diff line change
@@ -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:<article>`), his later word superseding earlier,
abandoned designs.

## Keys

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
5 changes: 4 additions & 1 deletion docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 35 additions & 6 deletions gems/devise-sqrl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
40 changes: 34 additions & 6 deletions gems/omniauth-sqrl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
37 changes: 31 additions & 6 deletions gems/sqrl-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
23 changes: 16 additions & 7 deletions gems/sqrl-client/lib/sqrl/client/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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
Expand Down
19 changes: 15 additions & 4 deletions gems/sqrl-client/lib/sqrl/client/conformance/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions gems/sqrl-client/spec/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("&amp;", "&")
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
Expand Down
Loading
Loading