Open platform to collect and prioritize feedback
fix(security): restrict custom invite copy to pro tenants
Matt Robertsdbb0c69bfcdc
diff --git a/app/actions/invite.go b/app/actions/invite.goindex 41d5dcee0..2110ee86d 100644--- a/app/actions/invite.go+++ b/app/actions/invite.go@@ -11,6 +11,7 @@ import ("github.com/getfider/fider/app/pkg/bus""github.com/getfider/fider/app"+ "github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/errors""github.com/getfider/fider/app/pkg/validate")@@ -24,6 +25,19 @@ type InviteUsers struct {Invitations []*UserInvitation `json:"-"`}+// DefaultInviteSubject returns the default invite subject for the given tenant.+// Mirrors the placeholder shown in the admin UI so non-pro tenants get a sensible default.+func DefaultInviteSubject(tenant *entity.Tenant) string {+ return fmt.Sprintf("[%s] We would like to hear from you!", tenant.Name)+}++// DefaultInviteMessage returns the default invite message for the given tenant and inviter.+// Mirrors the placeholder shown in the admin UI so non-pro tenants get a sensible default.+func DefaultInviteMessage(tenant *entity.Tenant, inviter *entity.User) string {+ return fmt.Sprintf("Hi,\n\nWe are inviting you to join the %s feedback site, a place where you can vote, discuss and share your ideas and thoughts on how to improve our services!\n\nClick the link below to join!\n\n%s\n\nRegards,\n%s (%s)",+ tenant.Name, app.InvitePlaceholder, inviter.Name, tenant.Name)+}+// IsAuthorized returns true if current user is authorized to perform this actionfunc (action *InviteUsers) IsAuthorized(ctx context.Context, user *entity.User) bool {return user != nil && user.IsCollaborator()@@ -33,6 +47,16 @@ func (action *InviteUsers) IsAuthorized(ctx context.Context, user *entity.User)func (action *InviteUsers) Validate(ctx context.Context, user *entity.User) *validate.Result {result := validate.Success()+ // On hosted Fider, only pro tenants may customize the invite copy. Free tenants+ // are forced onto safe defaults to prevent abuse via phishing-style copy.+ // Self-hosted (billing disabled) is unaffected.+ if env.IsBillingEnabled() {+ if tenant, ok := ctx.Value(app.TenantCtxKey).(*entity.Tenant); ok && tenant != nil && !tenant.IsPro {+ action.Subject = DefaultInviteSubject(tenant)+ action.Message = DefaultInviteMessage(tenant, user)+ }+ }+if action.Subject == "" {result.AddFieldFailure("subject", "Subject is required.")} else if len(action.Subject) > 70 {diff --git a/app/actions/invite_test.go b/app/actions/invite_test.goindex 3788de5ee..b6f707415 100644--- a/app/actions/invite_test.go+++ b/app/actions/invite_test.go@@ -11,8 +11,25 @@ import ("github.com/getfider/fider/app/models/query". "github.com/getfider/fider/app/pkg/assert""github.com/getfider/fider/app/pkg/bus"+ "github.com/getfider/fider/app/pkg/env")+func withBillingEnabled(t *testing.T) {+ t.Helper()+ originalKey := env.Config.Stripe.SecretKey+ originalMode := env.Config.HostMode+ env.Config.Stripe.SecretKey = "sk_test"+ env.Config.HostMode = "multi"+ t.Cleanup(func() {+ env.Config.Stripe.SecretKey = originalKey+ env.Config.HostMode = originalMode+ })+}++func ctxWithTenant(tenant *entity.Tenant) context.Context {+ return context.WithValue(context.Background(), app.TenantCtxKey, tenant)+}+func TestInviteUsers_Empty(t *testing.T) {RegisterT(t)@@ -172,3 +189,89 @@ func TestInviteUsers_SampleInvite_IgnoreRecipients(t *testing.T) {ExpectSuccess(action.Validate(context.Background(), nil))}++func TestInviteUsers_NonProTenant_OverridesCopy(t *testing.T) {+ RegisterT(t)+ withBillingEnabled(t)++ bus.AddHandler(func(ctx context.Context, q *query.GetUserByEmail) error {+ return app.ErrNotFound+ })++ tenant := &entity.Tenant{Name: "Acme", IsPro: false}+ inviter := &entity.User{Name: "Jon Snow"}+ action := &actions.InviteUsers{+ Subject: "Free Vinted vouchers!! Click here",+ Message: "Tap %invite% to claim your prize",+ Recipients: []string{"[email protected]"},+ }++ ExpectSuccess(action.Validate(ctxWithTenant(tenant), inviter))++ Expect(action.Subject).Equals(actions.DefaultInviteSubject(tenant))+ Expect(action.Message).Equals(actions.DefaultInviteMessage(tenant, inviter))+}++func TestInviteUsers_ProTenant_KeepsCustomCopy(t *testing.T) {+ RegisterT(t)+ withBillingEnabled(t)++ bus.AddHandler(func(ctx context.Context, q *query.GetUserByEmail) error {+ return app.ErrNotFound+ })++ tenant := &entity.Tenant{Name: "Acme", IsPro: true}+ inviter := &entity.User{Name: "Jon Snow"}+ customSubject := "Share your feedback."+ customMessage := "Use this link to join our community: %invite%"+ action := &actions.InviteUsers{+ Subject: customSubject,+ Message: customMessage,+ Recipients: []string{"[email protected]"},+ }++ ExpectSuccess(action.Validate(ctxWithTenant(tenant), inviter))++ Expect(action.Subject).Equals(customSubject)+ Expect(action.Message).Equals(customMessage)+}++func TestInviteUsers_BillingDisabled_KeepsCustomCopy(t *testing.T) {+ RegisterT(t)+ // Billing disabled: self-hosted Fider, no Stripe key configured.+ originalKey := env.Config.Stripe.SecretKey+ env.Config.Stripe.SecretKey = ""+ t.Cleanup(func() { env.Config.Stripe.SecretKey = originalKey })++ bus.AddHandler(func(ctx context.Context, q *query.GetUserByEmail) error {+ return app.ErrNotFound+ })++ tenant := &entity.Tenant{Name: "Acme", IsPro: false}+ inviter := &entity.User{Name: "Jon Snow"}+ customSubject := "Share your feedback."+ customMessage := "Use this link to join our community: %invite%"+ action := &actions.InviteUsers{+ Subject: customSubject,+ Message: customMessage,+ Recipients: []string{"[email protected]"},+ }++ ExpectSuccess(action.Validate(ctxWithTenant(tenant), inviter))++ Expect(action.Subject).Equals(customSubject)+ Expect(action.Message).Equals(customMessage)+}++func TestInviteUsers_DefaultCopyPassesValidation(t *testing.T) {+ RegisterT(t)++ tenant := &entity.Tenant{Name: "Acme"}+ inviter := &entity.User{Name: "Jon Snow"}++ subject := actions.DefaultInviteSubject(tenant)+ message := actions.DefaultInviteMessage(tenant, inviter)++ Expect(len(subject) <= 70).IsTrue()+ Expect(message).ContainsSubstring(app.InvitePlaceholder)+}diff --git a/public/pages/Administration/pages/Invitations.page.tsx b/public/pages/Administration/pages/Invitations.page.tsxindex a6d19fcd1..f710b01dc 100644--- a/public/pages/Administration/pages/Invitations.page.tsx+++ b/public/pages/Administration/pages/Invitations.page.tsx@@ -13,6 +13,22 @@ interface InvitationsPageState {error?: Failure}+const defaultSubject = () => `[${Fider.session.tenant.name}] We would like to hear from you!`++const defaultMessage = () => `Hi,++We are inviting you to join the ${Fider.session.tenant.name} feedback site, a place where you can vote, discuss and share your ideas and thoughts on how to improve our services!++Click the link below to join!++%invite%++Regards,+${Fider.session.user.name} (${Fider.session.tenant.name})`++// On hosted Fider, only pro tenants may customize the invite copy.+const canCustomizeCopy = () => !Fider.settings.isBillingEnabled || Fider.session.tenant.isPro+export default class InvitationsPage extends AdminBasePage<any, InvitationsPageState> {public id = "p-admin-invitations"public name = "invitations"@@ -23,17 +39,8 @@ export default class InvitationsPage extends AdminBasePage<any, InvitationsPageSsuper(props)this.state = {- subject: `[${Fider.session.tenant.name}] We would like to hear from you!`,- message: `Hi,--We are inviting you to join the ${Fider.session.tenant.name} feedback site, a place where you can vote, discuss and share your ideas and thoughts on how to improve our services!--Click the link below to join!--%invite%--Regards,-${Fider.session.user.name} (${Fider.session.tenant.name})`,+ subject: defaultSubject(),+ message: defaultMessage(),recipients: [],numOfRecipients: 0,rawRecipients: "",@@ -95,21 +102,31 @@ ${Fider.session.user.name} (${Fider.session.tenant.name})`,</div></TextArea>- <Input field="subject" label="Subject" value={this.state.subject} maxLength={70} onChange={this.setSubject}>- <p className="text-muted">This is the subject that will be used on the invitation email. Keep it short and sweet.</p>- </Input>-- <TextArea field="message" label="Message" minRows={8} value={this.state.message} onChange={this.setMessage}>- <div className="text-muted">- <p>- This is the content of the invite. Be polite and explain what this invite is for, otherwise there's a high change people will ignore your- message.- </p>- <p>- You're allowed to write whatever you want as long as you include the invitation link placeholder named <strong>%invite%</strong>.+ {canCustomizeCopy() ? (+ <>+ <Input field="subject" label="Subject" value={this.state.subject} maxLength={70} onChange={this.setSubject}>+ <p className="text-muted">This is the subject that will be used on the invitation email. Keep it short and sweet.</p>+ </Input>++ <TextArea field="message" label="Message" minRows={8} value={this.state.message} onChange={this.setMessage}>+ <div className="text-muted">+ <p>+ This is the content of the invite. Be polite and explain what this invite is for, otherwise there's a high change people will ignore your+ message.+ </p>+ <p>+ You're allowed to write whatever you want as long as you include the invitation link placeholder named <strong>%invite%</strong>.+ </p>+ </div>+ </TextArea>+ </>+ ) : (+ <Field label="Invite copy">+ <p className="text-muted">+ Recipients will receive the default invite subject and message. <a href="/admin/billing">Upgrade to Pro</a> to customize the subject and message.</p>- </div>- </TextArea>+ </Field>+ )}<Field label="Sample Invite">{Fider.session.user.email ? (