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
5 changes: 3 additions & 2 deletions web/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@ def can_approve_pos_override?

def pos_override_pin_set? = pos_override_pin_digest.present?

# Four to eight digits, stored only as a BCrypt digest.
# A new PIN is six to eight digits, stored only as a BCrypt digest. A four or five digit
# PIN set before that rule still verifies below until its owner next changes it.
def pos_override_pin=(pin)
raise ArgumentError, 'Use a PIN of 4 to 8 digits.' unless pin.to_s.match?(/\A\d{4,8}\z/)
raise ArgumentError, 'Use a PIN of 6 to 8 digits.' unless pin.to_s.match?(/\A\d{6,8}\z/)

self.pos_override_pin_digest = BCrypt::Password.create(pin.to_s)
self.pos_override_pin_set_at = Time.current
Expand Down
15 changes: 11 additions & 4 deletions web/app/services/compliance/sale_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ module Compliance
# plus the 21+ ID confirmation, then the manager override when one is offered.
# Every refusal and override is written to the compliance audit trail.
class SaleGate
# Wrong PINs count against the signed-in person who typed them, not the manager they
# picked: five in 15 minutes, or ten in a day, stop that person asking for overrides.
PIN_WINDOW = 15.minutes
PIN_FAILURES = 5
PIN_DAY_FAILURES = 10
REASON_RANGE = (8..500)

Override = Data.define(:approver_id, :pin, :reason) do
Expand Down Expand Up @@ -79,16 +82,20 @@ def self.check_override(override)

def self.refusal_for(approver, override)
return ['not_approver', 'Choose a manager who can approve overrides.'] unless approver&.can_approve_pos_override? && approver.pos_override_pin_set?
return ['locked_out', "Too many wrong PINs for this manager. Try again in #{PIN_WINDOW.inspect}."] if locked_out?(approver)
return ['locked_out', 'Too many wrong PINs from this sign-in. Wait before asking for another override, or ask an owner.'] if locked_out?(approver)
return ['reason_missing', "Give a reason of at least #{REASON_RANGE.min} characters."] unless REASON_RANGE.cover?(override.reason.length)
return ['wrong_pin', 'That PIN is not right.'] unless approver.valid_pos_override_pin?(override.pin)

nil
end

def self.locked_out?(approver)
ComplianceEvent.where(action: 'override_refused', approver_id: approver.id).where('created_at >= ?', PIN_WINDOW.ago)
.count { |event| event.details['refusal'] == 'wrong_pin' } >= PIN_FAILURES
# Counted by who typed the PIN (Current.user), so one person's wrong guesses never lock a
# manager out for everyone else. With nobody signed in, by the manager as before.
def self.locked_out?(approver, actor: Current.user)
scope = ComplianceEvent.where(action: 'override_refused').where('created_at >= ?', 1.day.ago)
wrong = (actor ? scope.where(user_id: actor.id) : scope.where(approver_id: approver.id))
.select { |event| event.details['refusal'] == 'wrong_pin' }
wrong.count { |event| event.created_at >= PIN_WINDOW.ago } >= PIN_FAILURES || wrong.size >= PIN_DAY_FAILURES
end

# Managers who can approve at a register: active, override-capable, PIN set.
Expand Down
6 changes: 3 additions & 3 deletions web/app/views/compliance/override_pins/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<p class="text-sm text-mocha"><%= Current.user.pos_override_pin_set? ? "Your PIN was set #{l(Current.user.pos_override_pin_set_at.in_time_zone, format: :short)}. Saving replaces it." : 'You have no PIN yet.' %></p>
<div><%= label_tag :current_password, 'Your password', class: 'field-label' %><%= password_field_tag :current_password, nil, required: true, autocomplete: 'current-password', class: 'input' %></div>
<div class="grid gap-4 sm:grid-cols-2">
<div><%= label_tag :pin, 'New PIN (4 to 8 digits)', class: 'field-label' %><%= password_field_tag :pin, nil, required: true, inputmode: 'numeric', pattern: '[0-9]{4,8}', maxlength: 8, autocomplete: 'new-password', class: 'input' %></div>
<div><%= label_tag :pin_confirmation, 'PIN again', class: 'field-label' %><%= password_field_tag :pin_confirmation, nil, required: true, inputmode: 'numeric', pattern: '[0-9]{4,8}', maxlength: 8, autocomplete: 'new-password', class: 'input' %></div>
<div><%= label_tag :pin, 'New PIN (6 to 8 digits)', class: 'field-label' %><%= password_field_tag :pin, nil, required: true, inputmode: 'numeric', pattern: '[0-9]{6,8}', maxlength: 8, autocomplete: 'new-password', class: 'input' %></div>
<div><%= label_tag :pin_confirmation, 'PIN again', class: 'field-label' %><%= password_field_tag :pin_confirmation, nil, required: true, inputmode: 'numeric', pattern: '[0-9]{6,8}', maxlength: 8, autocomplete: 'new-password', class: 'input' %></div>
</div>
<p class="text-xs text-mocha">Never share it. Five wrong tries lock it for 15 minutes.</p>
<p class="text-xs text-mocha">Never share it. Wrong tries count against the person who typed them: after five they wait 15 minutes, and your PIN keeps working for everyone else.</p>
<div class="form-actions"><%= submit_tag 'Save', class: 'btn-primary' %><%= link_to 'Cancel', compliance_path, class: 'btn-ghost' %></div>
<% end %>
</div>
8 changes: 4 additions & 4 deletions web/test/integration/compliance_guardrails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ class ComplianceGuardrailsTest < ActionDispatch::IntegrationTest

test 'a manager sets an override PIN only with their password' do
sign_in @manager
patch compliance_override_pin_path, params: { current_password: 'wrong', pin: '2468', pin_confirmation: '2468' }
assert_not @manager.reload.valid_pos_override_pin?('2468')
patch compliance_override_pin_path, params: { current_password: 'password123', pin: '2468', pin_confirmation: '2468' }
assert @manager.reload.valid_pos_override_pin?('2468')
patch compliance_override_pin_path, params: { current_password: 'wrong', pin: '246810', pin_confirmation: '246810' }
assert_not @manager.reload.valid_pos_override_pin?('246810')
patch compliance_override_pin_path, params: { current_password: 'password123', pin: '246810', pin_confirmation: '246810' }
assert @manager.reload.valid_pos_override_pin?('246810')
assert_equal 'override_pin_set', ComplianceEvent.last.action
delete logout_path
sign_in cashier
Expand Down
47 changes: 37 additions & 10 deletions web/test/integration/staff_controls/override_pin_lockout_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require 'test_helper'
require_relative '../../support/compliance_fixtures'

# Characterization: pins behaviour pending owner decision (docs/decisions/staff-controls-2026-11.md, item d).
# A manager's override PIN may be four digits. Wrong guesses are counted against the manager, not
# against the cashier who typed them: five in 15 minutes lock that manager's PIN for everyone.
# Owner decision (docs/decisions/staff-controls-2026-11.md, item d): a new override PIN is six to
# eight digits, and wrong guesses count against the cashier who typed them (five in 15 minutes, ten
# in a day), so nobody can lock a manager's PIN for everyone else.
class StaffControlsOverridePinLockoutTest < ActiveSupport::TestCase
include ComplianceFixtures

Expand All @@ -18,26 +18,53 @@ class StaffControlsOverridePinLockoutTest < ActiveSupport::TestCase
end
teardown { Current.reset }

test 'a four digit PIN is accepted and three digits are refused' do
test 'a new PIN needs six digits; a four digit PIN set earlier still works' do
user = User.new
user.pos_override_pin = '1234'
assert user.valid_pos_override_pin?('1234')
assert_raises(ArgumentError) { user.pos_override_pin = '123' }
assert_raises(ArgumentError) { user.pos_override_pin = '1234' }
user.pos_override_pin = '123456'
assert user.valid_pos_override_pin?('123456')
assert @manager.valid_pos_override_pin?('4321')
end

test 'five wrong PINs typed by one cashier lock the manager out for every cashier for 15 minutes' do
test 'five wrong PINs lock out the cashier who typed them, not the manager' do
Current.user = @guesser
5.times { |n| assert_match(/PIN is not right/, refusal(pin: "111#{n}")) }
assert_match(/Too many wrong PINs from this sign-in/, refusal(pin: '4321'))
assert_equal 'locked_out', ComplianceEvent.last.details['refusal']

Current.user = @colleague
assert_match(/Too many wrong PINs/, refusal(pin: '4321'))
assert_equal 'locked_out', ComplianceEvent.last.details['refusal']
assert_equal @manager, approve(pin: '4321')

Current.user = @guesser
travel_to 16.minutes.from_now do
assert_equal @manager, approve(pin: '4321')
end
end

test 'the lock follows the cashier to another manager' do
other_manager = manager_with_pin(pin: '654321')
Current.user = @guesser
5.times { refusal(pin: '0000') }

override = Compliance::SaleGate::Override.new(approver_id: other_manager.id.to_s, pin: '654321', reason: 'Owner approved card for this sale')
assert_equal 'locked_out', Compliance::SaleGate.refusal_for(other_manager, override).first
end

test 'ten wrong PINs in a day lock the cashier out until the day has passed' do
Current.user = @guesser
2.times do |round|
travel_to (round * 20).minutes.from_now do
4.times { assert_match(/PIN is not right/, refusal(pin: '0000')) }
end
end
travel_to 1.hour.from_now do
2.times { assert_match(/PIN is not right/, refusal(pin: '0000')) }
end

travel_to(3.hours.from_now) { assert_match(/Too many wrong PINs/, refusal(pin: '4321')) }
travel_to(26.hours.from_now) { assert_equal @manager, approve(pin: '4321') }
end

private

def approve(pin:)
Expand Down
8 changes: 5 additions & 3 deletions web/test/services/compliance_sale_gate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ComplianceSaleGateTest < ActiveSupport::TestCase
assert ComplianceEvent.all.all? { |e| e.action == 'override_refused' }
end

test 'five wrong PINs lock the manager out for fifteen minutes, even with the right PIN' do
test 'five wrong PINs lock the person who typed them out for fifteen minutes, even with the right PIN' do
5.times { assert_raises(Compliance::OverrideRefused) { enforce(evaluate('card'), override(@manager, '0000', 'A good long reason here')) } }
error = assert_raises(Compliance::OverrideRefused) { enforce(evaluate('card'), override(@manager, '4321', 'A good long reason here')) }
assert_match(/Too many wrong PINs/, error.message)
Expand All @@ -75,12 +75,14 @@ class ComplianceSaleGateTest < ActiveSupport::TestCase
assert_raises(ActiveRecord::ReadOnlyRecord) { ComplianceEvent.sole.update!(reason: 'edited') }
end

test 'PINs are stored only as a digest and must be 4 to 8 digits' do
test 'PINs are stored only as a digest; a new one must be 6 to 8 digits' do
assert_not_includes @manager.pos_override_pin_digest, '4321'
assert @manager.valid_pos_override_pin?('4321')
assert_not @manager.valid_pos_override_pin?('43210')
assert_raises(ArgumentError) { @manager.pos_override_pin = '12ab' }
assert_raises(ArgumentError) { @manager.pos_override_pin = '123' }
assert_raises(ArgumentError) { @manager.pos_override_pin = '12345' }
@manager.pos_override_pin = '123456'
assert @manager.valid_pos_override_pin?('123456')
end

private
Expand Down
3 changes: 2 additions & 1 deletion web/test/support/compliance_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def add_coa!(item, tested_on: 30.days.ago.to_date, **attrs)

def manager_with_pin(pin: '4321', email: "manager-#{SecureRandom.hex(3)}@example.com")
user = User.create!(email: email, password: 'password123', role: 'manager', tenant_id: Current.tenant_id, name: 'Morgan Manager')
user.pos_override_pin = pin
# Shorter than six digits: a PIN from before the six-digit rule, which still verifies.
pin.length >= 6 ? user.pos_override_pin = pin : user.pos_override_pin_digest = BCrypt::Password.create(pin)
user.save!
user
end
Expand Down
Loading