Skip to content

Commit

Permalink
Make iframes work cross domain
Browse files Browse the repository at this point in the history
  • Loading branch information
ostenbom committed Oct 23, 2023
1 parent 076650b commit 3d0cef1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
5 changes: 1 addition & 4 deletions linkup-cli/src/local_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,11 @@ async fn linkup_request_handler(
};

let mut extra_headers = get_additional_headers(&url, &headers, &session_name, &target_service);
extra_headers.insert(
LinkupHeaderName::Host,
Url::parse(&target_service.url).unwrap(),
);

// Proxy the request using the destination_url and the merged headers
let client = reqwest::Client::new();
headers.extend(&extra_headers);
headers.remove(LinkupHeaderName::Host);

let response_result = client
.request(req.method().clone(), &target_service.url)
Expand Down
2 changes: 1 addition & 1 deletion linkup-cli/src/services/caddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn start(local_config: &YamlLocalConfig) -> Result<()> {
let domains: Vec<String> = local_config
.top_level_domains()
.iter()
.map(|domain| format!("*.{}", domain))
.map(|domain| format!("{}, *.{}", domain, domain))
.collect();

write_caddyfile(&domains)?;
Expand Down
14 changes: 13 additions & 1 deletion linkup/src/headers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, fmt};

use unicase::UniCase;

Expand Down Expand Up @@ -79,6 +79,10 @@ impl HeaderMap {
self.0.extend(iter)
}

pub fn remove(&mut self, key: impl Into<UniCase<String>>) -> Option<String> {
self.0.remove(&key.into())
}

#[cfg(feature = "actix")]
pub fn from_actix_request(req: &actix_web::HttpRequest) -> Self {
req.headers().into()
Expand All @@ -90,6 +94,14 @@ impl HeaderMap {
}
}

impl fmt::Debug for HeaderMap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map()
.entries(self.0.iter().map(|(k, v)| (k.as_ref(), v.as_str())))
.finish()
}
}

#[cfg(feature = "reqwest")]
impl From<HeaderMap> for reqwest::header::HeaderMap {
fn from(value: HeaderMap) -> Self {
Expand Down
45 changes: 44 additions & 1 deletion linkup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn additional_response_headers() -> HeaderMap {
headers
}

#[cfg_attr(test, derive(Debug, PartialEq))]
#[derive(Debug, PartialEq)]
pub struct TargetService {
pub name: String,
pub url: String,
Expand Down Expand Up @@ -271,6 +271,8 @@ fn extrace_tracestate(tracestate: &str, linkup_key: String) -> String {

#[cfg(test)]
mod tests {
use std::fmt::format;

use super::*;

const CONF_STR: &str = r#"
Expand All @@ -287,6 +289,10 @@ mod tests {
}
]
},
{
"name": "other-frontend",
"location": "http://localhost:5000"
},
{
"name": "backend",
"rewrites": [
Expand Down Expand Up @@ -316,6 +322,10 @@ mod tests {
{
"domain": "api.example.com",
"default_service": "backend"
},
{
"domain": "other-example.com",
"default_service": "other-frontend"
}
]
}
Expand Down Expand Up @@ -592,4 +602,37 @@ mod tests {
assert_eq!(target.name, "backend");
assert_eq!(target.url, "http://localhost:8001/user");
}

#[tokio::test]
async fn test_iframable() {
let string_store = MemoryStringStore::new();
let sessions = SessionAllocator::new(&string_store);

let input_config_value: serde_json::Value = serde_json::from_str(CONF_STR).unwrap();
let input_config: Session = input_config_value.try_into().unwrap();

let name = sessions
.store_session(input_config, NameKind::Animal, "".to_string())
.await
.unwrap();

let mut headers = HeaderMap::new();
headers.insert(HeaderName::Referer, format!("{}.example.com", name));

let (name, config) = sessions
.get_request_session("other-example.com", &headers)
.await
.unwrap();

let target = get_target_service(
"http://other-example.com",
&headers,
&config,
&name,
)
.unwrap();

assert_eq!(target.name, "other-frontend");
assert_eq!(target.url, "http://localhost:5000/");
}
}

0 comments on commit 3d0cef1

Please sign in to comment.