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

Commit 2009556

Browse files
csstaubmbyczkowski
authored andcommitted
Better error handling around invalid headers
Cherry-picked from #282
1 parent 60a6e9d commit 2009556

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

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)