Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.

Commit 4ef0f1b

Browse files
authored
Merge pull request #282 from square/cs/jws-panic
Better error handling around invalid headers
2 parents f518123 + 363171a commit 4ef0f1b

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

jws.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ func (sig Signature) mergedHeaders() rawHeader {
102102
}
103103

104104
// Compute data to be signed
105-
func (obj JSONWebSignature) computeAuthData(payload []byte, signature *Signature) []byte {
105+
func (obj JSONWebSignature) computeAuthData(payload []byte, signature *Signature) ([]byte, error) {
106106
var authData bytes.Buffer
107107

108108
protectedHeader := new(rawHeader)
109109

110110
if signature.original != nil && signature.original.Protected != nil {
111111
if err := json.Unmarshal(signature.original.Protected.bytes(), protectedHeader); err != nil {
112-
panic(err)
112+
return nil, err
113113
}
114114
authData.WriteString(signature.original.Protected.base64())
115115
} else if signature.protected != nil {
@@ -134,7 +134,7 @@ func (obj JSONWebSignature) computeAuthData(payload []byte, signature *Signature
134134
authData.Write(payload)
135135
}
136136

137-
return authData.Bytes()
137+
return authData.Bytes(), nil
138138
}
139139

140140
// parseSignedFull parses a message in full format.

jws_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ package jose
1818

1919
import (
2020
"crypto/x509"
21+
"encoding/base64"
2122
"strings"
2223
"testing"
24+
25+
"github.com/stretchr/testify/assert"
2326
)
2427

2528
const trustedCA = `
@@ -647,3 +650,39 @@ func TestDetachedCompactSerialization(t *testing.T) {
647650
t.Fatalf("got '%s', expected '%s'", ser, msg)
648651
}
649652
}
653+
654+
func TestJWSComputeAuthDataBase64(t *testing.T) {
655+
jws := JSONWebSignature{}
656+
657+
_, err := jws.computeAuthData([]byte{0x01}, &Signature{
658+
original: &rawSignatureInfo{
659+
Protected: newBuffer([]byte("{!invalid-json}")),
660+
},
661+
})
662+
// Invalid header, should return error
663+
assert.NotNil(t, err)
664+
665+
payload := []byte{0x01}
666+
encodedPayload := base64.RawURLEncoding.EncodeToString(payload)
667+
668+
b64TrueHeader := newBuffer([]byte(`{"alg":"RSA-OAEP","enc":"A256GCM","b64":true}`))
669+
b64FalseHeader := newBuffer([]byte(`{"alg":"RSA-OAEP","enc":"A256GCM","b64":false}`))
670+
671+
data, err := jws.computeAuthData(payload, &Signature{
672+
original: &rawSignatureInfo{
673+
Protected: b64TrueHeader,
674+
},
675+
})
676+
assert.Nil(t, err)
677+
// Payload should be b64 encoded
678+
assert.Len(t, data, len(b64TrueHeader.base64())+len(encodedPayload)+1)
679+
680+
data, err = jws.computeAuthData(payload, &Signature{
681+
original: &rawSignatureInfo{
682+
Protected: b64FalseHeader,
683+
},
684+
})
685+
assert.Nil(t, err)
686+
// Payload should *not* be b64 encoded
687+
assert.Len(t, data, len(b64FalseHeader.base64())+len(payload)+1)
688+
}

signing.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,11 @@ func (obj JSONWebSignature) DetachedVerify(payload []byte, verificationKey inter
370370
}
371371
}
372372

373-
input := obj.computeAuthData(payload, &signature)
373+
input, err := obj.computeAuthData(payload, &signature)
374+
if err != nil {
375+
return ErrCryptoFailure
376+
}
377+
374378
alg := headers.getSignatureAlgorithm()
375379
err = verifier.verifyPayload(input, signature.Signature, alg)
376380
if err == nil {
@@ -421,7 +425,11 @@ outer:
421425
}
422426
}
423427

424-
input := obj.computeAuthData(payload, &signature)
428+
input, err := obj.computeAuthData(payload, &signature)
429+
if err != nil {
430+
continue
431+
}
432+
425433
alg := headers.getSignatureAlgorithm()
426434
err = verifier.verifyPayload(input, signature.Signature, alg)
427435
if err == nil {

0 commit comments

Comments
 (0)