Node.js based forum software built for the modern web
fix: throw in Mocks._normalize when `attributedTo` is not a URI. This normalization step is only run against incoming activities, so attributedTo should always be a URI
Julian Lamdc45bd7f9816
diff --git a/src/activitypub/mocks.js b/src/activitypub/mocks.jsindex e6ec585a6f..053223a221 100644--- a/src/activitypub/mocks.js+++ b/src/activitypub/mocks.js@@ -66,11 +66,15 @@ Mocks._normalize = async (object) => {break;}- case typeof attributedTo === 'object' && attributedTo.hasOwnProperty('id'): {+ case attributedTo && typeof attributedTo === 'object' && attributedTo.hasOwnProperty('id'): {attributedTo = attributedTo.id;}}+ if (!activitypub.helpers.isUri(attributedTo)) {+ throw new Error('[[error:activitypub.invalid-id]]');+ }+let sourceContent = source && source.mediaType === 'text/markdown' ? source.content : undefined;if (sourceContent) {content = null;diff --git a/src/activitypub/notes.js b/src/activitypub/notes.jsindex c31ea9694c..6999da2b2a 100644--- a/src/activitypub/notes.js+++ b/src/activitypub/notes.js@@ -481,7 +481,13 @@ Notes.assertPrivate = async (object) => {timestamp = Date.now();}- const payload = await activitypub.mocks.message(object);+ let payload;+ try {+ payload = await activitypub.mocks.message(object);+ } catch (e) {+ activitypub.helpers.log(`[activitypub/notes.assertPrivate] Failed to mock message: ${e.message}`);+ return null;+ }// Naive image appending (using src/posts/attachments.js is likely better, but not worth the effort)const attachments = payload._activitypub.attachment;diff --git a/test/activitypub/inbox.js b/test/activitypub/inbox.jsindex 0c92a818c0..cf1a388c4c 100644--- a/test/activitypub/inbox.js+++ b/test/activitypub/inbox.js@@ -177,6 +177,170 @@ describe('Inbox', () => {});});+ describe('attributedTo validation', () => {+ describe('private messages (non-public)', () => {+ before(async function () {+ this.uid = await user.create({ username: utils.generateUUID().slice(0, 10) });+ });++ it('should not create a message when attributedTo is numeric', async () => {+ const { note } = helpers.mocks.note({+ attributedTo: 1,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });+ const { activity } = helpers.mocks.create({+ object: note,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });++ const result = await activitypub.inbox.create({ body: activity });+ assert.strictEqual(result, null);+ assert.strictEqual(await messaging.messageExists(note.id), false);+ });++ it('should not create a message when attributedTo is null', async () => {+ const { note } = helpers.mocks.note({+ attributedTo: null,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });+ const { activity } = helpers.mocks.create({+ object: note,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });++ const result = await activitypub.inbox.create({ body: activity });+ assert.strictEqual(result, null);+ assert.strictEqual(await messaging.messageExists(note.id), false);+ });++ it('should not create a message when attributedTo is boolean', async () => {+ const { note } = helpers.mocks.note({+ attributedTo: true,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });+ const { activity } = helpers.mocks.create({+ object: note,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });++ const result = await activitypub.inbox.create({ body: activity });+ assert.strictEqual(result, null);+ assert.strictEqual(await messaging.messageExists(note.id), false);+ });++ it('should not create a message when attributedTo is non-URI string', async () => {+ const { note } = helpers.mocks.note({+ attributedTo: 'not-a-uri',+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });+ const { activity } = helpers.mocks.create({+ object: note,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });++ const result = await activitypub.inbox.create({ body: activity });+ assert.strictEqual(result, null);+ assert.strictEqual(await messaging.messageExists(note.id), false);+ });++ it('should not create a message when attributedTo is array of non-URI values', async () => {+ const { note } = helpers.mocks.note({+ attributedTo: [1, 'not-a-uri'],+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });+ const { activity } = helpers.mocks.create({+ object: note,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });++ const result = await activitypub.inbox.create({ body: activity });+ assert.strictEqual(result, null);+ assert.strictEqual(await messaging.messageExists(note.id), false);+ });++ it('should accept a Create(Note) with valid URI attributedTo', async () => {+ const { id: actor } = helpers.mocks.person();+ const { note } = helpers.mocks.note({+ attributedTo: actor,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });+ const { activity } = helpers.mocks.create({+ actor,+ object: note,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });++ const result = await activitypub.inbox.create({ body: activity });+ assert.notStrictEqual(result, null);+ assert.strictEqual(await messaging.messageExists(note.id), true);+ });+ });++ describe('public posts', () => {+ it('should not create a post when attributedTo is numeric', async () => {+ const { note } = helpers.mocks.note({+ attributedTo: 42,+ to: ['https://www.w3.org/ns/activitystreams#Public'],+ cc: [],+ });+ const { activity } = helpers.mocks.create({+ object: note,+ to: ['https://www.w3.org/ns/activitystreams#Public'],+ cc: [],+ });++ const result = await activitypub.notes.assert(0, note, { skipChecks: true });+ assert.strictEqual(result, null);+ });++ it('should not create a post when attributedTo is object with numeric id', async () => {+ const { note } = helpers.mocks.note({+ attributedTo: { type: 'Person', id: 99 },+ to: ['https://www.w3.org/ns/activitystreams#Public'],+ cc: [],+ });+ const { activity } = helpers.mocks.create({+ object: note,+ to: ['https://www.w3.org/ns/activitystreams#Public'],+ cc: [],+ });++ const result = await activitypub.notes.assert(0, note, { skipChecks: true });+ assert.strictEqual(result, null);+ });++ it('should accept a Create(Note) with valid URI attributedTo', async () => {+ const { id: actor } = helpers.mocks.person();+ const { note } = helpers.mocks.note({+ attributedTo: actor,+ to: ['https://www.w3.org/ns/activitystreams#Public'],+ cc: [],+ });+ const { activity } = helpers.mocks.create({+ actor,+ object: note,+ to: ['https://www.w3.org/ns/activitystreams#Public'],+ cc: [],+ });++ const result = await activitypub.notes.assert(0, note, { skipChecks: true });+ assert.notStrictEqual(result, null);+ });+ });+ });+describe('Announce', () => {let cid;