Skip to content

Commit 4cbbbcb

Browse files
initial attempt to fix title > 255 char (#1189)
* initial attempt to fix title > 255 char * added title to OttApiRequestRoomCreate * add validator to createRoom & throw proper error * ran yarn lint * add title test for create endpoint * fixed unit test for create endpoint, added unit test for patch endpoint * fixed stray import * fixed post endpoint unit test * one more fix * added authorization to request * ran yarn lint
1 parent 6126213 commit 4cbbbcb

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

common/models/rest-api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface OttApiResponseRoomGenerate {
3232

3333
/** Endpoint: `/api/room/create` */
3434
export interface OttApiRequestRoomCreate {
35+
title?: string;
3536
name: string;
3637
isTemporary?: boolean;
3738
visibility?: Visibility;

server/api/room.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ const generateRoom: RequestHandler<unknown, OttResponseBody<OttApiResponseRoomGe
9494
if (!conf.get("room.enable_create_temporary")) {
9595
throw new FeatureDisabledException("Temporary rooms are disabled.");
9696
}
97-
9897
let points = 50;
9998
if (!(await consumeRateLimitPoints(res, req.ip, points))) {
10099
return;
@@ -149,6 +148,14 @@ const createRoom: RequestHandler<
149148
"not allowed (too long, must be at most 32 characters)"
150149
);
151150
}
151+
152+
if (req.body.title && req.body.title.length > 255) {
153+
throw new BadApiArgumentException(
154+
"title",
155+
"not allowed (too long, must be at most 255 characters)"
156+
);
157+
}
158+
152159
if (!ROOM_NAME_REGEX.exec(req.body.name)) {
153160
throw new BadApiArgumentException("name", "not allowed (invalid characters)");
154161
}
@@ -237,6 +244,13 @@ const patchRoom: RequestHandler = async (req, res) => {
237244
delete req.body.permissions;
238245
}
239246

247+
if (req.body.title && req.body.title.length > 255) {
248+
throw new BadApiArgumentException(
249+
"title",
250+
"not allowed (too long, must be at most 255 characters)"
251+
);
252+
}
253+
240254
req.body.grants = new Grants(req.body.grants);
241255

242256
const result = await roommanager.getRoom(req.params.name);

server/tests/unit/api/room.spec.ts

+61
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ describe("Room API", () => {
225225
isTemporary: true,
226226
},
227227
],
228+
[
229+
{ arg: "title", reason: "not allowed (too long, must be at most 255 characters)" },
230+
{
231+
name: "foo",
232+
title: "abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab",
233+
isTemporary: true,
234+
},
235+
],
228236
[
229237
{ arg: "visibility", reason: "must be one of public,unlisted,private" },
230238
{ name: "test1", isTemporary: true, visibility: "invalid" },
@@ -309,4 +317,57 @@ describe("Room API", () => {
309317
});
310318
}
311319
});
320+
321+
describe("PATCH /api/room/:name", () => {
322+
let getSessionInfoSpy: jest.SpyInstance;
323+
let validateSpy: jest.SpyInstance;
324+
325+
beforeAll(async () => {
326+
getSessionInfoSpy = jest.spyOn(tokens, "getSessionInfo").mockResolvedValue({
327+
isLoggedIn: false,
328+
username: "test",
329+
});
330+
validateSpy = jest.spyOn(tokens, "validate").mockResolvedValue(true);
331+
332+
await roommanager.createRoom({
333+
name: "foo",
334+
isTemporary: true,
335+
});
336+
});
337+
338+
afterAll(async () => {
339+
getSessionInfoSpy.mockRestore();
340+
validateSpy.mockRestore();
341+
342+
try {
343+
await roommanager.unloadRoom("foo");
344+
} catch (e) {
345+
if (!(e instanceof RoomNotFoundException)) {
346+
throw e;
347+
}
348+
}
349+
});
350+
351+
it.each([
352+
[
353+
{ arg: "title", reason: "not allowed (too long, must be at most 255 characters)" },
354+
{
355+
title: "abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab",
356+
isTemporary: true,
357+
},
358+
],
359+
])("should fail to modify room for validation errors: %s", async (error, body) => {
360+
let resp = await request(app)
361+
.patch("/api/room/foo")
362+
.set({ Authorization: "Bearer foobar" })
363+
.send(body)
364+
.expect("Content-Type", /json/)
365+
.expect(400);
366+
expect(resp.body.success).toEqual(false);
367+
expect(resp.body.error).toMatchObject({
368+
name: "BadApiArgumentException",
369+
...error,
370+
});
371+
});
372+
});
312373
});

0 commit comments

Comments
 (0)