Skip to content

Commit

Permalink
Fix redirection with a relative location
Browse files Browse the repository at this point in the history
  • Loading branch information
seysn authored and Eugeny committed Sep 26, 2023
1 parent 4ecc6b6 commit dec0b97
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
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)
}

0 comments on commit dec0b97

Please sign in to comment.