feat: add expires timestamp for CSRF plugin#6201
Conversation
|
|
||
| === TEST 10: expired csrf token | ||
| --- config | ||
| location /t { |
There was a problem hiding this comment.
It seems that the test case doesn't access /t?
bisakhmondal
left a comment
There was a problem hiding this comment.
Hey, nice to see this PR.
| local expires_timestamp = ngx_time() + conf.expires | ||
| local sign = gen_sign(random, expires_timestamp, conf.key) |
There was a problem hiding this comment.
I'd say sign with the current timestamp. and at line 116 perform a diff (ngx_time()-expires) and test if it is greater than conf.expires. In this way, expiry is localized to apisix as it supports the current need of the user. Any mistake in setting up conf expiry (maybe a big value) value and the tokens generated with that value can't be used to exploit the infra because generated tokens can be revoked by reconfiguring the expires field.
Also, how about ignoring the check when conf.expires = 0
There was a problem hiding this comment.
Thank you for your guidance.
bisakhmondal
left a comment
There was a problem hiding this comment.
LGTM. Thanks for addressing the changes.
Also, how about ignoring the check when conf.expires = 0
What do you think?
Agree |
| if conf.expires == 0 then | ||
| return | ||
| end |
There was a problem hiding this comment.
Ahh, sorry I confused you. I didn't mean to ignore csrf token validation. I meant we can ignore token has expired check.
| if time_now - expires > conf.expires then | ||
| core.log.error("token has expired") | ||
| return false | ||
| end |
There was a problem hiding this comment.
| if time_now - expires > conf.expires then | |
| core.log.error("token has expired") | |
| return false | |
| end | |
| if conf.expires > 0 and time_now - expires > conf.expires then | |
| core.log.error("token has expired") | |
| return false | |
| end |
Just that's it.
| | expires | number | optional | `7200` | | Expiration time(s) of csrf cookie. | | ||
| | key | string | required | | | The secret key used to encrypt the cookie. | | ||
|
|
||
| **Note: When expires is set to 0 the plugin will ignore all checks** |
There was a problem hiding this comment.
| **Note: When expires is set to 0 the plugin will ignore all checks** | |
| **Note: When expires is set to 0 the plugin will ignore checking if the token is expired or not.** |
|
|
||
|
|
||
|
|
||
| === TEST 12: set expires 0 |
There was a problem hiding this comment.
please update it accordingly. Split it into 2 cases
- expires = 1 sec, sleep = 2 sec after drawing a token then check
token has expired - expires = 0, sleep = 1, no expiration
|
I have a problem when I change the test: I need to carry the cookie returned from the previous response. Is there any way to do it? The cookie is now encoded in the test code. |
|
You can refer to:
|
bisakhmondal
left a comment
There was a problem hiding this comment.
lgtm. Thanks, @Baoyuantop for addressing the changes. Some minor nitpicks.
| if res.status >= 300 then | ||
| ngx.status = res.status | ||
| end | ||
| } |
There was a problem hiding this comment.
set the ngx.status = res.status
ngx.say(res.body)
| === TEST 12: set expires 1 | ||
| --- config | ||
| location /t { | ||
| content_by_lua_block { | ||
| local t = require("lib.test_admin").test | ||
| local code, body = t('/apisix/admin/routes/1', | ||
| ngx.HTTP_PUT, | ||
| [[{ | ||
| "uri": "/hello", | ||
| "upstream": { | ||
| "type": "roundrobin", | ||
| "nodes": { | ||
| "127.0.0.1:1980": 1 | ||
| } | ||
| }, | ||
| "plugins": { | ||
| "csrf": { | ||
| "key": "userkey", | ||
| "expires": 1 | ||
| } | ||
| } | ||
| }]] | ||
| ) | ||
|
|
||
| if code >= 300 then | ||
| ngx.status = code | ||
| end | ||
| ngx.say(body) | ||
| } | ||
| } | ||
| --- response_body | ||
| passed |
There was a problem hiding this comment.
redundant? /route/1 already has the same config.
| end | ||
| ngx.say(res.body) | ||
| } | ||
| } |
There was a problem hiding this comment.
| end | |
| ngx.say(res.body) | |
| } | |
| } | |
| end | |
| ngx.status = res.status | |
| ngx.print(res.body) | |
| } | |
| } | |
| --- response_body | |
| hello world |
I think you missed checking the response-body. Add this snippet. Thanks
bisakhmondal
left a comment
There was a problem hiding this comment.
LGTM. Thanks, @Baoyuantop
What this PR does / why we need it:
resolve #6141
Pre-submission checklist: