Skip to content

Commit 5aa0398

Browse files
Implement URL normalization in code snippet handler
Add URL normalization checks using yarl in code snippets.
1 parent 89849c7 commit 5aa0398

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

bot/exts/info/code_snippets.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from urllib.parse import quote_plus
66

77
import discord
8+
import yarl
89
from aiohttp import ClientResponseError
910
from discord.ext.commands import Cog
1011

@@ -272,6 +273,20 @@ async def _parse_snippets(self, content: str) -> str:
272273

273274
for pattern, handler in self.pattern_handlers:
274275
for match in pattern.finditer(content):
276+
# ensure that the matched URL meets url normalization rules.
277+
# parsing with yarl resolves all parent urls such as `/../`,
278+
# we then check the regex again to make sure our groups stay the same
279+
unsanitized = match.group(0)
280+
normalized = str(yarl.URL(unsanitized))
281+
if normalized != unsanitized:
282+
match = pattern.fullmatch(normalized)
283+
if not match:
284+
log.info(
285+
"Received code snippet url %s which "
286+
"attempted to circumvent url normalisation.",
287+
unsanitized
288+
)
289+
continue
275290
try:
276291
result = await handler(**match.groupdict())
277292
except ClientResponseError as error:

0 commit comments

Comments
 (0)