File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3,13 +3,13 @@ var base64url = require('base64url');
33var DataStream = require ( './data-stream' ) ;
44var jwa = require ( 'jwa' ) ;
55var Stream = require ( 'stream' ) ;
6- var toString = require ( './tostring ' ) ;
6+ var toBuffer = require ( './to-buffer ' ) ;
77var util = require ( 'util' ) ;
88
99function jwsSecuredInput ( header , payload , encoding ) {
1010 encoding = encoding || 'utf8' ;
11- var encodedHeader = base64url ( toString ( header ) , 'binary' ) ;
12- var encodedPayload = base64url ( toString ( payload ) , encoding ) ;
11+ var encodedHeader = base64url ( toBuffer ( header ) ) ;
12+ var encodedPayload = base64url ( toBuffer ( payload , encoding ) ) ;
1313 return util . format ( '%s.%s' , encodedHeader , encodedPayload ) ;
1414}
1515
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ var Buffer = require ( 'safe-buffer' ) . Buffer ;
4+
5+ module . exports = function toBuffer ( val , encoding ) {
6+ if ( Buffer . isBuffer ( val ) ) {
7+ return val ;
8+ }
9+ if ( typeof val === 'string' ) {
10+ return Buffer . from ( val , encoding || 'utf8' ) ;
11+ }
12+ if ( typeof val === 'number' ) {
13+ // This won't work for very large or very small numbers, but is consistent
14+ // with previous behaviour at least
15+ val = val . toString ( ) ;
16+ return Buffer . from ( val , 'utf8' ) ;
17+ }
18+ return Buffer . from ( JSON . stringify ( val ) , 'utf8' ) ;
19+ } ;
Original file line number Diff line number Diff line change @@ -330,3 +330,16 @@ test('jws.isValid', function (t) {
330330 t . same ( jws . isValid ( valid ) , true ) ;
331331 t . end ( ) ;
332332} ) ;
333+
334+ test ( '#50 mangled binary payload' , function ( t ) {
335+ const sig = jws . sign ( {
336+ header : {
337+ alg : 'HS256'
338+ } ,
339+ payload : new Buffer ( 'TkJyotZe8NFpgdfnmgINqg==' , 'base64' ) ,
340+ secret : new Buffer ( '8NRxgIkVxP8LyyXSL4b1dg==' , 'base64' )
341+ } ) ;
342+
343+ t . same ( sig , 'eyJhbGciOiJIUzI1NiJ9.TkJyotZe8NFpgdfnmgINqg.9XilaLN_sXqWFtlUCdAlGI85PCEbJZSIQpakyAle-vo' ) ;
344+ t . end ( ) ;
345+ } ) ;
You can’t perform that action at this time.
0 commit comments