Skip to content

Keep key material out of SSH key errors and recipients-file errors - #144

Merged
pscheid92 merged 1 commit into
mainfrom
fix/review-s1-s3
Nov 30, 2026
Merged

pscheid92 merged 1 commit into
mainfrom
fix/review-s1-s3

Conversation

@pscheid92

Copy link
Copy Markdown
Owner

Three findings from the 2026-11-30 review, each with a test that failed before the change.

S1: SSH private key bytes in an exception message

A passphrase-protected SSH key that decrypts to something unusable reported what SSH.NET said about it, and SSH.NET quotes the "key type" it read from the decrypted section. aes256-ctr, ssh-keygen's default, is not authenticated, so one damaged byte made that key type run on into the private key. With ed25519_aes256ctr, 16 of the seed's 32 bytes were readable in AgeException.Message.

  • The error is now unreadable SSH private key: what the passphrase decrypts is not a usable key, with no text and no inner exception from SSH.NET.
  • A wrong passphrase for a legacy PEM key carries no inner exception either: with the right passphrase and a damaged file, the bytes SSH.NET describes are the key.
  • Test: SshKeyErrorLeakTests flips one ciphertext byte in the Ed25519 and RSA fixtures and checks that no 6-byte printable run of the private section appears anywhere in ex.ToString().

S3: a secret key pasted into a recipients file was echoed

Recipients.Parse repeated a line it could not read. It now names the line by number, as Identities.Parse and go-age do. Recipients.ParseOne still names its argument, which is the caller's own string.

S2: an ssh-rsa line was parsed twice

BouncyCastle validates the modulus every time it builds an RSA key, and that costs one full-size modular exponentiation.

  • Recipients.ParseOne on a 16,384-bit line: 20.5 s before, 9.8 s after (Debug build, M-series Mac).
  • A key SSH.NET decrypted was checked twice for the same reason; now once.
  • Not fixed: one validation per key remains. BouncyCastle offers no per-call way around it, so only a smaller size limit would reduce it, and that changes which keys are accepted. There is no test for this part: "parsed once" is not observable without a seam in the library.

Behaviour changes

Noted in TODO.md for the next release notes: the recipients-file message, the SSH key message, and the missing inner exceptions.

Checked

  • dotnet test: 1,176 vectors and 1,598 unit tests pass.
  • dotnet test -c Release -p:ForcePortableAead=true -p:TreatWarningsAsErrors=true: same counts, no warnings.
  • Age.Fuzz on recipients, sshpub and ssh for 25 s: no findings.

From the 2026-11-30 review.

A passphrase-protected SSH key that decrypts to something unusable
reported what SSH.NET said about it, and SSH.NET quotes the "key type" it
read from the decrypted section. aes256-ctr, ssh-keygen's default, is not
authenticated, so one damaged byte made that key type run on into the
private key: half of an Ed25519 seed was readable in the message. The
error now says only that the key is unusable, with no text and no inner
exception from SSH.NET; a wrong passphrase for a legacy PEM key carries no
inner exception either.

Recipients.Parse repeated a line it could not read, which is often a
secret key pasted into the wrong file. It now names the line by number,
as Identities.Parse and go-age do. ParseOne still names its argument.

An ssh-rsa line was parsed twice, and BouncyCastle validates the modulus
each time it builds a key: a 16384-bit line took 20.5 s and now takes
9.8 s. A key SSH.NET decrypted was checked twice for the same reason.
What remains is one validation per key, which only a smaller size limit
would reduce.
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.46%. Comparing base (fd2e9f9) to head (03bc282).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #144      +/-   ##
==========================================
- Coverage   97.51%   97.46%   -0.06%     
==========================================
  Files          75       75              
  Lines        4998     5001       +3     
  Branches      686      688       +2     
==========================================
  Hits         4874     4874              
- Misses         75       78       +3     
  Partials       49       49              
Flag Coverage Δ
macos-latest 97.42% <100.00%> (+<0.01%) ⬆️
ubuntu-latest 97.42% <100.00%> (-0.06%) ⬇️
windows-latest 97.36% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@pscheid92
pscheid92 merged commit 061fee0 into main Nov 30, 2026
6 checks passed
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