Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix(actions): handle explicit JSON null for image uploads

Matt Robertsd459b639d808
diff --git a/app/actions/oauth.go b/app/actions/oauth.go
index 5ba7f94bc..d73d34678 100644
--- a/app/actions/oauth.go
+++ b/app/actions/oauth.go
@@ -85,6 +85,11 @@ func (action *SetSystemProviderStatus) Validate(ctx context.Context, user *entit
func (action *CreateEditOAuthConfig) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
+ // An explicit "logo": null overrides the value set by NewCreateEditOAuthConfig
+ if action.Logo == nil {
+ action.Logo = &dto.ImageUpload{}
+ }
+
if action.Status == enum.OAuthConfigDisabled {
tenant := ctx.Value(app.TenantCtxKey).(*entity.Tenant)
activeProviders := &query.ListActiveOAuthProviders{}
diff --git a/app/actions/oauth_test.go b/app/actions/oauth_test.go
index 5eb05f72a..c95e3d708 100644
--- a/app/actions/oauth_test.go
+++ b/app/actions/oauth_test.go
@@ -2,6 +2,7 @@ package actions_test
import (
"context"
+ "encoding/json"
"testing"
"github.com/getfider/fider/app/models/dto"
@@ -217,3 +218,32 @@ func TestCreateEditOAuthConfig_EditNonExisting(t *testing.T) {
Expect(result.Err).Equals(app.ErrNotFound)
Expect(result.Ok).IsFalse()
}
+
+func TestCreateEditOAuthConfig_NullLogo(t *testing.T) {
+ RegisterT(t)
+
+ action := actions.NewCreateEditOAuthConfig()
+ err := json.Unmarshal([]byte(`{
+ "displayName": "My Provider",
+ "status": 2,
+ "clientID": "823187ahjjfdha8fds7yfdashfjkdsa",
+ "clientSecret": "secret",
+ "authorizeURL": "http://provider/oauth/authorize",
+ "tokenURL": "http://provider/oauth/token",
+ "scope": "profile email",
+ "profileURL": "http://provider/profile/me",
+ "jsonUserIDPath": "user.id",
+ "jsonUserNamePath": "user.name",
+ "logo": null
+ }`), action)
+ Expect(err).IsNil()
+ Expect(action.Logo).IsNil()
+
+ ctx := context.WithValue(context.Background(), app.TenantCtxKey, &entity.Tenant{
+ IsEmailAuthAllowed: true,
+ })
+
+ result := action.Validate(ctx, nil)
+ ExpectSuccess(result)
+ Expect(action.Logo).IsNotNil()
+}
diff --git a/app/actions/post.go b/app/actions/post.go
index 641a53e43..787f259e4 100644
--- a/app/actions/post.go
+++ b/app/actions/post.go
@@ -65,6 +65,7 @@ func (action *CreateNewPost) IsAuthorized(ctx context.Context, user *entity.User
// Validate if current model is valid
func (action *CreateNewPost) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
+ action.Attachments = withoutNilUploads(action.Attachments)
re := regexp.MustCompile(`\s+`)
normalizedTitle := strings.TrimSpace(re.ReplaceAllString(action.Title, " "))
@@ -133,6 +134,7 @@ func (input *UpdatePost) IsAuthorized(ctx context.Context, user *entity.User) bo
// Validate if current model is valid
func (action *UpdatePost) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
+ action.Attachments = withoutNilUploads(action.Attachments)
if action.Title == "" {
result.AddFieldFailure("title", propertyIsRequired(ctx, "title"))
@@ -218,6 +220,7 @@ func (action *AddNewComment) IsAuthorized(ctx context.Context, user *entity.User
// Validate if current model is valid
func (action *AddNewComment) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
+ action.Attachments = withoutNilUploads(action.Attachments)
if action.Content == "" {
result.AddFieldFailure("content", propertyIsRequired(ctx, "comment"))
@@ -345,6 +348,7 @@ func (action *EditComment) IsAuthorized(ctx context.Context, user *entity.User)
// Validate if current model is valid
func (action *EditComment) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
+ action.Attachments = withoutNilUploads(action.Attachments)
if action.Content == "" {
result.AddFieldFailure("content", propertyIsRequired(ctx, "comment"))
@@ -390,3 +394,18 @@ func (action *DeleteComment) IsAuthorized(ctx context.Context, user *entity.User
func (action *DeleteComment) Validate(ctx context.Context, user *entity.User) *validate.Result {
return validate.Success()
}
+
+// withoutNilUploads drops null entries, e.g. from "attachments": [null], which would otherwise
+// be dereferenced when the attachments are validated and stored
+func withoutNilUploads(uploads []*dto.ImageUpload) []*dto.ImageUpload {
+ if uploads == nil {
+ return nil
+ }
+ result := make([]*dto.ImageUpload, 0, len(uploads))
+ for _, upload := range uploads {
+ if upload != nil {
+ result = append(result, upload)
+ }
+ }
+ return result
+}
diff --git a/app/actions/post_test.go b/app/actions/post_test.go
index 5fff5fe64..ff8bd9132 100644
--- a/app/actions/post_test.go
+++ b/app/actions/post_test.go
@@ -2,6 +2,7 @@ package actions_test
import (
"context"
+ "encoding/json"
"strings"
"testing"
@@ -166,3 +167,44 @@ func TestEditComment_AtMaxLength(t *testing.T) {
result := action.Validate(context.Background(), nil)
ExpectSuccess(result)
}
+
+func TestCreateNewPost_NullAttachments(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetPostBySlug) error {
+ return app.ErrNotFound
+ })
+
+ action := &actions.CreateNewPost{}
+ err := json.Unmarshal([]byte(`{"title": "this is my new post", "attachments": [null, null]}`), action)
+ Expect(err).IsNil()
+
+ result := action.Validate(context.Background(), nil)
+ ExpectSuccess(result)
+ Expect(action.Attachments).HasLen(0)
+}
+
+func TestAddNewComment_NullAttachments(t *testing.T) {
+ RegisterT(t)
+
+ action := &actions.AddNewComment{}
+ err := json.Unmarshal([]byte(`{"content": "Nice idea", "attachments": [null]}`), action)
+ Expect(err).IsNil()
+
+ result := action.Validate(context.Background(), nil)
+ ExpectSuccess(result)
+ Expect(action.Attachments).HasLen(0)
+}
+
+func TestEditComment_NullAttachments(t *testing.T) {
+ RegisterT(t)
+
+ action := &actions.EditComment{}
+ err := json.Unmarshal([]byte(`{"content": "Nice idea", "attachments": [null, {"bkey": "attachments/a.png", "remove": true}]}`), action)
+ Expect(err).IsNil()
+
+ result := action.Validate(context.Background(), nil)
+ ExpectSuccess(result)
+ Expect(action.Attachments).HasLen(1)
+ Expect(action.Attachments[0].BlobKey).Equals("attachments/a.png")
+}
diff --git a/app/actions/settings.go b/app/actions/settings.go
index 936e07685..9fff80d59 100644
--- a/app/actions/settings.go
+++ b/app/actions/settings.go
@@ -33,6 +33,11 @@ func (action *UpdateUserSettings) IsAuthorized(ctx context.Context, user *entity
func (action *UpdateUserSettings) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
+ // An explicit "avatar": null overrides the value set by NewUpdateUserSettings
+ if action.Avatar == nil {
+ action.Avatar = &dto.ImageUpload{}
+ }
+
if action.Name == "" {
result.AddFieldFailure("name", propertyIsRequired(ctx, "name"))
}
diff --git a/app/actions/tenant.go b/app/actions/tenant.go
index 4c41abe8e..36d129bcc 100644
--- a/app/actions/tenant.go
+++ b/app/actions/tenant.go
@@ -215,6 +215,11 @@ func (action *UpdateTenantSettings) IsAuthorized(ctx context.Context, user *enti
func (action *UpdateTenantSettings) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
+ // An explicit "logo": null overrides the value set by NewUpdateTenantSettings
+ if action.Logo == nil {
+ action.Logo = &dto.ImageUpload{}
+ }
+
tenant, hasTenant := ctx.Value(app.TenantCtxKey).(*entity.Tenant)
if hasTenant {
action.Logo.BlobKey = tenant.LogoBlobKey
diff --git a/app/actions/tenant_test.go b/app/actions/tenant_test.go
index 979d24d50..e32c14a8e 100644
--- a/app/actions/tenant_test.go
+++ b/app/actions/tenant_test.go
@@ -2,6 +2,7 @@ package actions_test
import (
"context"
+ "encoding/json"
"testing"
"github.com/getfider/fider/app"
@@ -181,3 +182,22 @@ func TestUpdateTenantSettings_ExistingTenant_WithLogo(t *testing.T) {
ExpectSuccess(result)
Expect(action.Logo.BlobKey).Equals("hello-world.png")
}
+
+func TestUpdateTenantSettings_NullLogo(t *testing.T) {
+ RegisterT(t)
+
+ ctx := context.WithValue(context.Background(), app.TenantCtxKey, &entity.Tenant{
+ ID: 1,
+ LogoBlobKey: "hello-world.png",
+ })
+
+ action := actions.NewUpdateTenantSettings()
+ err := json.Unmarshal([]byte(`{"title": "OK", "locale": "en", "logo": null}`), action)
+ Expect(err).IsNil()
+ Expect(action.Logo).IsNil()
+
+ result := action.Validate(ctx, nil)
+ ExpectSuccess(result)
+ Expect(action.Logo).IsNotNil()
+ Expect(action.Logo.BlobKey).Equals("hello-world.png")
+}
diff --git a/app/handlers/settings_test.go b/app/handlers/settings_test.go
index c4524245d..35db10a57 100644
--- a/app/handlers/settings_test.go
+++ b/app/handlers/settings_test.go
@@ -77,6 +77,47 @@ func TestUpdateUserSettingsHandler_ValidName(t *testing.T) {
Expect(newName).Equals("Jon Stark")
}
+func TestUpdateUserSettingsHandler_NullAvatar(t *testing.T) {
+ RegisterT(t)
+
+ var updateUser *cmd.UpdateCurrentUser
+ bus.AddHandler(func(ctx context.Context, c *cmd.UpdateCurrentUser) error {
+ updateUser = c
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.UpdateCurrentUserSettings) error {
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.UploadImage) error {
+ Expect(c.Image).IsNotNil()
+ return nil
+ })
+
+ server := mock.NewServer()
+ code, _ := server.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ ExecutePost(handlers.UpdateUserSettings(), `{ "name": "Jon Stark", "avatarType": "letter", "avatar": null }`)
+
+ Expect(code).Equals(http.StatusOK)
+ Expect(updateUser.Avatar).IsNotNil()
+ Expect(updateUser.Avatar.BlobKey).Equals(mock.JonSnow.AvatarBlobKey)
+}
+
+func TestUpdateUserSettingsHandler_NullAvatar_CustomAvatarType(t *testing.T) {
+ RegisterT(t)
+
+ server := mock.NewServer()
+ code, _ := server.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ ExecutePost(handlers.UpdateUserSettings(), `{ "name": "Jon Stark", "avatarType": "custom", "avatar": null }`)
+
+ Expect(code).Equals(http.StatusBadRequest)
+}
+
func TestUpdateUserSettingsHandler_NewSettings(t *testing.T) {
RegisterT(t)