paths now need to contain processId before the actual path

This commit is contained in:
bitful-pannul 2023-10-20 00:10:58 +02:00
parent 509e4124d2
commit 63e3ec4379
5 changed files with 36 additions and 28 deletions

21
Cargo.lock generated
View File

@ -2509,7 +2509,7 @@ dependencies = [
"lalrpop-util",
"petgraph",
"regex",
"regex-syntax",
"regex-syntax 0.7.5",
"string_cache",
"term",
"tiny-keccak",
@ -3469,25 +3469,25 @@ dependencies = [
[[package]]
name = "regex"
version = "1.9.5"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
"regex-syntax 0.8.2",
]
[[package]]
name = "regex-automata"
version = "0.3.8"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"regex-syntax 0.8.2",
]
[[package]]
@ -3496,6 +3496,12 @@ version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "reqwest"
version = "0.11.20"
@ -4862,6 +4868,7 @@ dependencies = [
"open",
"public-ip",
"rand",
"regex",
"reqwest",
"ring",
"route-recognizer",

View File

@ -66,3 +66,4 @@ wasmtime-wasi = "12.0.1"
zip = "0.6"
base64 = "0.13"
route-recognizer = "0.3.1"
regex = "1.10.2"

View File

@ -230,10 +230,8 @@ async fn http_handle_messages(
// if no corresponding entry, nowhere to send response
None => { }
Some((path, channel)) => {
println!("have a http_response with path {}", path);
// if path is /rpc/message, return accordingly with base64 encoded payload
if path == "/rpc/message".to_string() {
println!("got rpc message!!");
let payload = payload.map(|p| {
let bytes = p.bytes;
let base64_bytes = base64::encode(&bytes);
@ -270,13 +268,7 @@ async fn http_handle_messages(
};
let bytes = payload.bytes;
let _ = print_tx
.send(Printout {
verbosity: 1,
content: format!("ID: {}", id.to_string()),
})
.await;
// for the login case, todo refactor out?
let segments: Vec<&str> = path
.split('/')
.filter(|&segment| !segment.is_empty())
@ -365,11 +357,13 @@ async fn http_handle_messages(
let mut path_bindings = path_bindings.lock().await;
let app = source.process.clone().to_string();
let mut path = path.clone();
if app != "homepage:homepage:uqbar" {
if !path_starts_with(&path, &app) {
println!("cannot bind");
return Err(HttpServerError::PathBind { error: "path needs to start with /app:package:publisher/".into() });
}
path = if path.starts_with("/") {
format!("/{}{}", app, path)
} else {
format!("/{}/{}", app, path)
};
}
let bound_path = BoundPath {
@ -714,22 +708,23 @@ async fn handler(
None => "".to_string(),
};
let path = path.as_str().to_string();
let raw_path = path.as_str().to_string();
let id: u64 = rand::random();
let real_headers = serialize_headers(&headers);
let path_bindings = path_bindings.lock().await;
let mut km: Option<KernelMessage> = None;
if let Ok(route) = path_bindings.recognize(&path) {
if let Ok(route) = path_bindings.recognize(&raw_path) {
let bound_path = route.handler();
let app = bound_path.app.to_string();
let url_params: HashMap<&str, &str> = route.params().into_iter().collect();
let path = remove_process_id(&raw_path);
if bound_path.authenticated {
let auth_token = real_headers.get("cookie").cloned().unwrap_or_default();
if !auth_cookie_valid(our.clone(), &auth_token, jwt_secret_bytes) {
println!("http_server: no secret, or invalid token");
// println!("http_server: no secret, or invalid token");
return Ok(
warp::reply::with_status(vec![], StatusCode::UNAUTHORIZED).into_response()
);
@ -812,7 +807,7 @@ async fn handler(
serde_json::json!({
"address": address,
"method": method.to_string(),
"raw_path": path.clone(),
"raw_path": raw_path.clone(),
"path": path.clone(),
"headers": serialize_headers(&headers),
"query_params": query_params,
@ -837,7 +832,7 @@ async fn handler(
http_response_senders
.lock()
.await
.insert(id, (path, response_sender));
.insert(id, (raw_path, response_sender));
let message = km.unwrap(); // DOUBLECHECK
send_to_loop.send(message).await.unwrap();

View File

@ -3,6 +3,7 @@ use futures::stream::SplitSink;
use hmac::{Hmac, Mac};
use jwt::{Error, VerifyWithKey};
use serde::{Deserialize, Serialize};
use regex::Regex;
use sha2::Sha256;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
@ -82,8 +83,12 @@ pub fn auth_cookie_valid(our_node: String, cookie: &str, jwt_secret: Vec<u8>) ->
}
}
pub fn path_starts_with(path: &str, app: &str) -> bool {
path.starts_with(app)
pub fn remove_process_id(path: &str) -> String {
let re = Regex::new(r"^/[^/]+/[^/]+(/.*)?").unwrap();
re.captures(path)
.and_then(|caps| caps.get(1).map(|m| m.as_str()))
.unwrap_or("/")
.to_string()
}
pub async fn handle_incoming_ws(

View File

@ -727,7 +727,7 @@ pub enum HttpServerError {
json
)]
BadJson { json: String, error: String },
#[error("http_server: path bindning error: {:?}", error)]
#[error("http_server: path binding error: {:?}", error)]
PathBind { error: String },
}