Repository navigation
[Server] Reject a token signing key too small for RS512 when it is loaded - #2389
Conversation
…aded Signed-off-by: Maneet Arvind Mehta <[email protected]>
utafrali
left a comment
There was a problem hiding this comment.
The fix is correct: test-signing at load time is the right approach since it lets the JVM determine usability rather than hard-coding a bit-length threshold, the error message names the file, the bit count, and gives a remediation command consistent with the project's conventions, and the new test row integrates cleanly into the existing parameterized suite. The two notes above are minor.
| try { | ||
| signature.initSign(key); | ||
| signature.update(new byte[0]); | ||
| signature.sign(); |
There was a problem hiding this comment.
signature.update(new byte[0]) is a no-op here. Signature.sign() will operate on whatever has been fed so far (an empty message in this case), so calling update with an empty array changes nothing. You can drop that line without affecting the validation.
| // RS512 cannot sign with a key this small, so loading it must fail, not the first token. | ||
| KeyPairGenerator small = KeyPairGenerator.getInstance("RSA"); | ||
| small.initialize(512); | ||
| Files.write(privateKey, small.generateKeyPair().getPrivate().getEncoded()); |
There was a problem hiding this comment.
The 512-bit key is written as raw DER bytes (Files.write). That exercises the DER branch of readKeyFile. The validation in requireCanSignRs512 is format-agnostic, so the PEM branch would hit the same check, but if you want to be thorough a second case with Files.writeString(privateKey, pem("PRIVATE" + " KEY", small.generateKeyPair().getPrivate().getEncoded())) would confirm the PEM path too. Not a blocker, just a gap worth noting.
Signed-off-by: Maneet Arvind Mehta <[email protected]>
timothyw553
left a comment
There was a problem hiding this comment.
approach looks good, just left two small comments on the recovery hint in the error message and 1024-bit test coverage. nothing blocking.
| "garbage bytes, Cannot read the RSA private key", | ||
| "EC key, expected an unencrypted RSA private key" | ||
| "EC key, expected an unencrypted RSA private key", | ||
| "512-bit key, is 512 bits", |
There was a problem hiding this comment.
the new tests only check rejection, while existing success tests all generate 2048-bit keys. could we add a success test that loads a working 1024-bit key and signs with configuration.algorithmRSA() so we don't accidentally regress to rejecting valid keys under 2048 bits?
There was a problem hiding this comment.
Added signsWithA1024BitPrivateKey in f843f1f. It loads a 1024-bit key, signs a token with algorithmRSA() and verifies it.
| + file.toAbsolutePath() | ||
| + " is " | ||
| + key.getModulus().bitLength() | ||
| + " bits, too short to sign tokens with RS512; create a 2048-bit key with" |
There was a problem hiding this comment.
requireCanSignRs512 also checks private_key.der in the configuration folder, so creating private_key.pem alone won't fix that path unless server.token-signing-private-key-file is set. if we keep the command instead of a neutral message, should we mention setting that property (and frame 2048 bits as a recommendation)?
There was a problem hiding this comment.
Updated in f843f1f. The message now says to set server.token-signing-private-key-file to a larger RSA key, words 2048 bits as a recommendation, and keeps the openssl command as an example.
There was a problem hiding this comment.
Follow-up in fc5ceb9: the message no longer assumes the key is too short. It now includes Java's own reason for the failure.
Signed-off-by: Maneet Arvind Mehta <[email protected]>
|
/review |
Signed-off-by: Maneet Arvind Mehta <[email protected]>
|
/review |
yili-db
left a comment
There was a problem hiding this comment.
[AI Assisted] Approving. The message now names the file, the key size and Java's own reason, and nothing that works today breaks: a key this check rejects already stops startup on main, because SecurityContext signs at construction whether or not authorization is enabled.
Not blocking:
- Follow-up: a rejection test for the fallback
private_key.derroute in the configuration folder, here or in a tracked issue. - Inline comments: one wording change in the test and one nit in the hint.
| privateKey, pem("PRIVATE" + " KEY", ec.generateKeyPair().getPrivate().getEncoded())); | ||
| } | ||
| case "512-bit key" -> { | ||
| // RS512 cannot sign with a key this small, so loading it must fail, not the first token. |
There was a problem hiding this comment.
[AI Assisted] Could we drop "not the first token"? The key is rejected at load and the message names the file, so the timing claim is not needed and can go stale.
| + e.getMessage() | ||
| + "; set " | ||
| + Property.TOKEN_SIGNING_PRIVATE_KEY_FILE.getKey() | ||
| + " to a working RSA key (2048 bits recommended), for example one created with" |
There was a problem hiding this comment.
[AI Assisted] nit: for a 2048-bit key with corrupted CRT data, "2048 bits recommended" points at a size the operator already has. Could the hint say to use a key that signs with RS512, and keep the size advice for keys under 2048 bits?
|
@yili-db Thanks for the review. To keep the approved commit as it is, I'd like to handle the comment wording, the hint and the private_key.der test in a follow-up PR. I can do them here instead if you prefer. |
PR Checklist
docsis updatedDescription of changes
Fixes #2365
With a 512-bit RSA key, the server failed at startup ("Key is too short for this signature algorithm") without naming the key file.
SecurityConfigurationnow test-signs with SHA512withRSA when it reads the private key. If signing fails, the error names the file, the key size and Java's own reason, says to setserver.token-signing-private-key-fileto a working RSA key (2048 bits recommended), and gives anopensslexample. Keys of 1024 bits still load (no new minimum).Tests in
SecurityConfigurationTest:algorithmRSA()and verifies it.How it was tested
I ran
build/sbt clean "server/testOnly *SecurityConfigurationTest"in GitHub Codespaces: 13 tests passed, 0 failed. I did not complete a run of the full server test suite.AI disclosure: I used Claude (an AI assistant). I read the diff and understand it.