Skip to content
Merged
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
6 changes: 6 additions & 0 deletions apps/api/src/pkg/keys/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,13 @@ export class KeyService {
duration: data.key.ratelimitDuration,
};
}

for (const r of req.ratelimits ?? []) {
if (r.name === "default" && "default" in ratelimits) {
// it's already added above
continue;
}

if (typeof r.limit !== "undefined" && typeof r.duration !== "undefined") {
ratelimits[r.name] = {
identity: data.identity?.id ?? data.key.id,
Expand Down
38 changes: 38 additions & 0 deletions apps/api/src/routes/v1_keys_verifyKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,44 @@ describe("with ratelimit override", () => {
});
});

describe("with default ratelimit", () => {
test("uses the on-key defined settings", { timeout: 20000 }, async (t) => {
const h = await IntegrationHarness.init(t);
const key = new KeyV1({ prefix: "test", byteLength: 16 }).toString();
await h.db.primary.insert(schema.keys).values({
id: newId("test"),
keyAuthId: h.resources.userKeyAuth.id,
hash: await sha256(key),
start: key.slice(0, 8),
workspaceId: h.resources.userWorkspace.id,
createdAt: new Date(),
ratelimitLimit: 10,
ratelimitDuration: 60_000,
ratelimitAsync: false,
});

const res = await h.post<V1KeysVerifyKeyRequest, V1KeysVerifyKeyResponse>({
url: "/v1/keys.verifyKey",
headers: {
"Content-Type": "application/json",
},
body: {
key,
apiId: h.resources.userApi.id,
ratelimits: [
{
name: "default",
},
],
},
});
expect(res.status, `expected 200, received: ${JSON.stringify(res, null, 2)}`).toBe(200);
expect(res.body.valid).toBe(true);
expect(res.body.ratelimit).toBeDefined();
expect(res.body.ratelimit!.limit).toEqual(10);
});
});

describe("with ratelimit", () => {
describe("with valid key", () => {
test.skip(
Expand Down