jwt-cli
jwt-cli copied to clipboard
Detect secret file type by content
Summary
Currently jwt-cli detects the content of a file by its file name extension (".json" for JWK, ".pem" for PEM and everything else is considered DER). This is fast but unfortunately not very robust.
In fact it has caused a lot of confusion, when a perfectly valid EC private key in PEM format caused jwt-cli to exit with the error "invalid ECDSA key".
The purpose of the PEM header is to identify the content of the data, so it seems to be a much more reasonable approach to check the file for that instead. It does not even have to be a certain PEM header, if the file starts like any PEM header and ends like a PEM footer, then this should be more than sufficient to identify the file content as PEM.
And instead of checking for the file name extension ".json", one could check if the content can be parsed as valid JSON.
The slightly slower processing is probably not noticeable for the user.
This PR implements the changes mentioned above. There should be no negative side effects, as all tests are still green.
Preflight checklist
- [x] Code formatted rustfmt (
$ cargo fmt) - [x] Code linter check with clippy (
$ cargo clippy) - ~~[ ] Relevant tests added~~
- ~~[ ] Any new documentation added~~