]> git.proxmox.com Git - proxmox.git/commitdiff
src/server/rest.rs: improve logs for unauthorized request
authorDietmar Maurer <dietmar@proxmox.com>
Sun, 17 Feb 2019 16:18:44 +0000 (17:18 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Sun, 17 Feb 2019 16:18:44 +0000 (17:18 +0100)
src/server/formatter.rs
src/server/rest.rs

index a4b898880a133dc5bc97767fe53250eb0c612784..b7ad5a65ae726c33ac478c1b6ea500e4970cea88 100644 (file)
@@ -1,7 +1,7 @@
 use failure::*;
 use serde_json::{json, Value};
 
-use crate::api_schema::router::RpcEnvironment;
+use crate::api_schema::router::{HttpError, RpcEnvironment};
 
 use hyper::{Body, Response, StatusCode};
 use hyper::header;
@@ -52,11 +52,19 @@ fn json_format_result(data: Value, rpcenv: &RpcEnvironment) -> Response<Body> {
 
 fn json_format_error(err: Error) -> Response<Body> {
 
-    let mut response = Response::new(Body::from(err.to_string()));
+    let mut response = if let Some(apierr) = err.downcast_ref::<HttpError>() {
+        let mut resp = Response::new(Body::from(apierr.message.clone()));
+        *resp.status_mut() = apierr.code;
+        resp
+    } else {
+        let mut resp = Response::new(Body::from(err.to_string()));
+        *resp.status_mut() = StatusCode::BAD_REQUEST;
+        resp
+    };
+
     response.headers_mut().insert(
         header::CONTENT_TYPE,
         header::HeaderValue::from_static(JSON_CONTENT_TYPE));
-    *response.status_mut() = StatusCode::BAD_REQUEST;
 
     response.extensions_mut().insert(ErrorMessageExtension(err.to_string()));
 
index 9c236d8225ac5e2637b158168f2e4b7ecb41176f..2a1546ab674bbfe7eecf43d3f6b78ad9c0997535 100644 (file)
@@ -454,10 +454,10 @@ fn check_auth(method: &hyper::Method, ticket: Option<String>, token: Option<Stri
 
     if method != hyper::Method::GET {
         if let Some(token) = token {
-            println!("CSRF prev token: {:?}", token);
+            println!("CSRF prevention token: {:?}", token);
             verify_csrf_prevention_token(csrf_secret(), &username, &token, -300, ticket_lifetime)?;
         } else {
-            bail!("");
+            bail!("missing CSRF prevention token");
         }
     }