Fix crash in RM_GetCurrentUserName() when the user isn't accessible (#13619)

The crash happens whenever the user isn't accessible, for example, it
isn't set for the context (when it is temporary) or in some other cases
like `notifyKeyspaceEvent`. To properly check for the ACL compliance, we
need to get the user name and the user to invoke other APIs. However, it
is not possible if it crashes, and it is impossible to work that around
in the code since we don't know (and **shouldn't know**!) when it is
available and when it is not.
This commit is contained in:
Shockingly Good 2024-10-28 14:26:29 +01:00 committed by GitHub
parent 0a8e546957
commit 611c950293
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 0 deletions

View File

@ -9678,6 +9678,12 @@ RedisModuleString *RM_GetModuleUserACLString(RedisModuleUser *user) {
* The returned string must be released with RedisModule_FreeString() or by * The returned string must be released with RedisModule_FreeString() or by
* enabling automatic memory management. */ * enabling automatic memory management. */
RedisModuleString *RM_GetCurrentUserName(RedisModuleCtx *ctx) { RedisModuleString *RM_GetCurrentUserName(RedisModuleCtx *ctx) {
/* Sometimes, the user isn't passed along the call stack or isn't
* even set, so we need to check for the members to avoid crashes. */
if (ctx->client == NULL || ctx->client->user == NULL || ctx->client->user->name == NULL) {
return NULL;
}
return RM_CreateString(ctx,ctx->client->user->name,sdslen(ctx->client->user->name)); return RM_CreateString(ctx,ctx->client->user->name,sdslen(ctx->client->user->name));
} }