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

Fix redirection with a relative location #896

Merged
merged 1 commit into from
Sep 26, 2023
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
1 change: 1 addition & 0 deletions warpgate-protocol-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ warpgate-sso = { version = "*", path = "../warpgate-sso" }
percent-encoding = "2.1"
uuid = { version = "1.2", features = ["v4"] }
regex = "1.6"
url = "2.4.1"
15 changes: 11 additions & 4 deletions warpgate-protocol-http/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use poem::web::websocket::{Message, WebSocket};
use poem::{Body, IntoResponse, Request, Response};
use tokio_tungstenite::{connect_async_with_config, tungstenite};
use tracing::*;
use url::Url;
use warpgate_common::{try_block, TargetHTTPOptions, TlsMode, WarpgateError};
use warpgate_web::lookup_built_file;

Expand Down Expand Up @@ -149,12 +150,18 @@ fn rewrite_request<B: SomeRequestBuilder>(mut req: B, options: &TargetHTTPOption
Ok(req)
}

fn rewrite_response(resp: &mut Response, options: &TargetHTTPOptions) -> Result<()> {
fn rewrite_response(
resp: &mut Response,
options: &TargetHTTPOptions,
source_uri: &Uri,
) -> Result<()> {
let target_uri = Uri::try_from(options.url.clone())?;
let headers = resp.headers_mut();

if let Some(value) = headers.get_mut(http::header::LOCATION) {
let redirect_uri = Uri::try_from(value.as_bytes())?;
let location = Url::parse(&source_uri.to_string())?.join(value.to_str()?)?;
let redirect_uri = Uri::try_from(location.to_string())?;

if redirect_uri.authority() == target_uri.authority() {
let old_value = value.clone();
*value = Uri::builder()
Expand Down Expand Up @@ -285,7 +292,7 @@ pub async fn proxy_normal_request(

log_request_result(req.method(), req.original_uri(), &status);

rewrite_response(&mut response, options)?;
rewrite_response(&mut response, options, &uri)?;
Ok(response)
}

Expand Down Expand Up @@ -470,6 +477,6 @@ async fn proxy_ws_inner(
.into_response();

copy_client_response(&client_response, &mut response);
rewrite_response(&mut response, options)?;
rewrite_response(&mut response, options, &uri)?;
Ok(response)
}
Loading