Skip to content

Commit

Permalink
fix(is_external_link): handle invalid url
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Feb 16, 2020
1 parent 7e084de commit 12bdb3a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/is_external_link.js
Expand Up @@ -16,8 +16,15 @@ function isExternalLink(input, sitehost, exclude) {
sitehost = parse(sitehost).hostname || sitehost;

if (!sitehost) return false;
// handle relative url
const data = new URL(input, `http://${sitehost}`);

// handle relative url and invalid url
let data;
try {
data = new URL(input, `http://${sitehost}`);
} catch (e) { }

// if input is invalid url, data should be undefined
if (typeof data !== 'object') return false;

// handle mailto: javascript: vbscript: and so on
if (data.origin === 'null') return false;
Expand Down
4 changes: 4 additions & 0 deletions test/is_external_link.spec.js
Expand Up @@ -9,6 +9,10 @@ describe('isExternalLink', () => {

const isExternalLink = require('../lib/is_external_link');

it('invalid url', () => {
isExternalLink('https://localhost:4000你好', ctx.config.url).should.eql(false);
});

it('external link', () => {
isExternalLink('https://hexo.io/', ctx.config.url).should.eql(true);
});
Expand Down

0 comments on commit 12bdb3a

Please sign in to comment.