Skip to content

Commit c67e3c5

Browse files
committed
🔒 Enhance security: Refactor SQL and JS injection patterns; add ReDoS test cases for input validation.
1 parent e8e0fc9 commit c67e3c5

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

packages/models-library/src/models_library/string_types.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
_LONG_TRUNCATED_STR_MAX_LENGTH: Final[int] = 65536 # same as github descriptions
2020

2121
_SQL_INJECTION_PATTERN: Final[re.Pattern] = re.compile(
22-
r"(\b(SELECT|INSERT|UPDATE|DELETE|DROP|UNION|ALTER|CREATE|EXEC|TRUNCATE|MERGE|GRANT|REVOKE|COMMIT|ROLLBACK|DECLARE|CAST|CONVERT)\b|--|;|/\*|\*/|')",
23-
re.IGNORECASE,
22+
r"(?i)\b(?:SELECT|INSERT|UPDATE|DELETE|DROP|UNION|ALTER|CREATE|EXEC|TRUNCATE|MERGE|GRANT|REVOKE|COMMIT|ROLLBACK|DECLARE|CAST|CONVERT)\b|--|;|/\*|\*/|'",
2423
)
2524
_JS_INJECTION_PATTERN: Final[re.Pattern] = re.compile(
26-
r"(<\s*script.*?>|</\s*script\s*>|<\s*iframe.*?>|</\s*iframe\s*>|<\s*object.*?>|</\s*object\s*>|<\s*embed.*?>|</\s*embed\s*>|<\s*link[^>]*href\s*=\s*[\"']?\s*javascript:|vbscript:|javascript:|data:text/html|&#x6A;avascript:|&#106;avascript:|<\s*img[^>]*onerror\s*=|<\s*svg[^>]*onload\s*=|on[a-z]+\s*=)",
27-
re.IGNORECASE,
25+
r"(?i)<(?:script|iframe|object|embed)\b[^>]*>|</(?:script|iframe|object|embed)>|<link\b[^>]*href\s*=\s*[\"']?\s*javascript:|(?:vb|java)script:|data:text/html|&#(?:x6A|106);avascript:|<(?:img|svg)\b[^>]*on\w+\s*=|on[a-z]+\s*=",
2826
)
2927
STRING_UNSAFE_CONTENT_ERROR_CODE: Final[str] = "string_unsafe_content"
3028

packages/models-library/tests/test_string_types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ class InputRequestModel(BaseModel):
112112
pytest.param(
113113
"SafeName", "&#106;avascript:alert(1)", False, id="invalid-desc-encoded-js"
114114
),
115+
# ❌ ReDoS (Regular expression Denial of Service) test patterns
116+
pytest.param(
117+
"SafeName",
118+
"<script" + ">" * 1000 + "alert(1)</script>",
119+
False,
120+
id="redos-nested-tags",
121+
),
122+
pytest.param(
123+
"SafeName",
124+
"SELECT " + "a" * 10000 + " FROM users",
125+
False,
126+
id="redos-long-sql-keyword",
127+
),
115128
],
116129
)
117130
def test_safe_string_types(name: str, description: str, should_pass: bool):

0 commit comments

Comments
 (0)