Skip to content

Commit 2b66710

Browse files
committed
Fix #3291: Resolve code scanning alert for URL sanitization
1 parent 5170035 commit 2b66710

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

core/utils.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,17 @@ def is_image_url(url: str, **kwargs) -> str:
147147
bool
148148
Whether the URL is a valid image URL.
149149
"""
150-
if url.startswith("https://gyazo.com") or url.startswith("http://gyazo.com"):
151-
# gyazo support
152-
url = re.sub(
153-
r"(http[s]?:\/\/)((?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)",
154-
r"\1i.\2.png",
155-
url,
156-
)
150+
try:
151+
result = parse.urlparse(url)
152+
if result.netloc == 'gyazo.com' and result.scheme in ['http', 'https']:
153+
# gyazo support
154+
url = re.sub(
155+
r"(https?://)((?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|%[0-9a-fA-F][0-9a-fA-F])+)",
156+
r"\1i.\2.png",
157+
url,
158+
)
159+
except ValueError:
160+
pass
157161

158162
return parse_image_url(url, **kwargs)
159163

0 commit comments

Comments
 (0)