Browse Source

Merge pull request #1631 from zaenzhang/fix/redis-cache-tenant-scope

fix(caching-management): enforce tenant scope for cache keys
dev
i2nets 3 weeks ago
committed by GitHub
parent
commit
8313dead4b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 26
      aspnet-core/modules/caching-management/LINGYUN.Abp.CachingManagement.StackExchangeRedis/LINGYUN/Abp/CachingManagement/StackExchangeRedis/StackExchangeRedisCacheManager.cs
  2. 2
      aspnet-core/modules/caching-management/LINGYUN.Abp.CachingManagement.StackExchangeRedis/README.EN.md
  3. 2
      aspnet-core/modules/caching-management/LINGYUN.Abp.CachingManagement.StackExchangeRedis/README.md

26
aspnet-core/modules/caching-management/LINGYUN.Abp.CachingManagement.StackExchangeRedis/LINGYUN/Abp/CachingManagement/StackExchangeRedis/StackExchangeRedisCacheManager.cs

@ -72,7 +72,7 @@ public class StackExchangeRedisCacheManager : ICacheManager, ISingletonDependenc
// 缓存键名规则: InstanceName + (t + TenantId)(CurrentTenant.IsAvailable) + CacheItemName + KeyPrefix + Key // 缓存键名规则: InstanceName + (t + TenantId)(CurrentTenant.IsAvailable) + CacheItemName + KeyPrefix + Key
// 缓存键名规则: InstanceName + (c:)(!CurrentTenant.IsAvailable) + CacheItemName + KeyPrefix + Key // 缓存键名规则: InstanceName + (c:)(!CurrentTenant.IsAvailable) + CacheItemName + KeyPrefix + Key
var match = "*"; var match = RedisCacheOptions.InstanceName ?? string.Empty;
// abp* // abp*
if (!RedisCacheOptions.InstanceName.IsNullOrWhiteSpace()) if (!RedisCacheOptions.InstanceName.IsNullOrWhiteSpace())
{ {
@ -82,7 +82,7 @@ public class StackExchangeRedisCacheManager : ICacheManager, ISingletonDependenc
// abp*c:* // abp*c:*
if (CurrentTenant.IsAvailable) if (CurrentTenant.IsAvailable)
{ {
match += "t:" + CurrentTenant.Id.ToString() + "*"; match += "t:" + CurrentTenant.Id.ToString() + ",c:*";
} }
else else
{ {
@ -152,6 +152,7 @@ public class StackExchangeRedisCacheManager : ICacheManager, ISingletonDependenc
public async virtual Task<CacheValueResponse> GetValueAsync(string key, CancellationToken cancellationToken = default) public async virtual Task<CacheValueResponse> GetValueAsync(string key, CancellationToken cancellationToken = default)
{ {
CheckKey(key);
long size = 0; long size = 0;
var values = new Dictionary<string, object>(); var values = new Dictionary<string, object>();
@ -210,6 +211,7 @@ public class StackExchangeRedisCacheManager : ICacheManager, ISingletonDependenc
public async virtual Task SetAsync(SetCacheRequest request, CancellationToken cancellationToken = default) public async virtual Task SetAsync(SetCacheRequest request, CancellationToken cancellationToken = default)
{ {
CheckKey(request.Key);
var cacheKey = request.Key; var cacheKey = request.Key;
if (!RedisCacheOptions.InstanceName.IsNullOrWhiteSpace() && cacheKey.StartsWith(RedisCacheOptions.InstanceName)) if (!RedisCacheOptions.InstanceName.IsNullOrWhiteSpace() && cacheKey.StartsWith(RedisCacheOptions.InstanceName))
{ {
@ -231,6 +233,7 @@ public class StackExchangeRedisCacheManager : ICacheManager, ISingletonDependenc
public async virtual Task RefreshAsync(RefreshCacheRequest request, CancellationToken cancellationToken = default) public async virtual Task RefreshAsync(RefreshCacheRequest request, CancellationToken cancellationToken = default)
{ {
CheckKey(request.Key);
var cacheKey = request.Key; var cacheKey = request.Key;
if (!RedisCacheOptions.InstanceName.IsNullOrWhiteSpace() && cacheKey.StartsWith(RedisCacheOptions.InstanceName)) if (!RedisCacheOptions.InstanceName.IsNullOrWhiteSpace() && cacheKey.StartsWith(RedisCacheOptions.InstanceName))
{ {
@ -259,6 +262,11 @@ public class StackExchangeRedisCacheManager : ICacheManager, ISingletonDependenc
public async virtual Task RemoveAsync(RemoveCacheRequest request, CancellationToken cancellationToken = default) public async virtual Task RemoveAsync(RemoveCacheRequest request, CancellationToken cancellationToken = default)
{ {
foreach (var key in request.Keys)
{
CheckKey(key);
}
var cacheKeys = request.Keys.Select((cacheKey) => var cacheKeys = request.Keys.Select((cacheKey) =>
{ {
if (!RedisCacheOptions.InstanceName.IsNullOrWhiteSpace() && cacheKey.StartsWith(RedisCacheOptions.InstanceName)) if (!RedisCacheOptions.InstanceName.IsNullOrWhiteSpace() && cacheKey.StartsWith(RedisCacheOptions.InstanceName))
@ -270,6 +278,20 @@ public class StackExchangeRedisCacheManager : ICacheManager, ISingletonDependenc
await RedisCache.RemoveManyAsync(cacheKeys, cancellationToken); await RedisCache.RemoveManyAsync(cacheKeys, cancellationToken);
} }
protected virtual void CheckKey(string key)
{
var prefix = RedisCacheOptions.InstanceName ?? string.Empty;
if (CurrentTenant.IsAvailable)
{
prefix += $"t:{CurrentTenant.Id},c:";
}
if (string.IsNullOrEmpty(key) || !key.StartsWith(prefix, StringComparison.Ordinal))
{
throw new Volo.Abp.Authorization.AbpAuthorizationException("Cache key is outside the current scope.");
}
}
protected virtual ValueTask<IDatabase> ConnectAsync(CancellationToken token = default) protected virtual ValueTask<IDatabase> ConnectAsync(CancellationToken token = default)
{ {
return (ValueTask<IDatabase>)ConnectAsyncMethod.Invoke(RedisCache, new object[] { token })!; return (ValueTask<IDatabase>)ConnectAsyncMethod.Invoke(RedisCache, new object[] { token })!;

2
aspnet-core/modules/caching-management/LINGYUN.Abp.CachingManagement.StackExchangeRedis/README.EN.md

@ -51,6 +51,8 @@ The module has built-in multi-tenancy support, automatically isolating cache dat
* Key format with tenant: `{InstanceName}t:{TenantId}:{KeyPrefix}:{Key}` * Key format with tenant: `{InstanceName}t:{TenantId}:{KeyPrefix}:{Key}`
* Key format without tenant: `{InstanceName}c:{KeyPrefix}:{Key}` * Key format without tenant: `{InstanceName}c:{KeyPrefix}:{Key}`
Read, set, refresh and delete operations validate the Redis instance prefix and, for tenant requests, the complete tenant key prefix. Bulk deletion validates every key before deleting any entries. Host administrators can still manage cache entries within the configured instance.
## More ## More
For more information, please refer to the following resources: For more information, please refer to the following resources:

2
aspnet-core/modules/caching-management/LINGYUN.Abp.CachingManagement.StackExchangeRedis/README.md

@ -51,6 +51,8 @@ Configure<AbpDistributedCacheOptions>(options =>
* 有租户时的键格式:`{InstanceName}t:{TenantId}:{KeyPrefix}:{Key}` * 有租户时的键格式:`{InstanceName}t:{TenantId}:{KeyPrefix}:{Key}`
* 无租户时的键格式:`{InstanceName}c:{KeyPrefix}:{Key}` * 无租户时的键格式:`{InstanceName}c:{KeyPrefix}:{Key}`
读取、设置、刷新和删除操作都会校验键的 Redis 实例前缀;租户请求还必须匹配完整的租户键前缀。批量删除先校验全部键,再执行删除。平台管理员仍可管理当前实例范围内的缓存。
## 更多 ## 更多
有关更多信息,请参阅以下资源: 有关更多信息,请参阅以下资源:

Loading…
Cancel
Save