fix(slack): resolve user IDs to DM channels before files.uploadV2 (#23773)

When a bare Slack user ID (U-prefix) is passed as the send target
without an explicit `user:` prefix, `parseSlackTarget` classifies it as
kind="channel".  `resolveChannelId` then passes it through to callers
without calling `conversations.open`.

This works for `chat.postMessage` (which tolerates user IDs), but
`files.uploadV2` delegates to `completeUploadExternal` which validates
`channel_id` against `^[CGDZ][A-Z0-9]{8,}$` — rejecting U-prefixed
IDs with `invalid_arguments`.

Fix: detect U-prefixed IDs in `resolveChannelId` regardless of the
parsed `kind`, and always resolve them via `conversations.open` to
obtain the DM channel ID (D-prefix).

Includes test coverage for bare, prefixed, and mention-style user ID
targets with file uploads, plus a channel-target negative case.
This commit is contained in:
Luis Conde
2026-02-22 14:04:53 -04:00
committed by GitHub
parent 568973e5ac
commit af9881b9c5
3 changed files with 132 additions and 1 deletions

View File

@@ -166,7 +166,14 @@ async function resolveChannelId(
client: WebClient,
recipient: SlackRecipient,
): Promise<{ channelId: string; isDm?: boolean }> {
if (recipient.kind === "channel") {
// Bare Slack user IDs (U-prefix) may arrive with kind="channel" when the
// target string had no explicit prefix (parseSlackTarget defaults bare IDs
// to "channel"). chat.postMessage tolerates user IDs directly, but
// files.uploadV2 → completeUploadExternal validates channel_id against
// ^[CGDZ][A-Z0-9]{8,}$ and rejects U-prefixed IDs. Always resolve user
// IDs via conversations.open to obtain the DM channel ID.
const isUserId = recipient.kind === "user" || /^U[A-Z0-9]+$/i.test(recipient.id);
if (!isUserId) {
return { channelId: recipient.id };
}
const response = await client.conversations.open({ users: recipient.id });