fix(loan_manager): cap how far a single extension can push the due date - #1725
Open
N-thnI wants to merge 1 commit into
Open
fix(loan_manager): cap how far a single extension can push the due date#1725N-thnI wants to merge 1 commit into
N-thnI wants to merge 1 commit into
Conversation
extend_loan capped how *many* times a loan could be extended (MAX_EXTENSIONS = 3) but said nothing about how *far*. extra_ledgers was only checked for non-zero and for u32 overflow, so one call with a huge value pushed due_date past any realistic horizon and deferred default indefinitely — for a flat 1% fee charged once on remaining principal. Total extendable time was unbounded. A single extension may now be at most the configured term ceiling, rejected with LoanError::InvalidExtension. Total deferral is consequently bounded by MAX_EXTENSIONS * max_term instead of being open-ended. The bound is derived from configuration, not a constant. New internal helper max_term_ledgers() reads DataKey::MaxTermLedgers, and get_max_term_ledgers() now delegates to it so the ceiling an admin reads back is the same expression as the one enforced, rather than two copies that can drift. The helper falls back to DEFAULT_TERM_LEDGERS when the key is unset, not to u32::MAX. refinance uses a u32::MAX fallback for the same key; copying that here would have left the new bound inert on any deployment whose admin had not configured term limits — precisely the deployments least likely to notice the abuse. refinance's own fallback is left alone as out of scope. Also converts the due-date checked_add from .expect() to a typed error. The issue asked for overflow-safe math with no unwrap/panic, and a panic there would revert the transaction after the borrower's extension fee had already transferred, with no diagnosable cause. The new ceiling makes overflow unreachable in practice; the arithmetic stays checked regardless. Preserved unchanged: the MAX_EXTENSIONS count cap, the non-zero check and its distinct InvalidTerm code, and the 1% fee formula (out of scope). No lower bound was added. It is listed as optional, and rejecting dust extensions would refuse legitimate short ones while preventing no abuse — a 1-ledger extension costs the borrower a full fee and burns one of three slots, so it only ever harms them. 7 new tests: over-long rejected with no state change, exactly-at-ceiling accepted, u32::MAX and u32::MAX-1 rejected (the latter previously reached checked_add), the ceiling tracking set_max_term_ledgers in both directions, three maximal extensions then the count cap refusing a fourth, and the non-zero check still returning InvalidTerm. 137 tests pass; cargo fmt --check and clippy clean (the one clippy warning is pre-existing in events.rs, untouched here).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
extend_loan capped how many times a loan could be extended (MAX_EXTENSIONS = 3) but said nothing about how far. extra_ledgers was only checked for non-zero and for u32 overflow, so one call with a huge value pushed due_date past any realistic horizon and deferred default indefinitely — for a flat 1% fee charged once on remaining principal. Total extendable time was unbounded.
A single extension may now be at most the configured term ceiling, rejected with LoanError::InvalidExtension. Total deferral is consequently bounded by MAX_EXTENSIONS * max_term instead of being open-ended.
The bound is derived from configuration, not a constant. New internal helper max_term_ledgers() reads DataKey::MaxTermLedgers, and get_max_term_ledgers() now delegates to it so the ceiling an admin reads back is the same expression as the one enforced, rather than two copies that can drift.
The helper falls back to DEFAULT_TERM_LEDGERS when the key is unset, not to u32::MAX. refinance uses a u32::MAX fallback for the same key; copying that here would have left the new bound inert on any deployment whose admin had not configured term limits — precisely the deployments least likely to notice the abuse. refinance's own fallback is left alone as out of scope.
Also converts the due-date checked_add from .expect() to a typed error. The issue asked for overflow-safe math with no unwrap/panic, and a panic there would revert the transaction after the borrower's extension fee had already transferred, with no diagnosable cause. The new ceiling makes overflow unreachable in practice; the arithmetic stays checked regardless.
Preserved unchanged: the MAX_EXTENSIONS count cap, the non-zero check and its distinct InvalidTerm code, and the 1% fee formula (out of scope).
No lower bound was added. It is listed as optional, and rejecting dust extensions would refuse legitimate short ones while preventing no abuse — a 1-ledger extension costs the borrower a full fee and burns one of three slots, so it only ever harms them.
7 new tests: over-long rejected with no state change, exactly-at-ceiling accepted, u32::MAX and u32::MAX-1 rejected (the latter previously reached checked_add), the ceiling tracking set_max_term_ledgers in both directions, three maximal extensions then the count cap refusing a fourth, and the non-zero check still returning InvalidTerm.
137 tests pass; cargo fmt --check and clippy clean (the one clippy warning is pre-existing in events.rs, untouched here).
Pull Request Checklist
Please ensure your PR follows these steps, mirroring our
CONTRIBUTING.mdguidelines.CONTRIBUTING.mddocument.closes #1126