Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix(security): scope invite token verification to tenant

Matt Robertsce4f44bb1055
diff --git a/app/services/sqlstore/postgres/tenant.go b/app/services/sqlstore/postgres/tenant.go
index 8093a6531..0051275b3 100644
--- a/app/services/sqlstore/postgres/tenant.go
+++ b/app/services/sqlstore/postgres/tenant.go
@@ -130,8 +130,8 @@ func getVerificationByKey(ctx context.Context, q *query.GetVerificationByKey) er
return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
verification := dbEntities.EmailVerification{}
- query := "SELECT id, email, name, key, code, created_at, verified_at, expires_at, kind, user_id, attempts FROM email_verifications WHERE key = $1 AND kind = $2 LIMIT 1"
- err := trx.Get(&verification, query, q.Key, q.Kind)
+ query := "SELECT id, email, name, key, code, created_at, verified_at, expires_at, kind, user_id, attempts FROM email_verifications WHERE key = $1 AND kind = $2 AND tenant_id = $3 LIMIT 1"
+ err := trx.Get(&verification, query, q.Key, q.Kind, tenant.ID)
if err != nil {
return errors.Wrap(err, "failed to get email verification by its key")
}
diff --git a/app/services/sqlstore/postgres/tenant_test.go b/app/services/sqlstore/postgres/tenant_test.go
index 59872bcb0..96506236e 100644
--- a/app/services/sqlstore/postgres/tenant_test.go
+++ b/app/services/sqlstore/postgres/tenant_test.go
@@ -316,6 +316,34 @@ func TestTenantStorage_SaveFindSet_ChangeEmailVerificationKey(t *testing.T) {
Expect(getKey.Result.ExpiresAt).TemporarilySimilar(getKey.Result.CreatedAt.Add(15*time.Minute), 1*time.Second)
}
+func TestTenantStorage_VerificationKey_IsTenantScoped(t *testing.T) {
+ SetupDatabaseTest(t)
+ defer TeardownDatabaseTest()
+
+ //Save new Key on the demo tenant
+ err := bus.Dispatch(demoTenantCtx, &cmd.SaveVerificationKey{
+ Key: "s3cr3tk3y",
+ Duration: 15 * time.Minute,
+ Request: &actions.CreateTenant{
+ Email: "[email protected]",
+ Name: "Jon Snow",
+ },
+ })
+ Expect(err).IsNil()
+
+ //Same key must NOT be retrievable from a different tenant
+ getKeyFromOtherTenant := &query.GetVerificationByKey{Kind: enum.EmailVerificationKindSignUp, Key: "s3cr3tk3y"}
+ err = bus.Dispatch(avengersTenantCtx, getKeyFromOtherTenant)
+ Expect(errors.Cause(err)).Equals(app.ErrNotFound)
+ Expect(getKeyFromOtherTenant.Result).IsNil()
+
+ //But it is retrievable from the tenant it belongs to
+ getKey := &query.GetVerificationByKey{Kind: enum.EmailVerificationKindSignUp, Key: "s3cr3tk3y"}
+ err = bus.Dispatch(demoTenantCtx, getKey)
+ Expect(err).IsNil()
+ Expect(getKey.Result.Email).Equals("[email protected]")
+}
+
func TestTenantStorage_FindUnknownVerificationKey(t *testing.T) {
SetupDatabaseTest(t)
defer TeardownDatabaseTest()