Skip to content

Commit 8c2ff72

Browse files
authored
fix(http): Allow relative redirect on https (#395)
Location header can now be relative: https://httpwg.org/specs/rfc9110.html#field.location
1 parent 26b524e commit 8c2ff72

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/http.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ local function shouldredirect(reqt, code, headers)
300300
if not location then return false end
301301
location = string.gsub(location, "%s", "")
302302
if location == "" then return false end
303+
-- the RFC says the redirect URL may be relative
304+
location = url.absolute(reqt.url, location)
303305
local scheme = url.parse(location).scheme
304306
if scheme and (not SCHEMES[scheme]) then return false end
305307
-- avoid https downgrades
@@ -323,8 +325,7 @@ end
323325
local trequest, tredirect
324326

325327
--[[local]] function tredirect(reqt, location)
326-
-- the RFC says the redirect URL has to be absolute, but some
327-
-- servers do not respect that
328+
-- the RFC says the redirect URL may be relative
328329
local newurl = url.absolute(reqt.url, location)
329330
-- if switching schemes, reset port and create function
330331
if url.parse(newurl).scheme ~= reqt.scheme then

test/httptest.lua

+31
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,37 @@ ignore = {
265265
}
266266
check_request(request, expect, ignore)
267267

268+
-- Use https://httpbin.org/#/Dynamic_data/get_base64__value_ for testing
269+
-----------------------------------------------------
270+
io.write("testing absolute https redirection: ")
271+
request = {
272+
url = "https://httpbin.org/redirect-to?url=https://httpbin.org/base64/THVhIFNvY2tldA=="
273+
}
274+
expect = {
275+
code = 200,
276+
body = "Lua Socket"
277+
}
278+
ignore = {
279+
status = 1,
280+
headers = 1
281+
}
282+
check_request(request, expect, ignore)
283+
284+
-----------------------------------------------------
285+
io.write("testing relative https redirection: ")
286+
request = {
287+
url = "https://httpbin.org/redirect-to?url=/base64/THVhIFNvY2tldA=="
288+
}
289+
expect = {
290+
code = 200,
291+
body = "Lua Socket"
292+
}
293+
ignore = {
294+
status = 1,
295+
headers = 1
296+
}
297+
check_request(request, expect, ignore)
298+
268299
------------------------------------------------------------------------
269300
--[[
270301
io.write("testing proxy with redirection: ")

0 commit comments

Comments
 (0)