Skip to content

Commit

Permalink
Make iframes work cross domain (#41)
Browse files Browse the repository at this point in the history
![Screenshot 2023-10-23 at 16 30
58](https://github.com/mentimeter/linkup/assets/7828615/1019d55c-d80b-4a75-a517-62ddf3c32258)

Host really seemed to be screwing things up, I found it better to remove
the header instead

---------

Co-authored-by: Augusto César <[email protected]>
  • Loading branch information
ostenbom and augustoccesar authored Oct 24, 2023
1 parent 076650b commit 05ba34b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
8 changes: 2 additions & 6 deletions linkup-cli/src/local_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use futures::stream::StreamExt;
use thiserror::Error;

use linkup::{HeaderMap as LinkupHeaderMap, HeaderName as LinkupHeaderName, *};
use url::Url;

use crate::LINKUP_LOCALSERVER_PORT;

Expand Down Expand Up @@ -189,15 +188,12 @@ 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(),
);
let extra_headers = get_additional_headers(&url, &headers, &session_name, &target_service);

// 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
5 changes: 5 additions & 0 deletions linkup/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;

use unicase::UniCase;

#[derive(Debug)]
pub struct HeaderMap(HashMap<UniCase<String>, String>);

pub enum HeaderName {
Expand Down Expand Up @@ -79,6 +80,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 Down
38 changes: 37 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 @@ -287,6 +287,10 @@ mod tests {
}
]
},
{
"name": "other-frontend",
"location": "http://localhost:5000"
},
{
"name": "backend",
"rewrites": [
Expand Down Expand Up @@ -316,6 +320,10 @@ mod tests {
{
"domain": "api.example.com",
"default_service": "backend"
},
{
"domain": "other-example.com",
"default_service": "other-frontend"
}
]
}
Expand Down Expand Up @@ -592,4 +600,32 @@ 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 05ba34b

Please sign in to comment.