Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix domain list generation within public suffix #1823

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions service/intel/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,28 +397,32 @@ func (e *Entity) getDomainLists(ctx context.Context) {
}

func splitDomain(domain string) []string {
domain = strings.Trim(domain, ".")
suffix, _ := publicsuffix.PublicSuffix(domain)
if suffix == domain {
// Get suffix.
d := strings.TrimSuffix(domain, ".")
suffix, icann := publicsuffix.PublicSuffix(d)
if suffix == d {
return []string{domain}
}

domainWithoutSuffix := domain[:len(domain)-len(suffix)]
domainWithoutSuffix = strings.Trim(domainWithoutSuffix, ".")

splitted := strings.FieldsFunc(domainWithoutSuffix, func(r rune) bool {
// Split all subdomain into labels.
labels := strings.FieldsFunc(d[:len(d)-len(suffix)], func(r rune) bool {
return r == '.'
})

domains := make([]string, 0, len(splitted))
for idx := range splitted {
// Build list of all domains up to the public suffix.
domains := make([]string, 0, len(labels)+1)
for idx := range labels {
domains = append(
domains,
strings.Join(labels[idx:], ".")+"."+suffix+".",
)
}

d := strings.Join(splitted[idx:], ".") + "." + suffix
if d[len(d)-1] != '.' {
d += "."
}
domains = append(domains, d)
// If the suffix is not a real TLD, but a public suffix, add it to the list.
if !icann {
domains = append(domains, suffix+".")
}

return domains
}

Expand Down
31 changes: 31 additions & 0 deletions service/intel/entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package intel

import (
"testing"

"github.com/stretchr/testify/assert"
)

var splitDomainTestCases = [][]string{
// Regular registered domains and subdomains.
{"example.com."},
{"www.example.com.", "example.com."},
{"sub.domain.example.com.", "domain.example.com.", "example.com."},
{"example.co.uk."},
{"www.example.co.uk.", "example.co.uk."},

// TLD or public suffix: Return as is.
{"com."},
{"googleapis.com."},

// Public suffix domains: Return including public suffix.
{"test.googleapis.com.", "googleapis.com."},
{"sub.domain.googleapis.com.", "domain.googleapis.com.", "googleapis.com."},
}

func TestSplitDomain(t *testing.T) {

Check failure on line 26 in service/intel/entity_test.go

View workflow job for this annotation

GitHub Actions / Linter

Function TestSplitDomain missing the call to method parallel
for _, testCase := range splitDomainTestCases {
splitted := splitDomain(testCase[0])
assert.Equal(t, testCase, splitted, "result must match")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add t.Parallel() for better test performance.

Calling t.Parallel() at the beginning of your test function allows tests to run concurrently, which can significantly improve test execution time.

func TestSplitDomain(t *testing.T) {
+	t.Parallel()
	for _, testCase := range splitDomainTestCases {
		splitted := splitDomain(testCase[0])
		assert.Equal(t, testCase, splitted, "result must match")
	}
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func TestSplitDomain(t *testing.T) {
for _, testCase := range splitDomainTestCases {
splitted := splitDomain(testCase[0])
assert.Equal(t, testCase, splitted, "result must match")
}
}
func TestSplitDomain(t *testing.T) {
t.Parallel()
for _, testCase := range splitDomainTestCases {
splitted := splitDomain(testCase[0])
assert.Equal(t, testCase, splitted, "result must match")
}
}
🧰 Tools
🪛 golangci-lint (1.62.2)

26-26: Function TestSplitDomain missing the call to method parallel

(paralleltest)

🪛 GitHub Check: Linter

[failure] 26-26:
Function TestSplitDomain missing the call to method parallel

Loading