Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic CSRF protection to built-in HTTP server #164

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading