Skip to content

nft live until ledger upper bound - #584

Merged
ozgunozerk merged 1 commit into
mainfrom
nft-live-until-ledger-bound
Feb 24, 2026
Merged

ozgunozerk merged 1 commit into
mainfrom
nft-live-until-ledger-bound

Conversation

@ozgunozerk

@ozgunozerk ozgunozerk commented Feb 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #583

PR Checklist

  • Tests
  • Documentation

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced validation for approval expiration dates to enforce both minimum and maximum bounds, preventing approvals from being assigned to past ledgers or expiring too far in the future.

@ozgunozerk
ozgunozerk requested a review from brozorec February 24, 2026 12:38
@ozgunozerk ozgunozerk self-assigned this Feb 24, 2026
@coderabbitai

coderabbitai Bot commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

The NFT module's approve_for_owner function validation is strengthened to enforce an upper bound check on the live_until_ledger parameter, in addition to the existing lower-bound check. Values exceeding the maximum allowable ledger sequence are now rejected.

Changes

Cohort / File(s) Summary
Live Until Ledger Validation
packages/tokens/src/non_fungible/storage.rs
Added upper bound validation for live_until_ledger parameter in approve_for_owner. Now enforces both lower and upper bounds, panicking if the value falls outside the acceptable range.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰✨ A rabbit bounced through bounds so tight,
Checking ledgers left and right,
No more limits lost in time—
Upper bounds now draw the line! 🎯

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The PR title 'nft live until ledger upper bound' accurately summarizes the main change: adding upper bound validation for the live_until_ledger field in NFT storage functions.
Description check ✅ Passed The PR description follows the template with a linked issue (#583), and both tests and documentation checklist items are marked as complete.
Linked Issues check ✅ Passed The code changes enforce upper bound validation for live_until_ledger in approve_for_owner, directly addressing issue #583's requirement to check the upper bound.
Out of Scope Changes check ✅ Passed All changes in packages/tokens/src/non_fungible/storage.rs are limited to strengthening live_until_ledger validation with upper bound checks, directly aligned with the linked issue #583.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch nft-live-until-ledger-bound

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Feb 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.23%. Comparing base (3bc99d8) to head (95a51db).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #584   +/-   ##
=======================================
  Coverage   96.23%   96.23%           
=======================================
  Files          57       57           
  Lines        5497     5498    +1     
=======================================
+ Hits         5290     5291    +1     
  Misses        207      207           

☔ View full report in Codecov by Sentry.
📢 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
packages/tokens/src/non_fungible/storage.rs (2)

448-458: ⚠️ Potential issue | 🟡 Minor

Doc comment for approve_for_owner is stale after this change.

Two issues:

  1. Lines 448–451: The parameter note still says live_until_ledger is "implicitly bounded … specifying a higher value will cause the code to panic". This is no longer accurate — the function now explicitly validates the upper bound and returns InvalidLiveUntilLedger.
  2. Lines 455–458: The # Errors entry for InvalidLiveUntilLedger only says "less than the current ledger number"; it should also cover the upper-bound case.
📝 Proposed doc update
-    ///   `live_until_ledger` argument is implicitly bounded by the maximum
-    ///   allowed TTL extension for a temporary storage entry and specifying a
-    ///   higher value will cause the code to panic.
+    ///   Must be between the current ledger sequence and `max_live_until_ledger`
+    ///   (inclusive), or `0` to revoke.
     ///
     /// # Errors
     ///
     /// * [`NonFungibleTokenError::InvalidApprover`] - If the owner address is
     ///   not the actual owner of the token.
-    /// * [`NonFungibleTokenError::InvalidLiveUntilLedger`] - If the ledger
-    ///   number is less than the current ledger number.
+    /// * [`NonFungibleTokenError::InvalidLiveUntilLedger`] - If the ledger
+    ///   number is less than the current ledger sequence or greater than
+    ///   `max_live_until_ledger`.

The same "implicitly bounded" language in the approve function doc at lines 303–305 should be updated analogously.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/tokens/src/non_fungible/storage.rs` around lines 448 - 458, Update
the doc comments for approve_for_owner and approve to reflect the new explicit
validation: replace the "implicitly bounded ... will cause the code to panic"
wording with text stating that live_until_ledger is explicitly validated and
values above the allowed TTL will return
NonFungibleTokenError::InvalidLiveUntilLedger; also expand the # Errors entry
for NonFungibleTokenError::InvalidLiveUntilLedger to mention both the case where
live_until_ledger is less than the current ledger and the case where it exceeds
the allowed maximum TTL.

372-378: ⚠️ Potential issue | 🟠 Major

approve_for_all is missing the same upper-bound check added to approve_for_owner.

The same bug pattern from issue #583 still exists here: a caller can pass live_until_ledger = u32::MAX (or any value beyond max_live_until_ledger) and the function will reach the extend_ttl call at line 385 with a massive live_for value, resulting in an SDK-level panic instead of a clean InvalidLiveUntilLedger error. Both approval functions should be consistent.

🐛 Proposed fix for `approve_for_all`
-        if live_until_ledger < current_ledger {
+        if live_until_ledger > e.ledger().max_live_until_ledger()
+            || live_until_ledger < current_ledger
+        {
             panic_with_error!(e, NonFungibleTokenError::InvalidLiveUntilLedger);
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/tokens/src/non_fungible/storage.rs` around lines 372 - 378,
approve_for_all is missing the upper-bound validation present in
approve_for_owner, allowing callers to pass live_until_ledger >
max_live_until_ledger (or u32::MAX) which leads to overflow in extend_ttl; add
the same check used in approve_for_owner to approve_for_all: compute
current_ledger via e.ledger().sequence() and compare live_until_ledger against
max_live_until_ledger (and current_ledger as already done), and if it exceeds
the maximum return the same NonFungibleTokenError::InvalidLiveUntilLedger error
before calling extend_ttl to prevent SDK-level panics.
🧹 Nitpick comments (1)
packages/tokens/src/non_fungible/storage.rs (1)

480-490: e.ledger().sequence() is called twice; cache it like approve_for_all does.

e.ledger().sequence() is called at line 481 (inside the guard condition) and again at line 490 (live_for calculation). The sibling function approve_for_all caches this as current_ledger at line 372 and reuses it at line 384. Matching that pattern keeps the two functions consistent.

♻️ Proposed refactor
+        let current_ledger = e.ledger().sequence();
+
-        if live_until_ledger > e.ledger().max_live_until_ledger()
-            || live_until_ledger < e.ledger().sequence()
+        if live_until_ledger > e.ledger().max_live_until_ledger()
+            || live_until_ledger < current_ledger
         {
             panic_with_error!(e, NonFungibleTokenError::InvalidLiveUntilLedger);
         }

         let approval_data = ApprovalData { approved: approved.clone(), live_until_ledger };

         e.storage().temporary().set(&key, &approval_data);

-        let live_for = live_until_ledger - e.ledger().sequence();
+        let live_for = live_until_ledger - current_ledger;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/tokens/src/non_fungible/storage.rs` around lines 480 - 490, The code
in the approval path repeatedly calls e.ledger().sequence()—cache this value
into a local variable (e.g., current_ledger) like approve_for_all does, and use
that cached variable in both the guard that compares live_until_ledger and in
the live_for calculation; update the guard that currently compares
live_until_ledger < e.ledger().sequence() to use current_ledger and compute
live_for as live_until_ledger - current_ledger, keeping ApprovalData and storage
calls unchanged (refer to approve_for_all for the naming/pattern).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@packages/tokens/src/non_fungible/storage.rs`:
- Around line 448-458: Update the doc comments for approve_for_owner and approve
to reflect the new explicit validation: replace the "implicitly bounded ... will
cause the code to panic" wording with text stating that live_until_ledger is
explicitly validated and values above the allowed TTL will return
NonFungibleTokenError::InvalidLiveUntilLedger; also expand the # Errors entry
for NonFungibleTokenError::InvalidLiveUntilLedger to mention both the case where
live_until_ledger is less than the current ledger and the case where it exceeds
the allowed maximum TTL.
- Around line 372-378: approve_for_all is missing the upper-bound validation
present in approve_for_owner, allowing callers to pass live_until_ledger >
max_live_until_ledger (or u32::MAX) which leads to overflow in extend_ttl; add
the same check used in approve_for_owner to approve_for_all: compute
current_ledger via e.ledger().sequence() and compare live_until_ledger against
max_live_until_ledger (and current_ledger as already done), and if it exceeds
the maximum return the same NonFungibleTokenError::InvalidLiveUntilLedger error
before calling extend_ttl to prevent SDK-level panics.

---

Nitpick comments:
In `@packages/tokens/src/non_fungible/storage.rs`:
- Around line 480-490: The code in the approval path repeatedly calls
e.ledger().sequence()—cache this value into a local variable (e.g.,
current_ledger) like approve_for_all does, and use that cached variable in both
the guard that compares live_until_ledger and in the live_for calculation;
update the guard that currently compares live_until_ledger <
e.ledger().sequence() to use current_ledger and compute live_for as
live_until_ledger - current_ledger, keeping ApprovalData and storage calls
unchanged (refer to approve_for_all for the naming/pattern).

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3bc99d8 and 95a51db.

📒 Files selected for processing (1)
  • packages/tokens/src/non_fungible/storage.rs

@ozgunozerk
ozgunozerk merged commit 58380d1 into main Feb 24, 2026
6 checks passed
@ozgunozerk
ozgunozerk deleted the nft-live-until-ledger-bound branch February 24, 2026 13:51
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.

🐞 [Bug]: In NFT module's approve_for_owner storage function, the upper bound of live until ledger is not checked

2 participants