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
19 changes: 19 additions & 0 deletions app/controllers/concerns/admin_attachment_gate.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions app/controllers/concerns/admin_search_gate.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 11 additions & 1 deletion app/services/google_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 0 additions & 4 deletions config/initializers/avo_associations.rb

This file was deleted.

9 changes: 9 additions & 0 deletions config/initializers/avo_gates.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions test/controllers/users/omniauth_callbacks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
86 changes: 86 additions & 0 deletions test/integration/admin_engine_gates_test.rb
Original file line number Diff line number Diff line change
@@ -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
50 changes: 27 additions & 23 deletions test/system/browser_floor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions wiki/initiatives/avo-admin-and-roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
5 changes: 4 additions & 1 deletion wiki/initiatives/secure-sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
title: Secure sessions for the rebuilt app
kind: initiative
status: living
updated: 2026-09-30
updated: 2026-10-14
repos:
- usput.ba
confidence: medium
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
Expand Down Expand Up @@ -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.