Skip to content

Commit ff7a64d

Browse files
authored
Merge pull request #1340 from yogeshojha/1128-bug-subdomain-import-could-fail-if-suffix-more-than-4-chars
(bug) Fix subdomain import for subdomains with suffix more than 4 chars Fixes #1128
2 parents 3af4d21 + 75d55dc commit ff7a64d

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

web/reNgine/common_func.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,29 @@ def get_domain_from_subdomain(subdomain):
430430
Returns:
431431
str: Domain name.
432432
"""
433-
ext = tldextract.extract(subdomain)
434-
return '.'.join(ext[1:3])
433+
# ext = tldextract.extract(subdomain)
434+
# return '.'.join(ext[1:3])
435+
436+
if not validators.domain(subdomain):
437+
return None
438+
439+
# Use tldextract to parse the subdomain
440+
extracted = tldextract.extract(subdomain)
441+
442+
# if tldextract recognized the tld then its the final result
443+
if extracted.suffix:
444+
domain = f"{extracted.domain}.{extracted.suffix}"
445+
else:
446+
# Fallback method for unknown TLDs, like .clouds or .local etc
447+
parts = subdomain.split('.')
448+
if len(parts) >= 2:
449+
domain = '.'.join(parts[-2:])
450+
else:
451+
return None
452+
453+
# Validate the domain before returning
454+
return domain if validators.domain(domain) else None
455+
435456

436457

437458
def sanitize_url(http_url):

0 commit comments

Comments
 (0)