Detect secret file type by content #398
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
$ cargo fmt)$ cargo clippy)[ ] Relevant tests added[ ] Any new documentation added