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

[BugFix]: fixing panic in evaluator when query from typeblock is unresolved #432

Merged
merged 1 commit into from
Dec 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion guard/src/rules/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1778,8 +1778,24 @@ pub(in crate::rules) fn eval_type_block_clause<'value, 'loc: 'value>(
}
}
}
QueryResult::UnResolved(ur) => {
resolver.end_record(
&context,
RecordType::TypeCheck(TypeBlockCheck {
type_name: &type_block.type_name,
block: BlockCheck {
at_least_one_matches: false,
status: Status::FAIL,
message: ur.reason.clone(),
},
}),
)?;

QueryResult::UnResolved(_) => unreachable!(),
return Err(Error::MissingValue(format!(
"Unable to resolve type block query: {}",
type_block.type_name,
)));
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions guard/tests/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,18 @@ mod validate_tests {
assert_eq!(StatusCode::SUCCESS, status_code);
}

#[test]
fn test_with_payload_failing_type_block() {
let payload = r#"{"data": [ "{}" ], "rules" : [ "d1z::Y\n\t\tm<0m<03333333" ]}"#;
let mut reader = Reader::new(ReadCursor(Cursor::new(Vec::from(payload.as_bytes()))));
let mut writer = Writer::new(WBVec(vec![]), WBVec(vec![]));
let status_code = ValidateTestRunner::default()
.payload()
.run(&mut writer, &mut reader);

assert_eq!(StatusCode::INTERNAL_FAILURE, status_code);
}

#[test]
fn test_with_payload_flag_fail() {
let payload = r#"{"data": ["{\"Resources\":{\"NewVolume\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":500,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2b\"}},\"NewVolume2\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":50,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2c\"}}},\"Parameters\":{\"InstanceName\":\"TestInstance\"}}","{\"Resources\":{\"NewVolume\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":500,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2b\"}},\"NewVolume2\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":50,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2c\"}}},\"Parameters\":{\"InstanceName\":\"TestInstance\"}}"], "rules" : [ "Parameters.InstanceName == \"TestInstance\"","Parameters.InstanceName == \"SomeRandomString\"" ]}"#;
Expand Down