Skip to content

Commit 93f10fc

Browse files
Add sanitizers for compiled regexes
1 parent 6ff7fb2 commit 93f10fc

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

python/ql/lib/semmle/python/security/dataflow/ServerSideRequestForgeryCustomizations.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ module ServerSideRequestForgery {
157157
branch = true and
158158
call = API::moduleImport("re").getMember(["match", "fullmatch"]).getACall() and
159159
strNode = [call.getArg(1), call.getArgByName("string")]
160+
or
161+
branch = true and
162+
call =
163+
API::moduleImport("re")
164+
.getMember("compile")
165+
.getReturn()
166+
.getMember(["match", "fullmatch"])
167+
.getACall() and
168+
strNode = [call.getArg(0), call.getArgByName("string")]
160169
)
161170
}
162171
}

python/ql/test/query-tests/Security/CWE-918-ServerSideRequestForgery/full_partial_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,13 @@ def partial_ssrf_7():
164164
if re.match(r'[a-zA-Z0-9]+', user_input):
165165
url = f"https://example.com/foo#{user_input}"
166166
requests.get(url) # NOT OK, but NOT FOUND - user input can contain arbitrary character as a suffix.
167+
168+
reg = re.compile(r'^[a-zA-Z0-9]+$')
169+
170+
if reg.match(user_input):
171+
url = f"https://example.com/foo#{user_input}"
172+
requests.get(url) # OK - user input can only contain alphanumerical characters
173+
174+
if reg.fullmatch(user_input):
175+
url = f"https://example.com/foo#{user_input}"
176+
requests.get(url) # OK - user input can only contain alphanumerical characters

0 commit comments

Comments
 (0)