From cfdcc27c23eba5f7564fc8db29fb4a4a24a6d35a Mon Sep 17 00:00:00 2001 From: Muhamed Isabegovic Date: Wed, 14 Oct 2026 13:13:50 +0200 Subject: [PATCH 1/2] Close the admin's file and search doors, and Google takeovers - Avo's attachments and search controllers sat outside every policy gate, and Avo Community's own check allows everything: a curator could delete or replace a traveller's avatar or a tour's audio and search accounts by email. AdminAttachmentGate asks the record's policy for update? before an upload or a delete; AdminSearchGate asks the resource's policy for index?. Each hole has a test that fails without its gate. - Linking Google to an unconfirmed account found by email now replaces its password and ends its sessions before confirming it, so a stranger who registered with someone else's address loses the account to its owner. A confirmed account keeps its password. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JEmikXtNoH7wAg6veTjbyT --- .../concerns/admin_attachment_gate.rb | 19 ++++ app/controllers/concerns/admin_search_gate.rb | 16 ++++ app/services/google_account.rb | 12 ++- config/initializers/avo_associations.rb | 4 - config/initializers/avo_gates.rb | 9 ++ .../omniauth_callbacks_controller_test.rb | 25 ++++++ test/integration/admin_engine_gates_test.rb | 86 +++++++++++++++++++ wiki/initiatives/avo-admin-and-roles.md | 4 + wiki/initiatives/secure-sessions.md | 5 +- 9 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 app/controllers/concerns/admin_attachment_gate.rb create mode 100644 app/controllers/concerns/admin_search_gate.rb delete mode 100644 config/initializers/avo_associations.rb create mode 100644 config/initializers/avo_gates.rb create mode 100644 test/integration/admin_engine_gates_test.rb diff --git a/app/controllers/concerns/admin_attachment_gate.rb b/app/controllers/concerns/admin_attachment_gate.rb new file mode 100644 index 00000000..aa22c6bc --- /dev/null +++ b/app/controllers/concerns/admin_attachment_gate.rb @@ -0,0 +1,19 @@ +# Avo uploads and deletes a record's files (a place's photos, a tour's audio, +# a user's avatar) through its own attachments controller, which the +# per-resource gate does not reach and whose own check allows everything in +# Avo Community. Changing a record's files counts as editing the record, so +# the record's policy decides: a curator may change a place's photos but not +# a user's avatar or a traveller's moment. +module AdminAttachmentGate + extend ActiveSupport::Concern + + included do + before_action :enforce_record_policy, only: %i[create destroy] + end + + private + + def enforce_record_policy + raise Avo::NotAuthorizedError unless AdminPolicy.for(@resource.model_class).new(_current_user, @record).update? + end +end diff --git a/app/controllers/concerns/admin_search_gate.rb b/app/controllers/concerns/admin_search_gate.rb new file mode 100644 index 00000000..a169f1f4 --- /dev/null +++ b/app/controllers/concerns/admin_search_gate.rb @@ -0,0 +1,16 @@ +# Avo's searchable fields ask its search controller, which the per-resource +# gate does not reach. Searching a resource is listing it, so the resource's +# policy decides: a curator searching users (by name or email) is refused. +module AdminSearchGate + extend ActiveSupport::Concern + + included do + before_action :enforce_index_policy, only: :show + end + + private + + def enforce_index_policy + raise Avo::NotAuthorizedError unless AdminPolicy.for(@resource.model_class).new(_current_user).index? + end +end diff --git a/app/services/google_account.rb b/app/services/google_account.rb index 6c08bcfb..7d9cd19d 100644 --- a/app/services/google_account.rb +++ b/app/services/google_account.rb @@ -26,10 +26,20 @@ def link_by_email return if user.identities.exists?(provider: PROVIDER) user.identities.create!(provider: PROVIDER, uid: uid) - user.confirm unless user.confirmed? + take_over(user) unless user.confirmed? user end + # An unconfirmed account was opened by whoever typed this email, and nobody + # has proved they own it until now: Google just did. Whatever password that + # stranger set, and any session they hold, must not survive the link. + def take_over(user) + user.password = Devise.friendly_token(32) + user.confirm + user.save! + user.end_sessions + end + def new_user User.create!(username: available_username, email: email, password: Devise.friendly_token(32)) do |user| user.skip_confirmation! diff --git a/config/initializers/avo_associations.rb b/config/initializers/avo_associations.rb deleted file mode 100644 index b390f633..00000000 --- a/config/initializers/avo_associations.rb +++ /dev/null @@ -1,4 +0,0 @@ -# The policy gate on Avo's own associations controller (see AdminAssociationGate). -Rails.application.config.to_prepare do - Avo::AssociationsController.include(AdminAssociationGate) unless Avo::AssociationsController < AdminAssociationGate -end diff --git a/config/initializers/avo_gates.rb b/config/initializers/avo_gates.rb new file mode 100644 index 00000000..5aa4d8a8 --- /dev/null +++ b/config/initializers/avo_gates.rb @@ -0,0 +1,9 @@ +# The policy gates on Avo's own controllers, which the per-resource gate +# (AdminResource) does not reach: associations, attachments and search. +Rails.application.config.to_prepare do + { + Avo::AssociationsController => AdminAssociationGate, + Avo::AttachmentsController => AdminAttachmentGate, + Avo::SearchController => AdminSearchGate + }.each { |controller, gate| controller.include(gate) unless controller < gate } +end diff --git a/test/controllers/users/omniauth_callbacks_controller_test.rb b/test/controllers/users/omniauth_callbacks_controller_test.rb index a655b670..d1b493a1 100644 --- a/test/controllers/users/omniauth_callbacks_controller_test.rb +++ b/test/controllers/users/omniauth_callbacks_controller_test.rb @@ -62,6 +62,31 @@ class Users::OmniauthCallbacksControllerTest < ActionDispatch::IntegrationTest assert_signed_in end + test "a stranger's password on an unconfirmed account does not survive the Google link" do + # Someone registered with this address before its owner arrived through Google. + squatter = User.create!(username: "squatter", email: "owner@example.com", password: "password123") + token_before = squatter.session_token + google(uid: "g-owner", email: "owner@example.com") + + sign_in_with_google + + squatter.reload + assert_not squatter.valid_password?("password123"), "the stranger's password no longer opens it" + assert_not_equal token_before, squatter.session_token, "the stranger's sessions ended" + assert squatter.confirmed? + assert_signed_in + end + + test "a confirmed account keeps its password when Google links to it" do + user = User.create!(username: "settled", email: "settled@example.com", password: "password123").tap(&:confirm) + google(uid: "g-settled", email: "settled@example.com") + + sign_in_with_google + + assert user.reload.valid_password?("password123"), "the owner had already proved the address" + assert_signed_in + end + test "a linked account signs in by its Google id even if the Google email changed" do user = User.create!(username: "moved", email: "old@example.com", password: "password123") user.identities.create!(provider: "google_oauth2", uid: "g-moved") diff --git a/test/integration/admin_engine_gates_test.rb b/test/integration/admin_engine_gates_test.rb new file mode 100644 index 00000000..8930b4fc --- /dev/null +++ b/test/integration/admin_engine_gates_test.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +require "test_helper" + +# Avo's attachments and search controllers sit outside the per-resource gate, +# and Avo Community's own authorization allows everything. The record's or +# the resource's policy decides there too. +class AdminEngineGatesTest < ActionDispatch::IntegrationTest + setup do + @admin = person(:admin) + @curator = person(:curator) + @traveller = person(:basic) + @traveller.avatar.attach(io: File.open("test/fixtures/files/real_image.jpg"), filename: "a.jpg", content_type: "image/jpeg") + @place = Location.create!(name: "Kravice", city: "LjubuĆĄki", lat: 43.1564, lng: 17.6081) + @place.photos.attach(io: File.open("test/fixtures/files/real_image.jpg"), filename: "p.jpg", content_type: "image/jpeg") + end + + test "a curator cannot delete a traveller's avatar" do + sign_in @curator + + delete_attachment "users", @traveller, :avatar, @traveller.avatar.attachment + + assert @traveller.reload.avatar.attached?, "the avatar survives" + end + + test "a curator cannot replace a traveller's avatar" do + sign_in @curator + before = @traveller.avatar.blob.id + + upload "users", @traveller, :avatar + + assert_equal before, @traveller.reload.avatar.blob.id, "the avatar is the traveller's own" + end + + test "a curator can still change a place's photos" do + sign_in @curator + + delete_attachment "locations", @place, :photos, @place.photos.first + + assert_not @place.reload.photos.attached? + end + + test "an admin can delete a traveller's avatar" do + sign_in @admin + + delete_attachment "users", @traveller, :avatar, @traveller.avatar.attachment + + assert_not @traveller.reload.avatar.attached? + end + + test "a curator cannot search accounts by name or email" do + sign_in @curator + + get "/admin/avo_api/users/search", params: { q: @traveller.email } + + assert_not_includes response.body, @traveller.email + assert_not response.successful? + end + + test "an admin can search accounts" do + sign_in @admin + + get "/admin/avo_api/users/search", params: { q: "gates_basic" } + + assert_response :success + assert_includes response.body, "gates_basic" + end + + private + + def delete_attachment(resource, record, name, attachment) + delete "/admin/resources/#{resource}/#{record.to_param}/active_storage_attachments/#{name}/#{attachment.id}", + headers: { "Accept" => "text/vnd.turbo-stream.html" } + end + + def upload(resource, record, name) + post "/admin/avo_api/resources/#{resource}/#{record.to_param}/attachments", + params: { attachment_key: name, filename: "x.jpg", + file: Rack::Test::UploadedFile.new("test/fixtures/files/real_image.jpg", "image/jpeg") } + end + + def person(role) + User.create!(username: "gates_#{role}", email: "gates_#{role}@example.com", + password: "password123", user_type: role).tap(&:confirm) + end +end diff --git a/wiki/initiatives/avo-admin-and-roles.md b/wiki/initiatives/avo-admin-and-roles.md index 8f875167..89b8d21e 100644 --- a/wiki/initiatives/avo-admin-and-roles.md +++ b/wiki/initiatives/avo-admin-and-roles.md @@ -12,6 +12,9 @@ depends_on: - decisions/jev-flags-reviews.md - decisions/archive-places-not-delete.md sources: +- app/controllers/concerns/admin_attachment_gate.rb +- app/controllers/concerns/admin_search_gate.rb +- config/initializers/avo_gates.rb - sources/conversations/2026-09-30--usput--place-translations-in-avo.md - sources/conversations/2026-09-30--usput--plans-in-avo.md - sources/conversations/2026-09-30--usput--platform-direction.md @@ -263,3 +266,4 @@ Slice 5 landed on 2026-09-30. - **The old curator queue is gone**, with its page, route, tests and locale keys; the curator menu's "Moments" link opens the admin queue. +**Gates on Avo's own controllers, 2026-10-14.** A review of the admin found that Avo's attachments and search controllers sat outside both `AdminResource` and `AdminAssociationGate`, and Avo Community's own check allows everything: a curator could delete or replace a traveller's avatar, or a tour's audio, and search accounts by email. `AdminAttachmentGate` now asks the record's policy for `update?` before an upload or a delete, and `AdminSearchGate` asks the resource's policy for `index?`; all three gates are registered in `config/initializers/avo_gates.rb`. Each hole has a test that fails with the gates off (`test/integration/admin_engine_gates_test.rb`). Enola's strict law covers `AdminResource` on the resource controllers; the engine's own controllers are covered by these tests. diff --git a/wiki/initiatives/secure-sessions.md b/wiki/initiatives/secure-sessions.md index d54e6aeb..b05e4cac 100644 --- a/wiki/initiatives/secure-sessions.md +++ b/wiki/initiatives/secure-sessions.md @@ -2,7 +2,7 @@ title: Secure sessions for the rebuilt app kind: initiative status: living -updated: 2026-09-30 +updated: 2026-10-14 repos: - usput.ba confidence: medium @@ -10,6 +10,8 @@ depends_on: - decisions/admin-through-avo.md - decisions/accounts-through-devise.md sources: +- app/services/google_account.rb +- test/controllers/users/omniauth_callbacks_controller_test.rb - sources/conversations/2026-09-30--usput--platform-direction.md - sources/conversations/2026-09-30--usput--devise-and-google.md - app/controllers/concerns/authenticatable.rb @@ -212,3 +214,4 @@ Slice 5, Rack::Attack, landed on 2026-09-30. added without being listed there, and checks the 429, the localhost safelist and the exploit blocklist end to end. +**Google link to an unconfirmed account, 2026-10-14.** Anyone could register with someone else's address and use the account unconfirmed for three days; when the address's owner later signed in with Google, `GoogleAccount` linked and confirmed that same account and kept the stranger's password and sessions. Now an unconfirmed account found by email gets a new random password and ends every session before it is confirmed (`GoogleAccount#take_over`); a confirmed account, whose owner had already proved the address, keeps its password. From 55f83adbf14ce4b2e7606e67db9e3a0e33bca823 Mon Sep 17 00:00:00 2001 From: Muhamed Isabegovic Date: Wed, 14 Oct 2026 13:21:25 +0200 Subject: [PATCH 2/2] Give the refused-storage test a browser of its own Under the full suite the shared browser answered the map's catalogue from an earlier test's HTTP cache, so the map drew no places and the test failed one run in two. Its own session starts with an empty cache, and its broken storage reaches no other test. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JEmikXtNoH7wAg6veTjbyT --- test/system/browser_floor_test.rb | 50 +++++++++++++++++-------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/test/system/browser_floor_test.rb b/test/system/browser_floor_test.rb index d7e4a6ca..44fc762d 100644 --- a/test/system/browser_floor_test.rb +++ b/test/system/browser_floor_test.rb @@ -11,33 +11,37 @@ class BrowserFloorSystemTest < ApplicationSystemTestCase end test "the app keeps working when the browser refuses storage, as private browsing can" do - # Before any page script: every read and write of localStorage throws. - # The browser is shared by the whole suite, so the injection is removed after. - injected = page.driver.browser.execute_cdp("Page.addScriptToEvaluateOnNewDocument", source: <<~JS) - for (const name of ["getItem", "setItem", "removeItem"]) { - Storage.prototype[name] = function () { throw new DOMException("refused", "SecurityError") } - } - JS + place = Location.create!(name: "Floor Bridge", city: "Mostar", lat: 43.337, lng: 17.815) - visit "/mine-check" + # A browser of its own: the suite's shared one carries other tests' HTTP + # cache (the map's catalogue answers 304 with whatever an earlier test + # fetched), and this one's storage is broken on purpose. + Capybara.using_session(:storage_refused) do + # Before any page script: every read and write of storage throws. + page.driver.browser.execute_cdp("Page.addScriptToEvaluateOnNewDocument", source: <<~JS) + for (const name of ["getItem", "setItem", "removeItem"]) { + Storage.prototype[name] = function () { throw new DOMException("refused", "SecurityError") } + } + JS - # The banner slides up from below the window once its controller connects; - # clicked through the page, since the headless window cuts off its bottom edge. - assert_selector ".cookie-consent-banner.cookie-consent-visible" - wait_for_controller "[data-controller='cookie-consent']", "cookie-consent" - page.execute_script("document.querySelector(\"[data-action='click->cookie-consent#accept']\").click()") - assert_no_selector ".cookie-consent-banner.cookie-consent-visible" - assert_selector "#browser-floor-notice", visible: :hidden + visit "/mine-check" - # The map caches its catalogue of places in session storage. - place = Location.create!(name: "Floor Bridge", city: "Mostar", lat: 43.337, lng: 17.815) - visit location_path(place) - # A click before the map controller connects is swallowed with no error. - wait_for_controller "[data-controller~='map']", "map" - find("[data-map-target='fullscreenButton']").click - assert_selector ".leaflet-marker-icon", wait: 10 + # The banner slides up from below the window once its controller connects; + # clicked through the page, since the headless window cuts off its bottom edge. + assert_selector ".cookie-consent-banner.cookie-consent-visible" + wait_for_controller "[data-controller='cookie-consent']", "cookie-consent" + page.execute_script("document.querySelector(\"[data-action='click->cookie-consent#accept']\").click()") + assert_no_selector ".cookie-consent-banner.cookie-consent-visible" + assert_selector "#browser-floor-notice", visible: :hidden + + # The map caches its catalogue of places in session storage. + visit location_path(place) + # A click before the map controller connects is swallowed with no error. + wait_for_controller "[data-controller~='map']", "map" + find("[data-map-target='fullscreenButton']").click + assert_selector ".leaflet-marker-icon", wait: 10 + end ensure place&.destroy - page.driver.browser.execute_cdp("Page.removeScriptToEvaluateOnNewDocument", identifier: injected["identifier"]) if injected end end