Skip to content

fix : add url validation to all commands#607

Merged
bupd merged 2 commits intogoharbor:mainfrom
Sypher845:fix/add-url-validation-to-all-commands
Feb 17, 2026
Merged

fix : add url validation to all commands#607
bupd merged 2 commits intogoharbor:mainfrom
Sypher845:fix/add-url-validation-to-all-commands

Conversation

@Sypher845
Copy link
Copy Markdown
Contributor

@Sypher845 Sypher845 commented Jan 20, 2026

Fixes #606
Adds URL validation to commands that accept URL inputs (instance create, registry create/update, scanner create/update, webhook edit).

Changes:

  • Added utils.ValidateURL() checks before API calls
  • Updated view files to use consistent URL validation in forms

ValidateURL function

What it checks:

  • Valid URL format (scheme, host, etc.)
  • Host is not empty
  • Host is a valid IP or domain name
  • Starts/ends with alphanumeric
  • Hyphens allowed in middle
  • Max 63 chars per label
  • TLD at least 2 letters
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,}$`)

	parsedURL, err := url.ParseRequestURI(rawURL)
	if err != nil {
		return fmt.Errorf("invalid URL format: %v", err)
	}

	host := parsedURL.Hostname()
	if host == "" {
		return fmt.Errorf("URL must contain a valid host")
	}

	if net.ParseIP(host) != nil {
		return nil
	}

	if !domainNameRegex.MatchString(host) {
		return fmt.Errorf("invalid host: must be a valid IP address or domain name")
	}

	return nil
}

@Sypher845
Copy link
Copy Markdown
Contributor Author

@bupd can you please review this

@bupd bupd self-requested a review January 21, 2026 19:06
@bupd bupd self-assigned this Jan 21, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 21, 2026

Codecov Report

❌ Patch coverage is 1.31579% with 75 lines in your changes missing coverage. Please review.
✅ Project coverage is 7.26%. Comparing base (60ad0bd) to head (11131d9).
⚠️ Report is 95 commits behind head on main.

Files with missing lines Patch % Lines
pkg/views/instance/create/view.go 0.00% 11 Missing ⚠️
cmd/harbor/root/registry/update.go 0.00% 10 Missing ⚠️
pkg/views/registry/update/view.go 0.00% 10 Missing ⚠️
cmd/harbor/root/registry/create.go 0.00% 8 Missing ⚠️
cmd/harbor/root/instance/create.go 0.00% 7 Missing ⚠️
pkg/views/scanner/update/view.go 0.00% 7 Missing ⚠️
pkg/views/webhook/edit/view.go 0.00% 6 Missing ⚠️
cmd/harbor/root/scanner/create.go 0.00% 5 Missing ⚠️
pkg/views/registry/create/view.go 0.00% 5 Missing ⚠️
cmd/harbor/root/scanner/update.go 0.00% 4 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff            @@
##             main    #607      +/-   ##
=========================================
- Coverage   10.99%   7.26%   -3.73%     
=========================================
  Files         173     260      +87     
  Lines        8671   12866    +4195     
=========================================
- Hits          953     935      -18     
- Misses       7612   11823    +4211     
- Partials      106     108       +2     

☔ 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.

@Sypher845
Copy link
Copy Markdown
Contributor Author

@bupd i have merged the main branch in this , can you please review again .
Also do i have to make any other changes?

@Sypher845 Sypher845 changed the title fix : add-url-validation-to-all-commands fix : add url validation to all commands Jan 21, 2026
@Sypher845 Sypher845 force-pushed the fix/add-url-validation-to-all-commands branch from fb81159 to 1be6b08 Compare January 22, 2026 17:20
@Sypher845
Copy link
Copy Markdown
Contributor Author

@NucleoFusion done with the changes

@Sypher845
Copy link
Copy Markdown
Contributor Author

@bupd @NucleoFusion any update on this?

@NucleoFusion
Copy link
Copy Markdown
Contributor

@Sypher845 If you could add to the description (and maybe even in a comment on the ValidateURL func).
Like what all it validates? For the regex mainly.
Rest of the conditions seem simple enough

Copy link
Copy Markdown
Contributor

@NucleoFusion NucleoFusion left a comment

Choose a reason for hiding this comment

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

Seems like a simple PR. Just some clarification on what the Regex matches.
The Validations work as intended from my testing

@Sypher845
Copy link
Copy Markdown
Contributor Author

Sypher845 commented Feb 4, 2026

@NucleoFusion added the description and made the changes, PTAL

Copy link
Copy Markdown
Contributor

@NucleoFusion NucleoFusion left a comment

Choose a reason for hiding this comment

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

Lgtm!

Copy link
Copy Markdown
Collaborator

@bupd bupd left a comment

Choose a reason for hiding this comment

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

please fix the suggestions from copilot - otherwise lgtm

@bupd bupd self-requested a review February 10, 2026 14:56
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

This PR aims to prevent confusing server-side failures by validating user-supplied URLs client-side (both in flag-driven commands and interactive TUI forms) before making Harbor API calls.

Changes:

  • Added utils.ValidateURL() checks in multiple commands when URL flags are provided.
  • Updated several interactive views to reject empty/whitespace input and to validate URLs consistently.
  • Refactored some commands to return errors (RunE) instead of logging and exiting, improving CLI error propagation.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
pkg/views/webhook/edit/view.go Adds endpoint URL non-empty (trimmed) validation and URL format validation in the webhook edit TUI.
pkg/views/scanner/update/view.go Adds trimmed non-empty validation and validates a normalized URL for scanner adapter URL input.
pkg/views/registry/update/view.go Trims whitespace for provider/name/url inputs and validates registry URL (currently only validates normalized value).
pkg/views/registry/create/view.go Replaces net/url parsing with utils.ValidateURL() for registry URL validation (currently only validates normalized value).
pkg/views/instance/create/view.go Adds trimmed non-empty validation and validates a normalized endpoint URL (currently only validates normalized value).
pkg/utils/helper.go Documents ValidateURL behavior (function used broadly for URL validation).
cmd/harbor/root/webhook/edit.go Validates endpoint URL before calling api.UpdateWebhook in non-interactive mode.
cmd/harbor/root/scanner/update.go Normalizes + validates --url before updating scanner registration.
cmd/harbor/root/scanner/create.go Normalizes + validates --url before creating/pinging scanner in non-interactive mode.
cmd/harbor/root/registry/update.go Converts to RunE, returns errors, and validates registry --url before update.
cmd/harbor/root/registry/create.go Converts to RunE, returns errors, and validates registry --url before create.
cmd/harbor/root/login.go Changes invalid server URL handling to return the raw validation error.
cmd/harbor/root/instance/create.go Converts to RunE, returns errors, and validates instance endpoint URL before create.

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

Signed-off-by: Sypher845 <suyashpatil845@gmail.com>
Signed-off-by: Sypher845 <suyashpatil845@gmail.com>
@Sypher845 Sypher845 force-pushed the fix/add-url-validation-to-all-commands branch from 639ee5b to 11131d9 Compare February 10, 2026 21:49
@Sypher845
Copy link
Copy Markdown
Contributor Author

@bupd done

Copy link
Copy Markdown
Collaborator

@bupd bupd left a comment

Choose a reason for hiding this comment

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

/lgtm

@bupd bupd merged commit f761937 into goharbor:main Feb 17, 2026
6 of 8 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.

Add URL validation to all commands that accept URL input

4 participants