Skip to content

Node.js based forum software built for the modern web

fix(activitypub): use absolute avatar/cover URLs as-is in user actor (#14447)

Enes Uysal7ccc3d2b7d12
diff --git a/src/activitypub/mocks.js b/src/activitypub/mocks.js
index 053223a221..461f5a5a81 100644
--- a/src/activitypub/mocks.js
+++ b/src/activitypub/mocks.js
@@ -416,21 +416,34 @@ Mocks.actors.user = async (uid) => {
if (picture) {
picture = utils.decodeHTMLEntities(picture);
const imagePath = await user.getLocalAvatarPath(uid);
- picture = {
- type: 'Image',
- mediaType: mime.getType(imagePath),
- url: `${nconf.get('url')}${picture}`,
- };
+ // Absolute (remote/SSO) avatars are used as-is; relative paths get the forum url prefixed
+ // (string concat, so subfolder installs keep their relative_path). new URL validates the
+ // result and unsets the field on malformed values.
+ const url = /^https?:\/\//i.test(picture) ? picture : `${nconf.get('url')}${picture}`;
+ try {
+ picture = {
+ type: 'Image',
+ mediaType: mime.getType(imagePath),
+ url: new URL(url).href,
+ };
+ } catch {
+ picture = undefined;
+ }
}
if (cover) {
cover = utils.decodeHTMLEntities(cover);
const imagePath = await user.getLocalCoverPath(uid);
- cover = {
- type: 'Image',
- mediaType: mime.getType(imagePath),
- url: `${nconf.get('url')}${cover}`,
- };
+ const url = /^https?:\/\//i.test(cover) ? cover : `${nconf.get('url')}${cover}`;
+ try {
+ cover = {
+ type: 'Image',
+ mediaType: mime.getType(imagePath),
+ url: new URL(url).href,
+ };
+ } catch {
+ cover = undefined;
+ }
}
const attachment = [];
diff --git a/test/activitypub/mocks.js b/test/activitypub/mocks.js
index 7bc0a19ba9..ca31fbd49b 100644
--- a/test/activitypub/mocks.js
+++ b/test/activitypub/mocks.js
@@ -314,5 +314,38 @@ describe('Mocking', () => {
});
});
});
+
+ describe('Actors', () => {
+ describe('.user()', () => {
+ it('should not double-prefix an absolute (remote/SSO) avatar url', async () => {
+ const uid = await user.create({ username: utils.generateUUID().slice(0, 8) });
+ const externalAvatar = 'https://example.org/avatar/abc123.png';
+ await user.setUserField(uid, 'picture', externalAvatar);
+
+ const actor = await activitypub.mocks.actors.user(uid);
+
+ assert.strictEqual(actor.icon.url, externalAvatar);
+ });
+
+ it('should prefix a relative avatar path with the forum url', async () => {
+ const uid = await user.create({ username: utils.generateUUID().slice(0, 8) });
+ const relativeAvatar = '/assets/uploads/profile/test.png';
+ await user.setUserField(uid, 'picture', relativeAvatar);
+
+ const actor = await activitypub.mocks.actors.user(uid);
+
+ assert.strictEqual(actor.icon.url, `${nconf.get('url')}${relativeAvatar}`);
+ });
+
+ it('should unset the avatar when the stored value is a malformed url', async () => {
+ const uid = await user.create({ username: utils.generateUUID().slice(0, 8) });
+ await user.setUserField(uid, 'picture', 'https://');
+
+ const actor = await activitypub.mocks.actors.user(uid);
+
+ assert.strictEqual(actor.icon, undefined);
+ });
+ });
+ });
});
});