44 */
55
66const crypto = require ( 'crypto' ) ;
7+ const smart_escape = require ( './utils/smart_escape' ) . smart_escape ;
8+
9+ const unsafe = / ( [ " # % & ' / : ; < = > ? @ [ \] ^ ` { | } ~ ] + ) / g;
710
811function digest ( message , key ) {
912 return crypto . createHmac ( "sha256" , Buffer . from ( key , "hex" ) ) . update ( message ) . digest ( 'hex' ) ;
@@ -15,7 +18,8 @@ function digest(message, key) {
1518 * @return {string } escaped url
1619 */
1720function escapeToLower ( url ) {
18- return encodeURIComponent ( url ) . replace ( / % ../ g, function ( match ) {
21+ const safeUrl = smart_escape ( url , unsafe ) ;
22+ return safeUrl . replace ( / % ../ g, function ( match ) {
1923 return match . toLowerCase ( ) ;
2024 } ) ;
2125}
@@ -41,6 +45,7 @@ function escapeToLower(url) {
4145 */
4246module . exports = function ( options ) {
4347 const tokenName = options . token_name ? options . token_name : "__cld_token__" ;
48+ const tokenSeparator = "~" ;
4449 if ( options . expiration == null ) {
4550 if ( options . duration != null ) {
4651 let start = options . start_time != null ? options . start_time : Math . round ( Date . now ( ) / 1000 ) ;
@@ -61,11 +66,11 @@ module.exports = function (options) {
6166 tokenParts . push ( `acl=${ escapeToLower ( options . acl ) } ` ) ;
6267 }
6368 let toSign = [ ...tokenParts ] ;
64- if ( options . url ) {
69+ if ( options . url != null && options . acl == null ) {
6570 let url = escapeToLower ( options . url ) ;
6671 toSign . push ( `url=${ url } ` ) ;
6772 }
68- let auth = digest ( toSign . join ( "~" ) , options . key ) ;
73+ let auth = digest ( toSign . join ( tokenSeparator ) , options . key ) ;
6974 tokenParts . push ( `hmac=${ auth } ` ) ;
70- return `${ tokenName } =${ tokenParts . join ( '~' ) } ` ;
75+ return `${ tokenName } =${ tokenParts . join ( tokenSeparator ) } ` ;
7176} ;
0 commit comments