fix(onebot): correctness pass on actions and message parser

Action-layer fixes, all cross-checked against NapCat's reference
implementation in packages/napcat-onebot:

* download_file (extended.ts:539) now sanitizes `name` via
  path.basename + resolve/relative containment check. Previously an
  authenticated client could write `../../config/onebot_<uin>.json`
  and replace access tokens / webhook URLs.
* mark_group_msg_as_read / mark_private_msg_as_read / mark_msg_as_read
  fall back to meta.targetId when the optional group_id/user_id arg
  is missing. The old code passed 0 to the Bridge. NapCat resolves the
  peer from message_id alone in MarkMsgAsRead.ts; we now match that.
* set_friend_remark / set_group_remark refuse a missing `remark` field.
  The previous `asString(...) ?? ''` silently CLEARED the remark for
  any client that forgot the parameter. NapCat's typebox schema makes
  `remark` required; we now do the same.
* upload_forward_msg threads `group_id` down to uploadForwardNodes so
  the resulting resId lives in the correct namespace (type:3 + groupUin
  vs type:1 + selfUid — see bridge/actions/forward.ts). NapCat passes
  `peer.chatType === GROUP ? +peerUid : 0` for the same reason.
  ApiActionContext.sendForwardMsg + uploadForwardMessage signatures
  pick up an optional groupId.
* Private-message reply meta now resolves senderUin from the cached
  event (real sender), with a self-id fallback when only meta exists
  (bot's own outbound message — `cacheMessageMeta` does not populate
  the event row). The old code used meta.targetId, which is the
  conversation peer — wrong when the bot replies to its own message.
* message-parser:
    - intOr(value, fallback) helper replaces parseInt(String(...)) at
      face/at/reply/image/xml/poke/node sites so non-numeric input no
      longer produces NaN downstream. Invalid face ids skip the
      element rather than emit NaN.
    - cqUnescape decodes `&#44;` (escaped comma) — the OneBot 11 spec
      requires it and clients in the wild emit it.
* Drop the dead Array.isArray(rawAuth) branch in onebot/network/utils
  (Node's headers.authorization is `string | undefined`, never an
  array) and the unused WebHonorType import in api-handler.
This commit is contained in:
时瑾 authored and DrSmoothl committed 2026-05-16 21:31:34 +08:00
1 parent 917f08ce4d
commit b2c53b56f7
6 files changed
+113 -41

No files matched your search

@@ -99,10 +99,32 @@ export async function sendPrivateMessage(
return ref.messageStore.resolveReplySequence(false, userId, replyMessageId);
},
resolveReplyMeta: (replyMessageId) => {
// Prefer the cached event (it carries the REAL sender). When only
// meta exists — typically because the message we're replying to was
// sent by the bot itself and never round-tripped through dispatch
// (reportSelfMessage off / no message_sent event) — fall back to
// selfId. The previous code used `meta.targetId` here, which is the
// conversation PEER and shows the wrong "回复 @某人" in QQ for any
// self-reply.
const event = ref.messageStore.findEvent(replyMessageId);
if (event) {
const senderUin = typeof event.user_id === 'number'
? event.user_id
: parseInt(String(event.user_id || '0'), 10);
const time = typeof event.time === 'number'
? event.time
: parseInt(String(event.time || '0'), 10);
const meta = ref.messageStore.findMeta(replyMessageId);
return {
senderUin,
time,
random: meta?.random ?? 0,
};
}
const meta = ref.messageStore.findMeta(replyMessageId);
if (meta) {
return {
senderUin: meta.targetId,
senderUin: ref.selfId,
time: meta.timestamp,
random: meta.random,
};
@@ -240,9 +262,12 @@ export async function sendPrivateForwardMessage(
export async function uploadForwardMessage(
ref: OneBotInstanceContext,
messages: JsonValue,
groupId?: number,
): Promise<{ forwardId: string }> {
const nodes = await parseForwardNodes(ref, messages);
const forwardId = await ref.bridge.uploadForwardNodes(nodes);
// groupId controls the resId namespace (group vs private). Without it,
// a resId minted here is unusable when later sent into a group.
const forwardId = await ref.bridge.uploadForwardNodes(nodes, groupId);
return { forwardId };
}