From c9b3c9bc412f807fe09a87b4a7794e39654fa2ac Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Mon, 6 Feb 2023 22:18:09 -0700 Subject: [PATCH] Better context check --- core/activitypub/object.js | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/core/activitypub/object.js b/core/activitypub/object.js index 50fa04f2..018dabca 100644 --- a/core/activitypub/object.js +++ b/core/activitypub/object.js @@ -4,6 +4,8 @@ const Endpoints = require('./endpoint'); // deps const { isString, isObject } = require('lodash'); +const Context = '@context'; + module.exports = class ActivityPubObject { constructor(obj, withContext = [ActivityStreamsContext]) { if (withContext) { @@ -39,16 +41,31 @@ module.exports = class ActivityPubObject { } isValid() { - const nonEmpty = s => isString(s) && s.length > 1; - // :TODO: Additional validation - if ( - (this['@context'] === ActivityStreamsContext || - this['@context'][0] === ActivityStreamsContext) && - nonEmpty(this.id) && - nonEmpty(this.type) - ) { - return true; + // + // If @context is present, it must be valid; + // child objects generally inherit, so they may not have one + // + if (this[Context]) { + if (!this.isContextValid()) { + return false; + } } + + const checkString = s => isString(s) && s.length > 1; + return checkString(this.id) && checkString(this.type); + } + + isContextValid() { + if (Array.isArray(this[Context])) { + if (this[Context][0] === ActivityStreamsContext) { + return true; + } + } else if (isString(this[Context])) { + if (ActivityStreamsContext === this[Context]) { + return true; + } + } + return false; }