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

fix(is_external_link): handle invalid url #183

Merged
merged 2 commits into from
Mar 6, 2020
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
3 changes: 1 addition & 2 deletions lib/full_url_for.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ function fullUrlForHelper(path = '/') {

// cacheId is designed to works across different hexo.config & options
return cache.apply(`${config.url}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => {
const pathRegex = /^(\/\/|http(s)?:)/;
if (pathRegex.test(path)) return path;
if (/^(\/\/|http(s)?:)/.test(path)) return path;

const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);
Expand Down
14 changes: 12 additions & 2 deletions lib/is_external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ const cache = new Cache();

function isExternalLink(input, sitehost, exclude) {
return cache.apply(`${input}-${sitehost}-${exclude}`, () => {
// Return false early for internal link
if (!/^(\/\/|http(s)?:)/.test(input)) return false;

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) { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about hexo.log.warn(...) to alert the user that there has an invalid URL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since hexo-util has no hexo-log's dependency, there is no way we can use hexo.log.warn in hexo-util.


// 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
5 changes: 1 addition & 4 deletions lib/url_for.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ function urlForHelper(path = '/', options) {

// cacheId is designed to works across different hexo.config & options
return cache.apply(`${config.url}-${root}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => {
const pathRegex = /^(#|\/\/|http(s)?:)/;
if (pathRegex.test(path)) {
return path;
}
if (/^(#|\/\/|http(s)?:)/.test(path)) return path;

const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);
Expand Down
6 changes: 6 additions & 0 deletions test/is_external_link.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ 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);
isExternalLink('//hexo.io/', ctx.config.url).should.eql(true);
});

it('internal link', () => {
isExternalLink('https://example.com', ctx.config.url).should.eql(false);
isExternalLink('//example.com', ctx.config.url).should.eql(false);
isExternalLink('//example.com/archives/foo.html', ctx.config.url).should.eql(false);
isExternalLink('/archives/foo.html', ctx.config.url).should.eql(false);
isExternalLink('/archives//hexo.io', ctx.config.url).should.eql(false);
});

it('hash, mailto, javascript', () => {
Expand Down