Skip to content

Commit 8e52ca2

Browse files
atcastleAndrewKushnir
authored andcommitted
fix(common): Don't generate srcsets with very large sources (#47997)
Fix an issue where users could inadvertently generate very large source images in ngOptimizedImage PR Close #47997
1 parent 408aed7 commit 8e52ca2

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/common/src/directives/ng_optimized_image/ng_optimized_image.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ const ASPECT_RATIO_TOLERANCE = .1;
7575
*/
7676
const OVERSIZED_IMAGE_TOLERANCE = 1000;
7777

78+
/**
79+
* Used to limit automatic srcset generation of very large sources for
80+
* fixed-size images. In pixels.
81+
*/
82+
const FIXED_SRCSET_WIDTH_LIMIT = 1920;
83+
const FIXED_SRCSET_HEIGHT_LIMIT = 1080;
84+
85+
7886
/** Info about built-in loaders we can test for. */
7987
export const BUILT_IN_LOADERS = [imgixLoaderInfo, imageKitLoaderInfo, cloudinaryLoaderInfo];
8088

@@ -422,8 +430,7 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
422430

423431
if (this.ngSrcset) {
424432
rewrittenSrcset = this.getRewrittenSrcset();
425-
} else if (
426-
!this._disableOptimizedSrcset && !this.srcset && this.imageLoader !== noopImageLoader) {
433+
} else if (this.shouldGenerateAutomaticSrcset()) {
427434
rewrittenSrcset = this.getAutomaticSrcset();
428435
}
429436

@@ -517,6 +524,11 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
517524
return finalSrcs.join(', ');
518525
}
519526

527+
private shouldGenerateAutomaticSrcset(): boolean {
528+
return !this._disableOptimizedSrcset && !this.srcset && this.imageLoader !== noopImageLoader &&
529+
!(this.width! > FIXED_SRCSET_WIDTH_LIMIT || this.height! > FIXED_SRCSET_HEIGHT_LIMIT);
530+
}
531+
520532
/** @nodoc */
521533
ngOnDestroy() {
522534
if (ngDevMode) {

packages/common/test/directives/ng_optimized_image_spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,30 @@ describe('Image directive', () => {
14961496
.toBe(`${IMG_BASE_URL}/img?w=100 1x, ${IMG_BASE_URL}/img?w=200 2x`);
14971497
});
14981498

1499+
it('should not add a fixed srcset to the img element if height is too large', () => {
1500+
setupTestingModule({imageLoader});
1501+
1502+
const template = `<img ngSrc="img" width="1100" height="2400">`;
1503+
const fixture = createTestComponent(template);
1504+
fixture.detectChanges();
1505+
1506+
const nativeElement = fixture.nativeElement as HTMLElement;
1507+
const img = nativeElement.querySelector('img')!;
1508+
expect(img.getAttribute('srcset')).toBeNull();
1509+
});
1510+
1511+
it('should not add a fixed srcset to the img element if width is too large', () => {
1512+
setupTestingModule({imageLoader});
1513+
1514+
const template = `<img ngSrc="img" width="3000" height="400">`;
1515+
const fixture = createTestComponent(template);
1516+
fixture.detectChanges();
1517+
1518+
const nativeElement = fixture.nativeElement as HTMLElement;
1519+
const img = nativeElement.querySelector('img')!;
1520+
expect(img.getAttribute('srcset')).toBeNull();
1521+
});
1522+
14991523
it('should use a custom breakpoint set if one is provided', () => {
15001524
const imageConfig = {
15011525
breakpoints: [16, 32, 48, 64, 96, 128, 256, 384, 640, 1280, 3840],

0 commit comments

Comments
 (0)