File tree 2 files changed +12
-13
lines changed
2 files changed +12
-13
lines changed Original file line number Diff line number Diff line change 34
34
function OnHttpRequest ()
35
35
Params = GetParams ()
36
36
PrepareMultiPartParams () -- if you handle file uploads
37
- GenerateCSRFToken ()
38
37
39
38
-- Remove code if you do not use arangodb
40
39
if (db_config ~= nil and db_config [" engine" ] == " arangodb" ) then
Original file line number Diff line number Diff line change 1
1
CSRFToken = EncodeBase64 (GetRandomBytes (64 ))
2
2
3
- GenerateCSRFToken = function ()
4
- CSRFToken = EncodeBase64 (GetRandomBytes (64 ))
3
+ CheckCSRFToken = function ()
4
+ if GetMethod () == " POST" then
5
+ local crypted_token = EncodeBase64 (GetCryptoHash (" SHA256" , GetBodyParams ()[" authenticity_token" ], ENV [' SECRET_KEY' ]))
6
+ print (GetBodyParams ()[" authenticity_token" ],crypted_token , GetCookie (" _authenticity_token" ))
7
+ assert (crypted_token == GetCookie (" _authenticity_token" ))
8
+ end
9
+ end
10
+
11
+ AuthenticityTokenTag = function ()
5
12
if GetCookie (" _authenticity_token" ) == nil then
13
+ CSRFToken = EncodeBase64 (GetRandomBytes (64 ))
14
+ print (CSRFToken , EncodeBase64 (GetCryptoHash (" SHA256" , CSRFToken , ENV [' SECRET_KEY' ])))
6
15
SetCookie (
7
16
" _authenticity_token" ,
8
- EncodeBase64 (GetRandomBytes ( 64 )),
17
+ EncodeBase64 (GetCryptoHash ( " SHA256 " , CSRFToken , ENV [ ' SECRET_KEY ' ] )),
9
18
{
10
19
HttpOnly = true ,
11
20
MaxAge = 60 * 30 ,
12
21
SameSite = " Strict"
13
22
} -- available for 30 minutes
14
23
)
15
24
end
16
- end
17
-
18
- CheckCSRFToken = function ()
19
- if GetMethod () == " POST" then
20
- assert (GetBodyParams ()[" authenticity_token" ] == GetCookie (" _authenticity_token" ))
21
- end
22
- end
23
-
24
- AuthenticityTokenTag = function ()
25
25
return ' <input type="hidden" name="authenticity_token" value="' .. CSRFToken .. ' " />'
26
26
end
You can’t perform that action at this time.
0 commit comments