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 Validation Domain for login command #271

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 54 additions & 0 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"errors"
"fmt"
"net"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -39,6 +40,59 @@ func FormatUrl(url string) string {
return url
}

// ValidateDomain validates subdomain, IP, or top-level domain formats
func ValidateDomain(domain string) error {
if strings.Contains(domain, ":") {
parts := strings.Split(domain, ":")
if len(parts) != 2 {
return errors.New("invalid domain format: too many colons")
}
port, err := strconv.Atoi(parts[1])
if err != nil || port < 0 || port > 65535 {
return errors.New("invalid port number")
}
domain = parts[0]
}

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

parts := strings.Split(domain, ".")
if len(parts) < 2 {
return errors.New("invalid domain: must have at least one dot")
}

for _, part := range parts {
if !isValidLabel(part) {
return fmt.Errorf("invalid domain label: %s", part)
}
}

if len(parts[len(parts)-1]) < 2 {
return errors.New("invalid top-level domain: must be at least 2 characters")
}

return nil
}

// Helper function to validate individual domain labels
func isValidLabel(label string) bool {
if len(label) == 0 || len(label) > 63 {
return false
}

for i, ch := range label {
if !(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9' || ch == '-') {
return false
}
if (i == 0 || i == len(label)-1) && ch == '-' {
return false
}
}
return true
}

func FormatSize(size int64) string {
mbSize := float64(size) / (1024 * 1024)
return fmt.Sprintf("%.2fMiB", mbSize)
Expand Down
8 changes: 4 additions & 4 deletions pkg/views/login/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package login
import (
"errors"
"fmt"
"net/url"
"strings"

"github.com/charmbracelet/huh"
Expand Down Expand Up @@ -32,10 +31,11 @@ func CreateView(loginView *LoginView) {
if strings.TrimSpace(str) == "" {
return errors.New("server cannot be empty or only spaces")
}
formattedUrl := utils.FormatUrl(str)
if _, err := url.ParseRequestURI(formattedUrl); err != nil {
return errors.New("please enter the correct server format")
err := utils.ValidateDomain(str)
if err != nil {
return err
}

return nil
}),
huh.NewInput().
Expand Down
Loading