Skip to content

Commit b64cce3

Browse files
Fail fast when the number of request environments hits a limit (#613)
Signed-off-by: Shaobo He <[email protected]>
1 parent 4d8c0ad commit b64cce3

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

cedar-drt/fuzz/fuzz_targets/validation-pbt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const LOG_FILENAME_ERR_INCORRECT_FORMAT: &str = "./logs/err_incorrect_format.txt
8282
const LOG_FILENAME_ERR_OTHER: &str = "./logs/err_other.txt";
8383
const LOG_FILENAME_ENTITIES_ERROR: &str = "./logs/err_entities.txt";
8484
const LOG_FILENAME_SCHEMA_ERROR: &str = "./logs/err_schema.txt";
85+
const LOG_FILENAME_TOO_MANY_REQ_ENVS_ERROR: &str = "./logs/err_too_many_req_envs.txt";
8586

8687
// In the below, "vyes" means the schema passed validation, while "vno" means we
8788
// got to the point of running the validator but validation failed
@@ -155,6 +156,9 @@ fn log_err<T>(res: Result<T>, doing_what: &str) -> Result<T> {
155156
Err(Error::OtherArbitrary(_)) => {
156157
checkpoint(LOG_FILENAME_ERR_OTHER.to_string() + "_" + doing_what)
157158
}
159+
Err(Error::TooManyReqEnvs(_)) => {
160+
checkpoint(LOG_FILENAME_TOO_MANY_REQ_ENVS_ERROR.to_string() + "_" + doing_what)
161+
}
158162
Ok(_) => (),
159163
}
160164
}

cedar-policy-generators/src/err.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ pub enum Error {
7676
/// ourselves.
7777
#[error(transparent)]
7878
OtherArbitrary(arbitrary::Error),
79+
/// Error thrown when the generator produces a schema that has too many
80+
/// valid request environments
81+
#[error("Too many request environments: {} vs upper bound {}", .0 , crate::schema::Schema::PER_ACTION_REQUEST_ENV_LIMIT)]
82+
TooManyReqEnvs(usize),
7983
}
8084

8185
/// Type alias for convenience
@@ -95,6 +99,7 @@ impl From<Error> for arbitrary::Error {
9599
Error::ContextError(_) => arbitrary::Error::IncorrectFormat,
96100
Error::SchemaError(_) => arbitrary::Error::IncorrectFormat,
97101
Error::OtherArbitrary(e) => e,
102+
Error::TooManyReqEnvs(_) => arbitrary::Error::IncorrectFormat,
98103
}
99104
}
100105
}

cedar-policy-generators/src/schema.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,11 @@ impl Schema {
951951
}
952952
}
953953

954+
// An upper bound on the number of request environment a schema can produce
955+
// See https://github.com/cedar-policy/cedar-spec/issues/610 for the
956+
// motivation why we want this limit
957+
pub(crate) const PER_ACTION_REQUEST_ENV_LIMIT: usize = 128;
958+
954959
/// Get an arbitrary `Schema`.
955960
pub fn arbitrary(settings: ABACSettings, u: &mut Unstructured<'_>) -> Result<Schema> {
956961
let namespace = arbitrary_namespace(u)?;
@@ -1063,7 +1068,9 @@ impl Schema {
10631068
u: &mut Unstructured<'_>|
10641069
-> Result<Vec<ast::InternalName>> {
10651070
// Pre-select the number of entity types (minimum 1), then randomly select that many indices
1066-
let num = u.int_in_range(1..=entity_types.len()).unwrap();
1071+
let num = u
1072+
.int_in_range(1..=(entity_types.len() % Self::PER_ACTION_REQUEST_ENV_LIMIT))
1073+
.unwrap();
10671074
let mut indices: Vec<usize> = (0..entity_types.len()).collect();
10681075
let mut selected_indices = Vec::with_capacity(num);
10691076

@@ -1108,6 +1115,13 @@ impl Schema {
11081115
} else {
11091116
principal_and_resource_types_exist = true;
11101117
}
1118+
let req_env_num =
1119+
picked_principal_types.len() * picked_resource_types.len();
1120+
// Fail fast if the number of request environment
1121+
// number is too large
1122+
if req_env_num > Self::PER_ACTION_REQUEST_ENV_LIMIT {
1123+
return Err(Error::TooManyReqEnvs(req_env_num));
1124+
}
11111125
Some(json_schema::ApplySpec {
11121126
resource_types: picked_resource_types,
11131127
principal_types: picked_principal_types,

0 commit comments

Comments
 (0)