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

Deny unknown fields in FFI. (#815) #1041

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cedar-policy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Cedar Language Version: 4.0

### Changed

- The API around `Request::new` has changed to remove the `Option`s
- The API around `Request::new` has changed to remove the `Option`s
around the entity type arguments.
- Significantly reworked all public-facing error types to address some issues
and improve consistency. See issue #745.
Expand All @@ -35,6 +35,7 @@ Cedar Language Version: 4.0
- Changed JSON schema parser so that `Set`, `Entity`, `Record`, and `Extension`
can be common type names; updated the error message when common type names
conflict with built-in primitive type names (#974, partially resolving #973)
- Changed FFI to error on typos or unexpected fields in the input JSON.

### Removed

Expand Down
2 changes: 2 additions & 0 deletions cedar-policy/src/ffi/is_authorized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ pub enum PartialAuthorizationAnswer {
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct AuthorizationCall {
/// The principal taking action
principal: EntityUid,
Expand Down Expand Up @@ -766,6 +767,7 @@ impl From<Links> for Vec<Link> {
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
struct RecvdSlice {
policies: PolicySet,
/// JSON object containing the entities data, in "natural JSON" form -- same
Expand Down
1 change: 1 addition & 0 deletions cedar-policy/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ mod utils;
pub use utils::*;
mod validate;
pub use validate::*;
mod tests;
90 changes: 90 additions & 0 deletions cedar-policy/src/ffi/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#[cfg(test)]
mod ffi_tests {
use crate::ffi::{is_authorized_json, validate_json};
use cool_asserts::assert_matches;

#[test]
fn test_fail_unknown_field_policy_slice() {
let json = serde_json::json!({
"principal": {
"type": "User",
"id": "alice"
},
"action": {
"type": "Photo",
"id": "view"
},
"resource": {
"type": "Photo",
"id": "door"
},
"context": {},
"slice": {
"policies": {},
"templatePolicies": {},
"entities": []
}
});

assert_matches!(is_authorized_json(json), Err(e) => {
assert!(e.to_string().contains("expected one of"));
});
}

#[test]
fn test_fail_unknown_field_request() {
let json = serde_json::json!({
"principal": {
"type": "User",
"id": "alice"
},
"action": {
"type": "Photo",
"id": "view"
},
"resource": {
"type": "Photo",
"id": "door"
},
"context": {},
"slice": {
"policies": {},
"entities": []
},
"enableRequestValidation": "foo",
});

assert_matches!(is_authorized_json(json), Err(e) => {
assert!(e.to_string().contains("expected one of"));
});
}

#[test]
fn test_fail_unknown_field_validation() {
let json = serde_json::json!({
"schema": { "json": { "": {
"entityTypes": {
"User": {
"memberOfTypes": [ ]
},
"Photo": {
"memberOfTypes": [ ]
}
},
"actions": {
"viewPhoto": {
"appliesTo": {
"resourceTypes": [ "Photo" ],
"principalTypes": [ "User" ]
}
}
}
}}},
"Policies": "forbid(principal, action, resource);permit(principal == Photo::\"photo.jpg\", action == Action::\"viewPhoto\", resource == User::\"alice\");"
});

assert_matches!(validate_json(json), Err(e) => {
assert!(e.to_string().contains("expected one of"));
});
}
}
2 changes: 2 additions & 0 deletions cedar-policy/src/ffi/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub fn validate_json_str(json: &str) -> Result<String, serde_json::Error> {
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct ValidationCall {
/// Validation settings
#[serde(default)]
Expand Down Expand Up @@ -154,6 +155,7 @@ impl ValidationCall {
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct ValidationSettings {
/// Whether validation is enabled. If this flag is set to `false`, then
/// only parsing is performed. The default value is `true`.
Expand Down
Loading