Skip to content

Commit

Permalink
Add basic CSRF protection to built-in HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
jpovixwm authored and qu1ck committed Feb 11, 2024
1 parent 0fe9b79 commit ea69a07
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src-tauri/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ async fn http_response(
args_lock: Arc<Semaphore>,
req: Request<Body>,
) -> hyper::Result<Response<Body>> {
let content_type = req.headers().get("Content-Type").and_then(|ct| ct.to_str().ok()).unwrap_or("");
if content_type != "application/json" && req.method() != Method::OPTIONS {
return Ok(invalid_request(req.headers(), "unexpected content-type"));
}

let toast = req.headers().get("X-TrguiNG-Toast").is_some();
let sound = req.headers().get("X-TrguiNG-Sound").is_some();

Expand All @@ -99,7 +104,7 @@ async fn http_response(
}

let lock = args_lock.acquire().await.unwrap();
send_payoad(&app, payload).await;
send_payload(&app, payload).await;
drop(lock);

Ok(Response::builder().body(Body::from("TrguiNG OK")).unwrap())
Expand Down Expand Up @@ -224,7 +229,7 @@ async fn proxy_fetch(req: Request<Body>) -> Result<Response<Body>, hyper::Error>
}
}

async fn send_payoad(app: &AppHandle, payload: Bytes) {
async fn send_payload(app: &AppHandle, payload: Bytes) {
app.get_window("main")
.unwrap()
.emit(
Expand Down
6 changes: 4 additions & 2 deletions src/rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export class TransmissionClient {

constructor(connection: ServerConnection, toastNotifications: boolean, toastNotificationSound: boolean, timeout = 15) {
this.url = encodeURIComponent(connection.url);
this.headers = {};
this.headers = {
"Content-Type": "application/json",
};
if (toastNotifications) {
this.headers["X-TrguiNG-toast"] = "true";
}
Expand Down Expand Up @@ -384,7 +386,7 @@ export class TransmissionClient {
const url = `${RUST_BACKEND}/iplookup`;
const body = JSON.stringify(ips);

const response = await fetch(url, { method: "POST", body });
const response = await fetch(url, { method: "POST", body, headers: { "Content-Type": "application/json" } });

if (response.ok) {
return await response.json();
Expand Down

0 comments on commit ea69a07

Please sign in to comment.