|
| 1 | +import _string |
1 | 2 | import json |
2 | 3 | import logging |
3 | 4 | import os |
|
10 | 11 | from string import Formatter |
11 | 12 | from typing import Dict, Optional |
12 | 13 |
|
13 | | -import _string |
14 | 14 | import discord |
15 | 15 | from discord.ext import commands |
16 | 16 |
|
@@ -345,6 +345,8 @@ class _Default: |
345 | 345 |
|
346 | 346 |
|
347 | 347 | class SafeFormatter(Formatter): |
| 348 | + max_width = 100 |
| 349 | + |
348 | 350 | def get_field(self, field_name, args, kwargs): |
349 | 351 | first, rest = _string.formatter_field_name_split(field_name) |
350 | 352 |
|
@@ -372,6 +374,18 @@ def get_field(self, field_name, args, kwargs): |
372 | 374 | pass |
373 | 375 | return "<Invalid>", first |
374 | 376 |
|
| 377 | + # https://stackoverflow.com/a/79533521 |
| 378 | + def format_field(self, value, format_spec): |
| 379 | + # format_spec is an expanded format spec. It cannot have any indirect |
| 380 | + # width references. |
| 381 | + for x in re.findall(r"\b\d+\b", format_spec): |
| 382 | + if int(x) > self.max_width: |
| 383 | + # This intentionally does not include the max width in the |
| 384 | + # error message, to make an attacker have to work a bit |
| 385 | + # harder. |
| 386 | + raise ValueError(f"Invalid format string width" f" specification {x} is too large") |
| 387 | + return super().format_field(value, format_spec) |
| 388 | + |
375 | 389 |
|
376 | 390 | class UnseenFormatter(Formatter): |
377 | 391 | def get_value(self, key, args, kwds): |
|
0 commit comments