diff --git a/models/forgefed/federationhost.go b/models/forgefed/federationhost.go index a828961727..4b632e9d54 100644 --- a/models/forgefed/federationhost.go +++ b/models/forgefed/federationhost.go @@ -18,7 +18,7 @@ type FederationHost struct { ID int64 `xorm:"pk autoincr"` HostFqdn string `xorm:"host_fqdn UNIQUE INDEX VARCHAR(255) NOT NULL"` NodeInfo NodeInfo `xorm:"extends NOT NULL"` - HostPort string `xorm:"NOT NULL DEFAULT '443'"` + HostPort uint16 `xorm:"NOT NULL DEFAULT '443'"` HostSchema string `xorm:"NOT NULL DEFAULT 'https'"` LatestActivity time.Time `xorm:"NOT NULL"` Created timeutil.TimeStamp `xorm:"created"` @@ -26,7 +26,7 @@ type FederationHost struct { } // Factory function for FederationHost. Created struct is asserted to be valid. -func NewFederationHost(hostFqdn string, nodeInfo NodeInfo, port, schema string) (FederationHost, error) { +func NewFederationHost(hostFqdn string, nodeInfo NodeInfo, port uint16, schema string) (FederationHost, error) { result := FederationHost{ HostFqdn: strings.ToLower(hostFqdn), NodeInfo: nodeInfo, diff --git a/modules/forgefed/actor.go b/modules/forgefed/actor.go index a35cbf9292..46d342540f 100644 --- a/modules/forgefed/actor.go +++ b/modules/forgefed/actor.go @@ -6,6 +6,7 @@ package forgefed import ( "fmt" "net/url" + "strconv" "strings" "forgejo.org/modules/validation" @@ -20,7 +21,7 @@ type ActorID struct { HostSchema string Path string Host string - HostPort string + HostPort uint16 UnvalidatedInput string IsPortSupplemented bool } @@ -44,7 +45,7 @@ func (id ActorID) AsURI() string { if id.IsPortSupplemented { result = fmt.Sprintf("%s://%s/%s/%s", id.HostSchema, id.Host, id.Path, id.ID) } else { - result = fmt.Sprintf("%s://%s:%s/%s/%s", id.HostSchema, id.Host, id.HostPort, id.Path, id.ID) + result = fmt.Sprintf("%s://%s:%d/%s/%s", id.HostSchema, id.Host, id.HostPort, id.Path, id.ID) } return result } @@ -192,18 +193,20 @@ func newActorID(uri string) (ActorID, error) { if validatedURI.Port() == "" && result.HostSchema == "https" { result.IsPortSupplemented = true - result.HostPort = "443" + result.HostPort = 443 result.UnvalidatedInput = fmt.Sprintf("%s://%s/%s/%s", result.HostSchema, result.Host, result.Path, result.ID) return result, nil } else if validatedURI.Port() == "" && result.HostSchema == "http" { result.IsPortSupplemented = true - result.HostPort = "80" + result.HostPort = 80 result.UnvalidatedInput = fmt.Sprintf("%s://%s/%s/%s", result.HostSchema, result.Host, result.Path, result.ID) return result, nil } - result.HostPort = validatedURI.Port() - result.UnvalidatedInput = fmt.Sprintf("%s://%s:%s/%s/%s", result.HostSchema, result.Host, result.HostPort, result.Path, result.ID) + numPort, _ := strconv.ParseUint(validatedURI.Port(), 10, 16) + result.HostPort = uint16(numPort) + + result.UnvalidatedInput = fmt.Sprintf("%s://%s:%d/%s/%s", result.HostSchema, result.Host, result.HostPort, result.Path, result.ID) return result, nil } diff --git a/modules/forgefed/actor_test.go b/modules/forgefed/actor_test.go index 053d4c067c..5ca98e9f22 100644 --- a/modules/forgefed/actor_test.go +++ b/modules/forgefed/actor_test.go @@ -21,7 +21,7 @@ func TestNewPersonId(t *testing.T) { expected.HostSchema = "https" expected.Path = "api/v1/activitypub/user-id" expected.Host = "an.other.host" - expected.HostPort = "443" + expected.HostPort = 443 expected.IsPortSupplemented = true expected.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1" @@ -36,7 +36,7 @@ func TestNewPersonId(t *testing.T) { expected.HostSchema = "https" expected.Path = "api/v1/activitypub/user-id" expected.Host = "an.other.host" - expected.HostPort = "443" + expected.HostPort = 443 expected.IsPortSupplemented = false expected.UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1" @@ -51,7 +51,7 @@ func TestNewPersonId(t *testing.T) { expected.HostSchema = "http" expected.Path = "api/v1/activitypub/user-id" expected.Host = "an.other.host" - expected.HostPort = "80" + expected.HostPort = 80 expected.IsPortSupplemented = false expected.UnvalidatedInput = "http://an.other.host:80/api/v1/activitypub/user-id/1" @@ -66,7 +66,7 @@ func TestNewPersonId(t *testing.T) { expected.HostSchema = "https" expected.Path = "api/v1/activitypub/user-id" expected.Host = "an.other.host" - expected.HostPort = "443" + expected.HostPort = 443 expected.IsPortSupplemented = false expected.UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1" @@ -84,7 +84,7 @@ func TestNewRepositoryId(t *testing.T) { expected.HostSchema = "http" expected.Path = "api/activitypub/repository-id" expected.Host = "localhost" - expected.HostPort = "3000" + expected.HostPort = 3000 expected.UnvalidatedInput = "http://localhost:3000/api/activitypub/repository-id/1" sut, _ := NewRepositoryID("http://localhost:3000/api/activitypub/repository-id/1", "forgejo") if sut != expected { @@ -98,7 +98,7 @@ func TestActorIdValidation(t *testing.T) { sut.HostSchema = "https" sut.Path = "api/v1/activitypub/user-id" sut.Host = "an.other.host" - sut.HostPort = "0" + sut.HostPort = 0 sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/" if sut.Validate()[0] != "userId should not be empty" { t.Errorf("validation error expected but was: %v\n", sut.Validate()) @@ -110,7 +110,7 @@ func TestActorIdValidation(t *testing.T) { sut.HostSchema = "https" sut.Path = "api/v1/activitypub/user-id" sut.Host = "an.other.host" - sut.HostPort = "0" + sut.HostPort = 0 sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1?illegal=action" if sut.Validate()[0] != "not all input was parsed, \nUnvalidated Input:\"https://an.other.host/api/v1/activitypub/user-id/1?illegal=action\" \nParsed URI: \"https://an.other.host/api/v1/activitypub/user-id/1\"" { t.Errorf("validation error expected but was: %v\n", sut.Validate()[0]) @@ -124,7 +124,7 @@ func TestPersonIdValidation(t *testing.T) { sut.HostSchema = "https" sut.Path = "path" sut.Host = "an.other.host" - sut.HostPort = "0" + sut.HostPort = 0 sut.UnvalidatedInput = "https://an.other.host/path/1" _, err := validation.IsValid(sut) @@ -138,7 +138,7 @@ func TestPersonIdValidation(t *testing.T) { sut.HostSchema = "https" sut.Path = "api/v1/activitypub/user-id" sut.Host = "an.other.host" - sut.HostPort = "0" + sut.HostPort = 0 sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1" if sut.Validate()[0] != "Value forgejox is not contained in allowed values [forgejo gitea]" { t.Errorf("validation error expected but was: %v\n", sut.Validate()[0]) diff --git a/modules/forgefed/nodeinfo.go b/modules/forgefed/nodeinfo.go index 09a9b33c6c..9bd142e3f3 100644 --- a/modules/forgefed/nodeinfo.go +++ b/modules/forgefed/nodeinfo.go @@ -10,10 +10,10 @@ import ( func (id ActorID) AsWellKnownNodeInfoURI() string { wellKnownPath := ".well-known/nodeinfo" var result string - if id.HostPort == "0" { + if id.HostPort == 0 { result = fmt.Sprintf("%s://%s/%s", id.HostSchema, id.Host, wellKnownPath) } else { - result = fmt.Sprintf("%s://%s:%s/%s", id.HostSchema, id.Host, id.HostPort, wellKnownPath) + result = fmt.Sprintf("%s://%s:%d/%s", id.HostSchema, id.Host, id.HostPort, wellKnownPath) } return result } diff --git a/modules/validation/validatable.go b/modules/validation/validatable.go index bc565bd194..237ff24e78 100644 --- a/modules/validation/validatable.go +++ b/modules/validation/validatable.go @@ -53,6 +53,10 @@ func ValidateNotEmpty(value any, name string) []string { if v.IsZero() { isValid = false } + case uint16: + if v == 0 { + isValid = false + } case int64: if v == 0 { isValid = false