feat: allow single-label hostnames in ValidateURL and improve performance#880
feat: allow single-label hostnames in ValidateURL and improve performance#880Yashbhu wants to merge 26 commits into
Conversation
|
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. |
There was a problem hiding this comment.
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.Regexpfor performance. - Extended
ValidateURLunit 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.
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
| // 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,}$`) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 !
…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>
409b427 to
3b3780c
Compare
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>
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>
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>
0a71d0f to
3ad9e11
Compare
|
@bupd PTAL |
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
Changes
domainNameRegexto a package-level global variable to avoid the overhead of re-compiling the regex on everyValidateURLcall.http://harbor,https://registry-server) and verified they pass alongside existing FQDN and IP tests.