Skip to content

Commit 620cbe1

Browse files
committed
fix: remove problematic redundant uuid conversion and add api input param validations to api server
1 parent 0551c8a commit 620cbe1

File tree

3 files changed

+958
-909
lines changed

3 files changed

+958
-909
lines changed

packages/client-direct/src/api.ts

+55-8
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,44 @@ import {
66
AgentRuntime,
77
elizaLogger,
88
getEnvVariable,
9+
UUID,
910
validateCharacterConfig,
1011
} from "@elizaos/core";
1112

1213
import { REST, Routes } from "discord.js";
1314
import { DirectClient } from ".";
14-
import { stringToUuid } from "@elizaos/core";
15+
import { validateUuid } from "@elizaos/core";
16+
17+
interface UUIDParams {
18+
agentId: UUID;
19+
roomId?: UUID;
20+
}
21+
22+
function validateUUIDParams(
23+
params: { agentId: string; roomId?: string },
24+
res: express.Response
25+
): UUIDParams | null {
26+
const agentId = validateUuid(params.agentId);
27+
if (!agentId) {
28+
res.status(400).json({
29+
error: "Invalid AgentId format. Expected to be a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
30+
});
31+
return null;
32+
}
33+
34+
if (params.roomId) {
35+
const roomId = validateUuid(params.roomId);
36+
if (!roomId) {
37+
res.status(400).json({
38+
error: "Invalid RoomId format. Expected to be a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
39+
});
40+
return null;
41+
}
42+
return { agentId, roomId };
43+
}
44+
45+
return { agentId };
46+
}
1547

1648
export function createApiRouter(
1749
agents: Map<string, AgentRuntime>,
@@ -46,7 +78,11 @@ export function createApiRouter(
4678
});
4779

4880
router.get("/agents/:agentId", (req, res) => {
49-
const agentId = req.params.agentId;
81+
const { agentId } = validateUUIDParams(req.params, res) ?? {
82+
agentId: null,
83+
};
84+
if (!agentId) return;
85+
5086
const agent = agents.get(agentId);
5187

5288
if (!agent) {
@@ -66,8 +102,11 @@ export function createApiRouter(
66102
});
67103

68104
router.post("/agents/:agentId/set", async (req, res) => {
69-
const agentId = req.params.agentId;
70-
console.log("agentId", agentId);
105+
const { agentId } = validateUUIDParams(req.params, res) ?? {
106+
agentId: null,
107+
};
108+
if (!agentId) return;
109+
71110
let agent: AgentRuntime = agents.get(agentId);
72111

73112
// update character
@@ -102,7 +141,11 @@ export function createApiRouter(
102141
});
103142

104143
router.get("/agents/:agentId/channels", async (req, res) => {
105-
const agentId = req.params.agentId;
144+
const { agentId } = validateUUIDParams(req.params, res) ?? {
145+
agentId: null,
146+
};
147+
if (!agentId) return;
148+
106149
const runtime = agents.get(agentId);
107150

108151
if (!runtime) {
@@ -128,8 +171,12 @@ export function createApiRouter(
128171
});
129172

130173
router.get("/agents/:agentId/:roomId/memories", async (req, res) => {
131-
const agentId = req.params.agentId;
132-
const roomId = stringToUuid(req.params.roomId);
174+
const { agentId, roomId } = validateUUIDParams(req.params, res) ?? {
175+
agentId: null,
176+
roomId: null,
177+
};
178+
if (!agentId || !roomId) return;
179+
133180
let runtime = agents.get(agentId);
134181

135182
// if runtime is null, look for runtime with the same name
@@ -146,7 +193,7 @@ export function createApiRouter(
146193

147194
try {
148195
const memories = await runtime.messageManager.getMemories({
149-
roomId,
196+
roomId: roomId,
150197
});
151198
const response = {
152199
agentId,

packages/core/src/uuid.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { sha1 } from "js-sha1";
22
import { UUID } from "./types.ts";
3+
import { z } from "zod";
4+
5+
export const uuidSchema = z.string().uuid() as z.ZodType<UUID>;
6+
7+
export function validateUuid(value: unknown): UUID | null {
8+
const result = uuidSchema.safeParse(value);
9+
return result.success ? result.data : null;
10+
}
311

412
export function stringToUuid(target: string | number): UUID {
513
if (typeof target === "number") {

0 commit comments

Comments
 (0)