Skip to content

Commit

Permalink
fix(captcha): log errors in /siteverify reqs
Browse files Browse the repository at this point in the history
  • Loading branch information
ABCxFF committed Sep 29, 2024
2 parents d9aba12 + 47deee2 commit dedc11c
Show file tree
Hide file tree
Showing 43 changed files with 276 additions and 387 deletions.
4 changes: 0 additions & 4 deletions modules/achievements/README.md

This file was deleted.

14 changes: 0 additions & 14 deletions modules/achievements/module.json

This file was deleted.

3 changes: 2 additions & 1 deletion modules/auth_email/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"tags": [
"core",
"auth",
"user"
"user",
"internal"
],
"authors": [
"rivet-gg",
Expand Down
3 changes: 2 additions & 1 deletion modules/auth_email_link/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"tags": [
"core",
"auth",
"user"
"user",
"internal"
],
"authors": [
"rivet-gg",
Expand Down
16 changes: 10 additions & 6 deletions modules/captcha/actors/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ export class Actor extends ActorBase<Input, State> {
throttle(_ctx: ActorContext, req: ThrottleRequest): ThrottleResponse {
const now = Date.now();

this.state.count += 1;

if (this.state.start === 0) {
this.reset(_ctx, {});
return { success: false };
}

if (now - this.state.start > req.period) {
this.state.start = now;
this.state.count = 1;
this.reset(_ctx, {});
return { success: true };
}

if (this.state.count >= req.requests) {
if (this.state.count > req.requests) {
return { success: false };
}

this.state.count += 1;

return { success: true };
}

reset(_ctx: ActorContext, req: Empty): Empty {
this.state.start = 0;
this.state.start = Date.now();
this.state.count = 0;

return {};
Expand Down
28 changes: 27 additions & 1 deletion modules/captcha/tests/e2e_guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,33 @@ test("e2e success and failure", async (ctx: TestContext) => {
}
}


// First should fail
assertEquals(true, await didFail(async () => {
await ctx.modules.captcha.guard({
type: "ip",
key: "aaaa",
requests: REQUESTS,
period: PERIOD,
captchaProvider
});
}));

// So we solve
assertEquals(false, await didFail(async () => {
await ctx.modules.captcha.guard({
type: "ip",
key: "aaaa",
requests: REQUESTS,
period: PERIOD,
captchaProvider,
captchaToken: "foo"
});
}))

// The next REQUESTS - 1 should succeed
assertEquals(false, await didFail(async () => {
for (let i = 0; i < REQUESTS; ++i) {
for (let i = 0; i < REQUESTS - 1; ++i) {
await ctx.modules.captcha.guard({
type: "ip",
key: "aaaa",
Expand All @@ -33,6 +58,7 @@ test("e2e success and failure", async (ctx: TestContext) => {
}
}));

// Afterwhich, it should fail
assertEquals(true, await didFail(async () => {
await ctx.modules.captcha.guard({
type: "ip",
Expand Down
4 changes: 3 additions & 1 deletion modules/captcha/utils/providers/hcaptcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const validateHCaptchaResponse = async (
const { success } = await result.json();

return success;
} catch {}
} catch (error) {
console.error("Failed to request hCaptcha /siteverify endpoint", error);
}

return false;
}
4 changes: 3 additions & 1 deletion modules/captcha/utils/providers/turnstile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const validateCFTurnstileResponse = async (
const { success } = await result.json();

return success;
} catch {}
} catch (error) {
console.error("Failed to request hCaptcha /siteverify endpoint", error);
}

return false;
}
3 changes: 0 additions & 3 deletions modules/eos/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions modules/eos/module.json

This file was deleted.

6 changes: 0 additions & 6 deletions modules/idem/README.md

This file was deleted.

16 changes: 0 additions & 16 deletions modules/idem/module.json

This file was deleted.

3 changes: 2 additions & 1 deletion modules/identities/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"tags": [
"core",
"user",
"auth"
"auth",
"internal"
],
"authors": [
"rivet-gg",
Expand Down
2 changes: 1 addition & 1 deletion modules/lobbies/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"list_regions": {
"name": "List Regions",
"description": "List available regions.",
"public": false
"public": true
},
"fetch_lobby_manager_state": {
"name": "Fetch Lobby Manager State",
Expand Down
8 changes: 6 additions & 2 deletions modules/lobbies/scripts/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface LobbyListEntry {
id: string;
version: string;
tags: Record<string, string>;
createdAt: string;
players: number;
maxPlayers: number;
maxPlayersDirect: number;
}

export async function run(
Expand Down Expand Up @@ -46,11 +50,11 @@ export async function run(
id: lobby.id,
version: lobby.version,
tags: lobby.tags,
createdAt: lobby.createdAt,
createdAt: new Date(lobby.createdAt).toISOString(),
players: lobby.players,
maxPlayers: lobby.maxPlayers,
maxPlayersDirect: lobby.maxPlayersDirect,
}));
} satisfies LobbyListEntry));

return { lobbies: lobbyList };
}
Loading

0 comments on commit dedc11c

Please sign in to comment.