From 4d5f1adcde0dc7fc15aab3f06312461372e5697e Mon Sep 17 00:00:00 2001 From: avif Date: Tue, 2 Apr 2024 13:13:17 +0300 Subject: [PATCH 1/3] feat: Add partitioned cookie support --- README.md | 5 +++-- lib/resty/cookie.lua | 1 + t/sanity.t | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 116b610..5ec7318 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Synopsis key = "Name", value = "Bob", path = "/", domain = "example.com", secure = true, httponly = true, expires = "Wed, 09 Jun 2021 10:18:14 GMT", max_age = 50, - samesite = "Strict", extension = "a4334aebaec" + samesite = "Strict", partitioned = true, extension = "a4334aebaec" }) if not ok then ngx.log(ngx.ERR, err) @@ -133,12 +133,13 @@ syntax: ok, err = cookie_obj:set({ expires = "Wed, 09 Jun 2021 10:18:14 GMT", max_age = 50, samesite = "Strict", + partitioned = true, extension = "a4334aebaec" }) ``` Set a cookie to client. This will add a new 'Set-Cookie' response header. `key` and `value` are required, all other fields are optional. -If the same cookie (whole cookie string, e.g. "Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly;") has already been setted, new cookie will be ignored. +If the same cookie (whole cookie string, e.g. "Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; Partitioned;") has already been set, new cookie will be ignored. [Back to TOC](#table-of-contents) diff --git a/lib/resty/cookie.lua b/lib/resty/cookie.lua index f4c81ab..3f9aa51 100644 --- a/lib/resty/cookie.lua +++ b/lib/resty/cookie.lua @@ -164,6 +164,7 @@ local function bake(cookie) .. (cookie.secure and "; Secure" or "") .. (cookie.httponly and "; HttpOnly" or "") .. (cookie.samesite and "; SameSite=" .. cookie.samesite or "") + .. (cookie.partitioned and "; Partitioned" or "") .. (cookie.extension and "; " .. cookie.extension or "") return str end diff --git a/t/sanity.t b/t/sanity.t index ab900ad..ea29cae 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -192,7 +192,7 @@ SID => foo key = "Name", value = "Bob", path = "/", domain = "example.com", secure = true, httponly = true, expires = "Wed, 09 Jun 2021 10:18:14 GMT", max_age = 50, - samesite = "Strict", extension = "a4334aebaec" + samesite = "Strict", partitioned = true, extension = "a4334aebaec" }) if not ok then ngx.log(ngx.ERR, err) @@ -206,7 +206,7 @@ GET /t --- no_error_log [error] --- response_headers -Set-Cookie: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Strict; a4334aebaec +Set-Cookie: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Strict; Partitioned; a4334aebaec --- response_body Set cookie From 127a7efc209db053cf8692dec0df84526dedffb1 Mon Sep 17 00:00:00 2001 From: Aurelien LAJOIE Date: Mon, 8 Apr 2024 13:38:15 +0200 Subject: [PATCH 2/3] test: add more test for partitioned --- t/sanity.t | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/t/sanity.t b/t/sanity.t index ea29cae..3524137 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -5,7 +5,7 @@ use Cwd qw(cwd); repeat_each(2); -plan tests => repeat_each() * (blocks() * 3 + 7); +plan tests => repeat_each() * (blocks() * 3 + 9); my $pwd = cwd(); @@ -462,3 +462,75 @@ GET /t [error] --- response_body Cookie string: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=None; a4334aebaec + + + +=== TEST 14: set partioned to false +--- http_config eval: $::HttpConfig +--- config + location /t { + content_by_lua ' + local ck = require "resty.cookie" + local cookie, err = ck:new() + if not cookie then + ngx.log(ngx.ERR, err) + return + end + + local ok, err = cookie:set({ + key = "Name", value = "Bob", path = "/", + domain = "example.com", secure = true, httponly = true, + expires = "Wed, 09 Jun 2021 10:18:14 GMT", max_age = 50, + samesite = "Strict", partitioned = false, extension = "a4334aebaec" + }) + if not ok then + ngx.log(ngx.ERR, err) + return + end + ngx.say("Set cookie") + '; + } +--- request +GET /t +--- no_error_log +[error] +--- response_headers +Set-Cookie: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Strict; a4334aebaec +--- response_body +Set cookie + + + +=== TEST 15: partioned not set +--- http_config eval: $::HttpConfig +--- config + location /t { + content_by_lua ' + local ck = require "resty.cookie" + local cookie, err = ck:new() + if not cookie then + ngx.log(ngx.ERR, err) + return + end + + local ok, err = cookie:set({ + key = "Name", value = "Bob", path = "/", + domain = "example.com", secure = true, httponly = true, + expires = "Wed, 09 Jun 2021 10:18:14 GMT", max_age = 50, + samesite = "Strict", extension = "a4334aebaec" + }) + if not ok then + ngx.log(ngx.ERR, err) + return + end + ngx.say("Set cookie") + '; + } +--- request +GET /t +--- no_error_log +[error] +--- response_headers +Set-Cookie: Name=Bob; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Max-Age=50; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Strict; a4334aebaec +--- response_body +Set cookie From ed35d7f1b7723ea7f59df1fbe139a7040ac332ea Mon Sep 17 00:00:00 2001 From: Aurelien LAJOIE Date: Mon, 8 Apr 2024 13:40:11 +0200 Subject: [PATCH 3/3] build: bump version --- ...kie-0.2.0-1.rockspec => lua-resty-cookie-0.3.0-1.rockspec} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename rockspecs/{lua-resty-cookie-0.2.0-1.rockspec => lua-resty-cookie-0.3.0-1.rockspec} (92%) diff --git a/rockspecs/lua-resty-cookie-0.2.0-1.rockspec b/rockspecs/lua-resty-cookie-0.3.0-1.rockspec similarity index 92% rename from rockspecs/lua-resty-cookie-0.2.0-1.rockspec rename to rockspecs/lua-resty-cookie-0.3.0-1.rockspec index 7c0810b..164c7cd 100644 --- a/rockspecs/lua-resty-cookie-0.2.0-1.rockspec +++ b/rockspecs/lua-resty-cookie-0.3.0-1.rockspec @@ -1,9 +1,9 @@ package = "lua-resty-cookie" -version = "0.2.0-1" +version = "0.3.0-1" source = { url = "git+https://github.com/utix/lua-resty-cookie", - tag = "v0.2.0", + tag = "v0.3.0", } description = {