fix(typescript/encryption): cap aggregate skippedKeys across DH ratchet steps#2184
Merged
imran-siddique merged 1 commit intoMay 12, 2026
Conversation
…et steps `DoubleRatchet.skipMessages` enforced a per-call cap (`maxSkip`) on the size of a single burst of skipped messages within one receive chain, but the `skippedKeys` map was unbounded across DH ratchet steps. A long- lived session whose peer repeatedly forced fresh DH rotations while leaving small out-of-order tails behind in each chain would accumulate skipped message keys indefinitely — memory grows in O(total skipped across the lifetime of the ratchet) rather than O(per-chain burst). Add a module-level `MAX_SKIPPED_KEYS_TOTAL = 2000` and FIFO-evict the oldest entries from `state.skippedKeys` whenever it exceeds the cap. `Map` iteration order is insertion order in ES2015+, so the first key the iterator returns is the oldest surviving insertion — same pattern the Python core implementation uses (`_MAX_SKIPPED_KEYS_TOTAL: int = 2000`) in `agent-governance-python/agent-mesh/src/agentmesh/encryption/ratchet.py`. Add a regression test that raises `maxSkip` above the global cap, skips a burst large enough to overflow it, and asserts the cached `skippedKeys.size` after delivery is exactly `MAX_SKIPPED_KEYS_TOTAL` (not larger). The full `tests/encryption.test.ts` suite (26 tests) still passes.
🤖 AI Agent: code-reviewer — View detailsTL;DR: 0 blockers, 0 warnings. No issues found. Clean change. |
🤖 AI Agent: security-scanner — View detailsNo security issues found. |
🤖 AI Agent: test-generator — `ratchet.ts`
|
🤖 AI Agent: docs-sync-checker — Docs SyncDocs Sync
|
🤖 AI Agent: breaking-change-detector — API CompatibilityAPI Compatibility
|
|
🟡 Contributor Check: MEDIUM
Automated check by AGT Contributor Check. |
PR Review Summary
Verdict: ✅ Ready for human review |
MohammadHaroonAbuomar
pushed a commit
to MohammadHaroonAbuomar/agt-acs
that referenced
this pull request
Jun 1, 2026
…et steps (microsoft#2184) `DoubleRatchet.skipMessages` enforced a per-call cap (`maxSkip`) on the size of a single burst of skipped messages within one receive chain, but the `skippedKeys` map was unbounded across DH ratchet steps. A long- lived session whose peer repeatedly forced fresh DH rotations while leaving small out-of-order tails behind in each chain would accumulate skipped message keys indefinitely — memory grows in O(total skipped across the lifetime of the ratchet) rather than O(per-chain burst). Add a module-level `MAX_SKIPPED_KEYS_TOTAL = 2000` and FIFO-evict the oldest entries from `state.skippedKeys` whenever it exceeds the cap. `Map` iteration order is insertion order in ES2015+, so the first key the iterator returns is the oldest surviving insertion — same pattern the Python core implementation uses (`_MAX_SKIPPED_KEYS_TOTAL: int = 2000`) in `agent-governance-python/agent-mesh/src/agentmesh/encryption/ratchet.py`. Add a regression test that raises `maxSkip` above the global cap, skips a burst large enough to overflow it, and asserts the cached `skippedKeys.size` after delivery is exactly `MAX_SKIPPED_KEYS_TOTAL` (not larger). The full `tests/encryption.test.ts` suite (26 tests) still passes.
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.
Problem
DoubleRatchet.skipMessagesenforced a per-call cap (maxSkip, defaultMAX_SKIP = 100) on the size of a single burst of skipped messages within one receive chain:But the
skippedKeysmap is not reset on a DH ratchet step (it is intentionally kept so out-of-order messages from old chains can still be decrypted). A long-lived session whose peer repeatedly forces fresh DH rotations while leaving small out-of-order tails behind in each chain accumulates skipped message keys indefinitely — memory grows in O(total skipped across the lifetime of the ratchet) rather than O(per-chain burst).The Python core implementation already pins this:
agent-governance-python/agent-mesh/src/agentmesh/encryption/ratchet.pydefines_MAX_SKIPPED_KEYS_TOTAL: int = 2000and FIFO-evicts the oldest entries. The TypeScript implementation had no equivalent.Fix
Add a module-level
MAX_SKIPPED_KEYS_TOTAL = 2000and FIFO-evict the oldest entries fromstate.skippedKeyswhenever it exceeds the cap.Mapiteration order is insertion order in ES2015+, sos.skippedKeys.keys().next().valuereturns the oldest surviving insertion — the same pattern Python'snext(iter(d))uses. The cap is intentionally kept identical to Python (2000) so the two SDKs have matching memory bounds for the same wire protocol.Test
Added a regression test in
tests/encryption.test.tsthat:maxSkipto 2051 viaDoubleRatchet.fromState, so the per-chain cap does not gate the burst.bRatchet.getState().skippedKeys.size === 2000after delivery (the oldest 50 were evicted FIFO).Recipe:
cd agent-governance-typescript && npx jest tests/encryption.test.ts --no-coverage.Surfaced during independent audit conducted by @finnoybu (Ken Tannenbaum, AEGIS Initiative); [LOW, TypeScript].