Skip to content

Node.js based forum software built for the modern web

fix(activitypub): align middleware keyId extraction with library last-wins behavior

Julian Lamd30f4c2f95ae
diff --git a/src/middleware/activitypub.js b/src/middleware/activitypub.js
index 80ebd12bc7..8733cdb220 100644
--- a/src/middleware/activitypub.js
+++ b/src/middleware/activitypub.js
@@ -61,7 +61,7 @@ middleware.verify = async function (req, res, next) {
// Set calling user
const keyId = req.headers.signature.split(',').filter(line => line.startsWith('keyId="'));
if (keyId.length) {
- req.uid = keyId.shift().slice(7, -1).replace(/#.*$/, '');
+ req.uid = keyId.at(-1).slice(7, -1).replace(/#.*$/, '');
}
activitypub.helpers.log('[middleware/activitypub] HTTP signature verification passed.');
diff --git a/test/activitypub/middleware.js b/test/activitypub/middleware.js
index 41a53fabe3..b1fc5e9adb 100644
--- a/test/activitypub/middleware.js
+++ b/test/activitypub/middleware.js
@@ -14,11 +14,18 @@ describe('middleware.verify', () => {
let uid;
let username;
let keyData;
+ let uid2;
+ let username2;
+ let keyData2;
before(async () => {
username = utils.generateUUID().slice(0, 10);
uid = await user.create({ username });
keyData = await activitypub.getPrivateKey('uid', uid);
+
+ username2 = utils.generateUUID().slice(0, 10);
+ uid2 = await user.create({ username: username2 });
+ keyData2 = await activitypub.getPrivateKey('uid', uid2);
});
function buildReq(method, path, headers = {}) {
@@ -149,4 +156,37 @@ describe('middleware.verify', () => {
assert.strictEqual(nextCalled, true);
});
});
+
+ describe('duplicate keyId parameters', () => {
+ it('should use the last keyId for req.uid to match verification keyId', async () => {
+ // Regression test: when a signature header contains duplicate keyId parameters,
+ // the middleware must use the same keyId that the library used for verification
+ // (last-wins), not the first occurrence (first-wins).
+ // See: https://github.com/nodebb/NodeBB/issues/...
+ const path = `/user/${username}/inbox`;
+ const signedHeaders = await getValidSignature(path);
+
+ // Construct a signature header with a spoofed keyId first, then the real one last.
+ // The real keyId matches what was used to sign (uid).
+ // The spoofed keyId references uid2 (a different user).
+ const realKeyId = `keyId="${nconf.get('url')}/uid/${uid}#key"`;
+ const spoofedKeyId = `keyId="${nconf.get('url')}/uid/${uid2}#key"`;
+ const dupeSignature = `${spoofedKeyId},${realKeyId},${signedHeaders.signature.split(',').slice(1).join(',')}`;
+
+ const req = buildReq('GET', path, {
+ signature: dupeSignature,
+ date: signedHeaders.date,
+ });
+ const res = buildRes();
+ const { nextCalled, res: response } = await runMiddleware(req, res);
+
+ assert.strictEqual(nextCalled, true);
+ assert.strictEqual(response.statusCode, null);
+ // req.uid should be set to the LAST keyId (uid), matching the keyId used for verification.
+ // The middleware strips the keyId= prefix and trailing quote, then removes the fragment.
+ const expectedUid = `${nconf.get('url')}/uid/${uid}`;
+ assert.strictEqual(req.uid, expectedUid);
+ assert.notStrictEqual(req.uid, `${nconf.get('url')}/uid/${uid2}`);
+ });
+ });
});