@kodelyth/zalo 2026.5.42 → 2026.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/klaw.plugin.json +509 -2
- package/package.json +19 -6
- package/api.ts +0 -8
- package/channel-plugin-api.ts +0 -1
- package/contract-api.ts +0 -5
- package/index.test.ts +0 -15
- package/index.ts +0 -20
- package/runtime-api.test.ts +0 -10
- package/runtime-api.ts +0 -71
- package/secret-contract-api.ts +0 -5
- package/setup-api.ts +0 -34
- package/setup-entry.ts +0 -13
- package/src/accounts.test.ts +0 -95
- package/src/accounts.ts +0 -65
- package/src/actions.runtime.ts +0 -5
- package/src/actions.test.ts +0 -32
- package/src/actions.ts +0 -62
- package/src/api.test.ts +0 -166
- package/src/api.ts +0 -265
- package/src/approval-auth.test.ts +0 -17
- package/src/approval-auth.ts +0 -25
- package/src/channel.directory.test.ts +0 -56
- package/src/channel.runtime.ts +0 -89
- package/src/channel.startup.test.ts +0 -121
- package/src/channel.ts +0 -309
- package/src/config-schema.test.ts +0 -30
- package/src/config-schema.ts +0 -29
- package/src/group-access.ts +0 -23
- package/src/monitor-durable.test.ts +0 -49
- package/src/monitor-durable.ts +0 -38
- package/src/monitor.group-policy.test.ts +0 -213
- package/src/monitor.image.polling.test.ts +0 -113
- package/src/monitor.lifecycle.test.ts +0 -194
- package/src/monitor.pairing.lifecycle.test.ts +0 -139
- package/src/monitor.polling.media-reply.test.ts +0 -433
- package/src/monitor.reply-once.lifecycle.test.ts +0 -178
- package/src/monitor.ts +0 -1009
- package/src/monitor.types.ts +0 -4
- package/src/monitor.webhook.test.ts +0 -808
- package/src/monitor.webhook.ts +0 -278
- package/src/outbound-media.test.ts +0 -186
- package/src/outbound-media.ts +0 -236
- package/src/outbound-payload.contract.test.ts +0 -143
- package/src/probe.ts +0 -45
- package/src/proxy.ts +0 -18
- package/src/runtime-api.ts +0 -71
- package/src/runtime-support.ts +0 -82
- package/src/runtime.ts +0 -9
- package/src/secret-contract.ts +0 -109
- package/src/secret-input.ts +0 -5
- package/src/send.test.ts +0 -150
- package/src/send.ts +0 -207
- package/src/session-route.ts +0 -32
- package/src/setup-allow-from.ts +0 -97
- package/src/setup-core.ts +0 -152
- package/src/setup-status.test.ts +0 -33
- package/src/setup-surface.test.ts +0 -193
- package/src/setup-surface.ts +0 -294
- package/src/status-issues.test.ts +0 -17
- package/src/status-issues.ts +0 -34
- package/src/test-support/lifecycle-test-support.ts +0 -456
- package/src/test-support/monitor-mocks-test-support.ts +0 -209
- package/src/token.test.ts +0 -92
- package/src/token.ts +0 -79
- package/src/types.ts +0 -50
- package/test-api.ts +0 -1
- package/tsconfig.json +0 -16
package/runtime-api.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
addWildcardAllowFrom,
|
|
3
|
-
applyAccountNameToChannelSection,
|
|
4
|
-
applyBasicWebhookRequestGuards,
|
|
5
|
-
applySetupAccountConfigPatch,
|
|
6
|
-
type BaseProbeResult,
|
|
7
|
-
type BaseTokenResolution,
|
|
8
|
-
buildBaseAccountStatusSnapshot,
|
|
9
|
-
buildChannelConfigSchema,
|
|
10
|
-
buildSecretInputSchema,
|
|
11
|
-
buildSingleChannelSecretPromptState,
|
|
12
|
-
buildTokenChannelStatusSummary,
|
|
13
|
-
type ChannelAccountSnapshot,
|
|
14
|
-
type ChannelMessageActionAdapter,
|
|
15
|
-
type ChannelMessageActionName,
|
|
16
|
-
type ChannelPlugin,
|
|
17
|
-
type ChannelStatusIssue,
|
|
18
|
-
chunkTextForOutbound,
|
|
19
|
-
createChannelPairingController,
|
|
20
|
-
createChannelMessageReplyPipeline,
|
|
21
|
-
createDedupeCache,
|
|
22
|
-
createFixedWindowRateLimiter,
|
|
23
|
-
createWebhookAnomalyTracker,
|
|
24
|
-
DEFAULT_ACCOUNT_ID,
|
|
25
|
-
deliverTextOrMediaReply,
|
|
26
|
-
formatAllowFromLowercase,
|
|
27
|
-
formatPairingApproveHint,
|
|
28
|
-
type GroupPolicy,
|
|
29
|
-
hasConfiguredSecretInput,
|
|
30
|
-
isNormalizedSenderAllowed,
|
|
31
|
-
isNumericTargetId,
|
|
32
|
-
jsonResult,
|
|
33
|
-
logTypingFailure,
|
|
34
|
-
type MarkdownTableMode,
|
|
35
|
-
mergeAllowFromEntries,
|
|
36
|
-
migrateBaseNameToDefaultAccount,
|
|
37
|
-
normalizeAccountId,
|
|
38
|
-
normalizeResolvedSecretInputString,
|
|
39
|
-
normalizeSecretInputString,
|
|
40
|
-
type KlawConfig,
|
|
41
|
-
type OutboundReplyPayload,
|
|
42
|
-
PAIRING_APPROVED_MESSAGE,
|
|
43
|
-
type PluginRuntime,
|
|
44
|
-
promptSingleChannelSecretInput,
|
|
45
|
-
readJsonWebhookBodyOrReject,
|
|
46
|
-
readStringParam,
|
|
47
|
-
registerPluginHttpRoute,
|
|
48
|
-
type RegisterWebhookPluginRouteOptions,
|
|
49
|
-
registerWebhookTarget,
|
|
50
|
-
type RegisterWebhookTargetOptions,
|
|
51
|
-
registerWebhookTargetWithPluginRoute,
|
|
52
|
-
type ReplyPayload,
|
|
53
|
-
resolveClientIp,
|
|
54
|
-
resolveDefaultGroupPolicy,
|
|
55
|
-
resolveInboundRouteEnvelopeBuilderWithRuntime,
|
|
56
|
-
resolveOpenProviderRuntimeGroupPolicy,
|
|
57
|
-
resolveWebhookPath,
|
|
58
|
-
resolveWebhookTargetWithAuthOrRejectSync,
|
|
59
|
-
runSingleChannelSecretStep,
|
|
60
|
-
type RuntimeEnv,
|
|
61
|
-
type SecretInput,
|
|
62
|
-
sendPayloadWithChunkedTextAndMedia,
|
|
63
|
-
setTopLevelChannelDmPolicyWithAllowFrom,
|
|
64
|
-
setZaloRuntime,
|
|
65
|
-
waitForAbortSignal,
|
|
66
|
-
warnMissingProviderGroupPolicyFallbackOnce,
|
|
67
|
-
WEBHOOK_ANOMALY_COUNTER_DEFAULTS,
|
|
68
|
-
WEBHOOK_RATE_LIMIT_DEFAULTS,
|
|
69
|
-
withResolvedWebhookRequestPipeline,
|
|
70
|
-
type WizardPrompter,
|
|
71
|
-
} from "./src/runtime-api.js";
|
package/secret-contract-api.ts
DELETED
package/setup-api.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { loadBundledEntryExportSync } from "klaw/plugin-sdk/channel-entry-contract";
|
|
2
|
-
|
|
3
|
-
type SetupSurfaceModule = typeof import("./src/setup-surface.js");
|
|
4
|
-
|
|
5
|
-
function createLazyObjectValue<T extends object>(load: () => T): T {
|
|
6
|
-
return new Proxy({} as T, {
|
|
7
|
-
get(_target, property, receiver) {
|
|
8
|
-
return Reflect.get(load(), property, receiver);
|
|
9
|
-
},
|
|
10
|
-
has(_target, property) {
|
|
11
|
-
return property in load();
|
|
12
|
-
},
|
|
13
|
-
ownKeys() {
|
|
14
|
-
return Reflect.ownKeys(load());
|
|
15
|
-
},
|
|
16
|
-
getOwnPropertyDescriptor(_target, property) {
|
|
17
|
-
const descriptor = Object.getOwnPropertyDescriptor(load(), property);
|
|
18
|
-
return descriptor ? { ...descriptor, configurable: true } : undefined;
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function loadSetupSurfaceModule(): SetupSurfaceModule {
|
|
24
|
-
return loadBundledEntryExportSync<SetupSurfaceModule>(import.meta.url, {
|
|
25
|
-
specifier: "./src/setup-surface.js",
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export { zaloDmPolicy, zaloSetupAdapter, createZaloSetupWizardProxy } from "./src/setup-core.js";
|
|
30
|
-
export { resolveZaloRuntimeGroupPolicy } from "./src/group-access.js";
|
|
31
|
-
|
|
32
|
-
export const zaloSetupWizard: SetupSurfaceModule["zaloSetupWizard"] = createLazyObjectValue(
|
|
33
|
-
() => loadSetupSurfaceModule().zaloSetupWizard as object,
|
|
34
|
-
) as SetupSurfaceModule["zaloSetupWizard"];
|
package/setup-entry.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { defineBundledChannelSetupEntry } from "klaw/plugin-sdk/channel-entry-contract";
|
|
2
|
-
|
|
3
|
-
export default defineBundledChannelSetupEntry({
|
|
4
|
-
importMetaUrl: import.meta.url,
|
|
5
|
-
plugin: {
|
|
6
|
-
specifier: "./api.js",
|
|
7
|
-
exportName: "zaloPlugin",
|
|
8
|
-
},
|
|
9
|
-
secrets: {
|
|
10
|
-
specifier: "./secret-contract-api.js",
|
|
11
|
-
exportName: "channelSecrets",
|
|
12
|
-
},
|
|
13
|
-
});
|
package/src/accounts.test.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
listEnabledZaloAccounts,
|
|
4
|
-
listZaloAccountIds,
|
|
5
|
-
resolveDefaultZaloAccountId,
|
|
6
|
-
resolveZaloAccount,
|
|
7
|
-
} from "./accounts.js";
|
|
8
|
-
|
|
9
|
-
describe("resolveZaloAccount", () => {
|
|
10
|
-
it("resolves account config when account key casing differs from normalized id", () => {
|
|
11
|
-
const resolved = resolveZaloAccount({
|
|
12
|
-
cfg: {
|
|
13
|
-
channels: {
|
|
14
|
-
zalo: {
|
|
15
|
-
webhookUrl: "https://top.example.com",
|
|
16
|
-
accounts: {
|
|
17
|
-
Work: {
|
|
18
|
-
name: "Work",
|
|
19
|
-
webhookUrl: "https://work.example.com",
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
accountId: "work",
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
expect(resolved.accountId).toBe("work");
|
|
29
|
-
expect(resolved.name).toBe("Work");
|
|
30
|
-
expect(resolved.config.webhookUrl).toBe("https://work.example.com");
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("falls back to top-level config for named accounts without overrides", () => {
|
|
34
|
-
const resolved = resolveZaloAccount({
|
|
35
|
-
cfg: {
|
|
36
|
-
channels: {
|
|
37
|
-
zalo: {
|
|
38
|
-
enabled: true,
|
|
39
|
-
webhookUrl: "https://top.example.com",
|
|
40
|
-
accounts: {
|
|
41
|
-
work: {},
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
accountId: "work",
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
expect(resolved.accountId).toBe("work");
|
|
50
|
-
expect(resolved.enabled).toBe(true);
|
|
51
|
-
expect(resolved.config.webhookUrl).toBe("https://top.example.com");
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("uses configured defaultAccount when accountId is omitted", () => {
|
|
55
|
-
const resolved = resolveZaloAccount({
|
|
56
|
-
cfg: {
|
|
57
|
-
channels: {
|
|
58
|
-
zalo: {
|
|
59
|
-
defaultAccount: "work",
|
|
60
|
-
accounts: {
|
|
61
|
-
work: {
|
|
62
|
-
name: "Work",
|
|
63
|
-
botToken: "work-token",
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
expect(resolved.accountId).toBe("work");
|
|
72
|
-
expect(resolved.name).toBe("Work");
|
|
73
|
-
expect(resolved.token).toBe("work-token");
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it("keeps the implicit default account when named accounts are added to top-level credentials", () => {
|
|
77
|
-
const cfg = {
|
|
78
|
-
channels: {
|
|
79
|
-
zalo: {
|
|
80
|
-
botToken: "default-token",
|
|
81
|
-
accounts: {
|
|
82
|
-
work: {
|
|
83
|
-
enabled: false,
|
|
84
|
-
botToken: "work-token",
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
expect(listZaloAccountIds(cfg)).toEqual(["default", "work"]);
|
|
92
|
-
expect(resolveDefaultZaloAccountId(cfg)).toBe("default");
|
|
93
|
-
expect(listEnabledZaloAccounts(cfg).map((account) => account.accountId)).toEqual(["default"]);
|
|
94
|
-
});
|
|
95
|
-
});
|
package/src/accounts.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createAccountListHelpers,
|
|
3
|
-
resolveMergedAccountConfig,
|
|
4
|
-
} from "klaw/plugin-sdk/account-helpers";
|
|
5
|
-
import { normalizeAccountId } from "klaw/plugin-sdk/account-id";
|
|
6
|
-
import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
|
|
7
|
-
import { normalizeOptionalString } from "klaw/plugin-sdk/string-coerce-runtime";
|
|
8
|
-
import { resolveZaloToken } from "./token.js";
|
|
9
|
-
import type { ResolvedZaloAccount, ZaloAccountConfig, ZaloConfig } from "./types.js";
|
|
10
|
-
|
|
11
|
-
export type { ResolvedZaloAccount };
|
|
12
|
-
|
|
13
|
-
const { listAccountIds: listZaloAccountIds, resolveDefaultAccountId: resolveDefaultZaloAccountId } =
|
|
14
|
-
createAccountListHelpers("zalo", {
|
|
15
|
-
implicitDefaultAccount: {
|
|
16
|
-
channelKeys: ["botToken", "tokenFile"],
|
|
17
|
-
envVars: ["ZALO_BOT_TOKEN"],
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
export { listZaloAccountIds, resolveDefaultZaloAccountId };
|
|
21
|
-
|
|
22
|
-
function mergeZaloAccountConfig(cfg: KlawConfig, accountId: string): ZaloAccountConfig {
|
|
23
|
-
return resolveMergedAccountConfig<ZaloAccountConfig>({
|
|
24
|
-
channelConfig: cfg.channels?.zalo as ZaloAccountConfig | undefined,
|
|
25
|
-
accounts: (cfg.channels?.zalo as ZaloConfig | undefined)?.accounts as
|
|
26
|
-
| Record<string, Partial<ZaloAccountConfig>>
|
|
27
|
-
| undefined,
|
|
28
|
-
accountId,
|
|
29
|
-
omitKeys: ["defaultAccount"],
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function resolveZaloAccount(params: {
|
|
34
|
-
cfg: KlawConfig;
|
|
35
|
-
accountId?: string | null;
|
|
36
|
-
allowUnresolvedSecretRef?: boolean;
|
|
37
|
-
}): ResolvedZaloAccount {
|
|
38
|
-
const accountId = normalizeAccountId(
|
|
39
|
-
params.accountId ?? (params.cfg.channels?.zalo as ZaloConfig | undefined)?.defaultAccount,
|
|
40
|
-
);
|
|
41
|
-
const baseEnabled = (params.cfg.channels?.zalo as ZaloConfig | undefined)?.enabled !== false;
|
|
42
|
-
const merged = mergeZaloAccountConfig(params.cfg, accountId);
|
|
43
|
-
const accountEnabled = merged.enabled !== false;
|
|
44
|
-
const enabled = baseEnabled && accountEnabled;
|
|
45
|
-
const tokenResolution = resolveZaloToken(
|
|
46
|
-
params.cfg.channels?.zalo as ZaloConfig | undefined,
|
|
47
|
-
accountId,
|
|
48
|
-
{ allowUnresolvedSecretRef: params.allowUnresolvedSecretRef },
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
accountId,
|
|
53
|
-
name: normalizeOptionalString(merged.name),
|
|
54
|
-
enabled,
|
|
55
|
-
token: tokenResolution.token,
|
|
56
|
-
tokenSource: tokenResolution.source,
|
|
57
|
-
config: merged,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function listEnabledZaloAccounts(cfg: KlawConfig): ResolvedZaloAccount[] {
|
|
62
|
-
return listZaloAccountIds(cfg)
|
|
63
|
-
.map((accountId) => resolveZaloAccount({ cfg, accountId }))
|
|
64
|
-
.filter((account) => account.enabled);
|
|
65
|
-
}
|
package/src/actions.runtime.ts
DELETED
package/src/actions.test.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { zaloMessageActions } from "./actions.js";
|
|
3
|
-
import type { KlawConfig } from "./runtime-api.js";
|
|
4
|
-
|
|
5
|
-
describe("zaloMessageActions.describeMessageTool", () => {
|
|
6
|
-
it("honors the selected Zalo account during discovery", () => {
|
|
7
|
-
const cfg: KlawConfig = {
|
|
8
|
-
channels: {
|
|
9
|
-
zalo: {
|
|
10
|
-
enabled: true,
|
|
11
|
-
botToken: "root-token",
|
|
12
|
-
accounts: {
|
|
13
|
-
default: {
|
|
14
|
-
enabled: false,
|
|
15
|
-
botToken: "default-token",
|
|
16
|
-
},
|
|
17
|
-
work: {
|
|
18
|
-
enabled: true,
|
|
19
|
-
botToken: "work-token",
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
expect(zaloMessageActions.describeMessageTool?.({ cfg, accountId: "default" })).toBeNull();
|
|
27
|
-
expect(zaloMessageActions.describeMessageTool?.({ cfg, accountId: "work" })).toEqual({
|
|
28
|
-
actions: ["send"],
|
|
29
|
-
capabilities: [],
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
});
|
package/src/actions.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { jsonResult, readStringParam } from "klaw/plugin-sdk/channel-actions";
|
|
2
|
-
import type {
|
|
3
|
-
ChannelMessageActionAdapter,
|
|
4
|
-
ChannelMessageActionName,
|
|
5
|
-
} from "klaw/plugin-sdk/channel-contract";
|
|
6
|
-
import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
|
|
7
|
-
import { createLazyRuntimeNamedExport } from "klaw/plugin-sdk/lazy-runtime";
|
|
8
|
-
import { extractToolSend } from "klaw/plugin-sdk/tool-send";
|
|
9
|
-
import { listEnabledZaloAccounts, resolveZaloAccount } from "./accounts.js";
|
|
10
|
-
|
|
11
|
-
const loadZaloActionsRuntime = createLazyRuntimeNamedExport(
|
|
12
|
-
() => import("./actions.runtime.js"),
|
|
13
|
-
"zaloActionsRuntime",
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
const providerId = "zalo";
|
|
17
|
-
|
|
18
|
-
function listEnabledAccounts(cfg: KlawConfig, accountId?: string | null) {
|
|
19
|
-
return (
|
|
20
|
-
accountId ? [resolveZaloAccount({ cfg, accountId })] : listEnabledZaloAccounts(cfg)
|
|
21
|
-
).filter((account) => account.enabled && account.tokenSource !== "none");
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const zaloMessageActions: ChannelMessageActionAdapter = {
|
|
25
|
-
describeMessageTool: ({ cfg, accountId }) => {
|
|
26
|
-
const accounts = listEnabledAccounts(cfg, accountId);
|
|
27
|
-
if (accounts.length === 0) {
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
const actions = new Set<ChannelMessageActionName>(["send"]);
|
|
31
|
-
return { actions: Array.from(actions), capabilities: [] };
|
|
32
|
-
},
|
|
33
|
-
extractToolSend: ({ args }) => extractToolSend(args, "sendMessage"),
|
|
34
|
-
handleAction: async ({ action, params, cfg, accountId }) => {
|
|
35
|
-
if (action === "send") {
|
|
36
|
-
const to = readStringParam(params, "to", { required: true });
|
|
37
|
-
const content = readStringParam(params, "message", {
|
|
38
|
-
required: true,
|
|
39
|
-
allowEmpty: true,
|
|
40
|
-
});
|
|
41
|
-
const mediaUrl = readStringParam(params, "media", { trim: false });
|
|
42
|
-
|
|
43
|
-
const { sendMessageZalo } = await loadZaloActionsRuntime();
|
|
44
|
-
const result = await sendMessageZalo(to ?? "", content ?? "", {
|
|
45
|
-
accountId: accountId ?? undefined,
|
|
46
|
-
mediaUrl: mediaUrl ?? undefined,
|
|
47
|
-
cfg: cfg,
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
if (!result.ok) {
|
|
51
|
-
return jsonResult({
|
|
52
|
-
ok: false,
|
|
53
|
-
error: result.error ?? "Failed to send Zalo message",
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return jsonResult({ ok: true, to, messageId: result.messageId });
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
throw new Error(`Action ${action} is not supported for provider ${providerId}.`);
|
|
61
|
-
},
|
|
62
|
-
};
|
package/src/api.test.ts
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
const resolvePinnedHostnameWithPolicyMock = vi.fn();
|
|
4
|
-
|
|
5
|
-
vi.mock("klaw/plugin-sdk/ssrf-runtime", () => ({
|
|
6
|
-
resolvePinnedHostnameWithPolicy: (...args: unknown[]) =>
|
|
7
|
-
resolvePinnedHostnameWithPolicyMock(...args),
|
|
8
|
-
}));
|
|
9
|
-
|
|
10
|
-
import { deleteWebhook, getWebhookInfo, sendChatAction, sendPhoto, type ZaloFetch } from "./api.js";
|
|
11
|
-
|
|
12
|
-
function createOkFetcher() {
|
|
13
|
-
return vi.fn<ZaloFetch>(async () => new Response(JSON.stringify({ ok: true, result: {} })));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function requireFirstFetchCall(fetcher: ReturnType<typeof createOkFetcher>, label: string) {
|
|
17
|
-
const [call] = fetcher.mock.calls;
|
|
18
|
-
if (!call) {
|
|
19
|
-
throw new Error(`expected ${label}`);
|
|
20
|
-
}
|
|
21
|
-
return call;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async function expectPostJsonRequest(run: (token: string, fetcher: ZaloFetch) => Promise<unknown>) {
|
|
25
|
-
const fetcher = createOkFetcher();
|
|
26
|
-
await run("test-token", fetcher);
|
|
27
|
-
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
28
|
-
const [, init] = requireFirstFetchCall(fetcher, "Zalo request");
|
|
29
|
-
if (!init) {
|
|
30
|
-
throw new Error("expected Zalo request init");
|
|
31
|
-
}
|
|
32
|
-
expect(init.method).toBe("POST");
|
|
33
|
-
expect(init.headers).toEqual({ "Content-Type": "application/json" });
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
describe("Zalo API request methods", () => {
|
|
37
|
-
beforeEach(() => {
|
|
38
|
-
resolvePinnedHostnameWithPolicyMock.mockReset();
|
|
39
|
-
resolvePinnedHostnameWithPolicyMock.mockResolvedValue({
|
|
40
|
-
hostname: "example.com",
|
|
41
|
-
addresses: ["93.184.216.34"],
|
|
42
|
-
lookup: vi.fn(),
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("uses POST for getWebhookInfo", async () => {
|
|
47
|
-
await expectPostJsonRequest(getWebhookInfo);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("keeps POST for deleteWebhook", async () => {
|
|
51
|
-
await expectPostJsonRequest(deleteWebhook);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("aborts sendChatAction when the typing timeout elapses", async () => {
|
|
55
|
-
vi.useFakeTimers();
|
|
56
|
-
try {
|
|
57
|
-
const fetcher = vi.fn<ZaloFetch>(
|
|
58
|
-
(_, init) =>
|
|
59
|
-
new Promise<Response>((_, reject) => {
|
|
60
|
-
init?.signal?.addEventListener("abort", () => reject(new Error("aborted")), {
|
|
61
|
-
once: true,
|
|
62
|
-
});
|
|
63
|
-
}),
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
const promise = sendChatAction(
|
|
67
|
-
"test-token",
|
|
68
|
-
{
|
|
69
|
-
chat_id: "chat-123",
|
|
70
|
-
action: "typing",
|
|
71
|
-
},
|
|
72
|
-
fetcher,
|
|
73
|
-
25,
|
|
74
|
-
);
|
|
75
|
-
const rejected = expect(promise).rejects.toThrow("aborted");
|
|
76
|
-
|
|
77
|
-
await vi.advanceTimersByTimeAsync(25);
|
|
78
|
-
|
|
79
|
-
await rejected;
|
|
80
|
-
const [, init] = requireFirstFetchCall(fetcher, "Zalo chat action request");
|
|
81
|
-
if (!init) {
|
|
82
|
-
throw new Error("expected Zalo chat action request init");
|
|
83
|
-
}
|
|
84
|
-
if (!init.signal) {
|
|
85
|
-
throw new Error("expected Zalo chat action abort signal");
|
|
86
|
-
}
|
|
87
|
-
expect(init.signal.aborted).toBe(true);
|
|
88
|
-
} finally {
|
|
89
|
-
vi.useRealTimers();
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it("validates outbound photo URLs against the SSRF guard before posting", async () => {
|
|
94
|
-
const fetcher = createOkFetcher();
|
|
95
|
-
|
|
96
|
-
await sendPhoto(
|
|
97
|
-
"test-token",
|
|
98
|
-
{
|
|
99
|
-
chat_id: "chat-123",
|
|
100
|
-
photo: "https://example.com/image.png",
|
|
101
|
-
},
|
|
102
|
-
fetcher,
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
expect(resolvePinnedHostnameWithPolicyMock).toHaveBeenCalledWith("example.com", {
|
|
106
|
-
policy: {},
|
|
107
|
-
});
|
|
108
|
-
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it("blocks private-network photo URLs before they reach the Zalo API", async () => {
|
|
112
|
-
const fetcher = createOkFetcher();
|
|
113
|
-
resolvePinnedHostnameWithPolicyMock.mockRejectedValueOnce(
|
|
114
|
-
new Error("Blocked hostname or private/internal/special-use IP address"),
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
await expect(
|
|
118
|
-
sendPhoto(
|
|
119
|
-
"test-token",
|
|
120
|
-
{
|
|
121
|
-
chat_id: "chat-123",
|
|
122
|
-
photo: "http://169.254.169.254/latest/meta-data/iam/security-credentials/",
|
|
123
|
-
},
|
|
124
|
-
fetcher,
|
|
125
|
-
),
|
|
126
|
-
).rejects.toThrow("Blocked hostname or private/internal/special-use IP address");
|
|
127
|
-
|
|
128
|
-
expect(fetcher).not.toHaveBeenCalled();
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it("rejects non-http photo URLs", async () => {
|
|
132
|
-
const fetcher = createOkFetcher();
|
|
133
|
-
|
|
134
|
-
await expect(
|
|
135
|
-
sendPhoto(
|
|
136
|
-
"test-token",
|
|
137
|
-
{
|
|
138
|
-
chat_id: "chat-123",
|
|
139
|
-
photo: "file:///etc/passwd",
|
|
140
|
-
},
|
|
141
|
-
fetcher,
|
|
142
|
-
),
|
|
143
|
-
).rejects.toThrow("Zalo photo URL must use HTTP or HTTPS");
|
|
144
|
-
|
|
145
|
-
expect(resolvePinnedHostnameWithPolicyMock).not.toHaveBeenCalled();
|
|
146
|
-
expect(fetcher).not.toHaveBeenCalled();
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("rejects non-URL strings", async () => {
|
|
150
|
-
const fetcher = createOkFetcher();
|
|
151
|
-
|
|
152
|
-
await expect(
|
|
153
|
-
sendPhoto(
|
|
154
|
-
"test-token",
|
|
155
|
-
{
|
|
156
|
-
chat_id: "chat-123",
|
|
157
|
-
photo: "not a url",
|
|
158
|
-
},
|
|
159
|
-
fetcher,
|
|
160
|
-
),
|
|
161
|
-
).rejects.toThrow("Zalo photo URL must be an absolute HTTP or HTTPS URL");
|
|
162
|
-
|
|
163
|
-
expect(resolvePinnedHostnameWithPolicyMock).not.toHaveBeenCalled();
|
|
164
|
-
expect(fetcher).not.toHaveBeenCalled();
|
|
165
|
-
});
|
|
166
|
-
});
|