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

Allow more relative links in safe mode (issue #517) #520

Merged
merged 5 commits into from
Jul 20, 2023
Merged
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
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## python-markdown2 2.4.10 (not yet released)

(nothing yet)
- [pull #520] Allow more relative links in safe mode (issue #517)


## python-markdown2 2.4.9
Expand Down
25 changes: 19 additions & 6 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,12 +1507,25 @@ def _protect_url(self, url):
self._escape_table[url] = key
return key

# _safe_href is copied from pagedown's Markdown.Sanitizer.js
# From: https://github.com/StackExchange/pagedown/blob/master/LICENSE.txt
# Original Showdown code copyright (c) 2007 John Fraser
# Modifications and bugfixes (c) 2009 Dana Robinson
# Modifications and bugfixes (c) 2009-2014 Stack Exchange Inc.
_safe_href = re.compile(r'^((https?|ftp):\/\/|\/|\.|#)[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)*[\]$]*$', re.I)
_safe_protocols = r'(?:https?|ftp):\/\/|(?:mailto|tel):'

@property
def _safe_href(self):
'''
_safe_href is adapted from pagedown's Markdown.Sanitizer.js
From: https://github.com/StackExchange/pagedown/blob/master/LICENSE.txt
Original Showdown code copyright (c) 2007 John Fraser
Modifications and bugfixes (c) 2009 Dana Robinson
Modifications and bugfixes (c) 2009-2014 Stack Exchange Inc.
'''
safe = r'-\w'
# omitted ['"<>] for XSS reasons
less_safe = r'#/\.!#$%&\(\)\+,/:;=\?@\[\]^`\{\}\|~'
# dot seperated hostname, optional port number, not followed by protocol seperator
domain = r'(?:[%s]+(?:\.[%s]+)*)(?:(?<!tel):\d+/?)?(?![^:/]*:/*)' % (safe, safe)
fragment = r'[%s]*' % (safe + less_safe)

return re.compile(r'^(?:(%s)?(%s)(%s)|(#|\.{,2}/)(%s))$' % (self._safe_protocols, domain, fragment, fragment), re.I)

def _do_links(self, text):
"""Turn Markdown link shortcuts into XHTML <a> and <img> tags.
Expand Down
38 changes: 37 additions & 1 deletion test/tm-cases/link_safe_urls.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
<h1>Normal links</h1>

<p><a href="https://www.example.com">Safe link 1</a></p>

<p><a href="http://www.example.com">Safe link 2</a></p>

<p><a href="ftp://www.example.com">Safe link 3</a></p>

<p><a href="#anchor">Safe link 4</a></p>
<p><a href="mailto:emailaddress@server.com">Safe link 4</a></p>

<p><a href="tel:0123456789">Safe link 5</a></p>

<p><a href="#anchor">Safe link 6</a></p>

<h1>Bad protocols</h1>

<p><a href="#">Unsafe link 1</a></p>

<p><a href="#">Unsafe link 2</a></p>

<h1>Relative links</h1>

<p><a href="example">Safe link 1</a></p>

<p><a href="./example">Safe link 2</a></p>

<p><a href="../../example">Safe link 3</a></p>

<p><a href="/example">Safe link 4</a></p>

<p><a href="#">Unsafe link 1</a></p>

<h1>Edge cases</h1>

<p><a href="www.example.com/abc:def">Safe link 1</a></p>

<p><a href="www.example.com/abc:/def">Safe link 2</a></p>

<p><a href="https://www.example.com:4200">Safe link 3</a></p>

<p><a href="https://www.example.com:4200/abcdef">Safe link 4</a></p>

<p><a href="#">Unsafe link 1</a></p>

<p><a href="#">Unsafe link 2</a></p>

<p><a href="#">Unsafe link 3</a></p>
40 changes: 38 additions & 2 deletions test/tm-cases/link_safe_urls.text
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
# Normal links

[Safe link 1](https://www.example.com)

[Safe link 2](http://www.example.com)

[Safe link 3](ftp://www.example.com)

[Safe link 4](#anchor)
[Safe link 4](mailto:emailaddress@server.com)

[Safe link 5](tel:0123456789)

[Safe link 6](#anchor)

# Bad protocols

[Unsafe link 1](unknown://www.example.com)

[Unsafe link 2](example)
[Unsafe link 2](mailfrom:www.example.com)

# Relative links

[Safe link 1](example)

[Safe link 2](./example)

[Safe link 3](../../example)

[Safe link 4](/example)

[Unsafe link 1](.../www.example.com)

# Edge cases

[Safe link 1](www.example.com/abc:def)

[Safe link 2](www.example.com/abc:/def)

[Safe link 3](https://www.example.com:4200)

[Safe link 4](https://www.example.com:4200/abcdef)

[Unsafe link 1](unknown://www.example.com://abc)

[Unsafe link 2](C:/Windows/System32)

[Unsafe link 3](C:\Windows\System32)
Loading