Skip to content

Commit 00e9913

Browse files
authored
fix: prevent unauthorized match winner setting and add state validation (#112)
Co-authored-by: Flegma <Flegma@users.noreply.github.com>
1 parent c2868fb commit 00e9913

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/matches/matches.controller.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ import { MatchRelayService } from "./match-relay/match-relay.service";
3737
export class MatchesController {
3838
private readonly appConfig: AppConfig;
3939

40+
private static readonly TERMINAL_STATUSES: string[] = [
41+
"Finished",
42+
"Canceled",
43+
"Forfeit",
44+
"Tie",
45+
"Surrendered",
46+
];
47+
4048
constructor(
4149
private readonly logger: Logger,
4250
private readonly hasura: HasuraService,
@@ -187,13 +195,7 @@ export class MatchesController {
187195
throw Error("unable to find match");
188196
}
189197

190-
if (
191-
matches_by_pk.status === "Tie" ||
192-
matches_by_pk.status === "Canceled" ||
193-
matches_by_pk.status === "Forfeit" ||
194-
matches_by_pk.status === "Finished" ||
195-
matches_by_pk.status === "Surrendered"
196-
) {
198+
if (MatchesController.TERMINAL_STATUSES.includes(matches_by_pk.status)) {
197199
response.status(204).end();
198200
return;
199201
}
@@ -330,11 +332,7 @@ export class MatchesController {
330332
*/
331333
if (
332334
data.op === "DELETE" ||
333-
status === "Tie" ||
334-
status === "Forfeit" ||
335-
status === "Canceled" ||
336-
status === "Finished" ||
337-
status === "Surrendered"
335+
MatchesController.TERMINAL_STATUSES.includes(status)
338336
) {
339337
this.matchRelayService.removeBroadcast(matchId);
340338
await this.removeDiscordIntegration(matchId);
@@ -606,7 +604,7 @@ export class MatchesController {
606604
}) {
607605
const { match_id, user, winning_lineup_id } = data;
608606

609-
if (await this.matchAssistant.isOrganizer(match_id, user)) {
607+
if (!(await this.matchAssistant.isOrganizer(match_id, user))) {
610608
throw Error("you are not a match organizer");
611609
}
612610

@@ -641,10 +639,27 @@ export class MatchesController {
641639
}) {
642640
const { match_id, user, winning_lineup_id } = data;
643641

644-
if (await this.matchAssistant.isOrganizer(match_id, user)) {
642+
if (!(await this.matchAssistant.isOrganizer(match_id, user))) {
645643
throw Error("you are not a match organizer");
646644
}
647645

646+
const { matches_by_pk: matchToForfeit } = await this.hasura.query({
647+
matches_by_pk: {
648+
__args: {
649+
id: match_id,
650+
},
651+
status: true,
652+
},
653+
});
654+
655+
if (!matchToForfeit) {
656+
throw Error("match not found");
657+
}
658+
659+
if (MatchesController.TERMINAL_STATUSES.includes(matchToForfeit.status)) {
660+
throw Error("cannot forfeit a match that has already ended");
661+
}
662+
648663
const { update_matches_by_pk: match } = await this.hasura.mutation({
649664
update_matches_by_pk: {
650665
__args: {

0 commit comments

Comments
 (0)