ParseKey: ignore trailing newlines when parsing base64-encoded keys (#51)

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
Alessandro (Ale) Segala 2023-06-05 11:22:46 -07:00 committed by GitHub
parent 9514168def
commit f49c7e5230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -76,8 +76,8 @@ func ParseKey(raw []byte, contentType string) (jwk.Key, error) {
}
func parseSymmetricKey(raw []byte) (jwk.Key, error) {
// Try parsing as base64; first: remove any padding if present
trimmedRaw := bytes.TrimRight(raw, "=")
// Try parsing as base64; first: remove any padding (and trailing newlines) if present
trimmedRaw := bytes.TrimRight(raw, "\n=")
// Try parsing as base64-standard
dst := make([]byte, base64.RawStdEncoding.DecodedLen(len(raw)))

View File

@ -71,8 +71,12 @@ func TestSymmetricKeys(t *testing.T) {
t.Run("raw bytes", testSymmetricKey(rawKey, ""))
t.Run("base64 standard with padding", testSymmetricKey([]byte(base64.StdEncoding.EncodeToString(rawKey)), ""))
t.Run("base64 standard without padding", testSymmetricKey([]byte(base64.RawStdEncoding.EncodeToString(rawKey)), ""))
t.Run("base64 standard with padding and trailing newline", testSymmetricKey([]byte(base64.StdEncoding.EncodeToString(rawKey)+"\n"), ""))
t.Run("base64 standard without padding and trailing newline", testSymmetricKey([]byte(base64.RawStdEncoding.EncodeToString(rawKey)+"\n"), ""))
t.Run("base64 url with padding", testSymmetricKey([]byte(base64.URLEncoding.EncodeToString(rawKey)), ""))
t.Run("base64 url without padding", testSymmetricKey([]byte(base64.RawURLEncoding.EncodeToString(rawKey)), ""))
t.Run("base64 url with padding and trailing newline", testSymmetricKey([]byte(base64.URLEncoding.EncodeToString(rawKey)+"\n"), ""))
t.Run("base64 url without padding and trailing newline", testSymmetricKey([]byte(base64.RawURLEncoding.EncodeToString(rawKey)+"\n"), ""))
t.Run("JSON with content-type", testSymmetricKey(rawKeyJSON, "application/json"))
t.Run("JSON without content-type", testSymmetricKey(rawKeyJSON, ""))
}