A discussion about a Host is invalid error in Discourse when setting allowed hosts for embedding, caused by a regex limit of 7 characters for the TLD. flimm identifies the issue in embeddable_host.rb and proposes changing the limit from 7 to 70, releasing the fix under CC0. Mittineague offers to test and submit a PR, suggesting caution due to potential impacts on HTML, databases, or other code. elijah notes that some TLDs like .xn--y9a3aq and Persian domains (e.g., نمونہ.آزمایشی) are not supported and suggests a more robust regex for valid domain names. sam indicates the issue may already be resolved in a newer version of the code.
When setting the allowed hosts for embedding, I get this error: Host is invalid.
This is because it is not passing this regex in the source code, line 26 in the source code of embeddable_host.rb: Link
def host_must_be_valid
if host !~ /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,7}(:[0-9]{1,5})?(\/.*)?\Z/i &&
host !~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/
errors.add(:host, I18n.t('errors.messages.invalid'))
end
end
This regex only passes if the TLD is seven characters long or fewer. There are many TLDs that are longer than that nowadays, for instance:
Because I haven’t decided what I think about CLAs in general or this one in particular. Also, I have not tested the fix. It should be fairly easy for someone who is fine with the CLA and who has already used to getting Discourse to build and to run tests.
def host_must_be_valid
if host !~ /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,70}(:[0-9]{1,5})?(\/.*)?\Z/i &&
host !~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/
errors.add(:host, I18n.t('errors.messages.invalid'))
end
end
There you go. I changed 7 to 70. I release my changes under the CC0 license by Creative Commons and under the public domain.
A lot of code touches “host” and it is hoped that allowing more characters won’t break anything eg.
an HTML element thats CSS doesn’t account for longer names.
a database table field that might truncate or null longer names.
other code that uses the value, eg. something like “reverse string position”
I made up a fake domain
“kleinfeltersville.travelersinsurance”
and it didn’t break the Admin → Settings → Onebox UI
But I could use your help in trying to embed a real domain.
My, from memory, understanding of the 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: