Skip to content

Commit 5eb7732

Browse files
Implement URL normalization in code snippet handler
Add URL normalization checks using yarl in code snippets.
1 parent 8e8d5b0 commit 5eb7732

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

bot/exts/info/code_snippets.py

Lines changed: 11 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,16 @@ 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("Received code snippet url %s which attempted to circumvent url normalisation.", unsanitized)
285+
continue
275286
try:
276287
result = await handler(**match.groupdict())
277288
except ClientResponseError as error:

0 commit comments

Comments
 (0)