Skip to content
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(link-elements): gracefully handle header parser error #14936

Merged
merged 4 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion core/gather/gatherers/link-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import FRGatherer from '../base-gatherer.js';
import {pageFunctions} from '../../lib/page-functions.js';
import DevtoolsLog from './devtools-log.js';
import {MainResource} from '../../computed/main-resource.js';
import {Util} from '../../../shared/util.js';
import * as i18n from '../../lib/i18n/i18n.js';

/* globals HTMLLinkElement getNodeDetails */

Expand All @@ -19,6 +21,17 @@ import {MainResource} from '../../computed/main-resource.js';
* headers of the main resource.
*/

const UIStrings = {
/**
* @description Warning message explaining that there was an error parsing a link header in an HTTP response. `error` will be an english string with more details on the error. `header` will be the value of the header that caused the error. `link` is a type of HTTP header and should not be translated.
* @example {Expected attribute delimiter at offset 94} error
* @example {<https://assets.calendly.com/assets/booking/css/booking-d0ac32b1.css>; rel=preload; as=style; nopush} error
*/
headerParseWarning: 'Error parsing `link` header ({error}): `{header}`',
};

const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);

/**
*
* @param {string} url
Expand Down Expand Up @@ -126,7 +139,21 @@ class LinkElements extends FRGatherer {
for (const header of mainDocument.responseHeaders) {
if (header.name.toLowerCase() !== 'link') continue;

for (const link of LinkHeader.parse(header.value).refs) {
/** @type {LinkHeader.Reference[]} */
let parsedRefs = [];

try {
adamraine marked this conversation as resolved.
Show resolved Hide resolved
parsedRefs = LinkHeader.parse(header.value).refs;
} catch (err) {
const truncatedHeader = Util.truncate(header.value, 100);
const warning = str_(UIStrings.headerParseWarning, {
error: err.message,
header: truncatedHeader,
});
context.baseArtifacts.LighthouseRunWarnings.push(warning);
}

for (const link of parsedRefs) {
linkElements.push({
rel: link.rel || '',
href: normalizeUrlOrNull(link.uri, context.baseArtifacts.URL.finalDisplayedUrl),
Expand Down Expand Up @@ -181,3 +208,4 @@ class LinkElements extends FRGatherer {
}

export default LinkElements;
export {UIStrings};
16 changes: 16 additions & 0 deletions core/test/gather/gatherers/link-elements-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('Link Elements gatherer', () => {
URL: {
finalDisplayedUrl: url,
},
LighthouseRunWarnings: [],
};
const passContext = {driver, url, baseArtifacts, computedCache: new Map()};
return [passContext, {}];
Expand Down Expand Up @@ -107,4 +108,19 @@ describe('Link Elements gatherer', () => {
link({source: 'headers', rel: 'prefetch', href: 'https://example.com/', as: 'image'}),
]);
});

it('adds toplevel warning on parser error', async () => {
const linkElementsInDOM = [];
const headers = [
{name: 'Link', value: '<https://example.com/>a'},
];

const passData = getPassData({linkElementsInDOM, headers});
const result = await new LinkElements().afterPass(...passData);
expect(result).toEqual([]);
expect(passData[0].baseArtifacts.LighthouseRunWarnings).toHaveLength(1);
expect(passData[0].baseArtifacts.LighthouseRunWarnings[0]).toBeDisplayString(
'Error parsing `link` header (Unexpected character "a" at offset 22): `<https://example.com/>a`'
);
});
});
3 changes: 3 additions & 0 deletions shared/localization/locales/en-US.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions shared/localization/locales/en-XL.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.