Skip to content

Commit efecc07

Browse files
authored
Update cors.lua
Change function cors_request: Fix error when origin header is not send in the request (nil exception) Change function cors_response: Check if transaction_data is set by the cors_request
1 parent b3ffb2a commit efecc07

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/cors.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,11 @@ end
9797
-- allowed_headers: Comma-delimited list of allowed headers. (e.g. X-Header1,X-Header2)
9898
function cors_request(txn, allowed_methods, allowed_origins, allowed_headers)
9999
local headers = txn.http:req_get_headers()
100-
local origin = headers["origin"][0]
101-
102100
local transaction_data = {}
103-
104-
if origin ~= nil then
101+
102+
if headers["origin"] ~= nil and headers["origin"][0] ~= nil then
105103
core.Debug("CORS: Got 'Origin' header: " .. headers["origin"][0])
106-
transaction_data["origin"] = origin
104+
transaction_data["origin"] = headers["origin"][0]
107105
end
108106

109107
transaction_data["allowed_methods"] = allowed_methods
@@ -124,6 +122,11 @@ end
124122
-- txn: The current transaction object that gives access to response properties.
125123
function cors_response(txn)
126124
local transaction_data = txn:get_priv()
125+
126+
if transaction_data == nil then
127+
return
128+
end
129+
127130
local origin = transaction_data["origin"]
128131
local allowed_origins = transaction_data["allowed_origins"]
129132
local allowed_methods = transaction_data["allowed_methods"]
@@ -154,4 +157,4 @@ end
154157

155158
-- Register the actions with HAProxy
156159
core.register_action("cors", {"http-req"}, cors_request, 3)
157-
core.register_action("cors", {"http-res"}, cors_response, 0)
160+
core.register_action("cors", {"http-res"}, cors_response, 0)

0 commit comments

Comments
 (0)