Skip to content

[Server] Reject a token signing key too small for RS512 when it is loaded - #2389

Merged
yili-db merged 4 commits into
unitycatalog:mainfrom
Maneet7805:reject-small-rs512-signing-key
Nov 30, 2026
Merged

yili-db merged 4 commits into
unitycatalog:mainfrom
Maneet7805:reject-small-rs512-signing-key

Conversation

@Maneet7805

@Maneet7805 Maneet7805 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

Description 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. SecurityConfiguration now 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 set server.token-signing-private-key-file to a working RSA key (2048 bits recommended), and gives an openssl example. Keys of 1024 bits still load (no new minimum).

Tests in SecurityConfigurationTest:

  • 512-bit rows (DER and PEM) in the unusable-key test.
  • A success test that loads a 1024-bit key, signs a token with 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.

@utafrali utafrali left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Maneet7805

Copy link
Copy Markdown
Contributor Author

@utafrali thanks for the review. I removed the redundant update(new byte[0]) call and added a 512-bit PEM row to the test, as suggested. Pushed in a070b47.

@timothyw553 timothyw553 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up in fc5ceb9: the message no longer assumes the key is too short. It now includes Java's own reason for the failure.

@yili-db

yili-db commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db

yili-db commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.der route 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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?

@Maneet7805

Copy link
Copy Markdown
Contributor Author

@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.

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.

[Server] Reject a token signing key too small for RS512 when it is loaded

4 participants