Skip to content

Commit f2fde6d

Browse files
committed
http/cookie: Enforce max_cookie_length when storing
1 parent 64f8d57 commit f2fde6d

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/modules/http.cookie.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ Defaults to a function based on [`os.time`](https://www.lua.org/manual/5.3/manua
5757

5858
### `store.max_cookie_length` <!-- --> {#http.cookie.store.max_cookie_length}
5959

60-
The default maximum cookie length for store methods such as `:lookup()`.
60+
The maximum length (in bytes) of cookies in the store; this value is also used as default maximum cookie length for `:lookup()`.
61+
Decreasing this value will only prevent new cookies from being added, it will not remove old cookies.
6162

6263
Defaults to infinity (no maximum size).
6364

http/cookie.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ local function add_to_store(self, cookie, req_is_http, now)
193193
self:remove(cookie.domain, cookie.path, cookie.name)
194194
else
195195
local name = cookie.name
196+
local cookie_length = #name + 1 + #cookie.value
197+
if cookie_length > self.max_cookie_length then
198+
return false
199+
end
200+
196201
local domain = cookie.domain
197202
local domain_cookies = self.domains[domain]
198203
local path_cookies

spec/cookie_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ describe("cookie module", function()
381381
assert.same("foo=path; bar=path; bar=domain; bar=time; foo=time", s:lookup("sub.example.com", "/path/longerpath", true, true))
382382
end)
383383
end)
384+
it("enforces store.max_cookie_length", function()
385+
local s = http_cookie.new_store()
386+
s.max_cookie_length = 3
387+
assert.falsy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("foo=foo")))
388+
s.max_cookie_length = 8
389+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("foo=foo")))
390+
assert.falsy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("bar=longervalue")))
391+
end)
384392
it("enforces store.max_cookies", function()
385393
local s = http_cookie.new_store()
386394
s.max_cookies = 0

0 commit comments

Comments
 (0)