Skip to content

Commit

Permalink
fix: do not mutate unencoded payload when signing for multiple parties
Browse files Browse the repository at this point in the history
resolves #89
  • Loading branch information
panva committed Aug 4, 2020
1 parent 495a787 commit 1695423
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/jws/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Sign {
/*
* @private
*/
[PROCESS_RECIPIENT] (recipient) {
[PROCESS_RECIPIENT] (recipient, first) {
const { key, protectedHeader, unprotectedHeader } = recipient

if (key.use === 'enc') {
Expand Down Expand Up @@ -95,7 +95,7 @@ class Sign {
this._b64 = joseHeader.protected.b64
}

if (!joseHeader.protected.b64) {
if (first && !joseHeader.protected.b64) {
if (this._binary) {
this._payload = base64url.decodeToBuffer(this._payload)
} else {
Expand Down Expand Up @@ -130,9 +130,9 @@ class Sign {

serializer.validate(this, this._recipients)

for (const recipient of this._recipients) {
this[PROCESS_RECIPIENT](recipient)
}
this._recipients.forEach((recipient, i) => {
this[PROCESS_RECIPIENT](recipient, i === 0)
})

return serializer(this._payload, this._recipients)
}
Expand Down
34 changes: 34 additions & 0 deletions test/jws/b64.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,40 @@ test('b64=false is supported for JWS', t => {
t.is(JWS.verify(FIXTURE, k, { crit: ['b64'] }), FIXTURE.payload)
})

test('b64=false is supported for JWS with multiple recipients (buffer input)', t => {
const s = new JWS.Sign(Buffer.from(FIXTURE.payload))
s.recipient(k, { alg: 'HS256', b64: false, crit: ['b64'] })
s.recipient(k, { alg: 'HS256', b64: false, crit: ['b64'] })

t.deepEqual(
s.sign('general'),
{
payload: Buffer.from(FIXTURE.payload),
signatures: [
{ protected: FIXTURE.protected, signature: FIXTURE.signature },
{ protected: FIXTURE.protected, signature: FIXTURE.signature }
]
}
)
})

test('b64=false is supported for JWS with multiple recipients (string input)', t => {
const s = new JWS.Sign(FIXTURE.payload)
s.recipient(k, { alg: 'HS256', b64: false, crit: ['b64'] })
s.recipient(k, { alg: 'HS256', b64: false, crit: ['b64'] })

t.deepEqual(
s.sign('general'),
{
payload: FIXTURE.payload,
signatures: [
{ protected: FIXTURE.protected, signature: FIXTURE.signature },
{ protected: FIXTURE.protected, signature: FIXTURE.signature }
]
}
)
})

test('b64=false with buffers', t => {
const payload = randomBytes(32)
const { payload: _, ...detached } = JWS.sign.flattened(payload, k, { alg: 'HS256', b64: false, crit: ['b64'] })
Expand Down

0 comments on commit 1695423

Please sign in to comment.