Skip to content

Commit

Permalink
Restrict allowed attribute value types
Browse files Browse the repository at this point in the history
  • Loading branch information
orhoj committed Sep 5, 2023
1 parent 21cf72b commit bae4f9a
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/browser-wallet/src/background/web3Id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
IdStatement,
StatementTypes,
AttributeType,
isTimestampAttribute,
TimestampAttribute,
} from '@concordium/web-sdk';
import {
sessionVerifiableCredentials,
Expand Down Expand Up @@ -126,10 +128,16 @@ function rejectRequest(message: string): { run: false; response: MessageStatusWr
};
}

// TODO Expose function from SDK and re-use.
function timestampToDate(attribute: TimestampAttribute): Date {
return new Date(Date.parse(attribute.timestamp));
}

function validateTimestampAttribute(attributeTag: string, attributeValue: AttributeType) {
if (
attributeValue instanceof Date &&
(attributeValue.getTime() < MIN_DATE_TIMESTAMP || attributeValue.getTime() > MAX_DATE_TIMESTAMP)
isTimestampAttribute(attributeValue) &&
(timestampToDate(attributeValue).getTime() < MIN_DATE_TIMESTAMP ||
timestampToDate(attributeValue).getTime() > MAX_DATE_TIMESTAMP)
) {
return `The attribute [${attributeValue}] for key [${attributeTag}] is out of bounds for a Date. The Date must be between ${MIN_DATE_ISO} and ${MAX_DATE_ISO}`;
}
Expand All @@ -154,6 +162,14 @@ function validateAttributeBounds(
attributeTag: string,
attributeValue: AttributeType
): { error: false } | { error: true; message: string } {
if (
typeof attributeValue !== 'string' &&
typeof attributeValue !== 'bigint' &&
!isTimestampAttribute(attributeValue)
) {
return { error: true, message: 'Unsupported attribute type' };
}

const stringError = validateStringAttribute(attributeTag, attributeValue);
if (stringError) {
return { error: true, message: stringError };
Expand Down

0 comments on commit bae4f9a

Please sign in to comment.