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
20 changes: 18 additions & 2 deletions apps/api/src/pkg/keys/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,19 @@ export class KeyService {
>
> {
try {
const res = await this._verifyKey(c, req);
let res = await this._verifyKey(c, req);
let remainingRetries = 3;
while (res.err && remainingRetries > 0) {
remainingRetries--;
this.logger.warn("retrying verification", {
remainingRetries,
previousError: res.err.message,
});

await this.cache.keyByHash.remove(await this.hash(req.key));
res = await this._verifyKey(c, req);
}

Comment on lines +145 to +157
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Redundant Retry Mechanisms in verifyKey and _verifyKey

The verifyKey method introduces a retry loop that attempts to call _verifyKey up to three times if an error occurs. However, the _verifyKey method already includes its own retry logic when fetching key data from the cache and database using the retry function.

This redundancy could lead to unnecessary duplication of retries, increasing the overall number of attempts and potentially prolonging response times. Consider consolidating the retry logic to a single location to improve efficiency. You might handle retries either in verifyKey or within _verifyKey, but not both.

if (res.err) {
this.metrics.emit({
metric: "metric.key.verification",
Expand Down Expand Up @@ -209,7 +221,7 @@ export class KeyService {
async () =>
this.cache.keyByHash.swr(keyHash, async (hash) => {
const dbStart = performance.now();
const dbRes = await this.db.readonly.query.keys.findFirst({
const query = this.db.readonly.query.keys.findFirst({
where: (table, { and, eq, isNull }) =>
and(eq(table.hash, hash), isNull(table.deletedAt)),
with: {
Expand Down Expand Up @@ -257,10 +269,14 @@ export class KeyService {
},
},
});

const dbRes = await query;
this.metrics.emit({
metric: "metric.db.read",
query: "getKeyAndApiByHash",
latency: performance.now() - dbStart,
dbRes: JSON.stringify(dbRes),
sql: query.toSQL().sql,
Comment on lines +278 to +279
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Potential Sensitive Data Exposure in Logs

Logging the entire database response (dbRes) and the raw SQL query (sql) may expose sensitive information, including personal data or security-related details. This could pose a security risk if logs are accessed by unauthorized parties.

Consider sanitizing or limiting the data being logged to exclude sensitive information. Instead of logging the full dbRes, log only non-sensitive attributes necessary for debugging. Similarly, ensure that the logged SQL query does not contain sensitive parameters. Here's a suggested change:

this.metrics.emit({
  metric: "metric.db.read",
  query: "getKeyAndApiByHash",
  latency: performance.now() - dbStart,
- dbRes: JSON.stringify(dbRes),
- sql: query.toSQL().sql,
+ resultSize: dbRes ? 1 : 0,
+ queryInfo: { /* include non-sensitive query details if necessary */ },
});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dbRes: JSON.stringify(dbRes),
sql: query.toSQL().sql,
resultSize: dbRes ? 1 : 0,
queryInfo: { /* include non-sensitive query details if necessary */ },

});
if (!dbRes?.keyAuth?.api) {
return null;
Expand Down
2 changes: 2 additions & 0 deletions internal/metrics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export const metricSchema = z.discriminatedUnion("metric", [
metric: z.literal("metric.db.read"),
query: z.enum(["getKeyAndApiByHash", "loadFromOrigin", "getKeysByKeyAuthId"]),
latency: z.number(),
dbRes: z.string().optional(),
sql: z.string().optional(),
}),
z.object({
metric: z.literal("metric.ratelimit"),
Expand Down