import { pascalCase } from "es-toolkit"; import metadata from "../deno.json" with { type: "json" }; import { getFieldName } from "./field.ts"; import type { PropertySchema, TypeSchema } from "./schema.ts"; import { hasSingularAccessor, isNonFunctionalProperty } from "./schema.ts"; import { areAllScalarTypes, getTypeNames } from "./type.ts"; function emitOverride( typeUri: string, types: Record, property: PropertySchema, ): string { const type = types[typeUri]; let supertypeUri = type.extends; while (supertypeUri != null) { const st = types[supertypeUri]; if (st.properties.find((p) => p.singularName === property.singularName)) { return "override"; } supertypeUri = st.extends; } return ""; } async function* generateProperty( type: TypeSchema, property: PropertySchema, types: Record, moduleVarNames: ReadonlyMap, ): AsyncIterable { const override = emitOverride(type.uri, types, property); const trustOwner = type.trustEmbeddedObjects === false ? "null" : "this.id"; const doc = `\n/** ${property.description.replaceAll("\n", "\n * ")}\n */\n`; if (areAllScalarTypes(property.range, types)) { if (hasSingularAccessor(property)) { yield doc; yield `${override} get ${property.singularName}(): (${getTypeNames( property.range, types, )} | null) { if (this._warning != null) { getLogger(this._warning.category).warn( this._warning.message, this._warning.values ); } if (this.${await getFieldName(property.uri)}.length < 1) return null; return this.${await getFieldName(property.uri)}[0]; } `; } if (isNonFunctionalProperty(property)) { yield doc; yield `get ${property.pluralName}(): (${getTypeNames( property.range, types, true, )})[] { return this.${await getFieldName(property.uri)}; } `; } } else { yield ` async #fetch${pascalCase(property.singularName)}( url: URL, options: { documentLoader?: DocumentLoader, contextLoader?: DocumentLoader, suppressError?: boolean, tracerProvider?: TracerProvider, crossOrigin?: "ignore" | "throw" | "trust"; gateways?: readonly (string | URL)[]; verifyPortableObject?: PortableObjectVerifier; inheritPortableObjectVerifier?: boolean; } = {}, ): Promise<${getTypeNames(property.range, types)} | null> { const documentLoader = options.documentLoader ?? this._documentLoader ?? getDocumentLoader(); const contextLoader = options.contextLoader ?? this._contextLoader ?? getDocumentLoader(); const tracerProvider = options.tracerProvider ?? this._tracerProvider ?? trace.getTracerProvider(); const tracer = tracerProvider.getTracer( ${JSON.stringify(metadata.name)}, ${JSON.stringify(metadata.version)}, ); // The fetched object uses the verifier given to this call (which falls // back to the parent's own default) for its accessors by default, // unless the call keeps its verifier to itself: const childVerifier = options.inheritPortableObjectVerifier === false ? this._verifyPortableObject : options.verifyPortableObject; return await tracer.startActiveSpan("activitypub.lookup_object", async (span) => { const dereferencePortable = async ( portableUrl: URL, extra: { inferredGateways?: readonly URL[]; response?: RemoteDocument; contextLoader?: DocumentLoader; } = {}, ) => { try { const obj = await dereferencePortableIri(portableUrl, { documentLoader, contextLoader: extra.contextLoader ?? contextLoader, tracerProvider, gateways: options.gateways, inferredGateways: extra.inferredGateways, response: extra.response, verifyPortableObject: options.verifyPortableObject, referrer: { object: this, property: ${JSON.stringify(property.uri)}, }, suppressError: options.suppressError, crossOrigin: options.crossOrigin, span, parse: (document, parseOptions) => this.#${property.singularName}_fromJsonLd(document, { documentLoader, contextLoader: parseOptions.contextLoader, tracerProvider, verifyPortableObject: childVerifier, baseUrl: parseOptions.baseUrl, }), }); if (obj != null) { span.setAttribute( "activitypub.object.id", (obj.id ?? portableUrl).href, ); span.setAttribute( "activitypub.object.type", // @ts-ignore: obj.constructor always has a typeId. obj.constructor.typeId.href ); } return obj; } catch (e) { span.setStatus({ code: SpanStatusCode.ERROR, message: String(e), }); throw e; } finally { span.end(); } }; if (isPortableIri(url)) { // A reference in a portable actor's own document, e.g., its outbox, // has no location hints, so the actor's gateways are used instead: return await dereferencePortable( url, options.gateways == null ? { inferredGateways: getReferrerGateways(this, url) } : {}, ); } const lookupUrl = formatIri(url); const portableMode = isPortableMode(this, options); if (portableMode) { const compatible = parseCompatibleEf61Reference(url); if (compatible === null) { span.end(); return rejectMalformedCompatibleReference(lookupUrl, options); } else if (compatible != null) { return await dereferencePortable(compatible.id, { inferredGateways: [compatible.gateway], }); } } let fetchResult: RemoteDocument; try { fetchResult = await documentLoader(lookupUrl, { suppressError: options.suppressError, }); } catch (error) { span.setStatus({ code: SpanStatusCode.ERROR, message: String(error), }); span.end(); if (options.suppressError) { getLogger(["fedify", "vocab"]).warn( "Failed to fetch {url}: {error}", { error, url: lookupUrl } ); return null; } throw error; } const { document, documentUrl } = fetchResult; // In portable mode, the document may turn out to be a portable object, // which has to be verified with the same context documents: const snapshot = portableMode ? createSnapshotContextLoader(contextLoader, options.suppressError) : null; const scopedContext = createScopedContextLoader( snapshot?.loader ?? contextLoader, snapshot == null && options.suppressError, ); try { let claim: PortableResponseClaim | null | undefined; try { const baseUrl = parseIri(documentUrl); const obj = await this.#${property.singularName}_fromJsonLd( document, { documentLoader, contextLoader: scopedContext.loader, tracerProvider, verifyPortableObject: childVerifier, baseUrl, } ); if (snapshot == null) { markUnverifiedPortableClaim(obj, url, documentUrl); } else { claim = getPortableResponseClaim(documentUrl, obj.id); } if (claim === undefined) { if ( obj?.id != null && !isTrustedIriOrigin(options, obj.id, baseUrl) ) { if (options.crossOrigin === "throw") { throw new Error( "The object's @id (" + obj.id.href + ") has a different origin " + "than the document URL (" + baseUrl.href + "); refusing to return " + "the object. If you want to bypass this check and are aware of" + 'the security implications, set the crossOrigin option to "trust".' ); } getLogger(["fedify", "vocab"]).warn( "The object's @id ({objectId}) has a different origin than the document " + "URL ({documentUrl}); refusing to return the object. If you want to " + "bypass this check and are aware of the security implications, " + 'set the crossOrigin option to "trust".', { ...fetchResult, objectId: obj.id.href }, ); span.end(); return null; } span.setAttribute("activitypub.object.id", (obj.id ?? url).href); span.setAttribute( "activitypub.object.type", // @ts-ignore: obj.constructor always has a typeId. obj.constructor.typeId.href ); span.end(); return obj; } } catch (e) { if (options.suppressError) { getLogger(["fedify", "vocab"]).warn( "Failed to parse {url}: {error}", { error: e, url: lookupUrl } ); span.end(); return null; } span.setStatus({ code: SpanStatusCode.ERROR, message: String(e), }); span.end(); throw e; } // The document stands for a portable object, so it is verified as // one instead of being trusted because of where it came from: if (claim === null) { span.end(); return rejectMalformedCompatibleReference(documentUrl, options); } return await dereferencePortable(claim.id, { inferredGateways: claim.inferredGateways, response: fetchResult, contextLoader: snapshot?.loader, }); } finally { scopedContext.release(); snapshot?.release(); } }); } async #${property.singularName}_fromJsonLd( jsonLd: unknown, options: { documentLoader?: DocumentLoader, contextLoader?: DocumentLoader, tracerProvider?: TracerProvider, verifyPortableObject?: PortableObjectVerifier, baseUrl?: URL } ): Promise<${getTypeNames(property.range, types)}> { const documentLoader = options.documentLoader ?? this._documentLoader ?? getDocumentLoader(); const contextLoader = options.contextLoader ?? this._contextLoader ?? getDocumentLoader(); const tracerProvider = options.tracerProvider ?? this._tracerProvider ?? trace.getTracerProvider(); const baseUrl = options.baseUrl; const verifyPortableObject = options.verifyPortableObject; `; if (property.preprocessors != null && property.preprocessors.length > 0) { yield ` if (jsonLd != null && typeof jsonLd === "object") { const _expanded = await jsonld.expand(jsonLd, { documentLoader: contextLoader, keepFreeFloatingNodes: true, }); for (const _pp_obj of _expanded) { `; for (const pp of property.preprocessors) { const varName = moduleVarNames.get(pp.module); if (varName == null) { throw new Error( `Preprocessor module "${pp.module}" is not registered ` + `in the generated imports. Ensure all preprocessor ` + `modules used in property schemas are available.`, ); } yield ` { const _result = await ${varName}[${JSON.stringify( pp.function, )}](_pp_obj, { documentLoader, contextLoader, tracerProvider, verifyPortableObject, baseUrl, }); if (_result instanceof Error) throw _result; if (_result !== undefined) { retainPreprocessedRawProperties(_result, _pp_obj); return _result as ${getTypeNames(property.range, types)}; } } `; } yield ` } } `; } for (const range of property.range) { if (!(range in types)) continue; const rangeType = types[range]; yield ` try { return await ${rangeType.name}.fromJsonLd( jsonLd, { documentLoader, contextLoader, tracerProvider, verifyPortableObject, baseUrl, }, ); } catch (e) { if (!(e instanceof TypeError)) throw e; } `; } yield ` throw new TypeError("Expected an object of any type of: " + ${JSON.stringify(property.range)}.join(", ")); } `; if (hasSingularAccessor(property)) { yield ` /** * Similar to * {@link ${type.name}.get${pascalCase(property.singularName)}}, * but returns its \`@id\` URL instead of the object itself. */ ${override} get ${property.singularName}Id(): URL | null { if (this._warning != null) { getLogger(this._warning.category).warn( this._warning.message, this._warning.values ); } if (this.${await getFieldName(property.uri)}.length < 1) return null; const v = this.${await getFieldName(property.uri)}[0]; if (v instanceof URL) return v; return v.id; } `; yield doc; yield ` ${override} async get${pascalCase(property.singularName)}( options: { documentLoader?: DocumentLoader, contextLoader?: DocumentLoader, suppressError?: boolean, tracerProvider?: TracerProvider, crossOrigin?: "ignore" | "throw" | "trust"; gateways?: readonly (string | URL)[]; /** * Whether to fetch referenced objects. Defaults to \`true\`. * If \`false\`, returns only embedded or cached objects accepted * by existing trust checks, or \`null\` when fetching is required. * @since 2.5.0 */ dereference?: boolean; verifyPortableObject?: PortableObjectVerifier; /** * Whether objects that this call newly fetches use the * \`verifyPortableObject\` option of this call as their default * verifier. Defaults to \`true\`. If \`false\`, the option * applies to this call only, and fetched objects use this object's * own default verifier, if any, as their default instead. Pass * \`false\` for a temporary verifier, e.g., one that accepts * everything. Objects that were already cached in this object or * embedded in it keep their defaults either way, and this option * itself is not passed on. * @since 2.4.0 */ inheritPortableObjectVerifier?: boolean; } = {} ): Promise<${getTypeNames(property.range, types)} | null> { if (this._warning != null) { getLogger(this._warning.category).warn( this._warning.message, this._warning.values ); } if (this.${await getFieldName(property.uri)}.length < 1) return null; if ( options.verifyPortableObject == null && this._verifyPortableObject != null ) { options = { ...options, verifyPortableObject: this._verifyPortableObject, }; } let v = this.${await getFieldName(property.uri)}[0]; if (!(v instanceof URL) && !(0 < (this.${await getFieldName( property.uri, "#_trust", )}[vocabTrustPrefix] ?? 0) || this.${await getFieldName(property.uri, "#_trust")}.has(0))) { if (v.id == null) { if (isUnsecuredPortableObject(this)) { warnUnverifiableEmbeddedObject(this, ${JSON.stringify( property.uri, )}); return null; } } else if ( !isTrustedIriOrigin(options, v.id, ${trustOwner}) || isUnsecuredPortableObject(this) || mustDereferencePortableObject(this, ${trustOwner}, v.id, options) ) { v = v.id; } } if (v instanceof URL) { if (options.dereference === false) return null; const fetched = await this.#fetch${pascalCase(property.singularName)}(v, options); if (fetched == null) return null; // Not cached, so that it is verified when the portable object // policy applies later: if (isUnverifiedPortableClaim(fetched)) return fetched; this.${await getFieldName(property.uri)}[0] = fetched; this.${await getFieldName(property.uri, "#_trust")}.add(0); invalidateCachedJsonLd(this); recordPortableReferrer(this, fetched, ${JSON.stringify( property.uri, )}); return fetched; } `; yield ` if (v?.id != null && ((${ type.trustEmbeddedObjects === false ? "" : "this.id != null && " }!isTrustedIriOrigin(options, v.id, ${trustOwner})) || isUnsecuredPortableObject(this) || mustDereferencePortableObject( this, ${trustOwner}, v.id, options, )) && !(0 < (this.${await getFieldName( property.uri, "#_trust", )}[vocabTrustPrefix] ?? 0) || this.${await getFieldName(property.uri, "#_trust")}.has(0))) { if (options.crossOrigin === "throw") { throw new Error( "The property object's @id (" + v.id.href + ") has a different " + "origin than the property owner's @id (" + ${ type.trustEmbeddedObjects === false ? '"untrusted metadata"' : "(this.id?.href ?? null)" } + "); " + "refusing to return the object. If you want to bypass this " + "check and are aware of the security implications, set the " + 'crossOrigin option to "trust".' ); } getLogger(["fedify", "vocab"]).warn( "The property object's @id ({objectId}) has a different origin " + "than the property owner's @id ({parentObjectId}); refusing to " + "return the object. If you want to bypass this check and are " + "aware of the security implications, set the crossOrigin option " + 'to "trust".', { objectId: v.id.href, parentObjectId: ${ type.trustEmbeddedObjects === false ? '"untrusted metadata"' : "(this.id?.href ?? null)" } }, ); return null; } recordPortableReferrer(this, v, ${JSON.stringify(property.uri)}); return v; } `; } if (isNonFunctionalProperty(property)) { yield ` /** * Similar to * {@link ${type.name}.get${pascalCase(property.pluralName)}}, * but returns their \`@id\`s instead of the objects themselves. */ ${override} get ${property.singularName}Ids(): URL[] { if (this._warning != null) { getLogger(this._warning.category).warn( this._warning.message, this._warning.values ); } return this.${await getFieldName(property.uri)}.map((v) => v instanceof URL ? v : v.id! ).filter(id => id !== null); } `; yield doc; yield ` ${override} async* get${pascalCase(property.pluralName)}( options: { documentLoader?: DocumentLoader, contextLoader?: DocumentLoader, suppressError?: boolean, tracerProvider?: TracerProvider, crossOrigin?: "ignore" | "throw" | "trust"; gateways?: readonly (string | URL)[]; /** * Whether to fetch referenced objects. Defaults to \`true\`. * If \`false\`, yields only embedded or cached objects accepted * by existing trust checks, skipping values that require fetching. * @since 2.5.0 */ dereference?: boolean; verifyPortableObject?: PortableObjectVerifier; /** * Whether objects that this call newly fetches use the * \`verifyPortableObject\` option of this call as their default * verifier. Defaults to \`true\`. If \`false\`, the option * applies to this call only, and fetched objects use this object's * own default verifier, if any, as their default instead. Pass * \`false\` for a temporary verifier, e.g., one that accepts * everything. Objects that were already cached in this object or * embedded in it keep their defaults either way, and this option * itself is not passed on. * @since 2.4.0 */ inheritPortableObjectVerifier?: boolean; } = {} ): AsyncIterable<${getTypeNames(property.range, types)}> { if (this._warning != null) { getLogger(this._warning.category).warn( this._warning.message, this._warning.values ); } if ( options.verifyPortableObject == null && this._verifyPortableObject != null ) { options = { ...options, verifyPortableObject: this._verifyPortableObject, }; } const vs = this.${await getFieldName(property.uri)}; for (let i = 0; i < vs.length; i++) { let v = vs[i]; if (!(v instanceof URL) && !(i < (this.${await getFieldName( property.uri, "#_trust", )}[vocabTrustPrefix] ?? 0) || this.${await getFieldName(property.uri, "#_trust")}.has(i))) { if (v.id == null) { if (isUnsecuredPortableObject(this)) { warnUnverifiableEmbeddedObject(this, ${JSON.stringify( property.uri, )}); continue; } } else if ( !isTrustedIriOrigin(options, v.id, ${trustOwner}) || isUnsecuredPortableObject(this) || mustDereferencePortableObject(this, ${trustOwner}, v.id, options) ) { v = v.id; } } if (v instanceof URL) { if (options.dereference === false) continue; const fetched = await this.#fetch${pascalCase(property.singularName)}(v, options); if (fetched == null) continue; // Not cached, so that it is verified when the portable object // policy applies later: if (isUnverifiedPortableClaim(fetched)) { yield fetched; continue; } vs[i] = fetched; this.${await getFieldName(property.uri, "#_trust")}.add(i); invalidateCachedJsonLd(this); recordPortableReferrer(this, fetched, ${JSON.stringify( property.uri, )}); yield fetched; continue; } `; yield ` if (v?.id != null && ((${ type.trustEmbeddedObjects === false ? "" : "this.id != null && " }!isTrustedIriOrigin(options, v.id, ${trustOwner})) || isUnsecuredPortableObject(this) || mustDereferencePortableObject( this, ${trustOwner}, v.id, options, )) && !(i < (this.${await getFieldName( property.uri, "#_trust", )}[vocabTrustPrefix] ?? 0) || this.${await getFieldName(property.uri, "#_trust")}.has(i))) { if (options.crossOrigin === "throw") { throw new Error( "The property object's @id (" + v.id.href + ") has a different " + "origin than the property owner's @id (" + ${ type.trustEmbeddedObjects === false ? '"untrusted metadata"' : "(this.id?.href ?? null)" } + "); " + "refusing to return the object. If you want to bypass this " + "check and are aware of the security implications, set the " + 'crossOrigin option to "trust".' ); } getLogger(["fedify", "vocab"]).warn( "The property object's @id ({objectId}) has a different origin " + "than the property owner's @id ({parentObjectId}); refusing to " + "return the object. If you want to bypass this check and are " + "aware of the security implications, set the crossOrigin " + 'option to "trust".', { objectId: v.id.href, parentObjectId: ${ type.trustEmbeddedObjects === false ? '"untrusted metadata"' : "(this.id?.href ?? null)" } }, ); continue; } recordPortableReferrer(this, v, ${JSON.stringify(property.uri)}); yield v; } } `; } } } export async function* generateProperties( typeUri: string, types: Record, moduleVarNames: ReadonlyMap, ): AsyncIterable { const type = types[typeUri]; for (const property of type.properties) { yield* generateProperty(type, property, types, moduleVarNames); } }