mirror of https://github.com/dapr/kit.git
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:
parent
9514168def
commit
f49c7e5230
|
@ -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)))
|
||||
|
|
|
@ -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, ""))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue