- Make authorization-code redemption atomic: claim the code with a guarded delete inside the transaction that creates the authorization, so concurrent exchanges can't both issue a token (RFC 6749 4.1.3) - Compare the token request's client_id host to the code's stored client_id (defense-in-depth alongside the exact redirect_uri match) - Reject unknown response_type values with unsupported_response_type - Validate the PKCE code_challenge (43-128 unreserved chars, S256) so a client can't persist an over-long or malformed challenge - Return invalid_request for missing token-endpoint parameters, and set Cache-Control: no-store on error responses too - Use the right separator when the redirect_uri already has a query string - Look codes up through the active scope; drop the unused consume!; serialize permissions as JSON - Purge abandoned authorization codes via a recurring Solid Queue task Part of #254
This commit is contained in:
9 files changed
+169
-40
No files matched your search
@@ -55,8 +55,12 @@ class Rs::OauthController < ApplicationController
|
||||
error_redirect(@redirect_uri, "error=invalid_client", @state) and return
|
||||
end
|
||||
|
||||
unless %w[token code].include?(@response_type)
|
||||
error_redirect(@redirect_uri, "error=unsupported_response_type", @state) and return
|
||||
end
|
||||
|
||||
if @response_type == "code" &&
|
||||
(@code_challenge.blank? || @code_challenge_method != "S256")
|
||||
!valid_code_challenge?(@code_challenge, @code_challenge_method)
|
||||
error_redirect(@redirect_uri, "error=invalid_request", @state) and return
|
||||
end
|
||||
|
||||
@@ -88,13 +92,17 @@ class Rs::OauthController < ApplicationController
|
||||
error_redirect(redirect_uri, "error=invalid_client", state, response_type) and return
|
||||
end
|
||||
|
||||
unless %w[token code].include?(response_type)
|
||||
error_redirect(redirect_uri, "error=unsupported_response_type", state, response_type) and return
|
||||
end
|
||||
|
||||
client_id.gsub!(/http(s)?:\/\//, "")
|
||||
|
||||
if response_type == "code"
|
||||
challenge = params[:code_challenge].presence
|
||||
method = params[:code_challenge_method].presence || "S256"
|
||||
|
||||
if challenge.blank? || method != "S256"
|
||||
unless valid_code_challenge?(challenge, method)
|
||||
error_redirect(redirect_uri, "error=invalid_request", state, response_type) and return
|
||||
end
|
||||
|
||||
@@ -108,7 +116,7 @@ class Rs::OauthController < ApplicationController
|
||||
code_challenge_method: method
|
||||
)
|
||||
|
||||
redirect_to url_with_state("#{redirect_uri}?code=#{CGI.escape(code.code)}", state),
|
||||
redirect_to url_with_state("#{redirect_uri}#{query_separator(redirect_uri)}code=#{CGI.escape(code.code)}", state),
|
||||
allow_other_host: true
|
||||
else
|
||||
auth = current_user.remote_storage_authorizations.create!(
|
||||
@@ -128,7 +136,12 @@ class Rs::OauthController < ApplicationController
|
||||
return head :ok if request.options?
|
||||
|
||||
unless params[:grant_type] == "authorization_code"
|
||||
return render json: { error: "unsupported_grant_type" }, status: :bad_request
|
||||
return render_token_error("unsupported_grant_type")
|
||||
end
|
||||
|
||||
if params[:code].blank? || params[:code_verifier].blank? ||
|
||||
params[:client_id].blank? || params[:redirect_uri].blank?
|
||||
return render_token_error("invalid_request")
|
||||
end
|
||||
|
||||
auth = RsOauthManager::ExchangeCode.call(
|
||||
@@ -138,10 +151,9 @@ class Rs::OauthController < ApplicationController
|
||||
code_verifier: params[:code_verifier]
|
||||
)
|
||||
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
render json: token_response(auth)
|
||||
render_token(token_response(auth))
|
||||
rescue RsOauthManagerService::InvalidGrant
|
||||
render json: { error: "invalid_grant" }, status: :bad_request
|
||||
render_token_error("invalid_grant")
|
||||
end
|
||||
|
||||
private
|
||||
@@ -191,10 +203,28 @@ class Rs::OauthController < ApplicationController
|
||||
end
|
||||
|
||||
def error_url(redirect_uri, error, state, response_type = @response_type)
|
||||
separator = response_type == "code" ? "?" : "#"
|
||||
separator = response_type == "code" ? query_separator(redirect_uri) : "#"
|
||||
url_with_state("#{redirect_uri}#{separator}#{error}", state)
|
||||
end
|
||||
|
||||
def query_separator(uri)
|
||||
uri.to_s.include?("?") ? "&" : "?"
|
||||
end
|
||||
|
||||
def valid_code_challenge?(challenge, method)
|
||||
method == "S256" && challenge.to_s.match?(/\A[A-Za-z0-9\-._~]{43,128}\z/)
|
||||
end
|
||||
|
||||
def render_token(body)
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
render json: body
|
||||
end
|
||||
|
||||
def render_token_error(error)
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
render json: { error: error }, status: :bad_request
|
||||
end
|
||||
|
||||
def url_with_state(url, state)
|
||||
state ? "#{url}&state=#{CGI.escape(state)}" : url
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ class RsAuthorizationCode < ApplicationRecord
|
||||
|
||||
CODE_TTL = 60.seconds
|
||||
|
||||
serialize :permissions, coder: YAML
|
||||
serialize :permissions, coder: JSON
|
||||
|
||||
attr_accessor :code
|
||||
|
||||
@@ -14,17 +14,13 @@ class RsAuthorizationCode < ApplicationRecord
|
||||
def self.find_by_code(raw_code)
|
||||
return nil if raw_code.blank?
|
||||
|
||||
find_by(code_digest: Digest::SHA256.hexdigest(raw_code))
|
||||
active.find_by(code_digest: Digest::SHA256.hexdigest(raw_code))
|
||||
end
|
||||
|
||||
def expired?
|
||||
expires_at <= Time.current
|
||||
end
|
||||
|
||||
def consume!
|
||||
destroy!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_code
|
||||
|
||||
@@ -9,9 +9,10 @@ module RsOauthManager
|
||||
|
||||
def call
|
||||
auth_code = RsAuthorizationCode.find_by_code(@code)
|
||||
raise InvalidGrant if auth_code.nil? || auth_code.expired?
|
||||
raise InvalidGrant if auth_code.nil?
|
||||
|
||||
raise InvalidGrant unless hostname_of(@client_id) == hostname_of(@redirect_uri)
|
||||
raise InvalidGrant unless hostname_of(@client_id) == hostname_of(auth_code.client_id)
|
||||
raise InvalidGrant unless @redirect_uri == auth_code.redirect_uri
|
||||
|
||||
raise InvalidGrant unless VerifyPkce.call(
|
||||
@@ -20,15 +21,25 @@ module RsOauthManager
|
||||
code_verifier: @code_verifier
|
||||
)
|
||||
|
||||
auth = auth_code.user.remote_storage_authorizations.create!(
|
||||
permissions: auth_code.permissions,
|
||||
client_id: auth_code.client_id,
|
||||
redirect_uri: auth_code.redirect_uri,
|
||||
app_name: auth_code.client_id,
|
||||
expire_at: auth_code.permission_expire_at
|
||||
)
|
||||
user = auth_code.user
|
||||
auth = nil
|
||||
|
||||
# Claim the code atomically so a concurrent redemption can't issue a
|
||||
# second token. The whole exchange is one transaction: if creating the
|
||||
# authorization fails, the code is not consumed and can be retried.
|
||||
RsAuthorizationCode.transaction do
|
||||
claimed = RsAuthorizationCode.where(id: auth_code.id).delete_all
|
||||
raise InvalidGrant if claimed != 1
|
||||
|
||||
auth = user.remote_storage_authorizations.create!(
|
||||
permissions: auth_code.permissions,
|
||||
client_id: auth_code.client_id,
|
||||
redirect_uri: auth_code.redirect_uri,
|
||||
app_name: auth_code.client_id,
|
||||
expire_at: auth_code.permission_expire_at
|
||||
)
|
||||
end
|
||||
|
||||
auth_code.consume!
|
||||
auth
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,3 +8,8 @@
|
||||
# command: "SoftDeletedRecord.due.delete_all"
|
||||
# priority: 2
|
||||
# schedule: at 5am every day
|
||||
|
||||
production:
|
||||
purge_rs_authorization_codes:
|
||||
command: "RsAuthorizationCode.where(expires_at: ..1.hour.ago).delete_all"
|
||||
schedule: every hour
|
||||
@@ -4,7 +4,7 @@ class CreateRsAuthorizationCodes < ActiveRecord::Migration[8.1]
|
||||
t.references :user, null: false, foreign_key: true
|
||||
t.string :client_id, null: false
|
||||
t.string :redirect_uri, null: false
|
||||
t.text :permissions, default: [].to_yaml
|
||||
t.text :permissions, default: [].to_json
|
||||
t.datetime :permission_expire_at
|
||||
t.string :code_digest, null: false
|
||||
t.string :code_challenge, null: false
|
||||
|
||||
+1
-1
@@ -125,7 +125,7 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_02_120000) do
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "expires_at", null: false
|
||||
t.datetime "permission_expire_at"
|
||||
t.text "permissions", default: "--- []\n"
|
||||
t.text "permissions", default: "[]"
|
||||
t.string "redirect_uri", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "user_id", null: false
|
||||
|
||||
@@ -130,6 +130,8 @@ RSpec.describe Rs::OauthController, type: :controller do
|
||||
|
||||
it "does not reuse the existing token (renders the consent dialog)" do
|
||||
expect(response.response_code).to eq(200)
|
||||
expect(response.location).to be_nil
|
||||
expect(user.remote_storage_authorizations.count).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -157,7 +159,7 @@ RSpec.describe Rs::OauthController, type: :controller do
|
||||
client_id: "example.com",
|
||||
scope: "documents",
|
||||
response_type: "code",
|
||||
code_challenge: "challenge123",
|
||||
code_challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
||||
code_challenge_method: "S256",
|
||||
state: "foobar123"
|
||||
}
|
||||
@@ -166,7 +168,7 @@ RSpec.describe Rs::OauthController, type: :controller do
|
||||
it "renders the consent dialog and exposes the code challenge" do
|
||||
expect(response.response_code).to eq(200)
|
||||
expect(assigns["response_type"]).to eq("code")
|
||||
expect(assigns["code_challenge"]).to eq("challenge123")
|
||||
expect(assigns["code_challenge"]).to eq("E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM")
|
||||
expect(assigns["code_challenge_method"]).to eq("S256")
|
||||
end
|
||||
|
||||
@@ -200,7 +202,7 @@ RSpec.describe Rs::OauthController, type: :controller do
|
||||
client_id: "example.com",
|
||||
scope: "documents",
|
||||
response_type: "code",
|
||||
code_challenge: "challenge123",
|
||||
code_challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
||||
code_challenge_method: "plain"
|
||||
}
|
||||
end
|
||||
@@ -210,6 +212,40 @@ RSpec.describe Rs::OauthController, type: :controller do
|
||||
end
|
||||
end
|
||||
|
||||
context "with an unsupported response_type" do
|
||||
before do
|
||||
get :new, params: {
|
||||
username: user.cn,
|
||||
redirect_uri: "https://example.com",
|
||||
client_id: "example.com",
|
||||
scope: "documents",
|
||||
response_type: "id_token"
|
||||
}
|
||||
end
|
||||
|
||||
it "redirects with unsupported_response_type" do
|
||||
expect(response).to redirect_to("https://example.com#error=unsupported_response_type")
|
||||
end
|
||||
end
|
||||
|
||||
context "with a malformed code_challenge" do
|
||||
before do
|
||||
get :new, params: {
|
||||
username: user.cn,
|
||||
redirect_uri: "https://example.com",
|
||||
client_id: "example.com",
|
||||
scope: "documents",
|
||||
response_type: "code",
|
||||
code_challenge: "too-short",
|
||||
code_challenge_method: "S256"
|
||||
}
|
||||
end
|
||||
|
||||
it "redirects with invalid_request" do
|
||||
expect(response).to redirect_to("https://example.com?error=invalid_request")
|
||||
end
|
||||
end
|
||||
|
||||
context "no scope" do
|
||||
before do
|
||||
get :new, params: {
|
||||
@@ -452,7 +488,7 @@ RSpec.describe Rs::OauthController, type: :controller do
|
||||
client_id: "example.com",
|
||||
state: "foobar123",
|
||||
response_type: "code",
|
||||
code_challenge: "challenge123",
|
||||
code_challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
||||
code_challenge_method: "S256",
|
||||
expire_at: 1.month.from_now
|
||||
}
|
||||
@@ -461,7 +497,7 @@ RSpec.describe Rs::OauthController, type: :controller do
|
||||
|
||||
it "creates an authorization code" do
|
||||
expect(@code.permissions).to eq(%w(documents photos contacts:rw))
|
||||
expect(@code.code_challenge).to eq("challenge123")
|
||||
expect(@code.code_challenge).to eq("E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM")
|
||||
expect(@code.code_challenge_method).to eq("S256")
|
||||
end
|
||||
|
||||
@@ -500,6 +536,43 @@ RSpec.describe Rs::OauthController, type: :controller do
|
||||
end
|
||||
end
|
||||
|
||||
context "with an unsupported response_type" do
|
||||
before do
|
||||
post :create, params: {
|
||||
user_id: user.id,
|
||||
scope: "documents",
|
||||
redirect_uri: "https://example.com",
|
||||
client_id: "example.com",
|
||||
response_type: "id_token",
|
||||
expire_at: 1.month.from_now
|
||||
}
|
||||
end
|
||||
|
||||
it "redirects with unsupported_response_type" do
|
||||
expect(response).to redirect_to("https://example.com#error=unsupported_response_type")
|
||||
end
|
||||
end
|
||||
|
||||
context "with response_type=code and a redirect_uri that already has a query" do
|
||||
before do
|
||||
post :create, params: {
|
||||
user_id: user.id,
|
||||
scope: "documents",
|
||||
redirect_uri: "https://example.com/?foo=bar",
|
||||
client_id: "example.com",
|
||||
state: "foobar123",
|
||||
response_type: "code",
|
||||
code_challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
||||
code_challenge_method: "S256",
|
||||
expire_at: 1.month.from_now
|
||||
}
|
||||
end
|
||||
|
||||
it "appends the code with an ampersand" do
|
||||
expect(response.location).to match(%r{\Ahttps://example\.com/\?foo=bar&code=.+&state=foobar123\z})
|
||||
end
|
||||
end
|
||||
|
||||
context "when the user_id is different from the signed in user" do
|
||||
before do
|
||||
post :create, params: {
|
||||
@@ -615,5 +688,22 @@ RSpec.describe Rs::OauthController, type: :controller do
|
||||
expect(JSON.parse(response.body)["error"]).to eq("unsupported_grant_type")
|
||||
end
|
||||
end
|
||||
|
||||
context "with missing required parameters" do
|
||||
before { post :token, params: { grant_type: "authorization_code", code: "x" } }
|
||||
|
||||
it "returns invalid_request" do
|
||||
expect(response.response_code).to eq(400)
|
||||
expect(JSON.parse(response.body)["error"]).to eq("invalid_request")
|
||||
end
|
||||
end
|
||||
|
||||
context "on error" do
|
||||
before { post :token, params: { grant_type: "password" } }
|
||||
|
||||
it "does not cache the response" do
|
||||
expect(response.headers["Cache-Control"]).to eq("no-store")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -57,13 +57,13 @@ RSpec.describe 'remoteStorage OAuth Dialog', type: :feature do
|
||||
client_id: "http://example.com",
|
||||
scope: "documents:rw",
|
||||
response_type: "code",
|
||||
code_challenge: "challenge123",
|
||||
code_challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
||||
code_challenge_method: "S256")
|
||||
end
|
||||
|
||||
it "includes the PKCE parameters in the consent form" do
|
||||
expect(page).to have_selector("input[name='response_type'][value='code']", visible: false)
|
||||
expect(page).to have_selector("input[name='code_challenge'][value='challenge123']", visible: false)
|
||||
expect(page).to have_selector("input[name='code_challenge'][value='E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM']", visible: false)
|
||||
expect(page).to have_selector("input[name='code_challenge_method'][value='S256']", visible: false)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -43,6 +43,11 @@ RSpec.describe RsAuthorizationCode, type: :model do
|
||||
it "returns nil for a blank code" do
|
||||
expect(RsAuthorizationCode.find_by_code(nil)).to be_nil
|
||||
end
|
||||
|
||||
it "returns nil for an expired code" do
|
||||
code = build_code(expires_at: 1.minute.ago)
|
||||
expect(RsAuthorizationCode.find_by_code(code.code)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "#expired?" do
|
||||
@@ -54,12 +59,4 @@ RSpec.describe RsAuthorizationCode, type: :model do
|
||||
expect(build_code(expires_at: 1.minute.ago)).to be_expired
|
||||
end
|
||||
end
|
||||
|
||||
describe "#consume!" do
|
||||
it "destroys the record" do
|
||||
code = build_code
|
||||
|
||||
expect { code.consume! }.to change(RsAuthorizationCode, :count).by(-1)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user