Skip to content

Commit 686e6c5

Browse files
committed
Support HttpOnly and SameSite for cookies
Make HttpOnly and SameSite configurable for session cookies, set HttpOnly=True and SameSite='Strict' by default.
1 parent 5a9e91b commit 686e6c5

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

docs/config.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ Sessions
100100
You can specify a path for the session cookie here. ``None`` means that the servlet path will be used, which is normally the best choice. If you rewrite the URL using different prefixes, you may have to specify a fixed prefix for all your URLs. Using the root path '/' will always work, but may have security issues if you are running less secure applications on the same server. Default: ``None``.
101101
``SecureSessionCookie``:
102102
If True, then the Application will use a secure cookie for the session ID if the request was using an HTTPS connection. Default: ``True``.
103+
``HttpOnlySessionCookie``:
104+
If True, then the Application will set the HttpOnly attribute on the session cookie . Default: ``True``.
105+
``SameSiteSessionCookie``:
106+
If not ``None``, then the Application will set this value as the SameSite attribute on the session cookie . Default: ``Strict``.
103107
``MaxDynamicMemorySessions``:
104108
The maximum number of dynamic memory sessions that will be retained in memory. When this number is exceeded, the least recently used, excess sessions will be pushed out to disk. This setting can be used to help control memory requirements, especially for busy sites. This is used only if the ``SessionStore`` is set to ``Dynamic``. Default: ``10000``.
105109
``DynamicSessionTimeout``:

webware/Application.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
SaveErrorMessages=True,
113113
SecureSessionCookie=True,
114114
SessionCookiePath=None,
115+
HttpOnlySessionCookie=True,
116+
SameSiteSessionCookie='Strict',
115117
SessionModule='Session',
116118
SessionName='_SID_',
117119
SessionPrefix='',

webware/Configs/Application.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ UseCookieSessions = True
6969
# If you rewrite the URL, you may need to specify this explicitly:
7070
SessionCookiePath = None # the servlet path is used if not specified
7171
SecureSessionCookie = True # use a secure cookie for HTTPS connections
72+
HttpOnlySessionCookie = True # session cookie should be HttpOnly
73+
SameSiteSessionCookie = 'Strict' # set SameSite attribute on session cookie
7274

7375
# Set this to True to allow extra path info to be attached to URLs
7476
ExtraPathInfo = False # no extra path info

webware/Cookie.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ def comment(self):
6464
def domain(self):
6565
return self._cookie['domain']
6666

67-
def maxAge(self):
68-
return self._cookie['max-age']
69-
7067
def expires(self):
7168
return self._cookie['expires']
7269

70+
def maxAge(self):
71+
return self._cookie['max-age']
72+
7373
def name(self):
7474
return self._name
7575

@@ -79,6 +79,15 @@ def path(self):
7979
def isSecure(self):
8080
return self._cookie['secure']
8181

82+
def httpOnly(self):
83+
return self._cookie['httponly']
84+
85+
def sameSite(self):
86+
try:
87+
return self._cookie['samesite']
88+
except KeyError: # Python < 3.8
89+
return ''
90+
8291
def value(self):
8392
return self._value
8493

@@ -107,6 +116,12 @@ def setPath(self, path):
107116
def setSecure(self, secure=True):
108117
self._cookie['secure'] = secure
109118

119+
def setHttpOnly(self, httpOnly=True):
120+
self._cookie['httponly'] = httpOnly
121+
122+
def setSameSite(self, sameSite='Strict'):
123+
self._cookie['samesite'] = sameSite
124+
110125
def setValue(self, value):
111126
self._value = value
112127
self._cookies[self._name] = value

webware/HTTPResponse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ def recordSession(self):
406406
cookie.setPath(app.sessionCookiePath(trans))
407407
if request.isSecure():
408408
cookie.setSecure(app.setting('SecureSessionCookie'))
409+
if app.setting('HttpOnlySessionCookie'):
410+
cookie.setHttpOnly()
411+
sameSite = app.setting('SameSiteSessionCookie')
412+
if sameSite:
413+
cookie.setSameSite(sameSite)
409414
self.addCookie(cookie)
410415
if debug:
411416
print('>> recordSession: Setting SID', identifier)

0 commit comments

Comments
 (0)