Skip to content

MOD-14902 - existence check to prevent Timeseries crash when Redis is not aligned with new redis-core changes - #1940

Merged
TalBarYakar merged 5 commits into
masterfrom
tal.ba/bug/ctx_user_api_exist_check
Apr 20, 2026
Merged

TalBarYakar merged 5 commits into
masterfrom
tal.ba/bug/ctx_user_api_exist_check

Conversation

@TalBarYakar

@TalBarYakar TalBarYakar commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor

This PR introduces a validation step to ensure the new API is available before we attempt to use it for retrieving the user context.


Note

Medium Risk
Touches ACL enforcement paths by conditionally skipping context-user setup/lookup when Redis core lacks the required APIs, which could change permission-check behavior on older cores (though it primarily prevents crashes).

Overview
Prevents crashes on Redis versions that don’t expose the newer context-user APIs by adding runtime feature detection and null-guards before calling RedisModule_SetContextUser/RedisModule_GetContextUser and related functions.

ApplyCtxUser/ReleaseCtxUser now no-op when the API set is unavailable, and CheckKeyIsAllowedByAcls safely handles missing RedisModule_GetContextUser instead of unconditionally calling it.

Reviewed by Cursor Bugbot for commit c802e36. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread src/libmr_integration.c Resolved
@TalBarYakar TalBarYakar changed the title Add existence verification before retrieving user context MOD-14902 - existence check to prevent Timeseries crash when Redis is not aligned with new redis-core changes Apr 15, 2026
Comment thread src/libmr_integration.c
@@ -439,25 +439,30 @@ Record *ListWithSeriesLastDatapoint(const Series *series, bool latest, bool resp
// Set the context user for ACL checks. Skips allocation if the context
// already has the same user set, to avoid redundant alloc+free cycles.
static void ApplyCtxUser(RedisModuleCtx *ctx, RedisModuleString *userName) {
if (!RedisModule_SetContextUser)
return;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor is kind of right here. For symmetry it is best to always check for the existence of both the set and the get.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was checked inside the function, but I think it’s better to do this at the beginning so the code stays clean
/* All module APIs used below must be non-NULL (Set+GetContextUser pair with ReleaseCtxUser). */ if (!RedisModule_SetContextUser || !RedisModule_GetContextUser || !RedisModule_GetModuleUserFromUserName || !RedisModule_GetUserUsername) return;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, but note what cursor said about the different checks in different places.
You should do something similar in the release function.
Probably best to #define API_USER_CONTEXT_SUPPORTED (...) and use it in both places (and maybe in other places where we require any of the user-context APIs).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread src/libmr_integration.c Outdated
return; // Same user already set, nothing to do
if (RedisModule_GetContextUser) {
const RedisModuleUser *currentUser = RedisModule_GetContextUser(ctx);
if (currentUser) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the else (i.e., when there is no current user yet for the ctx)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If currentUser was NULL, the context had no user yet: there is nothing to free
or compare.
we fall through and attach userName below.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something doesn't make sense here: if RedisModule_GetContextUser() returns NULL what does it mean? Does it mean the userName is "default"? If so, then we should treat the default user name differently (meaning: no need for the RedisModule_SetContextUser(), for example). What am I missing here?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also (kinda related): isn't RedisModule_FreeModuleUser(NULL) risky?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, it returns the user pointer associated with the context.
If it’s null — which is likely at the moment — it simply means it hasn’t been set yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can't be NULL since we are calling it if and only if currentUser is not null

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, it returns the user pointer associated with the context. If it’s null — which is likely at the moment — it simply means it hasn’t been set yet.

I still don't understand.
If we start with RedisModule_GetContextUser() being NULL and the client is not an ACL user (i.e., no AUTH <user> <password> was called) - what would be the userName parameter that's passed to ApplyCtxUser()?
Will it be the string "default"? If so - you should treat it the same as a NULL context user. Meaning: in this case you should not set the context user to be the user with name "default" but rather keep it as is (and if it were already an ACL user you should free the module user, I guess).

Please do not rely on the llm's assumptions about the code. Check all those cases explicitly:

  • moving from default user to default user (should do nothing; no free nor set context user)
  • moving from default to an actual acl user (should only set because there is nothing to free)
  • moving from an actual acl user to default user (should probably only free?)
  • moving from an actual acl user A to the same user A (should do nothing)
  • moving from actual acl user A to actual acl user B (should do the whole shenanigan)

Comment thread src/libmr_integration.c Resolved

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b94dee3297ef69372142d3b831add0e406671cc1. Configure here.

Comment thread src/libmr_integration.c
* set. */
#define API_USER_CONTEXT_SUPPORTED \
(RedisModule_SetContextUser && RedisModule_GetContextUser && \
RedisModule_GetModuleUserFromUserName && RedisModule_GetUserUsername)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API availability macro not shared across files

Low Severity

The API_USER_CONTEXT_SUPPORTED macro is defined locally in libmr_integration.c, while module.h uses a separate ad-hoc inline check (RedisModule_GetContextUser ? ... : NULL). The reviewer explicitly requested a shared #define usable in both locations. Defining the macro in a shared header (e.g., common.h) would ensure consistent API availability checks and reduce the risk of divergent guards as the codebase evolves.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b94dee3297ef69372142d3b831add0e406671cc1. Configure here.

@TalBarYakar
TalBarYakar force-pushed the tal.ba/bug/ctx_user_api_exist_check branch from b94dee3 to c802e36 Compare April 19, 2026 19:03
@sonarqubecloud

Copy link
Copy Markdown

@TalBarYakar
TalBarYakar merged commit baaf423 into master Apr 20, 2026
105 of 109 checks passed
@TalBarYakar
TalBarYakar deleted the tal.ba/bug/ctx_user_api_exist_check branch April 20, 2026 12:16
TalBarYakar added a commit that referenced this pull request May 13, 2026
… not aligned with new redis-core changes (#1940)

* check if the api is exist beofre using it

* format fix

* fix remarks

* use define for the api validation

* fix formmating
TalBarYakar added a commit that referenced this pull request May 13, 2026
#1985)

* MOD-15262 - Align TimeSeries with the RedisModule_GetUserUserName API changes (#1975)

* udate getUserUserName api

* formatting

* remove free

* align redismodules sdk master

* revert free remove

* MOD-14902 - existence check to prevent Timeseries crash when Redis is not aligned with new redis-core changes (#1940)

* check if the api is exist beofre using it

* format fix

* fix remarks

* use define for the api validation

* fix formmating

* change redis-ref to 8.8

* 8.7.91

* change the docker files as well

* change all missing file to point to 8.8
YaacovHazan pushed a commit to redis/redis that referenced this pull request Jun 14, 2026
## Summary

Bumps the pinned `MODULE_VERSION` for **RedisTimeSeries** from `v8.8.0`
to `v8.9.80`. RedisBloom and RedisJSON are unchanged.

| Module          | From    | To       |
|-----------------|---------|----------|
| RedisTimeSeries | v8.8.0  | v8.9.80  |

> **Note:** `v8.9.80` is the **8.10 LTS branch cut** (targets Redis
8.10) and has diverged from `v8.8.0`. The `v8.8.0...v8.9.80` ancestry
diff contains 37 commits, but **7 are backports already shipped in
`v8.8.0`** (verified with `git cherry`, see below); the **30 genuinely
new** changes are listed here.

## Module changes ([v8.8.0 →
v8.9.80](RedisTimeSeries/RedisTimeSeries@v8.8.0...v8.9.80))

- MOD-14124 — m commands acl
([#1913](RedisTimeSeries/RedisTimeSeries#1913))
- MOD-14250 — missing artifacts logs fix
([#1932](RedisTimeSeries/RedisTimeSeries#1932))
- MOD-14289 — Increase LibMR execution timeout for Valgrind builds
([#1933](RedisTimeSeries/RedisTimeSeries#1933))
- MOD-12726 — unique snapshot name + new output params for nighly event
([#1937](RedisTimeSeries/RedisTimeSeries#1937))
- MOD-14674 — oss refresh list
([#1935](RedisTimeSeries/RedisTimeSeries#1935))
- nightly build, upload snapshot artifact
([#1954](RedisTimeSeries/RedisTimeSeries#1954))
- MOD-14902 — existence check to prevent Timeseries crash when Redis is
not aligned with new redis-core changes
([#1940](RedisTimeSeries/RedisTimeSeries#1940))
- MOD-14850 — Bump libmr
([#1962](RedisTimeSeries/RedisTimeSeries#1962))
- MOD-14715 — add apt retries/timeouts to Debian/Ubuntu Dockerfiles
([#1965](RedisTimeSeries/RedisTimeSeries#1965))
- MOD-15262 — Align TimeSeries with the RedisModule_GetUserUserName API
changes
([#1975](RedisTimeSeries/RedisTimeSeries#1975))
- RED-180951 RED-180027 — fixing bugs and improving the code
([#2004](RedisTimeSeries/RedisTimeSeries#2004))
- MOD-14239 — Ignore known init-rdbchannel migration timeout under
sanitizer
([#1979](RedisTimeSeries/RedisTimeSeries#1979))
- MOD-15105 — make one generic getUser function
([#1981](RedisTimeSeries/RedisTimeSeries#1981))
- MOD-15741 MOD-14091 — add dont-cache command tip to non-cacheable read
commands
([#2031](RedisTimeSeries/RedisTimeSeries#2031))
- MOD-15731 — modules modernization alignment
([#2027](RedisTimeSeries/RedisTimeSeries#2027))
- MOD-15728 — Tal.ba/feat/blocking get
([#2028](RedisTimeSeries/RedisTimeSeries#2028))
- MOD-8187 — fix the aggregation bugs (fix reverse iterator)
([#2036](RedisTimeSeries/RedisTimeSeries#2036))
- MOD-15749 — Link to new version of LibMR with short-form CLUSTERSET
and add tests
([#2034](RedisTimeSeries/RedisTimeSeries#2034))
- Fix REDISMODULE_MAIN typo + drop common.h forward-decl workarounds
([#2044](RedisTimeSeries/RedisTimeSeries#2044))
- CI: switch flow-linux arm64 runner to standard ubuntu-24.04-arm
([#2048](RedisTimeSeries/RedisTimeSeries#2048))
- MOD-15999 — Centralize Redis ref into a single source-of-truth file
for CI
([#2045](RedisTimeSeries/RedisTimeSeries#2045))
- CI: trigger OSS cluster (multi-shard) benchmarks on demand
([#2050](RedisTimeSeries/RedisTimeSeries#2050))
- MOD-15899 — oom try calloc
([#2051](RedisTimeSeries/RedisTimeSeries#2051))
- MOD-8187 — fix non twa aggregation
([#2053](RedisTimeSeries/RedisTimeSeries#2053))
- MOD-16224 — Fix filter-blind neighbor lookups in EMPTY aggregation
([#2056](RedisTimeSeries/RedisTimeSeries#2056))
- MOD-15893 — Nrange new command
([#2052](RedisTimeSeries/RedisTimeSeries#2052))
- MOD-16160 — update blocking get syntax
([#2054](RedisTimeSeries/RedisTimeSeries#2054))
- MOD-16160 — Fix client-side cache tips for TS.BGET and
TS.NRANGE/TS.NREVRANGE
([#2057](RedisTimeSeries/RedisTimeSeries#2057))
- Cut 8.10 LTS branch: bump version to 8.9.80, target Redis 8.10
([27484f7](RedisTimeSeries/RedisTimeSeries@27484f7))
- Fix event-nightly CI on 8.10: read Redis ref from dedicated redis_ref
field
([#2058](RedisTimeSeries/RedisTimeSeries#2058))

<details>
<summary>7 commits excluded — already shipped in v8.8.0 as
backports</summary>

These show up in the `v8.8.0...v8.9.80` ancestry diff with different
SHAs, but `git cherry` confirms their patch content is already in
`v8.8.0`: they were backported to the 8.8 release branch (the "in 8.8
via" PR below), so they are **not** new in this bump.

- MOD-9320 — Multiple aggregators single command
([#1916](RedisTimeSeries/RedisTimeSeries#1916))
→ in 8.8 via
[#1921](RedisTimeSeries/RedisTimeSeries#1921)
- MOD-9320 — rename AGGREGATION token in m commands
([#1926](RedisTimeSeries/RedisTimeSeries#1926))
→ in 8.8 via
[#1927](RedisTimeSeries/RedisTimeSeries#1927)
- MOD-13142 — Allow changing number of threads while pool hasn't started
([#1928](RedisTimeSeries/RedisTimeSeries#1928))
→ in 8.8 via
[#1934](RedisTimeSeries/RedisTimeSeries#1934)
- MOD-14439 — Detect cluster topology changes during a multi-shard
command
([#1930](RedisTimeSeries/RedisTimeSeries#1930))
→ in 8.8 under the same PR
- ci: skip Event CI when PR only changes src/version.h
([#1984](RedisTimeSeries/RedisTimeSeries#1984))
→ in 8.8 via
[#1986](RedisTimeSeries/RedisTimeSeries#1986)
- MOD-15020 — remove bionic os support
([#1983](RedisTimeSeries/RedisTimeSeries#1983))
→ in 8.8 via
[#1996](RedisTimeSeries/RedisTimeSeries#1996)
- MOD-14420 — fix count reducers return wrong NaN
([#2013](RedisTimeSeries/RedisTimeSeries#2013))
→ in 8.8 via
[#2016](RedisTimeSeries/RedisTimeSeries#2016)

</details>

## Test plan
- [ ] CI passes for RedisTimeSeries at the new pinned version
- [ ] `make all` builds RedisTimeSeries cleanly against `unstable`
- [ ] Module tests run under `make test`

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants