Skip to content

Commit

Permalink
fix: Filter out null-valued entries
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Jul 19, 2023
1 parent 6b38893 commit 575ae42
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/crypto/src/form/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ export type EncryptedFormSubmission<FormData extends object> = {
/**
* To get a stable hash, we need the enumeration of entries (key-value tuples)
* to be stable. This sorts them by increasing lexicographic order of the keys.
*
* Also, we drop nullish-valued entries, as it makes little sense
* to encrypt those.
*/
function sortedEntries(input: object) {
return Object.entries(input).sort(([a], [b]) => (a > b ? 1 : a < b ? -1 : 0))
return Object.entries(input)
.sort(([a], [b]) => (a > b ? 1 : a < b ? -1 : 0))
.filter(([, value]) => value !== null && value !== undefined)
}

/**
Expand Down Expand Up @@ -246,9 +251,7 @@ export function verifyFormSubmissionSignature(
formPublicKey,
sodium.crypto_generichash_BYTES
)
const entries = sortedEntries(submission.encrypted).filter(
([encryptedField]) => Boolean(encryptedField)
)
const entries = sortedEntries(submission.encrypted)
sodium.crypto_generichash_update(hashState, numberToUint32LE(entries.length))
for (const [, encryptedField] of entries) {
sodium.crypto_generichash_update(
Expand Down

0 comments on commit 575ae42

Please sign in to comment.