Fixes #39287 - validate domain name format#10977
Conversation
f0c6993 to
933b749
Compare
ekohl
left a comment
There was a problem hiding this comment.
What about data in the database that is already invalid? Have you considered how users deal with that?
| MASK_REGEXP ||= /\A((255.){3}(0|128|192|224|240|248|252|254))|((255.){2}(0|128|192|224|240|248|252|254).0)|(255.(0|128|192|224|240|248|252|254)(.0){2})|((128|192|224|240|248|252|254)(.0){3})\z/ | ||
|
|
||
| HOST_REGEXP_ERR_MSG = N_("hostname can contain only lowercase letters, numbers, dashes and dots according to RFC921, RFC952 and RFC1123") | ||
| DOMAIN_NAME_FORMAT_ERR_MSG = N_("domain name can contain only lowercase letters, numbers, dashes and dots according to RFC921, RFC952 and RFC1123") |
There was a problem hiding this comment.
Domain names are case insensitive. It confuses me a bit why HOST_REGEXP does enforce lower case.
There was a problem hiding this comment.
Domain names are case insensitive. It confuses me a bit why
HOST_REGEXPdoes enforce lower case.
I followed HOST_REGEXP behavior here, but I agree enforcing lowercase for case-insensitive domain names is a bit confusing.
Actually I hadn’t thought through the existing invalid records yet. I’ll investigate possible migration options. |
3be3978 to
3a24b22
Compare
97d9a04 to
897047c
Compare
An idea for two-release enrollment:
|
897047c to
c0e9031
Compare
Yeah, got it. I have updated the pr to enforce new regex validation on new record creation. Also should I include the rake task in this same pr ? |
c0e9031 to
ce660c5
Compare
I have added rake task for checking and listing invalid domains |
nacoool
left a comment
There was a problem hiding this comment.
Waiting for the reviews to be completed so the changes can be tested.
ce660c5 to
354615e
Compare
|
|
||
| Exits 0 if all domain names match, 1 if any invalid rows were found. |
There was a problem hiding this comment.
| Exits 0 if all domain names match, 1 if any invalid rows were found. |
This behavior is expected; no need to mention it.
| if invalid_domains.zero? | ||
| puts "Domains: all #{domain_total} records match DOMAIN_NAME_REGEXP." | ||
| else | ||
| puts "Domains: #{invalid_domains} invalid (of #{domain_total} total)" | ||
| end | ||
|
|
||
| exit_code = invalid_domains.zero? ? 0 : 1 | ||
| puts | ||
| if exit_code.zero? | ||
| puts 'Done: no invalid domain names found.' | ||
| else | ||
| puts 'Done: fix invalid domain names before enforcing stricter validations on existing records.' | ||
| end | ||
| exit exit_code |
There was a problem hiding this comment.
| if invalid_domains.zero? | |
| puts "Domains: all #{domain_total} records match DOMAIN_NAME_REGEXP." | |
| else | |
| puts "Domains: #{invalid_domains} invalid (of #{domain_total} total)" | |
| end | |
| exit_code = invalid_domains.zero? ? 0 : 1 | |
| puts | |
| if exit_code.zero? | |
| puts 'Done: no invalid domain names found.' | |
| else | |
| puts 'Done: fix invalid domain names before enforcing stricter validations on existing records.' | |
| end | |
| exit exit_code | |
| if invalid_domains.zero? | |
| puts 'Done: no invalid domain names found.' | |
| exit 0 | |
| else | |
| puts "Domains: #{invalid_domains} invalid (of #{domain_total} total)" | |
| invalid_domains.each do |invalid_domain| | |
| puts "- #{invalid_domain.name}" | |
| end | |
| exit 1 | |
| end | |
(The code is not tested; I wrote it here without an actual run)
For the change, we should store names in the invalid_domains as an array, instead of a number, so we can list the invalid ones.
There was a problem hiding this comment.
Thanks for the suggestion. Since each invalid domain is already printed inside the find_each loop, is there any advantage to storing them in an array as well, or is printing them as they are found sufficient?
354615e to
3f322ef
Compare
3f322ef to
86d4211
Compare
Why
Domain names were validated only for presence/uniqueness in the
Domainmodel.This allowed invalid characters and malformed values to be accepted.
What changed
Domain#nameusingNet::Validations::HOST_REGEXPNet::Validations::DOMAIN_NAME_FORMAT_ERR_MSGdomain_testwith:example.com)Example.com,example_domain.com)*,*.*,*.example.com,example.*.com,!!!,example..com, etc.)Test plan
Steps
bundle exec rails test test/models/domain_test.rbExpected result