From e69e5df08975dd2dc838efb93eaad71a9d0985d2 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 22 Dec 2023 13:42:07 +0100 Subject: [PATCH] Assume validated url.URL for NewActorID --- models/forgefed/actor.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/models/forgefed/actor.go b/models/forgefed/actor.go index 3b3c41f951..378342833d 100644 --- a/models/forgefed/actor.go +++ b/models/forgefed/actor.go @@ -31,8 +31,9 @@ type RepositoryId struct { ActorId } -func newActorId(uri, source string) (ActorId, error) { - validatedUri, _ := url.Parse(uri) // ToDo: Why no err treatment at this place? +// newActorId receives already validated inputs +func newActorId(validatedUri *url.URL, source string) (ActorId, error) { + pathWithActorID := strings.Split(validatedUri.Path, "/") if containsEmptyString(pathWithActorID) { pathWithActorID = removeEmptyStrings(pathWithActorID) @@ -48,7 +49,7 @@ func newActorId(uri, source string) (ActorId, error) { result.Host = validatedUri.Hostname() result.Path = pathWithoutActorID result.Port = validatedUri.Port() - result.UnvalidatedInput = uri + result.UnvalidatedInput = validatedUri.String() if valid, err := result.IsValid(); !valid { return ActorId{}, err @@ -62,8 +63,12 @@ func NewPersonId(uri string, source string) (PersonId, error) { //if !validation.IsValidExternalURL(uri) { // return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri) //} + validatedUri, err := url.ParseRequestURI(uri) // ToDo: Why no err treatment at this place? + if err != nil { + return PersonId{}, err + } - actorId, err := newActorId(uri, source) + actorId, err := newActorId(validatedUri, source) if err != nil { return PersonId{}, err } @@ -83,7 +88,12 @@ func NewRepositoryId(uri string, source string) (RepositoryId, error) { return RepositoryId{}, fmt.Errorf("uri %s is not a valid repo url on this host %s", uri, setting.AppURL+"api") } - actorId, err := newActorId(uri, source) + validatedUri, err := url.ParseRequestURI(uri) // ToDo: Why no err treatment at this place? + if err != nil { + return RepositoryId{}, err + } + + actorId, err := newActorId(validatedUri, source) if err != nil { return RepositoryId{}, err }