Skip to content

testTLD incorrectly reports valid TLDs with subdomains as invalid (e.g., a.example.com) #5

Description

@korhojoa

edit: 100% Copilot-generated. I don't know why it doesn't say that it was authored by copilot, but it was.
edit2: When this is fixed, it's probably worth adding the maximum length check of 255 octets too.

Bug Report

Affected Function: testTLD in tests.php

Problem

When the LOCAL_TLD is set to a value like a.example.com, the function currently splits on the dot and only checks the first label (e.g., a). Since the code enforces a minimum length of 2 for the TLD, any TLD where the first label is less than 2 characters (as in a.example.com) is incorrectly flagged as invalid, even though it is a valid FQDN.

What happens:

  • testTLD runs this logic:
    $TLDmain = explode('.', trim($unRaidVars['LOCAL_TLD']))[0];
    if (strlen($TLDmain) < 2 || strlen($TLDmain) > 63 || preg_match('/[^a-zA-Z0-9\-]+/m', $TLDmain)) ...
  • This only validates the first label of the TLD, not the full TLD or all its labels.
  • Inputs like a.example.com cause false positives (invalid warning) even though each label is otherwise valid.

Expected Behavior

  • Each label in the TLD (split by .) should be checked against DNS rules:
    • Each label: 1-63 chars, only [a-zA-Z0-9-]
    • Whole TLD: up to 253 chars (DNS)
    • Dots should not be treated as invalid
  • No unnecessary warning for valid subdomains like a.example.com

Proposed Fix

Replace this block:

$TLDmain = explode('.', trim($unRaidVars['LOCAL_TLD']))[0];
if (!$unRaidVars['LOCAL_TLD'])
  addWarning(...);
elseif (strlen($TLDmain) < 2 || strlen($TLDmain) > 63 || preg_match('/[^a-zA-Z0-9\-]+/m', $TLDmain))
  addWarning(...);

With:

if (!$unRaidVars['LOCAL_TLD']) {
  addWarning(...);
} else {
  $tld_labels = explode('.', trim($unRaidVars['LOCAL_TLD']));
  $invalid = false;
  foreach ($tld_labels as $label) {
    if (strlen($label) < 1 || strlen($label) > 63 || preg_match('/[^a-zA-Z0-9\-]/', $label)) {
      $invalid = true;
      break;
    }
  }
  if ($invalid || strlen($unRaidVars['LOCAL_TLD']) > 253) {
    addWarning(...);
  }
}

References

Impact

  • Users with valid FQDNs including subdomains may see incorrect warnings about invalid TLDs.

Environment


Let me know if a patch or PR is desired!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions