Skip to content

Commit a1f50ed

Browse files
committed
Reject unsupported services
1 parent 31e678f commit a1f50ed

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/routes/postSkipSegments.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { HashedUserID, UserID } from "../types/user.model";
1717
import { isUserVIP } from "../utils/isUserVIP";
1818
import { isUserTempVIP } from "../utils/isUserTempVIP";
1919
import { parseUserAgent } from "../utils/userAgent";
20-
import { getService } from "../utils/getService";
20+
import { getService, getUnsupportedService } from "../utils/getService";
2121
import axios from "axios";
2222
import { vote } from "./voteOnSponsorTime";
2323
import { canSubmit } from "../utils/permissions";
@@ -500,6 +500,11 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
500500
proxySubmission(req);
501501
}
502502

503+
const unsupportedValue = getUnsupportedService(req.query.service, req.body.service)
504+
if (unsupportedValue) {
505+
return res.status(400).send(`Service is not supported: ${unsupportedValue}`);
506+
}
507+
503508
// eslint-disable-next-line prefer-const
504509
let { videoID, userID: paramUserID, service, videoDuration, videoDurationParam, segments, userAgent } = preprocessInput(req);
505510

src/utils/getService.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,22 @@ export function getService<T extends string>(...value: T[]): Service {
1515

1616
return Service.YouTube;
1717
}
18+
19+
export function getUnsupportedService<T extends string>(...value: T[]): string | undefined {
20+
const serviceByName = Object.values(Service).reduce((acc, serviceName) => {
21+
acc[serviceName.toLowerCase()] = serviceName;
22+
23+
return acc;
24+
}, {} as Record<string, Service>);
25+
26+
let unsupportedValue;
27+
for (const name of value) {
28+
const cleanName = name?.trim().toLowerCase()
29+
if (cleanName in serviceByName) {
30+
return null
31+
}
32+
if(cleanName) unsupportedValue = name
33+
}
34+
35+
return unsupportedValue
36+
}

test/cases/postSkipSegments.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,24 @@ describe("postSkipSegments", () => {
351351
.catch(err => done(err));
352352
});
353353

354+
it("Should not be able to submit with unknown service", (done) => {
355+
const videoID = "colon-1";
356+
postSkipSegmentJSON({
357+
userID: submitUserOne,
358+
videoID,
359+
service:"Unsupported-xxx",
360+
segments: [{
361+
segment: ["0", "3"],
362+
category: "sponsor",
363+
}]
364+
})
365+
.then(res => {
366+
assert.strictEqual(res.status, 400);
367+
done();
368+
})
369+
.catch(err => done(err));
370+
});
371+
354372
it("Should throw 409 on duplicate submission", (done) => {
355373
const videoID = "private-video";
356374
postSkipSegmentParam({

0 commit comments

Comments
 (0)