-
Notifications
You must be signed in to change notification settings - Fork 1
/
cors.lua
173 lines (144 loc) · 6.67 KB
/
cors.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
--
-- Cross-origin Request Sharing (CORS) implementation for HAProxy Lua host
--
-- CORS RFC:
-- https://www.w3.org/TR/cors/
--
-- Copyright (c) 2019. Nick Ramirez <[email protected]>
-- Copyright (c) 2019. HAProxy Technologies, LLC.
-- Loops through array to find the given string.
-- items: array of strings
-- test_str: string to search for
function contains(items, test_str)
for _,item in pairs(items) do
if item == test_str then
return true
end
end
return false
end
-- If the given origin is found within the allowed_origins string, it is returned. Otherwise, nil is returned.
-- origin: The value from the 'origin' request header
-- allowed_origins: Comma-delimited list of allowed origins. (e.g. localhost,localhost:8080,test.com)
function get_allowed_origin(origin, allowed_origins)
if origin ~= nil then
local allowed_origins = core.tokenize(allowed_origins, ",")
-- Strip whitespace
for index, value in ipairs(allowed_origins) do
allowed_origins[index] = value:gsub("%s+", "")
end
if contains(allowed_origins, "*") then
return "*"
elseif contains(allowed_origins, origin:match("//([^/]+)")) then
return origin
end
end
return nil
end
-- Adds headers for CORS preflight request and then attaches them to the response
-- after it comes back from the server. This works with versions of HAProxy prior to 2.2.
-- The downside is that the OPTIONS request must be sent to the backend server first and can't
-- be intercepted and returned immediately.
-- txn: The current transaction object that gives access to response properties
-- allowed_methods: Comma-delimited list of allowed HTTP methods. (e.g. GET,POST,PUT,DELETE)
-- allowed_headers: Comma-delimited list of allowed headers. (e.g. X-Header1,X-Header2)
function preflight_request_ver1(txn, allowed_methods, allowed_headers)
core.Debug("CORS: preflight request received")
txn.http:res_set_header("Access-Control-Allow-Methods", allowed_methods)
txn.http:res_set_header("Access-Control-Allow-Headers", allowed_headers)
txn.http:res_set_header("Access-Control-Max-Age", 600)
core.Debug("CORS: attaching allowed methods to response")
end
-- Add headers for CORS preflight request and then returns a 204 response.
-- The 'reply' function used here is available in HAProxy 2.2+. It allows HAProxy to return
-- a reply without contacting the server.
-- txn: The current transaction object that gives access to response properties
-- origin: The value from the 'origin' request header
-- allowed_methods: Comma-delimited list of allowed HTTP methods. (e.g. GET,POST,PUT,DELETE)
-- allowed_origins: Comma-delimited list of allowed origins. (e.g. localhost,localhost:8080,test.com)
-- allowed_headers: Comma-delimited list of allowed headers. (e.g. X-Header1,X-Header2)
function preflight_request_ver2(txn, origin, allowed_methods, allowed_origins, allowed_headers)
core.Debug("CORS: preflight request received")
local reply = txn:reply()
reply:set_status(204, "No Content")
reply:add_header("Content-Type", "text/html")
reply:add_header("Access-Control-Allow-Methods", allowed_methods)
reply:add_header("Access-Control-Allow-Headers", allowed_headers)
reply:add_header("Access-Control-Max-Age", 600)
local allowed_origin = get_allowed_origin(origin, allowed_origins)
if allowed_origin == nil then
core.Debug("CORS: " .. origin .. " not allowed")
else
core.Debug("CORS: " .. origin .. " allowed")
reply:add_header("Access-Control-Allow-Origin", allowed_origin)
if allowed_origin ~= "*" then
reply:add_header("Vary", "Accept-Encoding,Origin")
end
end
core.Debug("CORS: Returning reply to preflight request")
txn:done(reply)
end
-- When invoked during a request, captures the origin header if present and stores it in a private variable.
-- If the request is OPTIONS and it is a supported version of HAProxy, returns a preflight request reply.
-- Otherwise, the preflight request header is added to the response after it has returned from the server.
-- txn: The current transaction object that gives access to response properties
-- allowed_methods: Comma-delimited list of allowed HTTP methods. (e.g. GET,POST,PUT,DELETE)
-- allowed_origins: Comma-delimited list of allowed origins. (e.g. localhost,localhost:8080,test.com)
-- allowed_headers: Comma-delimited list of allowed headers. (e.g. X-Header1,X-Header2)
function cors_request(txn, allowed_methods, allowed_origins, allowed_headers)
local headers = txn.http:req_get_headers()
local transaction_data = {}
local origin = nil
if headers["origin"] ~= nil and headers["origin"][0] ~= nil then
core.Debug("CORS: Got 'Origin' header: " .. headers["origin"][0])
origin = headers["origin"][0]
end
-- Bail if client did not send an Origin
-- for example, it may be a regular OPTIONS request that is not a CORS preflight request
if origin == nil or origin == '' then
return
end
transaction_data["origin"] = origin
transaction_data["allowed_methods"] = allowed_methods
transaction_data["allowed_origins"] = allowed_origins
transaction_data["allowed_headers"] = allowed_headers
txn:set_priv(transaction_data)
local method = txn.sf:method()
transaction_data["method"] = method
if method == "OPTIONS" and txn.reply ~= nil then
preflight_request_ver2(txn, origin, allowed_methods, allowed_origins, allowed_headers)
end
end
-- When invoked during a response, sets CORS headers so that the browser can read the response from permitted domains.
-- txn: The current transaction object that gives access to response properties.
function cors_response(txn)
local transaction_data = txn:get_priv()
if transaction_data == nil then
return
end
local origin = transaction_data["origin"]
local allowed_origins = transaction_data["allowed_origins"]
local allowed_methods = transaction_data["allowed_methods"]
local allowed_headers = transaction_data["allowed_headers"]
local method = transaction_data["method"]
-- Bail if client did not send an Origin
if origin == nil or origin == '' then
return
end
local allowed_origin = get_allowed_origin(origin, allowed_origins)
if allowed_origin == nil then
core.Debug("CORS: " .. origin .. " not allowed")
else
if method == "OPTIONS" and txn.reply == nil then
preflight_request_ver1(txn, allowed_methods, allowed_headers)
end
core.Debug("CORS: " .. origin .. " allowed")
txn.http:res_set_header("Access-Control-Allow-Origin", allowed_origin)
if allowed_origin ~= "*" then
txn.http:res_add_header("Vary", "Accept-Encoding,Origin")
end
end
end
-- Register the actions with HAProxy
core.register_action("cors", {"http-req"}, cors_request, 3)
core.register_action("cors", {"http-res"}, cors_response, 0)