Search code examples
javaregexvalidationcoldfusionidn

Coldfusion and Java how to validate a punycode IDN


Recently I rebuilt some forms so they would accept International Domains like

http://例子.测试

I would then store these as punycode. Before this though we had validation code running to check whether the domain was valid using Coldfusions isValid() method:

if(not isValid("url", sURL)){
return false;
}

With punycode we have an issue of the isValid() function failing when the domain is like:

http://例子.测

or when it is converted to it's punycode using CreateObject( "java", "java.net.IDN" ).toASCII(sURL); and in certain cases comes out like:

xn--http://133ab--xn328sdf

(made up example but in certain cases there will be characters before the http:// part)

is there currently a way of using either a Java library or Coldfusion library or regex to validate IDNs and "normal" domains?


Solution

  • an IDN is a representation of the domain name only, you appear to including the protocol (http://) in the string you are converting.

    Remove the protocol first either with a simple replace() or using java.net.URL, then if required recombine the protocol and idn after.

    <cfscript>
    oUrl = createobject("java", "java.net.URL").init("httP://例子.测试");
    protocol = oUrl.getProtocol();
    domain = oUrl.getHost();
    idn = createobject("java", "java.net.IDN").toAscii(domain);
    writeDump(protocol);  // -> http
    writeDump(domain);    // -> 例子.测试
    writeDump(idn);       // -> xn--fsqu00a.xn--0zwm56d
    </cfscript>
    

    Once you have the punycode you should then be able to use isValid() on it.