Merge pull request #2052 from basecamp/flavorjones/fix-join-codes-bignum

Add validation for the join code usage limit
This commit is contained in:
Mike Dalessio
2025-12-10 10:50:54 -05:00
committed by GitHub
4 changed files with 29 additions and 4 deletions
@@ -9,8 +9,11 @@ class Account::JoinCodesController < ApplicationController
end
def update
@join_code.update!(join_code_params)
redirect_to account_join_code_path
if @join_code.update(join_code_params)
redirect_to account_join_code_path
else
render :edit, status: :unprocessable_entity
end
end
def destroy
+3
View File
@@ -1,8 +1,11 @@
class Account::JoinCode < ApplicationRecord
CODE_LENGTH = 12
USAGE_LIMIT_MAX = 10_000_000_000
belongs_to :account
validates :usage_limit, numericality: { less_than_or_equal_to: USAGE_LIMIT_MAX, message: "cannot be larger than the population of the planet" }
scope :active, -> { where("usage_count < usage_limit") }
before_create :generate_code, if: -> { code.blank? }
+11 -2
View File
@@ -14,12 +14,21 @@
<%= form_with model: @join_code, url: account_join_code_path, method: :patch, data: { controller: "form" }, html: { class: "flex flex-column gap" } do |form| %>
<%= form.number_field :usage_limit,
required: true, autofocus: true, min: @join_code.usage_count,
required: true, autofocus: true,
in: 0..Account::JoinCode::USAGE_LIMIT_MAX,
class: "input center txt-large fit-content font-weight-black txt-align-center", style: "max-inline-size: 8ch",
data: { action: "keydown.esc@document->form#cancel focus->form#select" } %>
<% if @join_code.errors.any? %>
<div class="txt-negative txt-small">
<% @join_code.errors.full_messages.each do |message| %>
<p class="margin-block-none"><%= message %></p>
<% end %>
</div>
<% end %>
<p class="margin-none txt-subtle">
This code has been used <%= @join_code.usage_count %>/<%= @join_code.usage_limit %> times.
This code has been used <%= @join_code.usage_count %>/<%= @join_code.usage_limit_in_database %> times.
</p>
<%= form.button type: :submit, class: "btn btn--link center txt-medium", data: { form_target: "submit" } do %>
@@ -37,4 +37,14 @@ class Account::JoinCodesControllerTest < ActionDispatch::IntegrationTest
delete account_join_code_path
assert_response :forbidden
end
test "update with extremely large usage_limit" do
# A number larger than bigint max (2^63 - 1 = 9223372036854775807)
huge_number = "99999999999999999999999999999999999"
put account_join_code_path, params: { account_join_code: { usage_limit: huge_number } }
assert_response :unprocessable_entity
assert_select ".txt-negative", text: /cannot be larger than the population of the planet/
end
end