2022-10-20 18:00:07 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2022-02-14 15:32:41 +01:00
|
|
|
use ::time::format_description::well_known::Rfc3339;
|
2022-01-12 15:35:33 +01:00
|
|
|
use maplit::hashmap;
|
|
|
|
use once_cell::sync::Lazy;
|
2022-02-14 15:32:41 +01:00
|
|
|
use time::{Duration, OffsetDateTime};
|
2022-01-12 15:35:33 +01:00
|
|
|
|
|
|
|
use super::authorization::{ALL_ACTIONS, AUTHORIZATIONS};
|
2023-09-11 16:50:53 +02:00
|
|
|
use crate::common::{Server, Value};
|
|
|
|
use crate::json;
|
2022-01-12 15:35:33 +01:00
|
|
|
|
2022-05-23 17:03:28 +02:00
|
|
|
fn generate_tenant_token(
|
|
|
|
parent_uid: impl AsRef<str>,
|
|
|
|
parent_key: impl AsRef<str>,
|
|
|
|
mut body: HashMap<&str, Value>,
|
|
|
|
) -> String {
|
2022-01-12 15:35:33 +01:00
|
|
|
use jsonwebtoken::{encode, EncodingKey, Header};
|
|
|
|
|
2022-05-23 17:03:28 +02:00
|
|
|
let parent_uid = parent_uid.as_ref();
|
|
|
|
body.insert("apiKeyUid", json!(parent_uid));
|
2022-10-20 18:00:07 +02:00
|
|
|
encode(&Header::default(), &body, &EncodingKey::from_secret(parent_key.as_ref().as_bytes()))
|
|
|
|
.unwrap()
|
2022-01-12 15:35:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static DOCUMENTS: Lazy<Value> = Lazy::new(|| {
|
|
|
|
json!([
|
|
|
|
{
|
|
|
|
"title": "Shazam!",
|
|
|
|
"id": "287947",
|
|
|
|
"color": ["green", "blue"]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Captain Marvel",
|
|
|
|
"id": "299537",
|
|
|
|
"color": ["yellow", "blue"]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Escape Room",
|
|
|
|
"id": "522681",
|
|
|
|
"color": ["yellow", "red"]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "How to Train Your Dragon: The Hidden World",
|
|
|
|
"id": "166428",
|
|
|
|
"color": ["green", "red"]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Glass",
|
|
|
|
"id": "450465",
|
|
|
|
"color": ["blue", "red"]
|
|
|
|
}
|
|
|
|
])
|
|
|
|
});
|
|
|
|
|
|
|
|
static INVALID_RESPONSE: Lazy<Value> = Lazy::new(|| {
|
|
|
|
json!({"message": "The provided API key is invalid.",
|
|
|
|
"code": "invalid_api_key",
|
|
|
|
"type": "auth",
|
2023-01-19 16:10:05 +01:00
|
|
|
"link": "https://docs.meilisearch.com/errors#invalid_api_key"
|
2022-01-12 15:35:33 +01:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
static ACCEPTED_KEYS: Lazy<Vec<Value>> = Lazy::new(|| {
|
|
|
|
vec![
|
|
|
|
json!({
|
|
|
|
"indexes": ["*"],
|
|
|
|
"actions": ["*"],
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
2022-01-12 15:35:33 +01:00
|
|
|
}),
|
|
|
|
json!({
|
|
|
|
"indexes": ["*"],
|
|
|
|
"actions": ["search"],
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
2022-01-12 15:35:33 +01:00
|
|
|
}),
|
|
|
|
json!({
|
|
|
|
"indexes": ["sales"],
|
|
|
|
"actions": ["*"],
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
2022-01-12 15:35:33 +01:00
|
|
|
}),
|
|
|
|
json!({
|
|
|
|
"indexes": ["sales"],
|
|
|
|
"actions": ["search"],
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
2022-01-12 15:35:33 +01:00
|
|
|
}),
|
2023-02-08 18:19:51 +01:00
|
|
|
json!({
|
|
|
|
"indexes": ["sal*", "prod*"],
|
|
|
|
"actions": ["search"],
|
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
|
|
|
}),
|
2022-01-12 15:35:33 +01:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
static REFUSED_KEYS: Lazy<Vec<Value>> = Lazy::new(|| {
|
|
|
|
vec![
|
|
|
|
// no search action
|
|
|
|
json!({
|
|
|
|
"indexes": ["*"],
|
|
|
|
"actions": ALL_ACTIONS.iter().cloned().filter(|a| *a != "search" && *a != "*").collect::<Vec<_>>(),
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
2022-01-12 15:35:33 +01:00
|
|
|
}),
|
|
|
|
json!({
|
|
|
|
"indexes": ["sales"],
|
|
|
|
"actions": ALL_ACTIONS.iter().cloned().filter(|a| *a != "search" && *a != "*").collect::<Vec<_>>(),
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
2022-01-12 15:35:33 +01:00
|
|
|
}),
|
|
|
|
// bad index
|
|
|
|
json!({
|
|
|
|
"indexes": ["products"],
|
|
|
|
"actions": ["*"],
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
2022-01-12 15:35:33 +01:00
|
|
|
}),
|
2023-02-08 18:19:51 +01:00
|
|
|
json!({
|
|
|
|
"indexes": ["prod*", "p*"],
|
|
|
|
"actions": ["*"],
|
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
|
|
|
}),
|
2022-01-12 15:35:33 +01:00
|
|
|
json!({
|
|
|
|
"indexes": ["products"],
|
|
|
|
"actions": ["search"],
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::days(1)).format(&Rfc3339).unwrap()
|
2022-01-12 15:35:33 +01:00
|
|
|
}),
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2022-06-28 00:59:33 +02:00
|
|
|
macro_rules! compute_authorized_search {
|
2022-01-12 15:35:33 +01:00
|
|
|
($tenant_tokens:expr, $filter:expr, $expected_count:expr) => {
|
|
|
|
let mut server = Server::new_auth().await;
|
2022-05-23 17:03:28 +02:00
|
|
|
server.use_admin_key("MASTER_KEY").await;
|
2022-01-12 15:35:33 +01:00
|
|
|
let index = server.index("sales");
|
|
|
|
let documents = DOCUMENTS.clone();
|
|
|
|
index.add_documents(documents, None).await;
|
|
|
|
index.wait_task(0).await;
|
|
|
|
index
|
|
|
|
.update_settings(json!({"filterableAttributes": ["color"]}))
|
|
|
|
.await;
|
|
|
|
index.wait_task(1).await;
|
|
|
|
drop(index);
|
|
|
|
|
|
|
|
for key_content in ACCEPTED_KEYS.iter() {
|
|
|
|
server.use_api_key("MASTER_KEY");
|
|
|
|
let (response, code) = server.add_api_key(key_content.clone()).await;
|
|
|
|
assert_eq!(code, 201);
|
|
|
|
let key = response["key"].as_str().unwrap();
|
2022-05-23 17:03:28 +02:00
|
|
|
let uid = response["uid"].as_str().unwrap();
|
2022-01-12 15:35:33 +01:00
|
|
|
|
|
|
|
for tenant_token in $tenant_tokens.iter() {
|
2022-05-23 17:03:28 +02:00
|
|
|
let web_token = generate_tenant_token(&uid, &key, tenant_token.clone());
|
2022-01-12 15:35:33 +01:00
|
|
|
server.use_api_key(&web_token);
|
|
|
|
let index = server.index("sales");
|
|
|
|
index
|
|
|
|
.search(json!({ "filter": $filter }), |response, code| {
|
|
|
|
assert_eq!(
|
|
|
|
code, 200,
|
|
|
|
"{} using tenant_token: {:?} generated with parent_key: {:?}",
|
|
|
|
response, tenant_token, key_content
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
response["hits"].as_array().unwrap().len(),
|
|
|
|
$expected_count,
|
|
|
|
"{} using tenant_token: {:?} generated with parent_key: {:?}",
|
|
|
|
response,
|
|
|
|
tenant_token,
|
|
|
|
key_content
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! compute_forbidden_search {
|
|
|
|
($tenant_tokens:expr, $parent_keys:expr) => {
|
|
|
|
let mut server = Server::new_auth().await;
|
2022-05-23 17:03:28 +02:00
|
|
|
server.use_admin_key("MASTER_KEY").await;
|
2022-01-12 15:35:33 +01:00
|
|
|
let index = server.index("sales");
|
|
|
|
let documents = DOCUMENTS.clone();
|
|
|
|
index.add_documents(documents, None).await;
|
|
|
|
index.wait_task(0).await;
|
|
|
|
drop(index);
|
|
|
|
|
|
|
|
for key_content in $parent_keys.iter() {
|
|
|
|
server.use_api_key("MASTER_KEY");
|
|
|
|
let (response, code) = server.add_api_key(key_content.clone()).await;
|
|
|
|
assert_eq!(code, 201, "{:?}", response);
|
|
|
|
let key = response["key"].as_str().unwrap();
|
2022-05-23 17:03:28 +02:00
|
|
|
let uid = response["uid"].as_str().unwrap();
|
2022-01-12 15:35:33 +01:00
|
|
|
|
|
|
|
for tenant_token in $tenant_tokens.iter() {
|
2022-05-23 17:03:28 +02:00
|
|
|
let web_token = generate_tenant_token(&uid, &key, tenant_token.clone());
|
2022-01-12 15:35:33 +01:00
|
|
|
server.use_api_key(&web_token);
|
|
|
|
let index = server.index("sales");
|
|
|
|
index
|
|
|
|
.search(json!({}), |response, code| {
|
|
|
|
assert_eq!(
|
|
|
|
response,
|
|
|
|
INVALID_RESPONSE.clone(),
|
|
|
|
"{} using tenant_token: {:?} generated with parent_key: {:?}",
|
|
|
|
response,
|
|
|
|
tenant_token,
|
|
|
|
key_content
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
code, 403,
|
|
|
|
"{} using tenant_token: {:?} generated with parent_key: {:?}",
|
|
|
|
response, tenant_token, key_content
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn search_authorized_simple_token() {
|
|
|
|
let tenant_tokens = vec![
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"*": {}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["*"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"sales": {}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["sales"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"*": {}}),
|
2023-09-11 16:50:53 +02:00
|
|
|
"exp" => json!(null)
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
2023-09-11 16:50:53 +02:00
|
|
|
"searchRules" => json!({"*": null}),
|
|
|
|
"exp" => json!(null)
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["*"]),
|
2023-09-11 16:50:53 +02:00
|
|
|
"exp" => json!(null)
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"sales": {}}),
|
2023-09-11 16:50:53 +02:00
|
|
|
"exp" => json!(null)
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
2023-09-11 16:50:53 +02:00
|
|
|
"searchRules" => json!({"sales": null}),
|
|
|
|
"exp" => json!(null)
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["sales"]),
|
2023-09-11 16:50:53 +02:00
|
|
|
"exp" => json!(null)
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
2023-02-08 18:19:51 +01:00
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["sa*"]),
|
2023-09-11 16:50:53 +02:00
|
|
|
"exp" => json!(null)
|
2023-02-08 18:19:51 +01:00
|
|
|
},
|
2022-01-12 15:35:33 +01:00
|
|
|
];
|
|
|
|
|
2022-06-28 00:59:33 +02:00
|
|
|
compute_authorized_search!(tenant_tokens, {}, 5);
|
2022-01-12 15:35:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn search_authorized_filter_token() {
|
|
|
|
let tenant_tokens = vec![
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"*": {"filter": "color = blue"}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"sales": {"filter": "color = blue"}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"*": {"filter": ["color = blue"]}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"sales": {"filter": ["color = blue"]}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
// filter on sales should override filters on *
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({
|
|
|
|
"*": {"filter": "color = green"},
|
|
|
|
"sales": {"filter": "color = blue"}
|
|
|
|
}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({
|
|
|
|
"*": {},
|
|
|
|
"sales": {"filter": "color = blue"}
|
|
|
|
}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({
|
|
|
|
"*": {"filter": "color = green"},
|
|
|
|
"sales": {"filter": ["color = blue"]}
|
|
|
|
}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({
|
|
|
|
"*": {},
|
|
|
|
"sales": {"filter": ["color = blue"]}
|
|
|
|
}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-06-28 00:59:33 +02:00
|
|
|
compute_authorized_search!(tenant_tokens, {}, 3);
|
2022-01-12 15:35:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn filter_search_authorized_filter_token() {
|
|
|
|
let tenant_tokens = vec![
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"*": {"filter": "color = blue"}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"sales": {"filter": "color = blue"}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"*": {"filter": ["color = blue"]}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"sales": {"filter": ["color = blue"]}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
// filter on sales should override filters on *
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({
|
|
|
|
"*": {"filter": "color = green"},
|
|
|
|
"sales": {"filter": "color = blue"}
|
|
|
|
}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({
|
|
|
|
"*": {},
|
|
|
|
"sales": {"filter": "color = blue"}
|
|
|
|
}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({
|
|
|
|
"*": {"filter": "color = green"},
|
|
|
|
"sales": {"filter": ["color = blue"]}
|
|
|
|
}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({
|
|
|
|
"*": {},
|
|
|
|
"sales": {"filter": ["color = blue"]}
|
|
|
|
}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
2023-02-08 18:19:51 +01:00
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({
|
|
|
|
"*": {},
|
|
|
|
"sal*": {"filter": ["color = blue"]}
|
|
|
|
}),
|
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
|
|
|
},
|
2022-01-12 15:35:33 +01:00
|
|
|
];
|
|
|
|
|
2022-06-28 00:59:33 +02:00
|
|
|
compute_authorized_search!(tenant_tokens, "color = yellow", 1);
|
2022-01-12 15:35:33 +01:00
|
|
|
}
|
|
|
|
|
2023-02-08 18:19:51 +01:00
|
|
|
/// Tests that those Tenant Token are incompatible with the REFUSED_KEYS defined above.
|
2022-01-12 15:35:33 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn error_search_token_forbidden_parent_key() {
|
|
|
|
let tenant_tokens = vec![
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"*": {}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
2023-09-11 16:50:53 +02:00
|
|
|
"searchRules" => json!({"*": null}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["*"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"sales": {}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
2023-09-11 16:50:53 +02:00
|
|
|
"searchRules" => json!({"sales": null}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["sales"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
2023-02-08 18:19:51 +01:00
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["sali*", "s*", "sales*"]),
|
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
|
|
|
},
|
2022-01-12 15:35:33 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
compute_forbidden_search!(tenant_tokens, REFUSED_KEYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn error_search_forbidden_token() {
|
|
|
|
let tenant_tokens = vec![
|
|
|
|
// bad index
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"products": {}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["products"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"products": {}}),
|
2023-09-11 16:50:53 +02:00
|
|
|
"exp" => json!(null)
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
2023-09-11 16:50:53 +02:00
|
|
|
"searchRules" => json!({"products": null}),
|
|
|
|
"exp" => json!(null)
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["products"]),
|
2023-09-11 16:50:53 +02:00
|
|
|
"exp" => json!(null)
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
// expired token
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"*": {}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
2023-09-11 16:50:53 +02:00
|
|
|
"searchRules" => json!({"*": null}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["*"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!({"sales": {}}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
2023-09-11 16:50:53 +02:00
|
|
|
"searchRules" => json!({"sales": null}),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
hashmap! {
|
|
|
|
"searchRules" => json!(["sales"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
compute_forbidden_search!(tenant_tokens, ACCEPTED_KEYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn error_access_forbidden_routes() {
|
|
|
|
let mut server = Server::new_auth().await;
|
|
|
|
server.use_api_key("MASTER_KEY");
|
|
|
|
|
|
|
|
let content = json!({
|
|
|
|
"indexes": ["*"],
|
|
|
|
"actions": ["*"],
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::hours(1)).format(&Rfc3339).unwrap(),
|
2022-01-12 15:35:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let (response, code) = server.add_api_key(content).await;
|
|
|
|
assert_eq!(code, 201);
|
|
|
|
assert!(response["key"].is_string());
|
|
|
|
|
|
|
|
let key = response["key"].as_str().unwrap();
|
2022-05-23 17:03:28 +02:00
|
|
|
let uid = response["uid"].as_str().unwrap();
|
2022-01-12 15:35:33 +01:00
|
|
|
|
|
|
|
let tenant_token = hashmap! {
|
|
|
|
"searchRules" => json!(["*"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
};
|
2022-10-25 10:53:29 +02:00
|
|
|
let web_token = generate_tenant_token(uid, key, tenant_token);
|
2022-01-12 15:35:33 +01:00
|
|
|
server.use_api_key(&web_token);
|
|
|
|
|
|
|
|
for ((method, route), actions) in AUTHORIZATIONS.iter() {
|
|
|
|
if !actions.contains("search") {
|
|
|
|
let (response, code) = server.dummy_request(method, route).await;
|
|
|
|
assert_eq!(response, INVALID_RESPONSE.clone());
|
|
|
|
assert_eq!(code, 403);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn error_access_expired_parent_key() {
|
|
|
|
use std::{thread, time};
|
|
|
|
let mut server = Server::new_auth().await;
|
|
|
|
server.use_api_key("MASTER_KEY");
|
|
|
|
|
|
|
|
let content = json!({
|
|
|
|
"indexes": ["*"],
|
|
|
|
"actions": ["*"],
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::seconds(1)).format(&Rfc3339).unwrap(),
|
2022-01-12 15:35:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let (response, code) = server.add_api_key(content).await;
|
|
|
|
assert_eq!(code, 201);
|
|
|
|
assert!(response["key"].is_string());
|
|
|
|
|
|
|
|
let key = response["key"].as_str().unwrap();
|
2022-05-23 17:03:28 +02:00
|
|
|
let uid = response["uid"].as_str().unwrap();
|
2022-01-12 15:35:33 +01:00
|
|
|
|
|
|
|
let tenant_token = hashmap! {
|
|
|
|
"searchRules" => json!(["*"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
};
|
2022-10-25 10:53:29 +02:00
|
|
|
let web_token = generate_tenant_token(uid, key, tenant_token);
|
2022-01-12 15:35:33 +01:00
|
|
|
server.use_api_key(&web_token);
|
|
|
|
|
|
|
|
// test search request while parent_key is not expired
|
2022-10-20 18:00:07 +02:00
|
|
|
let (response, code) = server.dummy_request("POST", "/indexes/products/search").await;
|
2022-01-12 15:35:33 +01:00
|
|
|
assert_ne!(response, INVALID_RESPONSE.clone());
|
|
|
|
assert_ne!(code, 403);
|
|
|
|
|
|
|
|
// wait until the key is expired.
|
|
|
|
thread::sleep(time::Duration::new(1, 0));
|
|
|
|
|
2022-10-20 18:00:07 +02:00
|
|
|
let (response, code) = server.dummy_request("POST", "/indexes/products/search").await;
|
2022-01-12 15:35:33 +01:00
|
|
|
assert_eq!(response, INVALID_RESPONSE.clone());
|
|
|
|
assert_eq!(code, 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn error_access_modified_token() {
|
|
|
|
let mut server = Server::new_auth().await;
|
|
|
|
server.use_api_key("MASTER_KEY");
|
|
|
|
|
|
|
|
let content = json!({
|
|
|
|
"indexes": ["*"],
|
|
|
|
"actions": ["*"],
|
2022-02-14 15:32:41 +01:00
|
|
|
"expiresAt": (OffsetDateTime::now_utc() + Duration::hours(1)).format(&Rfc3339).unwrap(),
|
2022-01-12 15:35:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let (response, code) = server.add_api_key(content).await;
|
|
|
|
assert_eq!(code, 201);
|
|
|
|
assert!(response["key"].is_string());
|
|
|
|
|
|
|
|
let key = response["key"].as_str().unwrap();
|
2022-05-23 17:03:28 +02:00
|
|
|
let uid = response["uid"].as_str().unwrap();
|
2022-01-12 15:35:33 +01:00
|
|
|
|
|
|
|
let tenant_token = hashmap! {
|
|
|
|
"searchRules" => json!(["products"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
};
|
2022-10-25 10:53:29 +02:00
|
|
|
let web_token = generate_tenant_token(uid, key, tenant_token);
|
2022-01-12 15:35:33 +01:00
|
|
|
server.use_api_key(&web_token);
|
|
|
|
|
|
|
|
// test search request while web_token is valid
|
2022-10-20 18:00:07 +02:00
|
|
|
let (response, code) = server.dummy_request("POST", "/indexes/products/search").await;
|
2022-01-12 15:35:33 +01:00
|
|
|
assert_ne!(response, INVALID_RESPONSE.clone());
|
|
|
|
assert_ne!(code, 403);
|
|
|
|
|
|
|
|
let tenant_token = hashmap! {
|
|
|
|
"searchRules" => json!(["*"]),
|
2022-02-14 15:32:41 +01:00
|
|
|
"exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp())
|
2022-01-12 15:35:33 +01:00
|
|
|
};
|
|
|
|
|
2022-10-25 10:53:29 +02:00
|
|
|
let alt = generate_tenant_token(uid, key, tenant_token);
|
2022-01-12 15:35:33 +01:00
|
|
|
let altered_token = [
|
|
|
|
web_token.split('.').next().unwrap(),
|
|
|
|
alt.split('.').nth(1).unwrap(),
|
|
|
|
web_token.split('.').nth(2).unwrap(),
|
|
|
|
]
|
|
|
|
.join(".");
|
|
|
|
|
|
|
|
server.use_api_key(&altered_token);
|
2022-10-20 18:00:07 +02:00
|
|
|
let (response, code) = server.dummy_request("POST", "/indexes/products/search").await;
|
2022-01-12 15:35:33 +01:00
|
|
|
assert_eq!(response, INVALID_RESPONSE.clone());
|
|
|
|
assert_eq!(code, 403);
|
|
|
|
}
|