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 security flaw when linkTarget option contains _blank #398

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions lib/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ rules.paragraph_close = function(tokens, idx /*, options, env */) {

rules.link_open = function(tokens, idx, options /* env */) {
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
var target = options.linkTarget ? (' target="' + options.linkTarget + '"') : '';
return '<a href="' + escapeHtml(tokens[idx].href) + '"' + title + target + '>';

var linkTarget = options.linkTarget;
var target = linkTarget ? (' target="' + options.linkTarget + '"') : '';
var rel = linkTarget && linkTarget.indexOf('_blank') !== -1 ? ' rel="noopener noreferrer"' : '';

return '<a href="' + escapeHtml(tokens[idx].href) + '"' + title + target + rel + '>';
};
rules.link_close = function(/* tokens, idx, options, env */) {
return '</a>';
Expand Down
11 changes: 10 additions & 1 deletion test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,20 @@ describe('Link target', function () {
});

it('Should add target to link when linkTarget is specified in options', function () {
var md = new Remarkable({ linkTarget: '_self' });

assert.strictEqual(
md.render('[test](http://example.com)'),
'<p><a href="http://example.com" target="_self">test</a></p>\n'
);
});

it('Should add rel attr when linkTarget contains _blank', function () {
var md = new Remarkable({ linkTarget: '_blank' });

assert.strictEqual(
md.render('[test](http://example.com)'),
'<p><a href="http://example.com" target="_blank">test</a></p>\n'
'<p><a href="http://example.com" target="_blank" rel="noopener noreferrer">test</a></p>\n'
);
});

Expand Down