mirror of
1
Fork 0

Fix error handling & add timestamp check

This commit is contained in:
Michael Jerger 2024-01-13 14:17:11 +01:00
parent 40ec049013
commit dabd773f6b
5 changed files with 56 additions and 27 deletions

View File

@ -4,6 +4,8 @@
package forgefed package forgefed
import ( import (
"time"
"code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/validation"
ap "github.com/go-ap/activitypub" ap "github.com/go-ap/activitypub"
) )
@ -15,22 +17,26 @@ type ForgeLike struct {
ap.Activity ap.Activity
} }
func (s ForgeLike) MarshalJSON() ([]byte, error) { func (like ForgeLike) MarshalJSON() ([]byte, error) {
return s.Activity.MarshalJSON() return like.Activity.MarshalJSON()
} }
func (s *ForgeLike) UnmarshalJSON(data []byte) error { func (like *ForgeLike) UnmarshalJSON(data []byte) error {
return s.Activity.UnmarshalJSON(data) return like.Activity.UnmarshalJSON(data)
} }
func (s ForgeLike) Validate() []string { func (like ForgeLike) IsNewer(compareTo time.Time) bool {
return like.StartTime.After(compareTo)
}
func (like ForgeLike) Validate() []string {
var result []string var result []string
result = append(result, validation.ValidateNotEmpty(string(s.Type), "type")...) result = append(result, validation.ValidateNotEmpty(string(like.Type), "type")...)
result = append(result, validation.ValidateOneOf(string(s.Type), []any{"Like"})...) result = append(result, validation.ValidateOneOf(string(like.Type), []any{"Like"})...)
result = append(result, validation.ValidateNotEmpty(s.Actor.GetID().String(), "actor")...) result = append(result, validation.ValidateNotEmpty(like.Actor.GetID().String(), "actor")...)
result = append(result, validation.ValidateNotEmpty(s.Object.GetID().String(), "object")...) result = append(result, validation.ValidateNotEmpty(like.Object.GetID().String(), "object")...)
result = append(result, validation.ValidateNotEmpty(s.StartTime.String(), "startTime")...) result = append(result, validation.ValidateNotEmpty(like.StartTime.String(), "startTime")...)
if s.StartTime.IsZero() { if like.StartTime.IsZero() {
result = append(result, "StartTime was invalid.") result = append(result, "StartTime was invalid.")
} }

View File

@ -4,6 +4,8 @@
package forgefed package forgefed
import ( import (
"time"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/validation"
) )
@ -14,7 +16,7 @@ type FederationInfo struct {
ID int64 `xorm:"pk autoincr"` ID int64 `xorm:"pk autoincr"`
HostFqdn string `xorm:"host_fqdn UNIQUE INDEX VARCHAR(255) NOT NULL"` HostFqdn string `xorm:"host_fqdn UNIQUE INDEX VARCHAR(255) NOT NULL"`
NodeInfo NodeInfo `xorm:"extends NOT NULL"` NodeInfo NodeInfo `xorm:"extends NOT NULL"`
LatestActivity timeutil.TimeStamp `xorm:"NOT NULL"` LatestActivity time.Time `xorm:"NOT NULL"`
Create timeutil.TimeStamp `xorm:"created"` Create timeutil.TimeStamp `xorm:"created"`
Updated timeutil.TimeStamp `xorm:"updated"` Updated timeutil.TimeStamp `xorm:"updated"`
} }

View File

@ -50,3 +50,11 @@ func CreateFederationInfo(ctx context.Context, info FederationInfo) error {
_, err := db.GetEngine(ctx).Insert(info) _, err := db.GetEngine(ctx).Insert(info)
return err return err
} }
func UpdateFederationInfo(ctx context.Context, info FederationInfo) error {
if res, err := validation.IsValid(info); !res {
return fmt.Errorf("FederationInfo is not valid: %v", err)
}
_, err := db.GetEngine(ctx).ID(info.ID).Update(info)
return err
}

View File

@ -5,8 +5,8 @@ package forgefed
import ( import (
"testing" "testing"
"time"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/validation"
) )
@ -16,7 +16,7 @@ func Test_FederationInfoValidation(t *testing.T) {
NodeInfo: NodeInfo{ NodeInfo: NodeInfo{
Source: "forgejo", Source: "forgejo",
}, },
LatestActivity: timeutil.TimeStampNow(), LatestActivity: time.Now(),
} }
if res, err := validation.IsValid(sut); !res { if res, err := validation.IsValid(sut); !res {
t.Errorf("sut should be valid but was %q", err) t.Errorf("sut should be valid but was %q", err)
@ -25,7 +25,7 @@ func Test_FederationInfoValidation(t *testing.T) {
sut = FederationInfo{ sut = FederationInfo{
HostFqdn: "host.do.main", HostFqdn: "host.do.main",
NodeInfo: NodeInfo{}, NodeInfo: NodeInfo{},
LatestActivity: timeutil.TimeStampNow(), LatestActivity: time.Now(),
} }
if res, _ := validation.IsValid(sut); res { if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid") t.Errorf("sut should be invalid")

View File

@ -51,7 +51,7 @@ func Repository(ctx *context.APIContext) {
repo.Name = ap.NaturalLanguageValuesNew() repo.Name = ap.NaturalLanguageValuesNew()
err := repo.Name.Set("en", ap.Content(ctx.Repo.Repository.Name)) err := repo.Name.Set("en", ap.Content(ctx.Repo.Repository.Name))
if err != nil { if err != nil {
ctx.ServerError("Set Name", err) ctx.Error(http.StatusInternalServerError, "Set Name", err)
return return
} }
@ -86,7 +86,7 @@ func RepositoryInbox(ctx *context.APIContext) {
activity := web.GetForm(ctx).(*forgefed.ForgeLike) activity := web.GetForm(ctx).(*forgefed.ForgeLike)
if res, err := validation.IsValid(activity); !res { if res, err := validation.IsValid(activity); !res {
ctx.ServerError("Validate activity", err) ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate activity", err)
return return
} }
log.Info("RepositoryInbox: activity validated:%v", activity) log.Info("RepositoryInbox: activity validated:%v", activity)
@ -96,33 +96,39 @@ func RepositoryInbox(ctx *context.APIContext) {
rawActorID, err := forgefed.NewActorID(actorUri) rawActorID, err := forgefed.NewActorID(actorUri)
federationInfo, err := forgefed.FindFederationInfoByHostFqdn(ctx, rawActorID.Host) federationInfo, err := forgefed.FindFederationInfoByHostFqdn(ctx, rawActorID.Host)
if err != nil { if err != nil {
ctx.ServerError("Error while loading FederationInfo: %v", err) ctx.Error(http.StatusInternalServerError,
"RepositoryInbox: Error while loading FederationInfo", err)
return return
} }
if federationInfo == nil { if federationInfo == nil {
result, err := createFederationInfo(ctx, rawActorID) result, err := createFederationInfo(ctx, rawActorID)
if err != nil { if err != nil {
ctx.ServerError("Validate actorId", err) ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate actorId", err)
return return
} }
federationInfo = &result federationInfo = &result
log.Info("RepositoryInbox: federationInfo validated: %v", federationInfo) log.Info("RepositoryInbox: federationInfo validated: %v", federationInfo)
} }
if !activity.IsNewer(federationInfo.LatestActivity) {
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate Activity",
fmt.Errorf("Activity already processed"))
return
}
actorID, err := forgefed.NewPersonID(actorUri, string(federationInfo.NodeInfo.Source)) actorID, err := forgefed.NewPersonID(actorUri, string(federationInfo.NodeInfo.Source))
if err != nil { if err != nil {
ctx.ServerError("Validate actorId", err) ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate actorId", err)
return return
} }
log.Info("RepositoryInbox: actorId validated: %v", actorID) log.Info("RepositoryInbox: actorId validated: %v", actorID)
// parse objectID (repository) // parse objectID (repository)
objectID, err := forgefed.NewRepositoryID(activity.Object.GetID().String(), string(forgefed.ForgejoSourceType)) objectID, err := forgefed.NewRepositoryID(activity.Object.GetID().String(), string(forgefed.ForgejoSourceType))
if err != nil { if err != nil {
ctx.ServerError("Validate objectId", err) ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate objectId", err)
return return
} }
if objectID.ID != fmt.Sprint(repository.ID) { if objectID.ID != fmt.Sprint(repository.ID) {
ctx.ServerError("Validate objectId", err) ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate objectId", err)
return return
} }
log.Info("RepositoryInbox: objectId validated: %v", objectID) log.Info("RepositoryInbox: objectId validated: %v", objectID)
@ -133,7 +139,7 @@ func RepositoryInbox(ctx *context.APIContext) {
// Check if user already exists // Check if user already exists
users, err := SearchUsersByLoginName(actorAsLoginID) users, err := SearchUsersByLoginName(actorAsLoginID)
if err != nil { if err != nil {
ctx.ServerError("Searching for user failed", err) ctx.Error(http.StatusInternalServerError, "RepositoryInbox: Searching for user failed", err)
return return
} }
log.Info("RepositoryInbox: local found users: %v", len(users)) log.Info("RepositoryInbox: local found users: %v", len(users))
@ -143,7 +149,8 @@ func RepositoryInbox(ctx *context.APIContext) {
{ {
user, err = createUserFromAP(ctx, actorID) user, err = createUserFromAP(ctx, actorID)
if err != nil { if err != nil {
ctx.ServerError("Creating user failed", err) ctx.Error(http.StatusInternalServerError,
"RepositoryInbox: Creating federated user failed", err)
return return
} }
log.Info("RepositoryInbox: created user from ap: %v", user) log.Info("RepositoryInbox: created user from ap: %v", user)
@ -155,8 +162,8 @@ func RepositoryInbox(ctx *context.APIContext) {
} }
default: default:
{ {
ctx.Error(http.StatusInternalServerError, "StarRepo", ctx.Error(http.StatusInternalServerError, "RepositoryInbox",
fmt.Errorf("found more than one matches for federated users")) fmt.Errorf(" more than one matches for federated users"))
return return
} }
} }
@ -166,10 +173,16 @@ func RepositoryInbox(ctx *context.APIContext) {
if !alreadyStared { if !alreadyStared {
err = repo_model.StarRepo(ctx, user.ID, repository.ID, true) err = repo_model.StarRepo(ctx, user.ID, repository.ID, true)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err) ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Star operation", err)
return return
} }
} }
federationInfo.LatestActivity = activity.StartTime
err = forgefed.UpdateFederationInfo(ctx, *federationInfo)
if err != nil {
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: error updateing federateionInfo", err)
return
}
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)
} }