mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 12:36:57 +00:00
merge: Verify links in remote accounts. (!964)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/964 Approved-by: dakkar <dakkar@thenautilus.net> Approved-by: Marie <github@yuugi.dev>
This commit is contained in:
commit
57a310a146
4 changed files with 49 additions and 6 deletions
|
@ -680,6 +680,7 @@ seems to do a decent job)
|
||||||
* from `packages/backend/src/queue/processors/InboxProcessorService.ts`
|
* from `packages/backend/src/queue/processors/InboxProcessorService.ts`
|
||||||
to `packages/backend/src/core/UpdateInstanceQueue.ts`
|
to `packages/backend/src/core/UpdateInstanceQueue.ts`
|
||||||
where `updateInstanceQueue` is impacted
|
where `updateInstanceQueue` is impacted
|
||||||
|
* from `verifyLink` in `packages/backend/src/core/activitypub/models/ApPersonService.ts` to `verifyFieldLink` in `packages/backend/src/misc/verify-field-link.ts` (if sensible)
|
||||||
* if there have been any changes to the federated user data (the
|
* if there have been any changes to the federated user data (the
|
||||||
`renderPerson` function in
|
`renderPerson` function in
|
||||||
`packages/backend/src/core/activitypub/ApRendererService.ts`), make
|
`packages/backend/src/core/activitypub/ApRendererService.ts`), make
|
||||||
|
|
|
@ -41,6 +41,8 @@ import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.j
|
||||||
import type { AccountMoveService } from '@/core/AccountMoveService.js';
|
import type { AccountMoveService } from '@/core/AccountMoveService.js';
|
||||||
import { ApUtilityService } from '@/core/activitypub/ApUtilityService.js';
|
import { ApUtilityService } from '@/core/activitypub/ApUtilityService.js';
|
||||||
import { MemoryKVCache } from '@/misc/cache.js';
|
import { MemoryKVCache } from '@/misc/cache.js';
|
||||||
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
||||||
|
import { verifyFieldLinks } from '@/misc/verify-field-link.js';
|
||||||
import { getApId, getApType, isActor, isCollection, isCollectionOrOrderedCollection, isPropertyValue } from '../type.js';
|
import { getApId, getApType, isActor, isCollection, isCollectionOrOrderedCollection, isPropertyValue } from '../type.js';
|
||||||
import { extractApHashtags } from './tag.js';
|
import { extractApHashtags } from './tag.js';
|
||||||
import type { OnModuleInit } from '@nestjs/common';
|
import type { OnModuleInit } from '@nestjs/common';
|
||||||
|
@ -112,6 +114,7 @@ export class ApPersonService implements OnModuleInit, OnApplicationShutdown {
|
||||||
|
|
||||||
private roleService: RoleService,
|
private roleService: RoleService,
|
||||||
private readonly apUtilityService: ApUtilityService,
|
private readonly apUtilityService: ApUtilityService,
|
||||||
|
private httpRequestService: HttpRequestService,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -362,6 +365,8 @@ export class ApPersonService implements OnModuleInit, OnApplicationShutdown {
|
||||||
|
|
||||||
const url = this.apUtilityService.findBestObjectUrl(person);
|
const url = this.apUtilityService.findBestObjectUrl(person);
|
||||||
|
|
||||||
|
const verifiedLinks = url ? await verifyFieldLinks(fields, url, this.httpRequestService) : [];
|
||||||
|
|
||||||
// Create user
|
// Create user
|
||||||
let user: MiRemoteUser | null = null;
|
let user: MiRemoteUser | null = null;
|
||||||
let publicKey: MiUserPublickey | null = null;
|
let publicKey: MiUserPublickey | null = null;
|
||||||
|
@ -436,6 +441,7 @@ export class ApPersonService implements OnModuleInit, OnApplicationShutdown {
|
||||||
followedMessage: person._misskey_followedMessage != null ? truncate(person._misskey_followedMessage, 256) : null,
|
followedMessage: person._misskey_followedMessage != null ? truncate(person._misskey_followedMessage, 256) : null,
|
||||||
url,
|
url,
|
||||||
fields,
|
fields,
|
||||||
|
verifiedLinks,
|
||||||
followingVisibility,
|
followingVisibility,
|
||||||
followersVisibility,
|
followersVisibility,
|
||||||
birthday: bday?.[0] ?? null,
|
birthday: bday?.[0] ?? null,
|
||||||
|
@ -579,6 +585,8 @@ export class ApPersonService implements OnModuleInit, OnApplicationShutdown {
|
||||||
|
|
||||||
const url = this.apUtilityService.findBestObjectUrl(person);
|
const url = this.apUtilityService.findBestObjectUrl(person);
|
||||||
|
|
||||||
|
const verifiedLinks = url ? await verifyFieldLinks(fields, url, this.httpRequestService) : [];
|
||||||
|
|
||||||
const updates = {
|
const updates = {
|
||||||
lastFetchedAt: new Date(),
|
lastFetchedAt: new Date(),
|
||||||
inbox: person.inbox,
|
inbox: person.inbox,
|
||||||
|
@ -661,6 +669,7 @@ export class ApPersonService implements OnModuleInit, OnApplicationShutdown {
|
||||||
await this.userProfilesRepository.update({ userId: exist.id }, {
|
await this.userProfilesRepository.update({ userId: exist.id }, {
|
||||||
url,
|
url,
|
||||||
fields,
|
fields,
|
||||||
|
verifiedLinks,
|
||||||
description: _description,
|
description: _description,
|
||||||
followedMessage: person._misskey_followedMessage != null ? truncate(person._misskey_followedMessage, 256) : null,
|
followedMessage: person._misskey_followedMessage != null ? truncate(person._misskey_followedMessage, 256) : null,
|
||||||
followingVisibility,
|
followingVisibility,
|
||||||
|
|
34
packages/backend/src/misc/verify-field-link.ts
Normal file
34
packages/backend/src/misc/verify-field-link.ts
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: piuvas and other Sharkey contributors
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { JSDOM } from 'jsdom';
|
||||||
|
import type { HttpRequestService } from '@/core/HttpRequestService.js';
|
||||||
|
|
||||||
|
type Field = { name: string, value: string };
|
||||||
|
|
||||||
|
export async function verifyFieldLinks(fields: Field[], profile_url: string, httpRequestService: HttpRequestService): Promise<string[]> {
|
||||||
|
const verified_links = [];
|
||||||
|
for (const field_url of fields
|
||||||
|
.filter(x => URL.canParse(x.value) && ['http:', 'https:'].includes((new URL(x.value).protocol)))) {
|
||||||
|
try {
|
||||||
|
const html = await httpRequestService.getHtml(field_url.value);
|
||||||
|
|
||||||
|
const { window } = new JSDOM(html);
|
||||||
|
const doc: Document = window.document;
|
||||||
|
|
||||||
|
const aEls = Array.from(doc.getElementsByTagName('a'));
|
||||||
|
const linkEls = Array.from(doc.getElementsByTagName('link'));
|
||||||
|
|
||||||
|
const includesProfileLinks = [...aEls, ...linkEls].some(link => link.rel === 'me' && link.href === profile_url);
|
||||||
|
if (includesProfileLinks) { verified_links.push(field_url.value); }
|
||||||
|
|
||||||
|
window.close();
|
||||||
|
} catch (err) {
|
||||||
|
// don't do anything.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return verified_links;
|
||||||
|
}
|
|
@ -31,6 +31,7 @@ import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.j
|
||||||
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
||||||
import type { Config } from '@/config.js';
|
import type { Config } from '@/config.js';
|
||||||
import { safeForSql } from '@/misc/safe-for-sql.js';
|
import { safeForSql } from '@/misc/safe-for-sql.js';
|
||||||
|
import { verifyFieldLinks } from '@/misc/verify-field-link.js';
|
||||||
import { AvatarDecorationService } from '@/core/AvatarDecorationService.js';
|
import { AvatarDecorationService } from '@/core/AvatarDecorationService.js';
|
||||||
import { notificationRecieveConfig } from '@/models/json-schema/user.js';
|
import { notificationRecieveConfig } from '@/models/json-schema/user.js';
|
||||||
import { userUnsignedFetchOptions } from '@/const.js';
|
import { userUnsignedFetchOptions } from '@/const.js';
|
||||||
|
@ -584,9 +585,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
this.globalEventService.publishInternalEvent('localUserUpdated', { id: user.id });
|
this.globalEventService.publishInternalEvent('localUserUpdated', { id: user.id });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const verified_links = await verifyFieldLinks(newFields, `${this.config.url}/@${user.username}`, this.httpRequestService);
|
||||||
|
|
||||||
await this.userProfilesRepository.update(user.id, {
|
await this.userProfilesRepository.update(user.id, {
|
||||||
...profileUpdates,
|
...profileUpdates,
|
||||||
verifiedLinks: [],
|
verifiedLinks: verified_links,
|
||||||
});
|
});
|
||||||
|
|
||||||
const iObj = await this.userEntityService.pack(user.id, user, {
|
const iObj = await this.userEntityService.pack(user.id, user, {
|
||||||
|
@ -611,15 +614,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
this.accountUpdateService.publishToFollowers(user.id);
|
this.accountUpdateService.publishToFollowers(user.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
const urls = updatedProfile.fields.filter(x => x.value.startsWith('https://'));
|
|
||||||
for (const url of urls) {
|
|
||||||
this.verifyLink(url.value, user);
|
|
||||||
}
|
|
||||||
|
|
||||||
return iObj;
|
return iObj;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this function is superseded by '@/misc/verify-field-link.ts'
|
||||||
private async verifyLink(url: string, user: MiLocalUser) {
|
private async verifyLink(url: string, user: MiLocalUser) {
|
||||||
if (!safeForSql(url)) return;
|
if (!safeForSql(url)) return;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue