Updating valid domain registration (#15436)

* Updating valid domain registration

Prior to this commit, our regular expression did not account for the `-`
character as valid within the domain.  The `-` character cannot be the
first nor last character of the domain (e.g. `-hello.com` nor
`hello-.com` are invalid but `hell-o.com` is valid).

In addition I tidied up the default value of the
`blocked_registration_email_domains` to match it's sibling `allowed_registration_email_domains`.

This relates to the spammer seo-hunt.com

* Adding validator domain validator spec

* Moving request spec to unit spec

This change does two things:

1) Allows for 2 character domains
2) Moves the spec for 2 character domains from an expensive spec to a
   cheaper to run spec (e.g., request-cycle to unit-test validator)

It preserves PR #12268
This commit is contained in:
Jeremy Friesen 2021-11-19 10:05:00 -05:00 committed by GitHub
parent 460d2f433a
commit 9687760056
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 10 deletions

View file

@ -11,7 +11,7 @@ module Settings
setting :apple_key_id, type: :string
setting :apple_pem, type: :string
setting :apple_team_id, type: :string
setting :blocked_registration_email_domains, type: :array, default: %(), validates: {
setting :blocked_registration_email_domains, type: :array, default: %w[], validates: {
valid_domain_csv: true
}
setting :display_email_domain_allow_list_publicly, type: :boolean, default: false

View file

@ -1,6 +1,10 @@
# @note While the validator implies a CSV, the implementation is that
# we have an array. Upstream implementors likely accept a CSV
# and coerce it into an array. See Authentication::Base for an
# example of coercing the CSV into an array.
class ValidDomainCsvValidator < ActiveModel::EachValidator
DEFAULT_MESSAGE = "must be a comma-separated list of valid domains".freeze
VALID_DOMAIN = /^[a-zA-Z0-9]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/
VALID_DOMAIN = /^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/
def validate_each(record, attribute, value)
return unless value

View file

@ -113,14 +113,6 @@ RSpec.describe "/admin/customization/config", type: :request do
expect(Settings::Authentication.allowed_registration_email_domains).to eq(%w[dev.to forem.com forem.dev])
end
it "allows 2-character domains" do
proper_list = "dev.to, forem.com, 2u.com"
post admin_settings_authentications_path, params: {
settings_authentication: { allowed_registration_email_domains: proper_list }
}
expect(Settings::Authentication.allowed_registration_email_domains).to eq(%w[dev.to forem.com 2u.com])
end
it "does not allow improper domain list" do
impproper_list = "dev.to, foremcom, forem.dev"
post admin_settings_authentications_path, params: {

View file

@ -0,0 +1,41 @@
require "rails_helper"
RSpec.describe ValidDomainCsvValidator do
let(:validatable) do
Class.new do
def self.name
"Validatable"
end
include ActiveModel::Validations
attr_accessor :domains
validates :domains, valid_domain_csv: true
end
end
let(:model) { validatable.new }
it "marks valid a domain with dashes in the middle" do
model.domains = ["seo-hunt.com"]
expect(model).to be_valid
end
it "marks valid a two character domain" do
model.domains = ["2u.com"]
expect(model).to be_valid
end
it "marks invalid a domain with a dash as a prefix" do
model.domains = ["-seo-hunt.com"]
expect(model).to be_invalid
end
it "marks valid an array of domains" do
model.domains = ["hello.com", "world.org"]
expect(model).to be_valid
end
it "marks invalid an array of domains if one is invalid" do
model.domains = ["hello.com", "world.org", "notadomain"]
expect(model).to be_invalid
end
end