Skip to content

MOD-18588 - create new dedicated config fr checking depth in old rdbs - #1657

Merged
TalBarYakar merged 5 commits into
masterfrom
tal.ba/bug/rdb_old_load_depth
Oct 7, 2026
Merged

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

Conversation

@TalBarYakar

@TalBarYakar TalBarYakar commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Note

Medium Risk
Opt-in by default, but enabling it can block RDB startup, replica sync, or RESTORE on deep legacy snapshots; RDB load path behavior changes across all encoding versions when the flag is on.

Overview
Adds an opt-in module setting json-legacy-rdb-depth-limit (default no) that caps JSON nesting at 128 while loading from RDB/RESTORE, to reduce stack-exhaustion risk from pathologically deep snapshots.

When enabled, legacy encver 0 loads track depth in json_rdb_load_inner and fail with the existing recursion-limit error; encvers 2–4 use ijson::decode_with_depth_limit (and depth-limited from_str for text encodings) instead of unbounded decode. ijson is bumped to a git rev that exposes that API.

module.conf documents that over-limit data errors on RESTORE and can fail startup or replica full sync (keys are not skipped). Pytests forge nested RESTORE payloads for encvers 0/2/3/4 to assert reject/accept behavior with the flag on vs off.

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

@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.

Stale Bugbot comment from a previous run.

Comment thread redis_json/src/backward.rs Resolved
@TalBarYakar
TalBarYakar force-pushed the tal.ba/bug/rdb_old_load_depth branch from 5f43d23 to 0f65308 Compare October 5, 2026 08:42

@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 using high effort 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 0f65308. Configure here.

Comment thread redis_json/src/redisjson.rs Resolved
@TalBarYakar
TalBarYakar force-pushed the tal.ba/bug/rdb_old_load_depth branch from 0f65308 to fb039b7 Compare October 5, 2026 12:31
@codecov

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.22%. Comparing base (3ae737d) to head (bb3c682).

Files with missing lines Patch % Lines
redis_json/src/redisjson.rs 42.85% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1657      +/-   ##
==========================================
+ Coverage   88.13%   88.22%   +0.09%     
==========================================
  Files          17       17              
  Lines        6049     6063      +14     
==========================================
+ Hits         5331     5349      +18     
+ Misses        718      714       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TalBarYakar
TalBarYakar force-pushed the tal.ba/bug/rdb_old_load_depth branch from c4aa023 to e4f640a Compare October 7, 2026 08:24
AvivDavid23
AvivDavid23 previously approved these changes Oct 7, 2026
rdb: *mut raw::RedisModuleIO,
encver: c_int,
) -> Option<RedisJSON<ijson::IValue>> {
let limit_depth = backward::legacy_rdb_depth_limit_enabled();

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.

Default no removes depth protection that master already has, so RESTORE can now crash the server.

On master, every RDB/RESTORE path was already bounded:

  • encver 4: ijson::decode ran ciborium's default from_reader (recursion limit 256) and then cbor_to_ivalue, which rejected at MAX_DEPTH.
  • encver 0/2/3: from_str(..., true, ...) kept serde_json's 128 recursion limit.

With this PR and the setting at its default (no):

  • encver 4 calls decode_with_depth_limit(buf, false), which uses from_reader_with_recursion_limit(bytes, usize::MAX) and skips the cbor_to_ivalue check. Recursion is unbounded.
  • encver 0/2/3 call from_str(..., false, ...), which runs disable_recursion_limit() with no serde_stacker, so the parser recurses without bound.

Repro: RESTORE k 0 <encver-4 payload of ~1M nested CBOR 0x81 bytes> (about 1 MB), or an encver 3 payload holding [ repeated 1M times. Either one should overflow the stack and SIGSEGV the process. On master both return an error. testRestoreDepthLimitForAllEncodingVersions only uses depth 300, so it doesn't catch this, and its depth-off-* asserts lock in the weaker behaviour.

The PR summary and the module.conf comment say default compatibility is unchanged and that enabling the setting "protects" against stack exhaustion. In fact, the default disables a protection that already shipped. If the goal is to let old, deeply nested data load, a safer option when the setting is off is a much larger finite bound (for example a few thousand levels, or the stack-safe limit you measure) instead of usize::MAX / disable_recursion_limit(). The flag would then only choose between the strict 128 limit and that larger limit.

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.

This behavior is intentional. The 128-level depth limit for encver 2/3 was introduced unintentionally alongside the JSON.SET depth limit; it was not intended to apply to legacy RDB loading.
Keeping a mandatory limit for encver 4 would create a compatibility problem: a deeply nested document loaded from encver 0/2/3 is subsequently saved as encver 4. If encver 4 enforces the limit independently of the configuration, the same document would fail to load after a restart or RESTORE.
Therefore, the configuration must apply consistently across all supported encoding versions: when enabled, all enforce the depth limit; when disabled, none enforce it.

@AvivDavid23 @gabsow do we all agree ?

Comment thread module.conf Outdated

# Reject RDB JSON documents nested deeper than 128 levels.
# Enabling this protects RESTORE and RDB loading from stack exhaustion, but
# may reject deeply nested legacy data. yes/no, default: no

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.

"May reject" understates what happens at startup. When this is yes, rdb_load returns NULL for an over-deep key. While Redis is loading the RDB at boot (or a replica is loading after full sync), a NULL from a module rdb_load is fatal: Redis logs a short-read/module load error and exits. It does not skip the key. So turning this on with a single deeply nested legacy document in the dataset leaves the server unable to start, and a replica unable to sync. Only RESTORE gets the per-command error that the tests check. Please spell this out in the doc, or handle the startup case differently.

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

@TalBarYakar
TalBarYakar merged commit 0a9d9a6 into master Oct 7, 2026
59 of 62 checks passed
@TalBarYakar
TalBarYakar deleted the tal.ba/bug/rdb_old_load_depth branch October 7, 2026 11:07
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.

3 participants