mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-17 14:00:29 +01:00
fix: handle case where pubKey cannot be found
This commit is contained in:
@@ -277,5 +277,6 @@
|
|||||||
"api.reauth-required": "The resource you are trying to access requires (re-)authentication.",
|
"api.reauth-required": "The resource you are trying to access requires (re-)authentication.",
|
||||||
|
|
||||||
"activitypub.invalid-id": "Unable to resolve the input id, likely as it is malformed.",
|
"activitypub.invalid-id": "Unable to resolve the input id, likely as it is malformed.",
|
||||||
"activitypub.get-failed": "Unable to retrieve the specified resource."
|
"activitypub.get-failed": "Unable to retrieve the specified resource.",
|
||||||
|
"activitypub.pubKey-not-found": "Unable to resolve public key, so payload verification cannot take place."
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,12 +89,16 @@ ActivityPub.getPrivateKey = async (uid) => {
|
|||||||
|
|
||||||
ActivityPub.fetchPublicKey = async (uri) => {
|
ActivityPub.fetchPublicKey = async (uri) => {
|
||||||
// Used for retrieving the public key from the passed-in keyId uri
|
// Used for retrieving the public key from the passed-in keyId uri
|
||||||
const { body } = await request.get(uri, {
|
const { res, body } = await request.get(uri, {
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!String(res.statusCode).startsWith('2') || !body.hasOwnProperty('publicKey')) {
|
||||||
|
throw new Error('[[error:activitypub.pubKey-not-found]]');
|
||||||
|
}
|
||||||
|
|
||||||
return body.publicKey;
|
return body.publicKey;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -143,9 +147,6 @@ ActivityPub.verify = async (req) => {
|
|||||||
return memo;
|
return memo;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
// Retrieve public key from remote instance
|
|
||||||
const { publicKeyPem } = await ActivityPub.fetchPublicKey(keyId);
|
|
||||||
|
|
||||||
// Re-construct signature string
|
// Re-construct signature string
|
||||||
const signed_string = headers.split(' ').reduce((memo, cur) => {
|
const signed_string = headers.split(' ').reduce((memo, cur) => {
|
||||||
if (cur === '(request-target)') {
|
if (cur === '(request-target)') {
|
||||||
@@ -159,6 +160,9 @@ ActivityPub.verify = async (req) => {
|
|||||||
|
|
||||||
// Verify the signature string via public key
|
// Verify the signature string via public key
|
||||||
try {
|
try {
|
||||||
|
// Retrieve public key from remote instance
|
||||||
|
const { publicKeyPem } = await ActivityPub.fetchPublicKey(keyId);
|
||||||
|
|
||||||
const verify = createVerify('sha256');
|
const verify = createVerify('sha256');
|
||||||
verify.update(signed_string);
|
verify.update(signed_string);
|
||||||
verify.end();
|
verify.end();
|
||||||
|
|||||||
Reference in New Issue
Block a user