I’m working on a tenant application and I was wondering how I can block tenant access other tenant data.
First, let me expose some facts:
- The app is not free, 100% for sure the malicious user is a client.
- All the primary keys/identity are integers (GUID solves this problem but we can't change right now).
- The app use shared database and shared schema.
- All the tenants are business group which own several shops.
- I'm using Forgery...
I have some remote data chosen by dropdown and its easy change the id's and access data from other tenants, if you have a little knowledge you can f*ck other tenants data.
The first thing I think was check every remote field but this is kind annoying...
So I built a solution compatible with Code First Migrations using Model Convention and Composite Keys, few tested, working as expected.
Here's the solution:
Convention Class
public class TenantSharedDatabaseSharedSchemaConvention : Convention where T : class
{
public Expression> PrimaryKey { get; private set; }
public Expression> TenantKey { get; private set; }
public TenantSharedDatabaseSharedSchemaConvention(Expression> primaryKey, Expression> tenantKey)
{
this.PrimaryKey = primaryKey;
this.TenantKey = tenantKey;
base.Types().Configure(m =>
{
var indexName = string.Format("IX_{0}_{1}", "Id", "CompanyId");
m.Property(this.PrimaryKey).IsKey().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnOrder(0).HasColumnAnnotation("Index", new IndexAnnotation(new[] {
new IndexAttribute(indexName, 0) { IsUnique = true }
}));
m.Property(this.TenantKey).IsKey().HasDatabaseGeneratedOption(DatabaseGeneratedOption.None).HasColumnOrder(1).HasColumnAnnotation("Index", new IndexAnnotation(new[] {
new IndexAttribute(indexName, 1) { IsUnique = true }
}));
});
}
}
Convention Registration:
** On convention register I pass two properties, first the primary key and second is the tenant id.
modelBuilder.Conventions.Add(new TenantSharedDatabaseSharedSchemaConvention(m => m.Id, m => m.CompanyId));
Base Entity Model
public class BaseEntity
{
public int Id { get; set; }
public int CompanyId { get; set; }
public Company Company { get; set; }
}
Order Entity (Example)
** Here I reference the currency and client with company and all works as expected...
public class Order : BaseEntity
{
[Required]
public int CurrencyId { get; set; }
[ForeignKey("CompanyId, CurrencyId")]
public virtual Currency Currency { get; set; }
[Required]
public int ClientId { get; set; }
[ForeignKey("CompanyId, ClientId")]
public virtual Client Client { get; set; }
public string Description { get; set; }
}
- Is there any impact on performance?
- Is there any disadvantage compared to check every remote field?
- Someone have the same idea and/or problem and came with another solution?