Skip to content

Commit d9d57ef

Browse files
committed
fix: remove problematic redundant uuid conversion and add api input param validations to api server
1 parent 5bade12 commit d9d57ef

File tree

3 files changed

+1045
-538
lines changed

3 files changed

+1045
-538
lines changed

packages/client-direct/src/api.ts

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

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

1850
export function createApiRouter(
1951
agents: Map<string, AgentRuntime>,
@@ -48,7 +80,11 @@ export function createApiRouter(
4880
});
4981

5082
router.get("/agents/:agentId", (req, res) => {
51-
const agentId = req.params.agentId;
83+
const { agentId } = validateUUIDParams(req.params, res) ?? {
84+
agentId: null,
85+
};
86+
if (!agentId) return;
87+
5288
const agent = agents.get(agentId);
5389

5490
if (!agent) {
@@ -68,8 +104,11 @@ export function createApiRouter(
68104
});
69105

70106
router.post("/agents/:agentId/set", async (req, res) => {
71-
const agentId = req.params.agentId;
72-
console.log("agentId", agentId);
107+
const { agentId } = validateUUIDParams(req.params, res) ?? {
108+
agentId: null,
109+
};
110+
if (!agentId) return;
111+
73112
let agent: AgentRuntime = agents.get(agentId);
74113

75114
// update character
@@ -104,7 +143,11 @@ export function createApiRouter(
104143
});
105144

106145
router.get("/agents/:agentId/channels", async (req, res) => {
107-
const agentId = req.params.agentId;
146+
const { agentId } = validateUUIDParams(req.params, res) ?? {
147+
agentId: null,
148+
};
149+
if (!agentId) return;
150+
108151
const runtime = agents.get(agentId);
109152

110153
if (!runtime) {
@@ -130,8 +173,12 @@ export function createApiRouter(
130173
});
131174

132175
router.get("/agents/:agentId/:roomId/memories", async (req, res) => {
133-
const agentId = req.params.agentId;
134-
const roomId = stringToUuid(req.params.roomId);
176+
const { agentId, roomId } = validateUUIDParams(req.params, res) ?? {
177+
agentId: null,
178+
roomId: null,
179+
};
180+
if (!agentId || !roomId) return;
181+
135182
let runtime = agents.get(agentId);
136183

137184
// if runtime is null, look for runtime with the same name
@@ -148,7 +195,7 @@ export function createApiRouter(
148195

149196
try {
150197
const memories = await runtime.messageManager.getMemories({
151-
roomId,
198+
roomId: roomId,
152199
});
153200
const response = {
154201
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)