Skip to content

Commit 2c3681f

Browse files
committed
Fix crash when setting a cookie with invalid domain (cztomczak#459).
1 parent fb2d94c commit 2c3681f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/cookie.pyx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ cdef class Cookie:
119119
return CefToPyString(cefString)
120120

121121
cpdef py_void SetDomain(self, py_string domain):
122+
pattern = re.compile(r"^(?:[a-z0-9](?:[a-z0-9-_]{0,61}[a-z0-9])?\.)"
123+
r"+[a-z0-9][a-z0-9-_]{0,61}[a-z]$")
124+
if PY_MAJOR_VERSION == 2:
125+
assert isinstance(domain, bytes), "domain type is not bytes"
126+
domain = domain.decode(g_applicationSettings["string_encoding"],
127+
errors=BYTES_DECODE_ERRORS)
128+
try:
129+
if not pattern.match(domain.encode("idna").decode("ascii")):
130+
raise Exception("Cookie.SetDomain() failed, invalid domain: {0}"
131+
.format(domain))
132+
except UnicodeError:
133+
raise Exception("Cookie.SetDomain() failed, invalid domain: {0}"
134+
.format(domain))
122135
cdef CefString cefString
123136
cefString.Attach(&self.cefCookie.domain, False)
124137
PyToCefString(domain, cefString)

0 commit comments

Comments
 (0)