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

Extra validation for w3c credentials and presentations #325

Merged
merged 2 commits into from
Feb 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/data_types/w3c/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::Result;
/// Note, that this definition is tied to AnonCreds W3C form
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct W3CCredential {
#[serde(rename = "@context")]
pub context: Contexts,
Expand Down Expand Up @@ -162,4 +163,16 @@ mod tests {
serde_json::from_str(&out_json).expect("Error deserializing w3c credential");
assert_eq!(cred1, cred2);
}

#[test]
fn serde_w3c_credential_deny_unknown() {
let cred_json = include_str!("sample_credential.json");
let mut cred: serde_json::Value =
serde_json::from_str(cred_json).expect("Error deserializing w3c credential");
cred.as_object_mut()
.unwrap()
.insert("prop".into(), "val".into());
let res = serde_json::from_value::<W3CCredential>(cred);
assert!(res.is_err());
}
}
13 changes: 13 additions & 0 deletions src/data_types/w3c/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::Result;
/// Note, that this definition is tied to AnonCreds W3C form
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct W3CPresentation {
#[serde(rename = "@context")]
pub context: Contexts,
Expand Down Expand Up @@ -69,4 +70,16 @@ mod tests {
serde_json::from_str(&out_json).expect("Error deserializing w3c presentation");
assert_eq!(pres1, pres2);
}

#[test]
fn serde_w3c_presentation_deny_unknown() {
let pres_json = include_str!("sample_presentation.json");
let mut pres: serde_json::Value =
serde_json::from_str(pres_json).expect("Error deserializing w3c presentation");
pres.as_object_mut()
.unwrap()
.insert("prop".into(), "val".into());
let res = serde_json::from_value::<W3CPresentation>(pres);
assert!(res.is_err());
}
}
64 changes: 64 additions & 0 deletions src/services/w3c/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,24 @@ fn check_request_data(
)?;
}

for (cred, proof) in presentation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these checks also be made when calling credential_process?

Otherwise looks good to me, thanks for picking this up

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there might be additional verification required for w3c credentials, I was only looking at presentations.

.verifiable_credential
.as_slice()
.iter()
.zip(credential_proofs)
{
let Some(cred_def_issuer) = cred_defs.get(&proof.cred_def_id).map(|cd| &cd.issuer_id) else {
return Err(err_msg!("Missing credential definition"));
};
if cred_def_issuer != &cred.issuer {
return Err(err_msg!("Inconsistent issuer ID"));
}
let di_proof = cred.get_data_integrity_proof()?;
if di_proof.verification_method != proof.cred_def_id.0 {
return Err(err_msg!("Inconsistent credential definition ID"));
}
}

Ok(())
}

Expand Down Expand Up @@ -892,4 +910,50 @@ pub(crate) mod tests {
.unwrap_err();
assert_eq!(ErrorKind::Input, err.kind());
}

#[rstest]
#[case(_presentation_request_with_single_attribute())]
fn test_check_cred_def_id_mismatch_fails(
schemas: HashMap<SchemaId, Schema>,
cred_defs: HashMap<CredentialDefinitionId, CredentialDefinition>,
presentation: W3CPresentation,
#[case] presentation_request: PresentationRequestPayload,
) {
let mut cred_proofs = presentation.credential_proofs();
cred_proofs[0].cred_def_id = "other:id".try_into().unwrap();

let err = check_request_data(
&presentation_request,
&presentation,
&schemas,
&cred_defs,
None,
&cred_proofs,
)
.unwrap_err();
assert_eq!(ErrorKind::Input, err.kind());
}

#[rstest]
#[case(_presentation_request_with_single_attribute())]
fn test_check_issuer_id_mismatch_fails(
schemas: HashMap<SchemaId, Schema>,
mut cred_defs: HashMap<CredentialDefinitionId, CredentialDefinition>,
presentation: W3CPresentation,
#[case] presentation_request: PresentationRequestPayload,
) {
let cred_def = cred_defs.iter_mut().next().unwrap().1;
cred_def.issuer_id = "other:id".try_into().unwrap();

let err = check_request_data(
&presentation_request,
&presentation,
&schemas,
&cred_defs,
None,
&presentation.credential_proofs(),
)
.unwrap_err();
assert_eq!(ErrorKind::Input, err.kind());
}
}
Loading