Skip to content
Open
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
9 changes: 9 additions & 0 deletions app/models/oidc/pending_trusted_publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class OIDC::PendingTrustedPublisher < ApplicationRecord
uniqueness: { case_sensitive: false, scope: %i[trusted_publisher_id trusted_publisher_type], conditions: -> { unexpired } }

validate :available_rubygem_name, on: :create
validate :protected_gem_typo, on: :create

scope :unexpired, -> { where(arel_table[:expires_at].eq(nil).or(arel_table[:expires_at].gt(Time.now.utc))) }
scope :expired, -> { where(arel_table[:expires_at].lteq(Time.now.utc)) }
Expand All @@ -37,4 +38,12 @@ def available_rubygem_name

errors.add(:rubygem_name, :unavailable)
end

def protected_gem_typo
return if rubygem_name.blank?
gem_typo = GemTypo.new(rubygem_name)

return unless gem_typo.protected_typo?
errors.add :rubygem_name, "'#{rubygem_name}' is too similar to an existing gem named '#{gem_typo.protected_gem}'"
end
end
10 changes: 10 additions & 0 deletions test/models/oidc/pending_trusted_publisher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ class OIDC::PendingTrustedPublisherTest < ActiveSupport::TestCase
refute_predicate publisher, :valid?
assert_equal ["is already in use"], publisher.errors[:rubygem_name]
end

test "validates rubygem name is not a protected typo" do
rubygem = create(:rubygem, name: "typekit", downloads: 15_000)
create(:version, rubygem: rubygem, created_at: 1.day.ago)

publisher = build(:oidc_pending_trusted_publisher, rubygem_name: "type_kit")

refute_predicate publisher, :valid?
assert_includes publisher.errors[:rubygem_name].first, "is too similar to an existing gem named 'typekit'"
end
end
Loading