fix(transport): range-check the manifest KDF iteration count before deriving the key (TRN-10, WP2 task 2.7)

BundleSecretEncryptor.Decrypt now refuses an iteration count outside
[MinPbkdf2Iterations, MaxPbkdf2Iterations] before PBKDF2 runs. The manifest
JSON path was already guarded by the EncryptionMetadata constructor (pinned by
a new test), but a record 'with' (or any non-ctor path) bypassed it and
derived with int.MaxValue iterations; the encryptor no longer relies on it.
This commit is contained in:
Joseph Doherty
2026-12-03 09:43:00 -05:00
parent f42c17d2ce
commit c207809ef3
2 changed files with 55 additions and 0 deletions
@@ -83,6 +83,19 @@ public sealed class BundleSecretEncryptor
throw new CryptographicException("Unsupported bundle encryption parameters.");
}
// TRN-10: the iteration count comes from the uploaded (untrusted) manifest.
// EncryptionMetadata's constructor range-checks it, but a record `with`
// expression or any other non-constructor path bypasses that, so re-check
// here — before PBKDF2 runs — or a hostile bundle declaring int.MaxValue
// pins a thread (and the single-flight transport gate) for tens of minutes.
if (metadata.Iterations < EncryptionMetadata.MinPbkdf2Iterations
|| metadata.Iterations > EncryptionMetadata.MaxPbkdf2Iterations)
{
throw new CryptographicException(
$"Bundle key-derivation iteration count {metadata.Iterations} is outside the supported range " +
$"[{EncryptionMetadata.MinPbkdf2Iterations}, {EncryptionMetadata.MaxPbkdf2Iterations}].");
}
var salt = Convert.FromBase64String(metadata.SaltB64);
var nonce = Convert.FromBase64String(metadata.IvB64);
var key = DeriveKey(passphrase, salt, metadata.Iterations);