Skip to content

Node.js based forum software built for the modern web

fix(activitypub): trim whitespace from signature parameter names in middleware reparses

Julian Lam038cc5a60860
diff --git a/src/middleware/activitypub.js b/src/middleware/activitypub.js
index 49462e5d86..cc3ded3e4b 100644
--- a/src/middleware/activitypub.js
+++ b/src/middleware/activitypub.js
@@ -59,9 +59,9 @@ middleware.verify = async function (req, res, next) {
}
// Set calling user
- const keyId = req.headers.signature.split(',').filter(line => line.startsWith('keyId="'));
+ const keyId = req.headers.signature.split(',').filter(line => line.trim().startsWith('keyId="'));
if (keyId.length) {
- req.uid = keyId.at(-1).slice(7, -1).replace(/#.*$/, '');
+ req.uid = keyId.at(-1).trim().slice(7, -1).replace(/#.*$/, '');
}
activitypub.helpers.log('[middleware/activitypub] HTTP signature verification passed.');
@@ -145,7 +145,7 @@ middleware.assertPayload = helpers.try(async function (req, res, next) {
const { signature } = req.headers;
let keyId = new Map(signature.split(',').filter(Boolean).map((v) => {
const index = v.indexOf('=');
- return [v.substring(0, index), v.slice(index + 1)];
+ return [v.substring(0, index).trim(), v.slice(index + 1)];
})).get('keyId');
keyId = (keyId || '').slice(1, -1).replace(/#[\w-]+$/, '');
if (compare !== keyId) {
diff --git a/test/activitypub/middleware.js b/test/activitypub/middleware.js
index b1fc5e9adb..3867d88dae 100644
--- a/test/activitypub/middleware.js
+++ b/test/activitypub/middleware.js
@@ -189,4 +189,102 @@ describe('middleware.verify', () => {
assert.notStrictEqual(req.uid, `${nconf.get('url')}/uid/${uid2}`);
});
});
+
+ describe('keyId with leading whitespace (algorithm before keyId)', () => {
+ it('should extract req.uid even when keyId is preceded by whitespace', async () => {
+ // Regression test: when algorithm precedes keyId in the signature header,
+ // the library trims the leading space before keyId, but the middleware's
+ // naive split(',') does not. Both code paths must trim to stay in sync.
+ const path = `/user/${username}/inbox`;
+ const signedHeaders = await getValidSignature(path);
+
+ // Reorder: algorithm before keyId introduces whitespace before keyId.
+ // The library trims it; the middleware must too.
+ const parts = signedHeaders.signature.split(',');
+ const algorithmPart = parts.find(p => p.startsWith('algorithm='));
+ const signaturePart = parts.find(p => p.startsWith('signature='));
+ const headersPart = parts.find(p => p.startsWith('headers='));
+ const reordered = `${algorithmPart}, keyId="${nconf.get('url')}/uid/${uid}#key", ${headersPart}, ${signaturePart}`;
+
+ const req = buildReq('GET', path, {
+ signature: reordered,
+ date: signedHeaders.date,
+ });
+ const res = buildRes();
+ const { nextCalled, res: response } = await runMiddleware(req, res);
+
+ assert.strictEqual(nextCalled, true);
+ assert.strictEqual(response.statusCode, null);
+ const expectedUid = `${nconf.get('url')}/uid/${uid}`;
+ assert.strictEqual(req.uid, expectedUid);
+ });
+ });
+});
+
+describe('middleware.assertPayload', () => {
+ let uid;
+ let username;
+ let keyData;
+ let path;
+
+ before(async () => {
+ username = utils.generateUUID().slice(0, 10);
+ uid = await user.create({ username });
+ keyData = await activitypub.getPrivateKey('uid', uid);
+ path = `/user/${username}/inbox`;
+ });
+
+ function buildReqWithSignature(method, path, headers = {}) {
+ const { host } = nconf.get('url_parsed');
+ return {
+ method,
+ path,
+ baseUrl: nconf.get('relative_path'),
+ headers: { host, ...headers },
+ };
+ }
+
+ function buildRes() {
+ const res = { statusCode: null };
+ res.sendStatus = (code) => { res.statusCode = code; };
+ return res;
+ }
+
+ async function runPayloadMiddleware(req, body) {
+ const res = buildRes();
+ let nextCalled = false;
+ req.body = body;
+ await middleware.assertPayload(req, res, () => { nextCalled = true; });
+ return { nextCalled, res };
+ }
+
+ it('should extract keyId from signature header with leading whitespace', async () => {
+ // Regression test: assertPayload's Map-based keyId parsing must trim
+ // whitespace from parameter names to match the library's behavior.
+ const signedHeaders = await activitypub.sign(keyData, `${nconf.get('url')}${path}`, null);
+
+ // Reorder so algorithm precedes keyId, introducing whitespace before keyId.
+ const parts = signedHeaders.signature.split(',');
+ const algorithmPart = parts.find(p => p.startsWith('algorithm='));
+ const signaturePart = parts.find(p => p.startsWith('signature='));
+ const headersPart = parts.find(p => p.startsWith('headers='));
+ const reordered = `${algorithmPart}, keyId="${nconf.get('url')}/uid/${uid}#key", ${headersPart}, ${signaturePart}`;
+
+ const req = buildReqWithSignature('POST', path, { signature: reordered, date: signedHeaders.date });
+ const body = {
+ id: `https://example.org/activity/${utils.generateUUID()}`,
+ type: 'Create',
+ actor: `${nconf.get('url')}/uid/${uid}`,
+ object: { type: 'Note', id: `${nconf.get('url')}/topic/1` },
+ };
+ const res = buildRes();
+ const { nextCalled, res: response } = await runPayloadMiddleware(req, body);
+
+ // The actor (local uid) has no stored keys in the remote AP DB, so
+ // compare is ''. After the fix, keyId is correctly parsed as the
+ // actual keyId URL. '' !== keyId → cross-check fails → 403.
+ // Before the fix, both were '' → cross-check passed → next() called.
+ assert.strictEqual(nextCalled, false);
+ assert.strictEqual(response.statusCode, 403);
+ });
});