1919import { Config } from "@spacebar/util" ;
2020import { createHmac , timingSafeEqual } from "node:crypto" ;
2121import ms , { StringValue } from "ms" ;
22+ import { TextDecoder } from "node:util" ;
23+ import { ConsoleUtils } from "./util/ConsoleUtils" ;
24+ import { ColorUtils } from "./util/ColorUtils" ;
2225
2326export class NewUrlUserSignatureData {
2427 ip ?: string ;
@@ -132,11 +135,14 @@ export const getUrlSignature = (data: NewUrlSignatureData): UrlSignResult => {
132135
133136function calculateHash ( request : UrlSignatureData ) : UrlSignResult {
134137 const { cdnSignatureKey } = Config . get ( ) . security ;
138+ const newData = createHmac ( "sha256" , cdnSignatureKey as string ) ;
135139 const data = createHmac ( "sha256" , cdnSignatureKey as string )
136140 . update ( request . path ! )
137141 . update ( request . issuedAt )
138142 . update ( request . expiresAt ) ;
139143
144+ let ipData = new Int8Array ( ) ;
145+
140146 if ( Config . get ( ) . security . cdnSignatureIncludeIp ) {
141147 if ( ! request . ip )
142148 console . log (
@@ -145,9 +151,11 @@ function calculateHash(request: UrlSignatureData): UrlSignResult {
145151 else {
146152 if ( process . env [ "LOG_CDN_SIGNATURES" ] ) console . log ( "[Signing] CDN Signature IP is enabled, adding IP to hash:" , request . ip ) ;
147153 data . update ( request . ip ! ) ;
154+ ipData = new Int8Array ( new TextEncoder ( ) . encode ( request . ip ! ) ) ;
148155 }
149156 }
150157
158+ let userAgentData = new Int8Array ( ) ;
151159 if ( Config . get ( ) . security . cdnSignatureIncludeUserAgent ) {
152160 if ( ! request . userAgent )
153161 console . log (
@@ -156,10 +164,29 @@ function calculateHash(request: UrlSignatureData): UrlSignResult {
156164 else {
157165 if ( process . env [ "LOG_CDN_SIGNATURES" ] ) console . log ( "[Signing] CDN Signature User-Agent is enabled, adding User-Agent to hash:" , request . userAgent ) ;
158166 data . update ( request . userAgent ! ) ;
167+ userAgentData = new Int8Array ( new TextEncoder ( ) . encode ( request . userAgent ! ) ) ;
159168 }
160169 }
161170
171+ const rawData = new Int8Array ( [
172+ ...new TextEncoder ( ) . encode ( request . path ! ) ,
173+ ...new TextEncoder ( ) . encode ( request . issuedAt ) ,
174+ ...new TextEncoder ( ) . encode ( request . expiresAt ) ,
175+ ...ipData ,
176+ ...userAgentData ,
177+ ] ) ;
178+ if ( process . env [ "LOG_CDN_SIGNATURES" ] ) {
179+ console . log ( "[Signing] Signature data for " , request . path ! ) ;
180+ hexdump ( rawData ) ;
181+ }
182+ newData . update ( rawData ) ;
183+
162184 const hash = data . digest ( "hex" ) ;
185+ const newHash = newData . digest ( "hex" ) ;
186+ if ( process . env [ "LOG_CDN_SIGNATURES" ] ) {
187+ console . log ( hash ) ;
188+ console . log ( newHash ) ;
189+ }
163190 const result = new UrlSignResult ( {
164191 path : request . path ,
165192 issuedAt : request . issuedAt ,
@@ -257,3 +284,29 @@ export const hasValidSignature = (req: NewUrlUserSignatureData, sig: UrlSignResu
257284
258285 return isHashValid ;
259286} ;
287+
288+ // port of https://github.com/TheArcaneBrony/ArcaneLibs/blob/master/ArcaneLibs/Extensions/DictionaryExtensions.cs#L80
289+ function hexdump ( arr : Int8Array , width : number = 32 , colorize : boolean = true ) {
290+ const colorizeFunc = colorize ? ( val : number , str : string ) => ConsoleUtils . ColoredString ( str , ColorUtils . cnv8To24 ( val ) ) : ( val : number , str : string ) => str ;
291+ for ( let i = 0 ; i < arr . length ; i += width ) {
292+ const end = Math . min ( i + width , arr . length ) ;
293+ const section = arr . slice ( i , end ) ;
294+ console . log (
295+ Array . from ( section )
296+ . map ( ( x ) => colorizeFunc ( x , x . toString ( 16 ) . toUpperCase ( ) ) )
297+ . join ( " " )
298+ . padEnd ( 3 * section . length ) ,
299+ "|" ,
300+ new TextDecoder ( "utf-8" )
301+ . decode ( section )
302+ . replaceAll ( "\n" , "." )
303+ . replaceAll ( "\r" , "." )
304+ . replaceAll ( "\0" , "." )
305+ . replaceAll ( "\t" , "." )
306+ . replaceAll ( "\v" , "." )
307+ . replaceAll ( "\b" , "." )
308+ . replaceAll ( "\x07" , "." ) // \a
309+ . replaceAll ( "\f" , "." ) ,
310+ ) ;
311+ }
312+ }
0 commit comments