Use SecureRandom for Utils key generation instead of MD5 of weak entropy
References
- Fixes #12667
Description
Utils.generateBytesKey() derived keys from MD5(VMID + timestamp + counter + java.util.Random) — predictable inputs hashed with a broken digest and seeded by a non-cryptographic RNG. It now returns bytes straight from a shared SecureRandom.
Instructions for Reviewers
List of changes in this PR:
Utils.generateBytesKey()now fills a 16-byte array from a sharedSecureRandom, instead of MD5-hashing a string built fromVMID, the current timestamp, acounterandjava.util.Randomoutput.- The key size is unchanged (16 bytes), so
generateKey()andgenerateHexKey()keep their documented output (≈38–40 digit and 32-character hex keys respectively) and their callers are unaffected. - Removed the now-unused
counter,java.util.RandomandVMIDfields and their imports.synchronizedis dropped from the method, sinceSecureRandomis thread-safe and there is no longer any shared mutable state. - Added unit tests in
UtilsTestcovering the key length, uniqueness across calls, and the 32-character hex output.
The change is confined to Utils.generateBytesKey(); the public getMD5() / getMD5Bytes() helpers (used elsewhere) are intentionally left untouched.
Checklist
- [x] My PR is created against the
mainbranch of code. - [x] My PR is small in size.
- [x] My PR passes Checkstyle validation (
mvn -pl dspace-api checkstyle:check→ 0 violations). - [x] My PR includes Javadoc for modified methods (existing Javadoc retained).
- [x] My PR includes new Unit Tests in
UtilsTest. - [x] My PR includes details on how to test it.
- [ ] ~New libraries/dependencies~ — none added.
- [ ] ~REST API endpoint changes~ — none.
- [ ] ~New configurations~ — none.
- [x] I've linked the issue (Fixes #12667).
@lgeggleston , IMHO, this change should preferably get two approvals.
@saschaszott Sounds good, 1 Approval label removed. Thanks for reviewing!
generateBytesKey is used by generateKey which is used by all the bitstream store services to generate internal bitstream IDs, so we do need to make sure the output looks the same / predictable in terms of what the rest of our code / integration expects (raised at dev mtg, sounds like the output will be the same format, just a more secure random generation method)
generateBytesKeyis used bygenerateKeywhich is used by all the bitstream store services to generate internal bitstream IDs, so we do need to make sure the output looks the same / predictable in terms of what the rest of our code / integration expects (raised at dev mtg, sounds like the output will be the same format, just a more secure random generation method)
Do these internal bitstreams IDs need to be generated by secureRandom or is it ok if they are based on md5 hashes? I expect secureRandom to perform slower than md5. If the internal bitstream IDs don't need to be as securely generated as the tokens, we could use two different methods here, one based on secureRandom and one on md5, for the two use cases.
Per @tinsch 's point above, I'm also starting to wonder if it'd be better to use both MD5 and SecureRandom for the different scenarios they are best used for:
- Continue to use MD5 for generating random Bitstreams IDs to determine where to store them in the Bitstore
- Update to use SecureRandom for things like Request a Copy, where the hash is used as a type of "authentication key".
The reason we may want to consider this is because my understanding is SecureRandom can be much slower (20-50 times slower) to generate because it is more secure. If we make all Bitstream IDs SecureRandom hashes, this will likely impact the performance of uploading & storing many bitstreams at once, and there's very little benefit to making these hashes secure (as they just determine the location where the file will be stored on disk)
Thanks @tdonohue — agreed, and the call sites line up with that. Tracing where the two public helpers land:
Utils.generateKey()→ Bitstream storage IDs (BitstreamStorageServiceImpl#store, plus the intermediate paths inDSBitStoreService/S3BitStoreService/JCloudBitStoreService) and event transaction IDs inBasicDispatcher— location/identity only, no security role.Utils.generateHexKey()→ bearer tokens: Request-a-Copytokenandaccess_token(RequestItemServiceImpl), plus the account registration / forgot-password tokens (AccountServiceImpl,RegistrationDataServiceImpl).
So every security-relevant value flows through generateHexKey(), which makes for a clean split. I'll keep SecureRandom for generateBytesKey()/generateHexKey() (the tokens) and give generateKey() its own fast, non-crypto RNG (ThreadLocalRandom) for the storage IDs — that keeps SecureRandom out of the bitstream-store path while still dropping the MD5 that started this (gone from both paths either way).
Happy to push that if it looks right to you. (FWIW a single SecureRandom.nextBytes(16) is still sub-millisecond and the store path is I/O-bound, so I'd expect the real-world delta to be small — but agreed there's no reason to spend it on a value that just picks a directory.)
Out of scope, but I wanted to capture the thought here: do we need a hash at all for the purpose of distributing bitstreams across an assetstore? We could use the UUID. The (local) assetstore design comes from a time when the DSO unique identifier was generated by a database SEQUENCE and thus had no randomness.
And for file integrity checking, a CRC might be good enough and faster. But I/O dominates checksum creation cost so it may not be worth redesigning.