Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[oidc proxy] Improve handling of redirects from protected resource #9080

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ydb/mvp/oidc_proxy/oidc_protected_page.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,20 @@ class THandlerSessionServiceCheck : public NActors::TActorBootstrapped<THandlerS
TString GetFixedLocationHeader(TStringBuf location) {
TStringBuf scheme, host, uri;
NHttp::CrackURL(ProtectedPageUrl, scheme, host, uri);
return TStringBuilder() << '/' << host << location;
if (location.StartsWith("//")) {
return TStringBuilder() << '/' << (scheme.empty() ? "" : TString(scheme) + "://") << location.SubStr(2);
} else if (location.StartsWith('/')) {
return TStringBuilder() << '/'
<< (scheme.empty() ? "" : TString(scheme) + "://")
<< host << location;
} else {
TStringBuf locScheme, locHost, locUri;
NHttp::CrackURL(location, locScheme, locHost, locUri);
if (!locScheme.empty()) {
return TStringBuilder() << '/' << location;
}
}
return TString(location);
}

NHttp::THttpOutgoingResponsePtr CreateResponseForbiddenHost() {
Expand Down
66 changes: 64 additions & 2 deletions ydb/mvp/oidc_proxy/oidc_proxy_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ Y_UNIT_TEST_SUITE(Mvp) {
std::unique_ptr<grpc::Server> sessionServer(builder.BuildAndStart());

NHttp::THttpIncomingRequestPtr incomingRequest = new NHttp::THttpIncomingRequest();
EatWholeString(incomingRequest, "GET /" + allowedProxyHost + "/counters HTTP/1.1\r\n"
EatWholeString(incomingRequest, "GET /http://" + allowedProxyHost + "/counters HTTP/1.1\r\n"
"Host: oidcproxy.net\r\n"
"Cookie: yc_session=allowed_session_cookie\r\n\r\n");
runtime.Send(new IEventHandle(target, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingRequest(incomingRequest)));
Expand All @@ -288,6 +288,8 @@ Y_UNIT_TEST_SUITE(Mvp) {
UNIT_ASSERT_STRINGS_EQUAL(outgoingRequestEv->Request->URL, "/counters");
UNIT_ASSERT_STRING_CONTAINS(outgoingRequestEv->Request->Headers, "Authorization: Bearer protected_page_iam_token");
UNIT_ASSERT_EQUAL(outgoingRequestEv->Request->Secure, false);

// Location start with '/'
NHttp::THttpIncomingResponsePtr incomingResponse = new NHttp::THttpIncomingResponse(outgoingRequestEv->Request);
EatWholeString(incomingResponse, "HTTP/1.1 307 Temporary Redirect\r\n"
"Connection: close\r\n"
Expand All @@ -297,7 +299,67 @@ Y_UNIT_TEST_SUITE(Mvp) {

auto outgoingResponseEv = runtime.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpOutgoingResponse>(handle);
UNIT_ASSERT_STRINGS_EQUAL(outgoingResponseEv->Response->Status, "307");
UNIT_ASSERT_STRING_CONTAINS(outgoingResponseEv->Response->Headers, "Location: /" + allowedProxyHost + "/node/12345/counters");
UNIT_ASSERT_STRING_CONTAINS(outgoingResponseEv->Response->Headers, "Location: /http://" + allowedProxyHost + "/node/12345/counters");

// Location start with "//"
runtime.Send(new IEventHandle(target, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingRequest(incomingRequest)));
outgoingRequestEv = runtime.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpOutgoingRequest>(handle);

incomingResponse = new NHttp::THttpIncomingResponse(outgoingRequestEv->Request);
EatWholeString(incomingResponse, "HTTP/1.1 302 Found\r\n"
"Connection: close\r\n"
"Location: //new.oidc.proxy.host:1234/node/12345/counters\r\n"
"Content-Length:0\r\n\r\n");
runtime.Send(new IEventHandle(handle->Sender, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingResponse(outgoingRequestEv->Request, incomingResponse)));

outgoingResponseEv = runtime.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpOutgoingResponse>(handle);
UNIT_ASSERT_STRINGS_EQUAL(outgoingResponseEv->Response->Status, "302");
UNIT_ASSERT_STRING_CONTAINS(outgoingResponseEv->Response->Headers, "Location: /http://new.oidc.proxy.host:1234/node/12345/counters");

// Location start with ".."
runtime.Send(new IEventHandle(target, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingRequest(incomingRequest)));
outgoingRequestEv = runtime.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpOutgoingRequest>(handle);

incomingResponse = new NHttp::THttpIncomingResponse(outgoingRequestEv->Request);
EatWholeString(incomingResponse, "HTTP/1.1 302 Found\r\n"
"Connection: close\r\n"
"Location: ../node/12345/counters\r\n"
"Content-Length:0\r\n\r\n");
runtime.Send(new IEventHandle(handle->Sender, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingResponse(outgoingRequestEv->Request, incomingResponse)));

outgoingResponseEv = runtime.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpOutgoingResponse>(handle);
UNIT_ASSERT_STRINGS_EQUAL(outgoingResponseEv->Response->Status, "302");
UNIT_ASSERT_STRING_CONTAINS(outgoingResponseEv->Response->Headers, "Location: ../node/12345/counters");

// Location is absolute URL
runtime.Send(new IEventHandle(target, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingRequest(incomingRequest)));
outgoingRequestEv = runtime.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpOutgoingRequest>(handle);

incomingResponse = new NHttp::THttpIncomingResponse(outgoingRequestEv->Request);
EatWholeString(incomingResponse, "HTTP/1.1 302 Found\r\n"
"Connection: close\r\n"
"Location: https://some.new.oidc.host:9876/counters/v1\r\n"
"Content-Length:0\r\n\r\n");
runtime.Send(new IEventHandle(handle->Sender, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingResponse(outgoingRequestEv->Request, incomingResponse)));

outgoingResponseEv = runtime.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpOutgoingResponse>(handle);
UNIT_ASSERT_STRINGS_EQUAL(outgoingResponseEv->Response->Status, "302");
UNIT_ASSERT_STRING_CONTAINS(outgoingResponseEv->Response->Headers, "Location: /https://some.new.oidc.host:9876/counters/v1");

// Location is sub-resources URL
runtime.Send(new IEventHandle(target, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingRequest(incomingRequest)));
outgoingRequestEv = runtime.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpOutgoingRequest>(handle);

incomingResponse = new NHttp::THttpIncomingResponse(outgoingRequestEv->Request);
EatWholeString(incomingResponse, "HTTP/1.1 302 Found\r\n"
"Connection: close\r\n"
"Location: v1/\r\n"
"Content-Length:0\r\n\r\n");
runtime.Send(new IEventHandle(handle->Sender, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingResponse(outgoingRequestEv->Request, incomingResponse)));

outgoingResponseEv = runtime.GrabEdgeEvent<NHttp::TEvHttpProxy::TEvHttpOutgoingResponse>(handle);
UNIT_ASSERT_STRINGS_EQUAL(outgoingResponseEv->Response->Status, "302");
UNIT_ASSERT_STRING_CONTAINS(outgoingResponseEv->Response->Headers, "Location: v1/");
}


Expand Down