@@ -41,6 +41,7 @@ export class ImageRequest {
41
41
imageRequestInfo = { ...imageRequestInfo , ...originalImage } ;
42
42
43
43
imageRequestInfo . headers = this . parseImageHeaders ( event , imageRequestInfo . requestType ) ;
44
+ this . validateRequestExpires ( imageRequestInfo ) ;
44
45
45
46
// If the original image is SVG file and it has any edits but no output format, change the format to WebP.
46
47
if ( imageRequestInfo . contentType === 'image/svg+xml' && imageRequestInfo . edits && Object . keys ( imageRequestInfo . edits ) . length > 0 && ! imageRequestInfo . edits . toFormat ) {
@@ -443,4 +444,29 @@ export class ImageRequest {
443
444
}
444
445
}
445
446
}
447
+
448
+ private validateRequestExpires ( requestInfo : ImageRequestInfo ) : void {
449
+ try {
450
+ const expires = requestInfo . headers ?. expires ;
451
+ if ( expires !== undefined ) {
452
+ const parsedDate = new Date ( expires ) ;
453
+ if ( isNaN ( parsedDate . getTime ( ) ) ) {
454
+ throw new ImageHandlerError ( StatusCodes . BAD_REQUEST , 'ImageRequestExpiryFormat' , 'Request has invalid expiry date.' ) ;
455
+ }
456
+ const now = new Date ( ) ;
457
+ if ( now > parsedDate ) {
458
+ throw new ImageHandlerError ( StatusCodes . FORBIDDEN , 'ImageRequestExpired' , 'Request has expired.' ) ;
459
+ }
460
+ }
461
+ } catch ( error ) {
462
+ if ( error . code === 'ImageRequestExpired' ) {
463
+ throw error ;
464
+ }
465
+ if ( error . code === 'ImageRequestExpiryFormat' ) {
466
+ throw error ;
467
+ }
468
+ console . error ( 'Error occurred while checking expiry.' , error ) ;
469
+ throw new ImageHandlerError ( StatusCodes . INTERNAL_SERVER_ERROR , 'ExpiryDateCheckFailure' , 'Expiry date check failed.' ) ;
470
+ }
471
+ }
446
472
}
0 commit comments