Skip to content

Commit d8e3787

Browse files
committed
Fix double encoding in new url transform
Closes GH-797.
1 parent 55d8d83 commit d8e3787

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

lib/index.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
import {unreachable} from 'devlop'
8585
import {toJsxRuntime} from 'hast-util-to-jsx-runtime'
8686
import {urlAttributes} from 'html-url-attributes'
87-
import {sanitizeUri} from 'micromark-util-sanitize-uri'
8887
// @ts-expect-error: untyped.
8988
import {Fragment, jsx, jsxs} from 'react/jsx-runtime'
9089
import remarkParse from 'remark-parse'
@@ -297,5 +296,26 @@ export function Markdown(options) {
297296
* Safe URL.
298297
*/
299298
export function defaultUrlTransform(value) {
300-
return sanitizeUri(value, safeProtocol)
299+
// Same as:
300+
// <https://github.com/micromark/micromark/blob/929275e/packages/micromark-util-sanitize-uri/dev/index.js#L34>
301+
// But without the `encode` part.
302+
const colon = value.indexOf(':')
303+
const questionMark = value.indexOf('?')
304+
const numberSign = value.indexOf('#')
305+
const slash = value.indexOf('/')
306+
307+
if (
308+
// If there is no protocol, it’s relative.
309+
colon < 0 ||
310+
// If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
311+
(slash > -1 && colon > slash) ||
312+
(questionMark > -1 && colon > questionMark) ||
313+
(numberSign > -1 && colon > numberSign) ||
314+
// It is a protocol, it should be allowed.
315+
safeProtocol.test(value.slice(0, colon))
316+
) {
317+
return value
318+
}
319+
320+
return ''
301321
}

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
"hast-util-to-jsx-runtime": "^2.0.0",
8282
"html-url-attributes": "^3.0.0",
8383
"mdast-util-to-hast": "^13.0.0",
84-
"micromark-util-sanitize-uri": "^2.0.0",
8584
"remark-parse": "^11.0.0",
8685
"remark-rehype": "^11.0.0",
8786
"unified": "^11.0.0",

test.jsx

+7
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ test('react-markdown', async function (t) {
326326
)
327327
})
328328

329+
await t.test('should support hash (`&`) in a URL', function () {
330+
assert.equal(
331+
asHtml(<Markdown children="[](a?b&c=d)" />),
332+
'<p><a href="a?b&amp;c=d"></a></p>'
333+
)
334+
})
335+
329336
await t.test('should support hash (`#`) in a URL', function () {
330337
assert.equal(
331338
asHtml(<Markdown children="[](a#javascript:alert(1))" />),

0 commit comments

Comments
 (0)