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: external link ignore mailto: & javascript: #3812

Merged
merged 4 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function externalLinkFilter(data) {
config.external_link.field !== 'post') return;

data.content = data.content.replace(/<a.*?(href=['"](.*?)['"]).*?>/gi, (str, hrefStr, href) => {
if (/target=/gi.test(str) || !isExternal(href, config)) return str;
if (/target=/gi.test(str) || href.startsWith('mailto:') || href.startsWith('javascript:') || !isExternal(href, config)) return str;
Copy link
Contributor

Choose a reason for hiding this comment

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

there are countless of types (data:, vbscript: and so on), can test whether it's a link protocol (http/ftp) or not by testing whether URL(href).origin is null. See encodeURL().

Copy link
Member Author

@SukkaW SukkaW Oct 28, 2019

Choose a reason for hiding this comment

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

OK, I will change to that.


It required WHATWG URL API. I might mitgate external_link filter to use it in this PR.


if (/rel=/gi.test(str)) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
Expand Down
32 changes: 20 additions & 12 deletions test/scripts/filters/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,27 @@ describe('External link - post', () => {
'# External link test',
'1. External link',
'<a href="https://hexo.io/">Hexo</a>',
'2. External link with "rel" Attribute',
'2. Link with hash (#), mailto: , javascript: shouldn\'t be processed',
'<a href="#top">Hexo</a>',
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

'<a href="mailto:[email protected]">Hexo</a>',
'<a href="javascript:alert(\'Hexo is awesome!\');">Hexo</a>',
'3. External link with "rel" Attribute',
'<a rel="external" href="https://hexo.io/">Hexo</a>',
'<a href="https://hexo.io/" rel="external">Hexo</a>',
'<a rel="noopenner" href="https://hexo.io/">Hexo</a>',
'<a href="https://hexo.io/" rel="noopenner">Hexo</a>',
'<a rel="external noopenner" href="https://hexo.io/">Hexo</a>',
'<a href="https://hexo.io/" rel="external noopenner">Hexo</a>',
'3. External link with Other Attributes',
'4. External link with Other Attributes',
'<a class="img" href="https://hexo.io/">Hexo</a>',
'<a href="https://hexo.io/" class="img">Hexo</a>',
'4. Internal link',
'5. Internal link',
'<a href="/archives/foo.html">Link</a>',
'5. Ignore links have "target" attribute',
'6. Ignore links have "target" attribute',
'<a href="https://hexo.io/" target="_blank">Hexo</a>',
'6. Ignore links don\'t have "href" attribute',
'7. Ignore links don\'t have "href" attribute',
'<a>Anchor</a>',
'7. Ignore links whose hostname is same as config',
'8. Ignore links whose hostname is same as config',
'<a href="https://example.com">Example Domain</a>'
].join('\n');

Expand All @@ -235,23 +239,27 @@ describe('External link - post', () => {
'# External link test',
'1. External link',
'<a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>',
'2. External link with "rel" Attribute',
'2. Link with hash (#), mailto: , javascript: shouldn\'t be processed',
'<a href="#top">Hexo</a>',
'<a href="mailto:[email protected]">Hexo</a>',
'<a href="javascript:alert(\'Hexo is awesome!\');">Hexo</a>',
'3. External link with "rel" Attribute',
'<a rel="external noopener" href="https://hexo.io/" target="_blank">Hexo</a>',
'<a href="https://hexo.io/" target="_blank" rel="external noopener">Hexo</a>',
'<a rel="noopenner" href="https://hexo.io/" target="_blank">Hexo</a>',
'<a href="https://hexo.io/" target="_blank" rel="noopenner">Hexo</a>',
'<a rel="external noopenner" href="https://hexo.io/" target="_blank">Hexo</a>',
'<a href="https://hexo.io/" target="_blank" rel="external noopenner">Hexo</a>',
'3. External link with Other Attributes',
'4. External link with Other Attributes',
'<a class="img" href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>',
'<a href="https://hexo.io/" target="_blank" rel="noopener" class="img">Hexo</a>',
'4. Internal link',
'5. Internal link',
'<a href="/archives/foo.html">Link</a>',
'5. Ignore links have "target" attribute',
'6. Ignore links have "target" attribute',
'<a href="https://hexo.io/" target="_blank">Hexo</a>',
'6. Ignore links don\'t have "href" attribute',
'7. Ignore links don\'t have "href" attribute',
'<a>Anchor</a>',
'7. Ignore links whose hostname is same as config',
'8. Ignore links whose hostname is same as config',
'<a href="https://example.com">Example Domain</a>'
].join('\n'));
});
Expand Down