Skip to content

Commit a97c3e9

Browse files
authored
fix: regression in allowed image links using absolute or relative paths (#339)
1 parent 395f380 commit a97c3e9

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/runtime/parser/utils/props.ts

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ function isAnchorLinkAllowed(value: string) {
1515
.replace(/&#(\d+);?/g, '')
1616
.replace(/&[a-z]+;?/gi, '')
1717

18+
// Check if the URL is a relative path
19+
if (urlSanitized.startsWith('/') || urlSanitized.startsWith('./') || urlSanitized.startsWith('../')) {
20+
return true
21+
}
22+
1823
try {
1924
const url = new URL(urlSanitized)
2025
if (unsafeLinkPrefix.some(prefix => url.protocol.toLowerCase().startsWith(prefix))) {

test/markdown/images.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect, it } from 'vitest'
2+
import { parseMarkdown } from '../utils/parser'
3+
4+
const md = `
5+
# Some headline
6+
7+
Following are some image links:
8+
9+
![absolute image](/path/to/my/image.png)
10+
11+
![relative image](../relative/path/to/image.png)
12+
13+
![image](https://placehold.co/200x200.png)
14+
15+
`.trim()
16+
17+
it('Sanity test for image links, all should be allowed', async () => {
18+
const { body } = await parseMarkdown(md)
19+
20+
expect(body.children[2].children[0].tag).toEqual('img')
21+
expect(body.children[2].children[0].props.src).toEqual('/path/to/my/image.png')
22+
23+
expect(body.children[3].children[0].tag).toEqual('img')
24+
expect(body.children[3].children[0].props.src).toEqual('../relative/path/to/image.png')
25+
26+
expect(body.children[4].children[0].tag).toEqual('img')
27+
expect(body.children[4].children[0].props.src).toEqual('https://placehold.co/200x200.png')
28+
})

0 commit comments

Comments
 (0)