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(network): handle invalid NetworkRequests #6157

Merged
merged 4 commits into from
Oct 2, 2018
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
6 changes: 3 additions & 3 deletions lighthouse-core/lib/network-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class NetworkRecorder extends EventEmitter {
*/
_findRealRequest(requestId) {
let request = this._recordsById.get(requestId);
if (!request) return undefined;
if (!request || !request.isValid) return undefined;

while (request.redirectDestination) {
request = request.redirectDestination;
Expand All @@ -339,8 +339,8 @@ class NetworkRecorder extends EventEmitter {
// playback all the devtools messages to recreate network records
devtoolsLog.forEach(message => networkRecorder.dispatch(message));

// get out the list of records
const records = networkRecorder.getRecords();
// get out the list of records & filter out invalid records
const records = networkRecorder.getRecords().filter(record => record.isValid);
exterkamp marked this conversation as resolved.
Show resolved Hide resolved

// create a map of all the records by URL to link up initiator
const recordsByURL = new Map();
Expand Down
12 changes: 10 additions & 2 deletions lighthouse-core/lib/network-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module.exports = class NetworkRequest {
this.url = '';
this.protocol = '';
this.isSecure = false;
this.isValid = false;
this.parsedURL = /** @type {ParsedURL} */ ({scheme: ''});
this.documentURL = '';

Expand Down Expand Up @@ -124,8 +125,14 @@ module.exports = class NetworkRequest {
*/
onRequestWillBeSent(data) {
this.requestId = data.requestId;

const url = new URL(data.request.url);
let url;
try {
// try to construct the url and fill in request
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
url = new URL(data.request.url);
} catch (e) {
// isValid left false, all other data is blank
return;
}
this.url = data.request.url;
this.documentURL = data.documentURL;
this.parsedURL = {
Expand All @@ -146,6 +153,7 @@ module.exports = class NetworkRequest {

this.frameId = data.frameId;
this.isLinkPreload = data.initiator.type === 'preload' || !!data.request.isLinkPreload;
this.isValid = true;
}

onRequestServedFromCache() {
Expand Down
60 changes: 60 additions & 0 deletions lighthouse-core/test/lib/network-recorder-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,66 @@ describe('network recorder', function() {
assert.equal(mainDocument.resourceType, 'Document');
});

it('recordsFromLogs ignores invalid records', function() {
const logs = [
{ // valid request
'method': 'Network.requestWillBeSent',
'params': {
'requestId': '1',
'frameId': '1',
'loaderId': '1',
'documentURL': 'https://www.example.com',
'request': {
'url': 'https://www.example.com',
'method': 'GET',
'headers': {
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) ' +
' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2774.2 Safari/537.36',
},
'mixedContentType': 'none',
'initialPriority': 'VeryHigh',
},
'timestamp': 107988.912007,
'wallTime': 1466620735.21187,
'initiator': {
'type': 'other',
},
'type': 'Document',
},
},
{ // invalid request
'method': 'Network.requestWillBeSent',
'params': {
'requestId': '2',
'loaderId': '2',
'documentURL': 'https://www.example.com',
'request': {
'url': 'https:',
'method': 'GET',
'headers': {
'Origin': 'https://www.example.com',
},
'mixedContentType': 'blockable',
'initialPriority': 'VeryLow',
'referrerPolicy': 'no-referrer-when-downgrade',
},
'timestamp': 831346.969485,
'wallTime': 1538411434.25547,
'initiator': {
'type': 'other',
},
'type': 'Font',
'frameId': '1',
'hasUserGesture': false,
},
},
];
assert.equal(logs.length, 2);
const records = NetworkRecorder.recordsFromLogs(logs);
assert.equal(records.length, 1);
});

describe('#findNetworkQuietPeriods', () => {
function record(data) {
const url = data.url || 'https://example.com';
Expand Down