Browse Source

Punycode domain names only in known schemas

In urls like `skype:XXX` it prevents XXX to be parsed as domain
name and encoded with punycode.
pull/82/head
Alex Kocharin 10 years ago
parent
commit
b05bfd6e8b
  1. 32
      lib/index.js
  2. 16
      test/fixtures/markdown-it/normalize.txt

32
lib/index.js

@ -21,7 +21,7 @@ var config = {
}; };
var BAD_PROTOCOLS = [ 'vbscript', 'javascript', 'file' ]; var BAD_PROTOCOLS = [ 'vbscript', 'javascript', 'file' ];
function validateLink(url) { function validateLink(url) {
// url should be normalized at this point, and existing entities are decoded // url should be normalized at this point, and existing entities are decoded
@ -34,13 +34,23 @@ function validateLink(url) {
return true; return true;
} }
var RECODE_HOSTNAME_FOR = [ 'http:', 'https:', 'mailto:' ];
function normalizeLink(url) { function normalizeLink(url) {
var parsed = mdurl.parse(url, true); var parsed = mdurl.parse(url, true);
if (parsed.hostname) { if (parsed.hostname) {
try { // Encode hostnames in urls like:
parsed.hostname = punycode.toASCII(parsed.hostname); // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
} catch(er) {} //
// We don't encode unknown schemas, because it's likely that we encode
// something we shouldn't (e.g. `skype:name` treated as `skype:host`)
//
if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
try {
parsed.hostname = punycode.toASCII(parsed.hostname);
} catch(er) {}
}
} }
return mdurl.encode(mdurl.format(parsed)); return mdurl.encode(mdurl.format(parsed));
@ -50,9 +60,17 @@ function normalizeLinkText(url) {
var parsed = mdurl.parse(url, true); var parsed = mdurl.parse(url, true);
if (parsed.hostname) { if (parsed.hostname) {
try { // Encode hostnames in urls like:
parsed.hostname = punycode.toUnicode(parsed.hostname); // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
} catch(er) {} //
// We don't encode unknown schemas, because it's likely that we encode
// something we shouldn't (e.g. `skype:name` treated as `skype:host`)
//
if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
try {
parsed.hostname = punycode.toUnicode(parsed.hostname);
} catch(er) {}
}
} }
return mdurl.decode(mdurl.format(parsed)); return mdurl.decode(mdurl.format(parsed));

16
test/fixtures/markdown-it/normalize.txt

@ -43,6 +43,22 @@ Invalid punycode (non-ascii):
<p><a href="http://xn--xn---emd.com/">http://xn–γ.com/</a></p> <p><a href="http://xn--xn---emd.com/">http://xn–γ.com/</a></p>
. .
Two slashes should start a domain:
.
[](//☃.net/)
.
<p><a href="//xn--n3h.net/"></a></p>
.
Don't encode domains in unknown schemas:
.
[](skype:γγγ)
.
<p><a href="skype:%CE%B3%CE%B3%CE%B3"></a></p>
.
Should auto-add protocol to autolinks: Should auto-add protocol to autolinks:
. .

Loading…
Cancel
Save