Skip to content

Commit e831228

Browse files
authored
Fix jws.Verify not respecting the b64 header in the protected headers (#683)
* Add failing test * Apply fix from #681
1 parent b66a2cb commit e831228

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

jws/jws.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -476,16 +476,6 @@ func (ctx *verifyCtx) verifyCompact(signed []byte) ([]byte, error) {
476476
return nil, errors.Wrap(err, `failed extract from compact serialization format`)
477477
}
478478

479-
verifyBuf := pool.GetBytesBuffer()
480-
defer pool.ReleaseBytesBuffer(verifyBuf)
481-
482-
verifyBuf.Write(protected)
483-
verifyBuf.WriteByte('.')
484-
if len(payload) == 0 && ctx.detachedPayload != nil {
485-
payload = ctx.detachedPayload
486-
}
487-
verifyBuf.Write(payload)
488-
489479
decodedSignature, err := base64.Decode(signature)
490480
if err != nil {
491481
return nil, errors.Wrap(err, `failed to decode signature`)
@@ -501,6 +491,20 @@ func (ctx *verifyCtx) verifyCompact(signed []byte) ([]byte, error) {
501491
return nil, errors.Wrap(err, `failed to decode headers`)
502492
}
503493

494+
verifyBuf := pool.GetBytesBuffer()
495+
defer pool.ReleaseBytesBuffer(verifyBuf)
496+
497+
verifyBuf.Write(protected)
498+
verifyBuf.WriteByte('.')
499+
if len(payload) == 0 && ctx.detachedPayload != nil {
500+
if getB64Value(hdr) {
501+
payload = base64.Encode(ctx.detachedPayload)
502+
} else {
503+
payload = ctx.detachedPayload
504+
}
505+
}
506+
verifyBuf.Write(payload)
507+
504508
if !ctx.useJKU {
505509
if hdr.KeyID() != "" {
506510
if jwkKey, ok := ctx.key.(jwk.Key); ok {

jws/jws_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -1778,3 +1778,20 @@ func TestJKU(t *testing.T) {
17781778
}
17791779
})
17801780
}
1781+
1782+
func TestGH681(t *testing.T) {
1783+
privkey, err := jwxtest.GenerateRsaKey()
1784+
if !assert.NoError(t, err, "failed to create private key") {
1785+
return
1786+
}
1787+
1788+
buf, err := jws.Sign(nil, jwa.RS256, privkey, jws.WithDetachedPayload([]byte("Lorem ipsum")))
1789+
if !assert.NoError(t, err, "failed to sign payload") {
1790+
return
1791+
}
1792+
1793+
_, err = jws.Verify(buf, jwa.RS256, &privkey.PublicKey, jws.WithDetachedPayload([]byte("Lorem ipsum")))
1794+
if !assert.NoError(t, err, "failed to verify JWS message") {
1795+
return
1796+
}
1797+
}

0 commit comments

Comments
 (0)