Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3294 bug embed cant handle unlimited api keys #3316

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions embed/__tests__/rateLimiter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ---- Testing libraries
import { jest, it, describe, expect, beforeEach } from "@jest/globals";

import { parseRateLimit } from "../src/rateLimiter.js";

jest.mock("../src/redis", () => {
return {
redis: {
call: async (...args: string[]): Promise<any> => Promise.resolve({}),
},
};
});

beforeEach(() => {
// CLear the spy stats
jest.clearAllMocks();
});

describe("parseRateLimit", function () {
it("parse non-empty valid string", async () => {
expect(parseRateLimit("1000/1m")).toEqual(1000);
expect(parseRateLimit("1000/1s")).toEqual(60000);
expect(parseRateLimit("60/1h")).toEqual(1);
expect(parseRateLimit("1440/1d")).toEqual(1);
expect(parseRateLimit("")).toEqual(Infinity);
expect(parseRateLimit(null)).toEqual(Infinity);
});

it("throws error when unsupported time unit is used", async () => {
expect(() => parseRateLimit("1000/1x")).toThrow(
new Error("Invalid rate limit spec format. Expected format: '<requests>/<time><unit> where unit is one of 'smhd'")
);
expect(() => parseRateLimit("abcd")).toThrow(
new Error("Invalid rate limit spec format. Expected format: '<requests>/<time><unit> where unit is one of 'smhd'")
);
});
});
11 changes: 9 additions & 2 deletions embed/src/rateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { RedisReply, RedisStore } from "rate-limit-redis";

import axios from "axios";

function parseRateLimit(rateLimitSpec: string): number {
export function parseRateLimit(rateLimitSpec: string | null): number {
if (rateLimitSpec === "" || rateLimitSpec === null) {
return Infinity;
}

// Regular expression to match the format "<requests>/<time>"
const regex = /^(\d+)\/(\d+)([smhd])$/; // Supports seconds (s), minutes (m), hours (h), and days (d)
const match = rateLimitSpec.match(regex);

if (!match) {
throw new Error("Invalid rate limit spec format. Expected format: '<requests>/<time><unit>'");
throw new Error(
"Invalid rate limit spec format. Expected format: '<requests>/<time><unit> where unit is one of 'smhd'"
);
}

const totalRequests = parseInt(match[1], 10); // e.g., 125
Expand Down Expand Up @@ -67,6 +73,7 @@ export async function apiKeyRateLimit(req: Request, res: Response): Promise<numb
return rateLimit;
}
} catch (err) {
console.error("error checking rate limit:", err);
res.status(500).send({ message: "Unauthorized! Unexpected error validating API key" });
throw "ERROR";
}
Expand Down
Loading