From a071e26b8a2aeabcbaa57f9ded35d8be482b8b74 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Thu, 7 May 2026 00:20:21 -0500 Subject: [PATCH] fix & harden pmp logic --- src/app/features/room/RoomInput.tsx | 45 +++++++++++++++---- src/app/hooks/useCommands.ts | 2 +- src/app/hooks/usePerMessageProfile.ts | 8 ++-- .../PKitCommandMessageHandler.ts | 11 +++-- .../PKitProxyMessageHandler.ts | 11 +++-- 5 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 373948925..36469c3c6 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -63,6 +63,7 @@ import { replaceWithElement, BlockType, } from '$components/editor'; +import { plainToEditorInput } from '$components/editor/input'; import { EmojiBoard, EmojiBoardTab } from '$components/emoji-board'; import { UseStateProvider } from '$components/UseStateProvider'; import type { TUploadContent } from '$utils/matrix'; @@ -777,7 +778,7 @@ export const RoomInput = forwardRef( // check if its a pk command if (pkCompatEnable && PKitCommandMessageHandler.isPKCommand(plainText)) { - pluralkitCmdMessageHandler.handleMessage(plainText); + await pluralkitCmdMessageHandler.handleMessage(plainText); resetEditor(editor); // clear the editor return; // don't do anything besides handling the command } @@ -813,6 +814,37 @@ export const RoomInput = forwardRef( if (plainText === '') return; + // PluralKit-style proxy wrappers (per-message profile proxies) must be stripped + // *before* building `content`, otherwise we end up sending the wrapper verbatim. + let proxiedPerMessageProfile: + | Awaited> + | undefined; + if (pmpProxyingEnable) { + proxiedPerMessageProfile = + await pluralkitProxyMessageHandler.getPmpBasedOnMessage(plainText); + if (proxiedPerMessageProfile) { + const stripped = pluralkitProxyMessageHandler.stripProxyFromMessage(plainText); + if (stripped !== undefined) { + // Re-run the normal outgoing pipeline on the stripped content so the message + // goes through the same transforms/parsers as any other message. + serializedChildren = plainToEditorInput(stripped); + + outgoingMessageTransforms.forEach((transform) => { + if (!transform.shouldApply(serializedChildren, outgoingTransformContext)) return; + serializedChildren = transform.apply(serializedChildren, outgoingTransformContext); + }); + + plainText = toPlainText(serializedChildren, true, nicknameReplacement).trim(); + customHtml = trimCustomHtml( + toMatrixCustomHTML(serializedChildren, { + stripNickname: true, + nickNameReplacement: nicknameReplacement, + }) + ); + } + } + } + const body = plainText; const formattedBody = customHtml; const mentionData = getMentions(mx, roomId, editor); @@ -848,13 +880,10 @@ export const RoomInput = forwardRef( * This allows the server to apply the correct profile-based transformations (e.g. font size adjustments) when processing the message, * and also allows clients to display an accurate preview of how the message will look with the profile applied while it's being composed. */ - const perMessageProfile = - pmpProxyingEnable && pluralkitProxyMessageHandler.isAProxiedMessage(plainText) - ? await pluralkitProxyMessageHandler.getPmpBasedOnMessage(plainText) - : await getCurrentlyUsedPerMessageProfileForRoom(mx, roomId); - - if (pmpProxyingEnable && pluralkitProxyMessageHandler.isAProxiedMessage(plainText)) - plainText = pluralkitProxyMessageHandler.stripProxyFromMessage(plainText) ?? plainText; + let perMessageProfile = await getCurrentlyUsedPerMessageProfileForRoom(mx, roomId); + if (pmpProxyingEnable) { + if (proxiedPerMessageProfile) perMessageProfile = proxiedPerMessageProfile; + } if (perMessageProfile) { content[prefix.MATRIX_UNSTABLE_PER_MESSAGE_PROFILE_PROPERTY_NAME] = convertPerMessageProfileToBeeperFormat( diff --git a/src/app/hooks/useCommands.ts b/src/app/hooks/useCommands.ts index 07b987a51..94a12f8a6 100644 --- a/src/app/hooks/useCommands.ts +++ b/src/app/hooks/useCommands.ts @@ -632,7 +632,7 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => { exe: async (payload) => { const pid: string = splitWithSpace(payload)[0] ?? ''; const proxy: string = splitWithSpace(payload)[1] ?? ''; - pkitcmdHandler.handleMessage(`pk;member "${pid}" proxy ${proxy}`, true); + await pkitcmdHandler.handleMessage(`pk;member "${pid}" proxy ${proxy}`, true); }, }, [Command.MyRoomAvatar]: { diff --git a/src/app/hooks/usePerMessageProfile.ts b/src/app/hooks/usePerMessageProfile.ts index 0f9c7c284..82f0cfc96 100644 --- a/src/app/hooks/usePerMessageProfile.ts +++ b/src/app/hooks/usePerMessageProfile.ts @@ -158,10 +158,12 @@ export type InternalPerMessageProfileProxyAssociation = { export function parsePerMessageProfileProxyAssociation( assoc: PerMessageProfileProxyAssociation ): InternalPerMessageProfileProxyAssociation { + const m = assoc.regexString.match(/^\/([\s\S]*)\/([gimsuy]*)$/); + const source = m?.[1] ?? assoc.regexString; + const flags = m?.[2] ?? ''; return { profileId: assoc.profileId, - // we need to remove artifacts from the toString conversion - regex: new RegExp(assoc.regexString.slice(1, -1)), + regex: new RegExp(source, flags), setAt: assoc.setAt, } satisfies InternalPerMessageProfileProxyAssociation; } @@ -431,7 +433,7 @@ export async function getProfileAssociatedWithProxy( mx: MatrixClient, proxy: string ): Promise { - const profileId = getAssociationsMap( + const profileId = getProxyAssociationMap( mx .getAccountData( `${ACCOUNT_DATA_PREFIX}.proxyassociation` as Parameters[0] diff --git a/src/app/plugins/pluralkit-handler/PKitCommandMessageHandler.ts b/src/app/plugins/pluralkit-handler/PKitCommandMessageHandler.ts index 7ceb1313f..83a61453b 100644 --- a/src/app/plugins/pluralkit-handler/PKitCommandMessageHandler.ts +++ b/src/app/plugins/pluralkit-handler/PKitCommandMessageHandler.ts @@ -102,7 +102,10 @@ export class PKitCommandMessageHandler { this.room, this.mx.getSafeUserId() ); - addOrUpdatePerMessageProfile(this.mx, { id: generatedID, name: memberName }); + await addOrUpdatePerMessageProfile(this.mx, { + id: generatedID, + name: memberName, + }); sendFeedback( `added new member has been created with id: ${generatedID} and name ${memberName}`, this.room, @@ -169,7 +172,7 @@ export class PKitCommandMessageHandler { this.room, this.mx.getSafeUserId() ); - addOrUpdatePerMessageProfile(this.mx, pmp); + await addOrUpdatePerMessageProfile(this.mx, pmp); } else if (pkMemberRemoveProxy.test(this.message)) { const cmdParts = pkMemberRemoveProxy.exec(this.message); if (!cmdParts) return; @@ -195,7 +198,7 @@ export class PKitCommandMessageHandler { ); return; } - dropProxyAssociationForPMP(this.mx, matchAgainst); + await dropProxyAssociationForPMP(this.mx, matchAgainst); sendFeedback( `Persona with ${this.useIdInsteadOfNameWherePossible ? 'id' : 'name'} "${name}" (${pmpId}) is now no longer associated with ${matchAgainst}`, @@ -227,7 +230,7 @@ export class PKitCommandMessageHandler { return; } const matchAgainstRegExp = buildRegex(matchAgainst); - associateProxyWithProfile(this.mx, pmpId, matchAgainst, matchAgainstRegExp, false); + await associateProxyWithProfile(this.mx, pmpId, matchAgainst, matchAgainstRegExp, false); sendFeedback( `Persona with ${this.useIdInsteadOfNameWherePossible ? 'id' : 'name'} "${name}" (${pmpId}) is now associated with ${matchAgainst}`, this.room, diff --git a/src/app/plugins/pluralkit-handler/PKitProxyMessageHandler.ts b/src/app/plugins/pluralkit-handler/PKitProxyMessageHandler.ts index 4e310da29..b23efdd4c 100644 --- a/src/app/plugins/pluralkit-handler/PKitProxyMessageHandler.ts +++ b/src/app/plugins/pluralkit-handler/PKitProxyMessageHandler.ts @@ -54,7 +54,9 @@ export class PKitProxyMessageHandler { this.succInit = true; } catch (err) { this.succInit = false; - throw new Error(`failed to init pmp proxy handler: ${String(err)}`, { cause: err }); + throw new Error(`failed to init pmp proxy handler: ${String(err)}`, { + cause: err, + }); } } @@ -63,7 +65,7 @@ export class PKitProxyMessageHandler { * @param message the message to check */ public isAProxiedMessage(message: string): boolean { - if (!this.succInit) throw new Error('PK proxy message handler is not initialized'); + if (!this.succInit) return false; return this.proxiesAssocs.some((assoc) => assoc.regex.test(message)); } @@ -73,7 +75,8 @@ export class PKitProxyMessageHandler { * @returns the matching Per-Message-Profile, if any */ public async getPmpBasedOnMessage(message: string): Promise { - if (!this.succInit) await this.init(); + // Always refresh so newly-added proxies apply immediately. + await this.init(); // check if the message matches our formats // maybe a bit unsafe, as we are evaluating regex that aren't necessarily by us, could be _maybe_ manipulated const profileId = this.proxiesAssocs.find((assoc) => assoc.regex.test(message))?.profileId; @@ -89,7 +92,7 @@ export class PKitProxyMessageHandler { * @memberof PKitProxyMessageHandler */ public stripProxyFromMessage(message: string): string | undefined { - if (!this.succInit) throw new Error('PK proxy message handler is not initialized'); + if (!this.succInit) return undefined; let m; this.proxiesAssocs.forEach((assoc) => { const match = assoc.regex.exec(message);