Files
adolf/qa/convex-credential-broker/convex/payload-validation.ts
alvis bedb527145
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Vendor OpenClaw source as Adolf fork baseline
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11),
free to diverge. Tree copied sans upstream .git; upstream remote added for
future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19.
Preserves docs/ARCHITECTURE.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
2026-07-05 09:36:54 +00:00

279 lines
8.6 KiB
TypeScript

// Payload Validation module supports OpenClaw QA credential workflows.
export class CredentialPayloadValidationError extends Error {
code: string;
httpStatus: number;
constructor(httpStatus: number, code: string, message: string) {
super(message);
this.name = "CredentialPayloadValidationError";
this.httpStatus = httpStatus;
this.code = code;
}
}
type PayloadValidationFailureFactory = (httpStatus: number, code: string, message: string) => Error;
const DISCORD_SNOWFLAKE_RE = /^\d{17,20}$/u;
const E164_RE = /^\+[1-9]\d{6,14}$/u;
const SHA256_HEX_RE = /^[a-f0-9]{64}$/u;
const TELEGRAM_CHAT_ID_RE = /^-?\d+$/u;
const TELEGRAM_USER_ID_RE = /^\d+$/u;
function createCredentialPayloadValidationError(httpStatus: number, code: string, message: string) {
return new CredentialPayloadValidationError(httpStatus, code, message);
}
function throwPayloadError(createFailure: PayloadValidationFailureFactory, message: string): never {
throw createFailure(400, "INVALID_PAYLOAD", message);
}
function requirePayloadString(
payload: Record<string, unknown>,
key: string,
kind: string,
createFailure: PayloadValidationFailureFactory,
): string {
const raw = payload[key];
if (typeof raw !== "string") {
throwPayloadError(
createFailure,
`Credential payload for kind "${kind}" must include "${key}" as a string.`,
);
}
const value = raw.trim();
if (!value) {
throwPayloadError(
createFailure,
`Credential payload for kind "${kind}" must include a non-empty "${key}" value.`,
);
}
return value;
}
function requireDiscordSnowflakePayloadString(
payload: Record<string, unknown>,
key: string,
createFailure: PayloadValidationFailureFactory,
) {
const value = requirePayloadString(payload, key, "discord", createFailure);
if (!DISCORD_SNOWFLAKE_RE.test(value)) {
throwPayloadError(
createFailure,
`Credential payload for kind "discord" must include "${key}" as a Discord snowflake string.`,
);
}
return value;
}
function normalizeTelegramCredentialPayload(
payload: Record<string, unknown>,
createFailure: PayloadValidationFailureFactory,
) {
const groupId = requirePayloadString(payload, "groupId", "telegram", createFailure);
if (!TELEGRAM_CHAT_ID_RE.test(groupId)) {
throwPayloadError(
createFailure,
'Credential payload for kind "telegram" must include a numeric "groupId" string.',
);
}
const driverToken = requirePayloadString(payload, "driverToken", "telegram", createFailure);
const sutToken = requirePayloadString(payload, "sutToken", "telegram", createFailure);
return {
groupId,
driverToken,
sutToken,
} satisfies Record<string, unknown>;
}
function normalizeTelegramUserCredentialPayload(
payload: Record<string, unknown>,
createFailure: PayloadValidationFailureFactory,
) {
const kind = "telegram-user";
const groupId = requirePayloadString(payload, "groupId", kind, createFailure);
if (!TELEGRAM_CHAT_ID_RE.test(groupId)) {
throwPayloadError(
createFailure,
'Credential payload for kind "telegram-user" must include a numeric "groupId" string.',
);
}
const testerUserId = requirePayloadString(payload, "testerUserId", kind, createFailure);
if (!TELEGRAM_USER_ID_RE.test(testerUserId)) {
throwPayloadError(
createFailure,
'Credential payload for kind "telegram-user" must include a numeric "testerUserId" string.',
);
}
const telegramApiId = requirePayloadString(payload, "telegramApiId", kind, createFailure);
if (!TELEGRAM_USER_ID_RE.test(telegramApiId)) {
throwPayloadError(
createFailure,
'Credential payload for kind "telegram-user" must include a numeric "telegramApiId" string.',
);
}
const tdlibArchiveSha256 = requirePayloadString(
payload,
"tdlibArchiveSha256",
kind,
createFailure,
).toLowerCase();
const desktopTdataArchiveSha256 = requirePayloadString(
payload,
"desktopTdataArchiveSha256",
kind,
createFailure,
).toLowerCase();
if (!SHA256_HEX_RE.test(tdlibArchiveSha256)) {
throwPayloadError(
createFailure,
'Credential payload for kind "telegram-user" must include "tdlibArchiveSha256" as a SHA-256 hex string.',
);
}
if (!SHA256_HEX_RE.test(desktopTdataArchiveSha256)) {
throwPayloadError(
createFailure,
'Credential payload for kind "telegram-user" must include "desktopTdataArchiveSha256" as a SHA-256 hex string.',
);
}
return {
groupId,
sutToken: requirePayloadString(payload, "sutToken", kind, createFailure),
testerUserId,
testerUsername: requirePayloadString(payload, "testerUsername", kind, createFailure),
telegramApiId,
telegramApiHash: requirePayloadString(payload, "telegramApiHash", kind, createFailure),
tdlibDatabaseEncryptionKey: requirePayloadString(
payload,
"tdlibDatabaseEncryptionKey",
kind,
createFailure,
),
tdlibArchiveBase64: requirePayloadString(payload, "tdlibArchiveBase64", kind, createFailure),
tdlibArchiveSha256,
desktopTdataArchiveBase64: requirePayloadString(
payload,
"desktopTdataArchiveBase64",
kind,
createFailure,
),
desktopTdataArchiveSha256,
} satisfies Record<string, unknown>;
}
function normalizeDiscordCredentialPayload(
payload: Record<string, unknown>,
createFailure: PayloadValidationFailureFactory,
) {
const guildId = requireDiscordSnowflakePayloadString(payload, "guildId", createFailure);
const channelId = requireDiscordSnowflakePayloadString(payload, "channelId", createFailure);
const sutApplicationId = requireDiscordSnowflakePayloadString(
payload,
"sutApplicationId",
createFailure,
);
const voiceChannelId =
typeof payload.voiceChannelId === "string" && payload.voiceChannelId.trim()
? payload.voiceChannelId.trim()
: undefined;
if (voiceChannelId && !DISCORD_SNOWFLAKE_RE.test(voiceChannelId)) {
throwPayloadError(
createFailure,
'Credential payload for kind "discord" must include "voiceChannelId" as a Discord snowflake string when set.',
);
}
const driverBotToken = requirePayloadString(payload, "driverBotToken", "discord", createFailure);
const sutBotToken = requirePayloadString(payload, "sutBotToken", "discord", createFailure);
return {
guildId,
channelId,
driverBotToken,
sutBotToken,
sutApplicationId,
...(voiceChannelId ? { voiceChannelId } : {}),
} satisfies Record<string, unknown>;
}
function requireE164PayloadString(
payload: Record<string, unknown>,
key: string,
kind: string,
createFailure: PayloadValidationFailureFactory,
) {
const value = requirePayloadString(payload, key, kind, createFailure);
if (!E164_RE.test(value)) {
throwPayloadError(
createFailure,
`Credential payload for kind "${kind}" must include "${key}" as an E.164 phone number string.`,
);
}
return value;
}
function normalizeWhatsAppCredentialPayload(
payload: Record<string, unknown>,
createFailure: PayloadValidationFailureFactory,
) {
const driverPhoneE164 = requireE164PayloadString(
payload,
"driverPhoneE164",
"whatsapp",
createFailure,
);
const sutPhoneE164 = requireE164PayloadString(payload, "sutPhoneE164", "whatsapp", createFailure);
if (driverPhoneE164 === sutPhoneE164) {
throwPayloadError(
createFailure,
'Credential payload for kind "whatsapp" must use distinct driverPhoneE164 and sutPhoneE164 values.',
);
}
const driverAuthArchiveBase64 = requirePayloadString(
payload,
"driverAuthArchiveBase64",
"whatsapp",
createFailure,
);
const sutAuthArchiveBase64 = requirePayloadString(
payload,
"sutAuthArchiveBase64",
"whatsapp",
createFailure,
);
const groupJid =
typeof payload.groupJid === "string" && payload.groupJid.trim()
? payload.groupJid.trim()
: undefined;
return {
driverPhoneE164,
sutPhoneE164,
driverAuthArchiveBase64,
sutAuthArchiveBase64,
...(groupJid ? { groupJid } : {}),
} satisfies Record<string, unknown>;
}
const credentialPayloadNormalizers: Record<
string,
(
payload: Record<string, unknown>,
createFailure: PayloadValidationFailureFactory,
) => Record<string, unknown>
> = {
discord: normalizeDiscordCredentialPayload,
telegram: normalizeTelegramCredentialPayload,
"telegram-user": normalizeTelegramUserCredentialPayload,
whatsapp: normalizeWhatsAppCredentialPayload,
};
export function normalizeCredentialPayloadForKind(
kind: string,
payload: Record<string, unknown>,
createFailure: PayloadValidationFailureFactory = createCredentialPayloadValidationError,
) {
return credentialPayloadNormalizers[kind]?.(payload, createFailure) ?? payload;
}