Skip to content

Commit

Permalink
Fix error handling in LD Signatures verification
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Nov 12, 2024
1 parent 9ccac4a commit 51a4824
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ To be released.
<q>Body already consumed</q> when the content type of the response was
an HTML document and there's no link to a JSON-LD document.

- Fixed a bug where `verifySignature()` and `verifyJsonLd()` functions
sometimes had thrown a `jsonld.ValidationError` error. Now such errors
are caught and logged as warnings, and the signature to verify is considered
as invalid.


Version 1.0.7
-------------
Expand Down
22 changes: 20 additions & 2 deletions src/sig/ld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,28 @@ export async function verifySignature(
delete sigOpts.type;
delete sigOpts.id;
delete sigOpts.signatureValue;
const sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader);
let sigOptsHash: string;
try {
sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader);
} catch (error) {
logger.warn(
"Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}",
{ signatureOptions: sigOpts, error },
);
return null;
}
const document: { signature?: unknown } = { ...jsonLd };
delete document.signature;
const docHash = await hashJsonLd(document, options.contextLoader);
let docHash: string;
try {
docHash = await hashJsonLd(document, options.contextLoader);
} catch (error) {
logger.warn(
"Failed to verify; failed to hash the document: {document}\n{error}",
{ document, error },
);
return null;
}
const encoder = new TextEncoder();
const message = sigOptsHash + docHash;
const messageBytes = encoder.encode(message);
Expand Down

0 comments on commit 51a4824

Please sign in to comment.