Skip to content

feat: allow single-label hostnames in ValidateURL and improve performance#880

Open
Yashbhu wants to merge 26 commits into
goharbor:mainfrom
Yashbhu:urlvalidationissue
Open

feat: allow single-label hostnames in ValidateURL and improve performance#880
Yashbhu wants to merge 26 commits into
goharbor:mainfrom
Yashbhu:urlvalidationissue

Conversation

@Yashbhu
Copy link
Copy Markdown
Contributor

@Yashbhu Yashbhu commented May 5, 2026

Description

This PR relaxes the URL validation logic to support internal hostnames (single-label hostnames like http://harbor), which are standard in DevOps environments but were previously blocked. It also improves performance by optimizing regex handling.

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Documentation update
  • Chore / maintenance

Changes

  • RFC 1123 Support: Updated the domain regex to allow hostnames without dots, enabling compatibility with Kubernetes service names and Docker Compose networking.
  • Performance Optimization: Moved the domainNameRegex to a package-level global variable to avoid the overhead of re-compiling the regex on every ValidateURL call.
  • Enhanced Testing: Added new unit test cases for internal service names (e.g., http://harbor, https://registry-server) and verified they pass alongside existing FQDN and IP tests.

Copilot AI review requested due to automatic review settings May 5, 2026 12:36
@Yashbhu
Copy link
Copy Markdown
Contributor Author

Yashbhu commented May 5, 2026

i did some changes in the helper_test.go:

http://harbor: It verifies that a simple, single-word hostname (which used to fail) now passes.
https://registry-server: This verifies that the regex correctly handles hyphens inside a single label. Many Kubernetes services use hyphens (like harbor-core), so i think we need to make sure they work
http://harbor:8080: This verifies that the hostname validation still works when a port number is attached to it we want to make sure our regex only looks at the host part and doesn't get confused by the :8080.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates URL host validation to accept single-label internal hostnames (common in in-cluster / DevOps setups) while reducing per-call overhead by reusing a precompiled domain regex.

Changes:

  • Relaxed domain-name validation to allow single-label hostnames in ValidateURL.
  • Moved the domain regex to a package-level precompiled regexp.Regexp for performance.
  • Extended ValidateURL unit tests to cover internal service-name style hosts.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/utils/helper.go Precompiles and reuses an RFC1123-style domain regex; ValidateURL now permits single-label hostnames.
pkg/utils/helper_test.go Adds test cases verifying single-label hostnames (with/without hyphens/ports) are accepted.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/utils/helper.go Outdated
@Yashbhu Yashbhu changed the title feat: allow single-label hostnames in ValidateURL and improve perforance feat: allow single-label hostnames in ValidateURL and improve performance May 5, 2026
@Yashbhu
Copy link
Copy Markdown
Contributor Author

Yashbhu commented May 5, 2026

@bupd PTAL whenever you have bandwidth
if this issue makes sense or not #879
thanks !

@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 8.56%. Comparing base (60ad0bd) to head (3ad9e11).
⚠️ Report is 162 commits behind head on main.

Files with missing lines Patch % Lines
pkg/api/instance_handler.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             main    #880      +/-   ##
=========================================
- Coverage   10.99%   8.56%   -2.43%     
=========================================
  Files         173     288     +115     
  Lines        8671   14444    +5773     
=========================================
+ Hits          953    1237     +284     
- Misses       7612   13087    +5475     
- Partials      106     120      +14     

☔ View full report in Codecov by Sentry.
📢 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.

Comment thread pkg/utils/helper.go
// ValidateURL checks that the URL has a valid format and a non-empty host.
// The host may be an IP address, "localhost", or an RFC 1123-style hostname validated by domainNameRegex.
func ValidateURL(rawURL string) error {
var domainNameRegex = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I dont know why are you moving removing the var from here - just to declare above the function.

That doesnt make sense - is there any specific reason to do this.

thanks

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the only reason to move it outside i thought of is to avoid the overhead of re-compiling the regex on every call
though you are right to address it that in a CLI it's not much needed and rather clean code setup should be much prioritized i've reverted this
thanks !

Yashbhu added 4 commits May 8, 2026 19:17
…ance

Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
@Yashbhu Yashbhu force-pushed the urlvalidationissue branch from 409b427 to 3b3780c Compare May 8, 2026 13:47
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
@Yashbhu Yashbhu requested a review from bupd May 8, 2026 13:55
Yashbhu added 15 commits May 8, 2026 19:26
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
…issues

Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
… style

Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Yashbhu added 6 commits May 16, 2026 18:42
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
… style

Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
… style

Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
Signed-off-by: yash bahuguna <yashbahuguna918@gmail.com>
@Yashbhu Yashbhu force-pushed the urlvalidationissue branch from 0a71d0f to 3ad9e11 Compare May 16, 2026 13:31
@Yashbhu
Copy link
Copy Markdown
Contributor Author

Yashbhu commented May 16, 2026

@bupd PTAL

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.

[bug]:The ValidateURL function fails to validate URLs that use single-label hostnames

3 participants