Skip to content

Commit

Permalink
Add URL validation check and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justinribeiro committed Sep 8, 2018
1 parent 7ec9a22 commit 1cce214
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lighthouse-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const log = require('lighthouse-logger');
const ChromeProtocol = require('./gather/connections/cri.js');
const Config = require('./config/config');

const URL = require('./lib/url-shim.js');
const LHError = require('./lib/lh-error.js');

/** @typedef {import('./gather/connections/connection.js')} Connection */

/*
Expand All @@ -36,6 +39,11 @@ const Config = require('./config/config');
* @return {Promise<LH.RunnerResult|undefined>}
*/
async function lighthouse(url, flags = {}, configJSON, connection) {
// verify the url is valid and that protocol is allowed
if (url && (!URL.isValid(url) || !URL.isProtocolAllowed(url))) {
throw new LHError(LHError.errors.INVALID_URL);
}

// set logging preferences, assume quiet
flags.logLevel = flags.logLevel || 'error';
log.setLevel(flags.logLevel);
Expand Down
24 changes: 21 additions & 3 deletions lighthouse-core/test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Module Tests', function() {
});

it('should throw an error when the second parameter is not an object', function() {
return lighthouse('SOME_URL', 'flags')
return lighthouse('chrome://version', 'flags')
.then(() => {
throw new Error('Should not have resolved when second arg is not an object');
}, err => {
Expand All @@ -61,7 +61,7 @@ describe('Module Tests', function() {
});

it('should throw an error when the config is invalid', function() {
return lighthouse('SOME_URL', {}, {})
return lighthouse('chrome://version', {}, {})
.then(() => {
throw new Error('Should not have resolved when second arg is not an object');
}, err => {
Expand All @@ -70,7 +70,7 @@ describe('Module Tests', function() {
});

it('should throw an error when the config contains incorrect audits', function() {
return lighthouse('SOME_URL', {}, {
return lighthouse('chrome://version', {}, {
passes: [{
gatherers: [
'viewport',
Expand All @@ -87,6 +87,24 @@ describe('Module Tests', function() {
});
});

it('should throw an error when the url is invalid', function() {
return lighthouse('https:/i-am-not-valid', {}, {})
.then(() => {
throw new Error('Should not have resolved when url is invalid');
}, err => {
assert.ok(err);
});
});

it('should throw an error when the url is invalid protocol (file:///)', function() {
return lighthouse('file:///a/fake/index.html', {}, {})
.then(() => {
throw new Error('Should not have resolved when url is file:///');
}, err => {
assert.ok(err);
});
});

it('should return formatted LHR when given no categories', function() {
const exampleUrl = 'https://www.reddit.com/r/nba';
return lighthouse(exampleUrl, {
Expand Down

0 comments on commit 1cce214

Please sign in to comment.