File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -78,11 +78,10 @@ function _createMultihash({digest}) {
7878function _parseHeaderValue ( headerValue ) {
7979 const [ key , digest ] = headerValue . split ( / = ( .+ ) / ) ;
8080
81- // Unwrap in case the base64-encoded digest bytes are wrapped in colons
82- const encodedDigest = digest ?. replace ( / ^ : ( .* ) : $ / , '$1' ) ;
83-
81+ let encodedDigest ;
8482 let algorithm ;
8583 if ( key === 'mh' ) {
84+ encodedDigest = digest ;
8685 // if `encodedDigest` starts with `uEi`, then it is a base64url-encoded
8786 // sha-256 multihash
8887 if ( encodedDigest . startsWith ( 'uEi' ) ) {
@@ -92,6 +91,9 @@ function _parseHeaderValue(headerValue) {
9291 `Only base64url-encoded, sha-256 multihash is supported.` ) ;
9392 }
9493 } else {
94+ // Unwrap in case the base64-encoded digest bytes are wrapped in colons
95+ encodedDigest = digest ?. replace ( / ^ : ( .* ) : $ / , '$1' ) ;
96+
9597 algorithm = key . replace ( '-' , '' ) . toLowerCase ( ) ;
9698 if ( algorithm !== 'sha256' ) {
9799 throw new Error ( `Algorithm "${ algorithm } " is not supported.` ) ;
Original file line number Diff line number Diff line change @@ -158,6 +158,22 @@ describe('http-signature-digest', () => {
158158 should . exist ( verifyResult ) ;
159159 verifyResult . verified . should . equal ( true ) ;
160160 } ) ;
161+ it ( 'should verify false if a multihash digest is wrapped in colons' ,
162+ async ( ) => {
163+ const data = '{"hello":"world"}' ;
164+ const headerValue =
165+ `mh=:uEiCTojlxqRTl6svwqNJRVM2jCcPBxy-7mRTUfGDzy2gViA:` ;
166+ let verifyResult ;
167+ let err ;
168+ try {
169+ verifyResult = await verifyHeaderValue ( { data, headerValue} ) ;
170+ } catch ( e ) {
171+ err = e ;
172+ }
173+ should . not . exist ( err ) ;
174+ should . exist ( verifyResult ) ;
175+ verifyResult . verified . should . equal ( false ) ;
176+ } ) ;
161177 it ( 'should verify false if verifying bad data object' , async ( ) => {
162178 const data = { hello : 'world' } ;
163179 const headerValue = await createHeaderValue (
You can’t perform that action at this time.
0 commit comments