Skip to content

Fix domain/hostname generation to comply with RFC 1123 - #1901

Merged
kingthorin merged 1 commit into
datafaker-net:mainfrom
kingthorin:company-name-domains
Aug 20, 2026
Merged

Fix domain/hostname generation to comply with RFC 1123#1901
kingthorin merged 1 commit into
datafaker-net:mainfrom
kingthorin:company-name-domains

Conversation

@kingthorin

@kingthorin kingthorin commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Follow-up to PR #1757 where invalid domain names were being generated, particularly affecting non-ASCII locales and special characters in company names.

Problems fixed:

  • Ampersands and special characters in company names now properly sanitized
  • Non-ASCII locales now generate valid ASCII hostnames (via Punycode)
  • Raw Unicode characters no longer leak into URLs
  • Invalid Punycode output eliminated

Solution:

  • New HostnameHelper class enforces RFC 1123 LDH (Letter-Digit-Hyphen) rules
  • HostnameHelper applies IDN.toASCII() for Unicode input, enabling Punycode conversion
  • Punycode prefix ("xn--") is preserved during sanitization
  • Domain suffixes now route through toAsciiHostname() to preserve multi-label TLDs (e.g., "co.uk")
  • All domain generation methods sanitize output to valid RFC 1123 hostnames
  • FakerIDN guarantees valid ASCII output, never throws exceptions
  • Test patterns updated to accept 2+ label domains

All generated domains are now valid for use with java.net.URL/URI constructors, support multi-label TLDs, and properly handle international domain names via Punycode.

This is a hardening fix with no breaking changes to the public API.

Full disclosure

Claude code was leveraged in part during preparation of this PR.

@what-the-diff

what-the-diff Bot commented Aug 19, 2026

Copy link
Copy Markdown

PR Summary

  • Improvements to FakerIDN class:

    • Adjusted certain methods to handle empty or null inputs more gracefully.
    • Added functionality to better process text written in bidirectional languages or with characters that cannot be converted.
    • Added ways to keep track of different processing results.
  • Creation of HostnameHelper class:

    • This new class helps with keeping hostnames clean and adhering to specific rules defined in technical document RFC 1123.
    • New methods have been introduced to help with converting and validating hostnames.
  • Changes to Company and Internet class:

    • Both classes are now using the newly created HostnameHelper to generate correct and valid domain names and URLs.
  • Unit test updates and additions:

    • New tests have been added to confirm the works of HostnameHelper class and it's compliance with the RFC 1123 rules.
    • Old tests in Company class were updated to confirm the validity of the new functionalities.
    • Also, tests for FakerIDN were enhanced to make sure that new fallback mechanisms are working correctly.
  • Updated validation patterns for better compliance:

    • Two new patterns were added to ensure that the hostnames and domain names are in line with the rules set out in the RFC 1123 document.
    • Existing tests for web domains and domain names were updated to use these new patterns.
  • Enhanced testing for multilingual URLs:

    • Added new tests that ensure web and domain names using Hebrew, Cyrillic, and CJK (Chinese, Japanese, Korean) characters comply with correct rules and do not cause any issues when being used.

@codecov-commenter

codecov-commenter commented Aug 19, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 77.90698% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.31%. Comparing base (a947fda) to head (5c0721a).

Files with missing lines Patch % Lines
...n/java/net/datafaker/internal/helper/FakerIDN.java 62.85% 4 Missing and 9 partials ⚠️
.../net/datafaker/internal/helper/HostnameHelper.java 84.21% 4 Missing and 2 partials ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1901      +/-   ##
============================================
- Coverage     92.56%   92.31%   -0.26%     
- Complexity     3573     3598      +25     
============================================
  Files           347      348       +1     
  Lines          7052     7115      +63     
  Branches        675      695      +20     
============================================
+ Hits           6528     6568      +40     
- Misses          365      375      +10     
- Partials        159      172      +13     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kingthorin
kingthorin force-pushed the company-name-domains branch 2 times, most recently from f43fc84 to aa15f79 Compare August 19, 2026 22:29
@kingthorin
kingthorin requested a lite review from Copilot August 19, 2026 22:43

This comment was marked as outdated.

Addresses issues from PR datafaker-net#1757 where invalid domain names were being generated,
particularly affecting non-ASCII locales and special characters in company names.

Problems fixed:
- Ampersands and special characters in company names now properly sanitized
- Non-ASCII locales now generate valid ASCII hostnames (via Punycode)
- Raw Unicode characters no longer leak into URLs
- Invalid Punycode output eliminated

Solution:
- New HostnameHelper class enforces RFC 1123 LDH (Letter-Digit-Hyphen) rules
- All domain generation methods now route through the sanitizer
- FakerIDN guarantees valid ASCII output, never throws exceptions
- Domain suffixes are also sanitized to handle locale-specific TLDs

All generated domains are now valid for use with java.net.URL/URI constructors
and conform to standard DNS hostname requirements.

This is a hardening fix with no breaking changes to the public API.

Signed-off-by: kingthorin <kingthorin@users.noreply.github.com>
@kingthorin
kingthorin force-pushed the company-name-domains branch from aa15f79 to 5c0721a Compare August 19, 2026 23:35
@kingthorin

Copy link
Copy Markdown
Collaborator Author

Sorry about the churn, it's now ready for review.

@asolntsev asolntsev added this to the 3.0.0 milestone Aug 20, 2026
@kingthorin
kingthorin merged commit 357e594 into datafaker-net:main Aug 20, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants