Skip to content

feat: add maxIterations, a read-side cap on the PBKDF2 count - #30

Merged
adelzemzemi merged 1 commit into
mainfrom
feat/read-iteration-cap
Nov 20, 2026
Merged

adelzemzemi merged 1 commit into
mainfrom
feat/read-iteration-cap

Conversation

@adelzemzemi

Copy link
Copy Markdown
Owner

Why

Reported in a comment on the dev.to article: the iteration count in a version 2 header is authenticated, but it has to be used to derive the key before the tag can be checked. Raising it to 5,000,000 on a stored value makes the read fail only after about eight times the default work.

Reproduced on 2.0.0: a failing read goes from 73 ms to about 580 ms on this machine.

What

  • maxIterations on decrypt(), rewrap() and AesGcmEncryptor (instance default, per-call override).
  • A version 2 envelope declaring more is refused with INVALID_ITERATIONS before any key derivation. With the cap at 600,000 the same tampered value is refused in 11 ms (0 ms with allowLegacy: false).
  • Opt-in: the default stays at 5,000,000, so values written with a higher count than the one configured today keep decrypting.
  • An encryptor refuses a cap lower than the count it writes with.
  • Version 0 values are not affected: their count is fixed, and a 1.x value whose salt happens to look like a header above the cap is still read and migrated.
  • README, SECURITY.md and CHANGELOG updated; version set to 2.1.0.

Checks

npm run verify passes locally (coverage thresholds included).

After merge, publishing is a v2.1.0 tag.

The iteration count is read from the envelope and used to derive the key
before the authentication tag can be checked. Whoever can write to the
store could raise it to the 5,000,000 ceiling and make a failing read
cost about eight times the default.

With maxIterations set, a version 2 envelope declaring more is refused
with INVALID_ITERATIONS before any key derivation. The cap is opt-in so
values written with a higher count than the configured one keep reading.

Releases as 2.1.0.
@adelzemzemi adelzemzemi self-assigned this Nov 20, 2026
@adelzemzemi
adelzemzemi merged commit 6420dfa into main Nov 20, 2026
8 checks passed
@adelzemzemi
adelzemzemi deleted the feat/read-iteration-cap branch November 20, 2026 13:47
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.

1 participant