-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core(image-elements): do not set untrusted natural dimensions #11457
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,8 @@ function getHTMLImages(allElements) { | |
|
||
return allImageElements.map(element => { | ||
const computedStyle = window.getComputedStyle(element); | ||
const isPicture = !!element.parentElement && element.parentElement.tagName === 'PICTURE'; | ||
connorjclark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const canTrustNaturalDimensions = !isPicture && !element.srcset; | ||
return { | ||
// currentSrc used over src to get the url as determined by the browser | ||
// after taking into account srcset/media/sizes/etc. | ||
|
@@ -65,18 +67,18 @@ function getHTMLImages(allElements) { | |
displayedWidth: element.width, | ||
displayedHeight: element.height, | ||
clientRect: getClientRect(element), | ||
naturalWidth: element.naturalWidth, | ||
naturalHeight: element.naturalHeight, | ||
naturalWidth: canTrustNaturalDimensions ? element.naturalWidth : 0, | ||
naturalHeight: canTrustNaturalDimensions ? element.naturalHeight : 0, | ||
attributeWidth: element.getAttribute('width') || '', | ||
attributeHeight: element.getAttribute('height') || '', | ||
cssWidth: undefined, // this will get overwritten below | ||
cssHeight: undefined, // this will get overwritten below | ||
cssComputedPosition: getPosition(element, computedStyle), | ||
isCss: false, | ||
isPicture, | ||
// @ts-expect-error: loading attribute not yet added to HTMLImageElement definition. | ||
loading: element.loading, | ||
resourceSize: 0, // this will get overwritten below | ||
isPicture: !!element.parentElement && element.parentElement.tagName === 'PICTURE', | ||
usesObjectFit: ['cover', 'contain', 'scale-down', 'none'].includes( | ||
computedStyle.getPropertyValue('object-fit') | ||
), | ||
|
@@ -175,7 +177,7 @@ function determineNaturalSize(url) { | |
} | ||
|
||
/** | ||
* @param {LH.Crdp.CSS.CSSStyle} [style] | ||
* @param {LH.Crdp.CSS.CSSStyle|undefined} style | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drive by fix, vscode flags this as a ts error |
||
* @param {string} property | ||
* @return {string | undefined} | ||
*/ | ||
|
@@ -191,7 +193,7 @@ function findSizeDeclaration(style, property) { | |
/** | ||
* Finds the most specific directly matched CSS font-size rule from the list. | ||
* | ||
* @param {Array<LH.Crdp.CSS.RuleMatch>} [matchedCSSRules] | ||
* @param {Array<LH.Crdp.CSS.RuleMatch>|undefined} matchedCSSRules | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drive by fix, vscode flags this as a ts error |
||
* @param {string} property | ||
* @returns {string | undefined} | ||
*/ | ||
|
@@ -346,7 +348,6 @@ class ImageElements extends Gatherer { | |
// or it's not in the top 50 largest images. | ||
if ( | ||
(element.isPicture || element.isCss || element.srcset) && | ||
networkRecord && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. confusion fix, we always default |
||
top50Images.includes(networkRecord) | ||
) { | ||
element = await this.fetchElementWithSizeInformation(driver, element); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only semi-related, at one point debugging I put a redirect that contained non-ascii characters and it crashed the server entirely, which is suboptimal. returning a 500 makes more sense :)