Skip to content

Commit

Permalink
Merge pull request #361 from Concordium/improve-schema-validation
Browse files Browse the repository at this point in the history
Prevent adding a credential with attributes not listed in schema
  • Loading branch information
orhoj authored Aug 29, 2023
2 parents 5aee208 + 63b5209 commit 4a12187
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/browser-wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- An issue where the import window would fail to open.
- Updated the JSON schema for the verifiable credential schema validation, so that invalid schemas are rejected.
- An issue where a verifiable with the `NotActivated` status would show as `Pending`.
- An issue that allowed empty credential statements to be accepted by the wallet-api.
- An issue where the wallet allowed for requests adding credentials with more attributes than listed in the schema.

## 1.1.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
const network = useAtomValue(networkConfigurationAtom);

const [error, setError] = useState<string>();
const [validationComplete, setValidationComplete] = useState<boolean>(false);

const { credential: rawCredential, url, metadataUrl } = state.payload;
const credential: APIVerifiableCredential = parse(rawCredential);
Expand Down Expand Up @@ -121,10 +122,24 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
}

if (missingRequiredAttributeKeys.length > 0) {
setError(t('error.attribute', { attributeKeys: missingRequiredAttributeKeys }));
setError(t('error.attribute.required', { attributeKeys: missingRequiredAttributeKeys }));
setValidationComplete(true);
return;
}

// Ensure that a credential with more attributes than listed by the schema cannot be added.
const schemaAttributes = Object.keys(schema.properties.credentialSubject.properties.attributes.properties);
for (const credentialAttribute of Object.keys(credential.credentialSubject.attributes)) {
if (!schemaAttributes.includes(credentialAttribute)) {
setError(t('error.attribute.additional', { credentialAttribute, schemaAttributes }));
setValidationComplete(true);
return;
}
}

setValidationComplete(true);
}
}, [schema?.properties.credentialSubject.properties.attributes.required]);
}, [Boolean(schema)]);

useEffect(() => () => controller.abort(), []);

Expand Down Expand Up @@ -193,7 +208,7 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
return credentialSubjectId;
}

if (web3IdCredentials.loading || storedWeb3IdCredentials.loading) {
if (web3IdCredentials.loading || storedWeb3IdCredentials.loading || !validationComplete) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const t = {
'We are unable to add the web3Id credential to the wallet due to the following issue, please report this to the issuer:',
metadata: 'We are unable to load the metadata for the credential.',
schema: 'We are unable to load the schema specification for the credential.',
attribute: 'The received credential is missing one or more required attributes ({{ attributeKeys }})',
attribute: {
required: 'The received credential is missing one or more required attributes ({{ attributeKeys }})',
additional:
'The attribute with key [{{ credentialAttribute }}] is not available in the list of schema attributes: [{{ schemaAttributes }}]',
},
localization: 'Failed to get localization',
},
};
Expand Down

0 comments on commit 4a12187

Please sign in to comment.