Skip to content

Commit

Permalink
Only allow same origin (domain + subdomains) for preload
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Apr 29, 2018
1 parent b150d71 commit 32a4b20
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
24 changes: 19 additions & 5 deletions lighthouse-core/audits/uses-rel-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const Audit = require('./audit');
const Util = require('../report/v2/renderer/util');
const URL = require('../lib/url-shim');
const UnusedBytes = require('./byte-efficiency/byte-efficiency-audit');
const THRESHOLD_IN_MS = 100;

Expand Down Expand Up @@ -51,8 +52,22 @@ class UsesRelPreloadAudit extends Audit {
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*
* @param {LH.WebInspector.NetworkRequest} request
* @param {LH.WebInspector.NetworkRequest} mainResource
* @return {boolean}
*/
static shouldPreload(request, mainResource) {
if (request._isLinkPreload || request.protocol === 'data') {
return false;
}

return URL.rootDomainsMatch(request.url, mainResource.url);
}

/**
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
const devtoolsLogs = artifacts.devtoolsLogs[UsesRelPreloadAudit.DEFAULT_PASS];
Expand All @@ -63,14 +78,13 @@ class UsesRelPreloadAudit extends Audit {
]).then(([critChains, mainResource]) => {
const results = [];
let maxWasted = 0;
// get all critical requests 2 + mainResourceIndex levels deep
// get all critical requests 2 levels deep (starting from mainResource index)
const mainResourceIndex = mainResource.redirects ? mainResource.redirects.length : 0;

const criticalRequests = UsesRelPreloadAudit._flattenRequests(critChains,
3 + mainResourceIndex, 2 + mainResourceIndex);
criticalRequests.forEach(request => {
const networkRecord = request;
if (!networkRecord._isLinkPreload && networkRecord.protocol !== 'data') {
if (UsesRelPreloadAudit.shouldPreload(request, mainResource)) {
// calculate time between mainresource.endTime and resource start time
const wastedMs = Math.min(request._startTime - mainResource._endTime,
request._endTime - request._startTime) * 1000;
Expand Down
20 changes: 20 additions & 0 deletions lighthouse-core/lib/url-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ class URLShim extends URL {
}
}

/**
* Check if rootDomains matches
*
* @param {string} urlA
* @param {string} urlB
*/
static rootDomainsMatch(urlA, urlB) {
const urlAInfo = new URL(urlA);
const urlBInfo = new URL(urlB);

if (!urlAInfo.host || !urlBInfo.host) {
return false;
}

const urlARootDomain = urlAInfo.host.split('.').slice(-2).join('.');
const urlBRootDomain = urlBInfo.host.split('.').slice(-2).join('.');

return urlARootDomain === urlBRootDomain;
}

/**
* @param {string} url
* @param {{numPathParts: number, preserveQuery: boolean, preserveHost: boolean}=} options
Expand Down
32 changes: 24 additions & 8 deletions lighthouse-core/test/audits/uses-rel-preload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const UsesRelPreload = require('../../audits/uses-rel-preload.js');
const assert = require('assert');
const defaultMainResource = {
url: 'http://www.example.com',
_endTime: 1,
};

Expand All @@ -37,16 +38,24 @@ describe('Performance: uses-rel-preload audit', () => {
const networkRecords = [
{
requestId: '2',
_endTime: 1,
_startTime: 10,
_endTime: 19,
_isLinkPreload: false,
_url: 'http://www.example.com',
url: 'http://www.example.com/script.js',
},
{
requestId: '3',
_startTime: 10,
_endTime: 19,
_startTime: 7,
_endTime: 17,
_isLinkPreload: false,
url: 'http://sub.example.com/secondary_script.js',
},
{
requestId: '4',
_startTime: 7,
_endTime: 17,
_isLinkPreload: false,
_url: 'http://www.example.com/script.js',
url: 'http://otherdomain.com/terniary_script.js',
},
];
const chains = {
Expand All @@ -55,12 +64,19 @@ describe('Performance: uses-rel-preload audit', () => {
'2': {
children: {
'3': {
request: networkRecords[0],
children: {
'4': {
request: networkRecords[0],
children: {},
},
'5': {
request: networkRecords[1],
children: {},
},
'6': {
request: networkRecords[2],
children: {},
},
},
},
},
Expand All @@ -72,7 +88,7 @@ describe('Performance: uses-rel-preload audit', () => {
return UsesRelPreload.audit(mockArtifacts(networkRecords, chains, mainResource))
.then(output => {
assert.equal(output.rawValue, 9000);
assert.equal(output.details.items.length, 1);
assert.equal(output.details.items.length, 2);
});
});

Expand All @@ -82,7 +98,7 @@ describe('Performance: uses-rel-preload audit', () => {
requestId: '3',
_startTime: 10,
_isLinkPreload: true,
_url: 'http://www.example.com/script.js',
url: 'http://www.example.com/script.js',
},
];
const chains = {
Expand Down

0 comments on commit 32a4b20

Please sign in to comment.