Skip to content

Commit db15f63

Browse files
authored
Merge pull request #3409 from onerandomusername/patch-1
Implement URL normalization in code snippet handler
2 parents 89849c7 + 4b36bfc commit db15f63

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-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 an absolute url 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:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies = [
2525
"sentry-sdk==2.22.0",
2626
"tenacity==9.0.0",
2727
"tldextract==5.1.3",
28+
"yarl==1.22.0",
2829
]
2930
name = "bot"
3031
version = "1.0.1"

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)