Skip to content

Commit

Permalink
Validate Note on createNote
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Apr 20, 2019
1 parent 76b3298 commit c97b017
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/remote/activitypub/models/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,32 @@ import { apLogger } from '../logger';
import { IDriveFile } from '../../../models/drive-file';
import { deliverQuestionUpdate } from '../../../services/note/polls/update';
import Instance from '../../../models/instance';
import { extractDbHost } from '../../../misc/convert-host';
import { extractDbHost, extractApHost } from '../../../misc/convert-host';

const logger = apLogger;

export function validateNote(object: any, uri: string) {
const expectHost = extractApHost(uri);

if (object == null) {
return new Error('invalid Note: object is null');
}

if (!['Note', 'Question', 'Article'].includes(object.type)) {
return new Error(`invalid Note: invalied object type ${object.type}`);
}

if (object.id && extractApHost(object.id) !== expectHost) {
return new Error(`invalid Note: id has different host. expected: ${expectHost}, actual: ${extractApHost(object.id)}`);
}

if (object.attributedTo && extractApHost(object.attributedTo) !== expectHost) {
return new Error(`invalid Note: attributedTo has different host. expected: ${expectHost}, actual: ${extractApHost(object.attributedTo)}`);
}

return null;
}

/**
* Noteをフェッチします。
*
Expand Down Expand Up @@ -57,8 +79,10 @@ export async function createNote(value: any, resolver?: Resolver, silent = false

const object: any = await resolver.resolve(value);

if (!object || !['Note', 'Question', 'Article'].includes(object.type)) {
logger.error(`invalid note: ${value}`, {
const entryUri = value.id || value;
const err = validateNote(object, entryUri);
if (err) {
logger.error(`${err.message}`, {
resolver: {
history: resolver.getHistory()
},
Expand Down

0 comments on commit c97b017

Please sign in to comment.