Skip to content

Commit

Permalink
fix(validator): restrict to allow only HTTP protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Sep 2, 2024
1 parent cf6e687 commit 5dc2752
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
21 changes: 18 additions & 3 deletions internal/utils/validator.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package utils

import "github.com/asaskevich/govalidator"
import (
"net/url"

"github.com/asaskevich/govalidator"
)

func ValidateEmail(email string) bool {
return govalidator.IsEmail(email)
}

func ValidateURL(url string) bool {
return govalidator.IsRequestURL(url)
func ValidateURL(v string) bool {
u, err := url.ParseRequestURI(v)
if err != nil {
return false
}
if len(u.Scheme) == 0 {
return false
}
// Must https or http
if u.Scheme != "https" && u.Scheme != "http" {
return false
}
return true
}
3 changes: 2 additions & 1 deletion internal/utils/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func TestValidateURL(t *testing.T) {
}{
{"https://example.com", true},
{"invalid.url", false},
{"https://example.com/path?param=value", true},
{"http://example.com/path?param=value", true},
{"ftp://example.com", false},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 5dc2752

Please sign in to comment.