From dcb0e5f1e5a3a3082c40fe96cbc6d46785d0aa11 Mon Sep 17 00:00:00 2001 From: Julien Ma Date: Fri, 26 Jul 2019 22:29:48 +0200 Subject: [PATCH] Fix "Host is invalid" error when TLD >10 chars (#7948) Related to https://meta.discourse.org/t/host-is-invalid-error-when-tld-is-longer-than-7-characters/46081. Using Discourse `v2.4.0.beta2 +119`, I can't add an host (when embedding, cf. `/admin/customize/embedding`) ending with `.engineering`. Turns out current regex limits to 10 characters. Fix is dumb: it only allows for up to 24 chars, which is the **current** max TLD length, see https://stackoverflow.com/a/22038535/1907212. --- Maybe a better (and longer-term) fix would be to allow for up to 64 chars, which I understand comes from the RFC. I'm not at ease with regexes, so can't be sure about it, but [this suggestion](https://meta.discourse.org/t/host-is-invalid-error-when-tld-is-longer-than-7-characters/46081/8?u=julienma) seems pretty good: > rules of DNS labels are: > > - All labels are 1 to 63 characters, case insensitive A to Z, 0 to 9 and - (hyphen), all from ASCII. > - No labels may start with a hyphen. > - No top level domain label may start with a number. > >That means a regexp for a valid domain name would look like: > >`/^([a-z0-9][a-z0-9-]{0,62}\.)+[a-z][a-z0-9-]{0,62}\.?$/` > >Domains that are just a TLD are sufficiently bizarre as to be worth ignoring. --- app/models/embeddable_host.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/embeddable_host.rb b/app/models/embeddable_host.rb index 545eeba5f20..71b44785a88 100644 --- a/app/models/embeddable_host.rb +++ b/app/models/embeddable_host.rb @@ -63,7 +63,7 @@ class EmbeddableHost < ActiveRecord::Base end def host_must_be_valid - if host !~ /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,10}(:[0-9]{1,5})?(\/.*)?\Z/i && + if host !~ /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,24}(:[0-9]{1,5})?(\/.*)?\Z/i && host !~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(:[0-9]{1,5})?(\/.*)?\Z/ && host !~ /\A([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.)?localhost(\:[0-9]{1,5})?(\/.*)?\Z/i errors.add(:host, I18n.t('errors.messages.invalid'))