Repository: aiogram/aiogram
Pull request #1630 (2025-01-14 18:53:02)

Fix `BAD_PATTERN` regex in deeplinking process.
# Description

The regex pattern which identifies the "bad characters" for [deeplink payload](https://core.telegram.org/bots/features#deep-linking) is not correct:

```python
import re

print(f"{ord('A')=}")
print(f"{ord('Z')=}")
print(f"{ord('a')=}")
print(f"{ord('z')=}")
print("-----------------------------------")
for i in range(91, 97):
    print(f"{i}: {chr(i)}")
print("-----------------------------------")
print(re.search(r"[^A-z0-9-]", "hi`bye"))      # None
print(re.search(r"[^A-Za-z0-9-_]", "hi`bye"))  # <re.Match object; span=(2, 3), match='`'>
```
There is a gap between `Z` and `a`. We should explicitly include the ranges `A-Z` and `a-z` to avoid accepting characters like backtick.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

# How Has This Been Tested?

I modified the `WRONG_PAYLOADS` list in `tests/test_utils/test_deep_linking.py` to demonstrate the bug.

**Test Configuration**:
* Operating System: MacOS
* Python version: 3.12

# Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes


github-actions[bot]: # :x: Changelog is required!

You need to add a brief description of the changes to the `CHANGES` directory.

Changes file should be named like `<issue or PR number>.<category>.rst`,
example `1234.bugfix.rst` where `1234` is the PR or issue number and `bugfix` is the category.

The content of the file should be a brief description of the changes in
the PR in the format of a description of what has been done.

Possible categories are: `feature`, `bugfix`, `doc`, `removal` and `misc`.

Patch:
diff --git a/CHANGES/1630.bugfix.rst b/CHANGES/1630.bugfix.rst
new file mode 100644
index 00000000..e083dc1f
--- /dev/null
+++ b/CHANGES/1630.bugfix.rst
@@ -0,0 +1,1 @@
+Fix the regex pattern that finds the "bad characters" for deeplink payload.
\ No newline at end of file
diff --git a/CHANGES/1631.misc.rst b/CHANGES/1631.misc.rst
new file mode 100644
index 00000000..76e2fa47
--- /dev/null
+++ b/CHANGES/1631.misc.rst
@@ -0,0 +1,1 @@
+ Increased max :code:`redis` version support from “<5.1.0” to “<5.3.0”
diff --git a/aiogram/utils/deep_linking.py b/aiogram/utils/deep_linking.py
index 19cc64c6..fe2764f7 100644
--- a/aiogram/utils/deep_linking.py
+++ b/aiogram/utils/deep_linking.py
@@ -18,7 +18,7 @@ from aiogram.utils.payload import decode_payload, encode_payload
 if TYPE_CHECKING:
     from aiogram import Bot
 
-BAD_PATTERN = re.compile(r"[^A-z0-9-]")
+BAD_PATTERN = re.compile(r"[^a-zA-Z0-9-_]")
 
 
 async def create_start_link(
diff --git a/pyproject.toml b/pyproject.toml
index 1e2f2572..a5b7cd8d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -60,7 +60,7 @@ fast = [
     "aiodns>=3.0.0",
 ]
 redis = [
-    "redis[hiredis]>=5.0.1,<5.1.0",
+    "redis[hiredis]>=5.0.1,<5.3.0",
 ]
 mongo = [
     "motor>=3.3.2,<3.7.0",


Test patch:
diff --git a/tests/test_utils/test_deep_linking.py b/tests/test_utils/test_deep_linking.py
index 85a6027b..c5f5259a 100644
--- a/tests/test_utils/test_deep_linking.py
+++ b/tests/test_utils/test_deep_linking.py
@@ -10,12 +10,14 @@ PAYLOADS = [
     "aaBBccDDeeFF5544332211",
     -12345678901234567890,
     12345678901234567890,
+    "underscore_and-dash",
 ]
 WRONG_PAYLOADS = [
     "@BotFather",
     "Some:special$characters#=",
     "spaces spaces spaces",
     1234567890123456789.0,
+    "has`backtick",
 ]