Skip to content

Commit 593f8a3

Browse files
rouaultrprinceley
andcommitted
/vsicurl/ GetFileSizeOrHeaders(): do not propagate authentication sent to the original URL to a S3-like redirect
Co-authored-by: Robin Princeley <[email protected]>
1 parent a5baaad commit 593f8a3

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

autotest/gcore/vsicurl.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,9 @@ def test_vsicurl_test_redirect_x_amz(server):
617617
current_time = 1500
618618

619619
def method(request):
620+
621+
assert request.headers["Authorization"] == "Bearer xxx"
622+
620623
response = "HTTP/1.1 302 Found\r\n"
621624
response += "Server: foo\r\n"
622625
response += (
@@ -625,7 +628,7 @@ def method(request):
625628
+ "\r\n"
626629
)
627630
response += "Location: %s\r\n" % (
628-
"http://localhost:%d/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s"
631+
"http://127.0.0.1:%d/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s"
629632
% (
630633
server.port,
631634
time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)),
@@ -642,9 +645,13 @@ def method(request):
642645
403,
643646
{"Server": "foo"},
644647
"",
648+
unexpected_headers=["Authorization"],
645649
)
646650

647651
def method(request):
652+
653+
assert "Authorization" not in request.headers
654+
648655
if "Range" in request.headers:
649656
if request.headers["Range"] == "bytes=0-16383":
650657
request.protocol_version = "HTTP/1.1"
@@ -687,10 +694,25 @@ def method(request):
687694
custom_method=method,
688695
)
689696

690-
with webserver.install_http_handler(handler):
691-
f = gdal.VSIFOpenL(
692-
"/vsicurl/http://localhost:%d/test_redirect/test.bin" % server.port,
693-
"rb",
697+
gdal.SetPathSpecificOption(
698+
"/vsicurl/http://localhost:%d/test_redirect" % server.port,
699+
"GDAL_HTTP_AUTH",
700+
"BEARER",
701+
)
702+
gdal.SetPathSpecificOption(
703+
"/vsicurl/http://localhost:%d/test_redirect" % server.port,
704+
"GDAL_HTTP_BEARER",
705+
"xxx",
706+
)
707+
try:
708+
with webserver.install_http_handler(handler):
709+
f = gdal.VSIFOpenL(
710+
"/vsicurl/http://localhost:%d/test_redirect/test.bin" % server.port,
711+
"rb",
712+
)
713+
finally:
714+
gdal.ClearPathSpecificOptions(
715+
"/vsicurl/http://localhost:%d/test_redirect" % server.port
694716
)
695717
assert f is not None
696718

port/cpl_vsil_curl.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,8 +1145,27 @@ vsi_l_offset VSICurlHandle::GetFileSizeOrHeaders(bool bSetError,
11451145
++nTryCount;
11461146
CURL *hCurlHandle = curl_easy_init();
11471147

1148-
struct curl_slist *headers =
1149-
VSICurlSetOptions(hCurlHandle, osURL.c_str(), m_aosHTTPOptions.List());
1148+
struct curl_slist *headers = nullptr;
1149+
if (bS3LikeRedirect)
1150+
{
1151+
// Do not propagate authentication sent to the original URL to a S3-like
1152+
// redirect.
1153+
CPLStringList aosHTTPOptions{};
1154+
for (const auto &pszOption : m_aosHTTPOptions)
1155+
{
1156+
if (STARTS_WITH_CI(pszOption, "HTTPAUTH") ||
1157+
STARTS_WITH_CI(pszOption, "HTTP_BEARER"))
1158+
continue;
1159+
aosHTTPOptions.AddString(pszOption);
1160+
}
1161+
headers = VSICurlSetOptions(hCurlHandle, osURL.c_str(),
1162+
aosHTTPOptions.List());
1163+
}
1164+
else
1165+
{
1166+
headers = VSICurlSetOptions(hCurlHandle, osURL.c_str(),
1167+
m_aosHTTPOptions.List());
1168+
}
11501169

11511170
WriteFuncStruct sWriteFuncHeaderData;
11521171
VSICURLInitWriteFuncStruct(&sWriteFuncHeaderData, nullptr, nullptr,

0 commit comments

Comments
 (0)