1
0
Fork 0
mirror of https://git.42l.fr/neil/sncf.git synced 2024-04-27 19:52:45 +02:00
sncf/src/sniff.rs

102 lines
2.9 KiB
Rust
Raw Normal View History

2020-08-19 01:21:42 +02:00
use actix_web::web;
use serde_json::Value;
use crate::debug;
// checks to be done on user requests
// if it returns true, cancels the request
pub fn check_request(route: &str, body: &web::Bytes) -> bool {
match route {
"/ocs/v2.php/apps/forms/api/v1.1/form/update" => rq_form_update(body),
2020-08-19 01:21:42 +02:00
_ => false,
}
}
// prevents the user from doing anything other than link sharing.
fn rq_form_update(body: &web::Bytes) -> bool {
let req = String::from_utf8_lossy(body);
// try to serialize the body.
// If the parsing fails, drop the request
let v: Value = serde_json::from_str(&req).unwrap_or_else(|e| {
eprintln!("check_request: failed to parse JSON: {}", e);
Value::Null
});
// if the type or isAnonymous is set (isn't null),
// drop the request.
// Also drop if v is null because of parsing fail.
v == Value::Null
|| v["keyValuePairs"]["isAnonymous"] != Value::Null
|| v["keyValuePairs"]["access"]["type"] != Value::Null
}
// checks to be done on responses from the Nextcloud instance
// if it returns true, cancels the request
// NOTE: unused for now
/*pub fn check_response(_route: &str, _body: &web::Bytes) -> bool {
2020-08-19 01:21:42 +02:00
false
}*/
2020-08-19 01:21:42 +02:00
// checks if a form has been created.
// if it's the case, sets some parameters.
// this part may need code quality improvements
// the body MUST come from the "create new form" route
// (this is checked upstream)
// returns the form UID and the request body
pub fn check_new_form(body: &web::Bytes) -> u64 {
2020-08-19 01:21:42 +02:00
let req = String::from_utf8_lossy(body);
// finds the form ID
let v: Value = serde_json::from_str(&req).unwrap_or_else(|e| {
eprintln!("check_new_form: failed to parse JSON: {}", e);
Value::Null
});
if v != Value::Null
&& v["ocs"].is_object()
2021-03-24 20:17:08 +01:00
&& v["ocs"]["data"].is_object()
&& v["ocs"]["data"]["id"] != Value::Null
&& v["ocs"]["data"]["isAnonymous"] == Value::Null
{
//getting form id
2021-03-24 20:29:24 +01:00
v["ocs"]["data"]["id"].as_u64().unwrap_or_else(|| {
2021-03-24 20:17:08 +01:00
eprintln!("check_new_form: failed to parse formid: {}", v);
0
2021-03-24 20:29:24 +01:00
})
2021-03-24 20:17:08 +01:00
} else {
eprintln!("error: check_new_form: can't find formid: {}", v);
0
2020-08-19 01:21:42 +02:00
}
}
2020-09-08 18:36:27 +02:00
// those routes won't be redirected
2020-08-22 16:38:24 +02:00
const BLOCKED_ROUTES: &[&str] = &[
2020-08-19 01:21:42 +02:00
"/apps/settings",
"/login",
"/settings",
"/ocs/v",
"/remote.php",
"/core/templates/filepicker.html",
2020-08-19 01:21:42 +02:00
];
2020-09-08 18:36:27 +02:00
// ...except if they are in this list
2021-03-24 20:17:08 +01:00
const ALLOWED_ROUTES: &[&str] = &["/ocs/v2.php/apps/forms/", "/status.php"];
2020-09-08 18:36:27 +02:00
2020-08-19 01:21:42 +02:00
// checks if the accessed route is allowed for the user.
// if it returns true, redirects elsewhere
pub fn check_route(route: &str) -> bool {
debug(route);
for r in BLOCKED_ROUTES {
if route.starts_with(r) {
2020-09-08 18:36:27 +02:00
for s in ALLOWED_ROUTES {
if route.starts_with(s) {
return false;
}
}
2020-08-19 01:21:42 +02:00
return true;
}
}
false
}