Skip to content
Closed
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
14 changes: 8 additions & 6 deletions src/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import {
const GITHUB_API = "https://api.github.com";
const GITHUB_GRAPHQL = "https://api.github.com/graphql";


function getRateLimitReset(res: Response): number {
const resetHeader = res.headers.get("X-RateLimit-Reset");
return resetHeader ? parseInt(resetHeader, 10) : Math.floor(Date.now() / 1000) + 3600;

Choose a reason for hiding this comment

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

critical

The current implementation can lead to an unhandled exception. If the X-RateLimit-Reset header is present but contains a non-numeric string (e.g., an empty string or malformed value), parseInt will return NaN. This NaN value is then passed to the RateLimitError constructor, which will create an Invalid Date. Calling .toISOString() on an invalid date throws a RangeError, which will crash the application. It's crucial to handle this case by validating the result of parseInt.

  const resetTimestamp = parseInt(resetHeader || "", 10);
  return !Number.isNaN(resetTimestamp) ? resetTimestamp : Math.floor(Date.now() / 1000) + 3600;

}

function headers(token?: string): HeadersInit {
const h: HeadersInit = {
Accept: "application/vnd.github+json",
Expand All @@ -38,9 +44,7 @@ async function handleResponse<T>(res: Response): Promise<T> {
throw new UserNotFoundError("unknown");
}
if (res.status === 403) {
const resetHeader = res.headers.get("X-RateLimit-Reset");
const resetTimestamp = resetHeader ? parseInt(resetHeader, 10) : Math.floor(Date.now() / 1000) + 3600;
throw new RateLimitError(resetTimestamp);
throw new RateLimitError(getRateLimitReset(res));
}
if (!res.ok) {
const body = await res.text().catch(() => "Unknown error");
Expand All @@ -65,9 +69,7 @@ async function graphql<T>(query: string, token?: string, variables?: Record<stri
next: { revalidate: 300 },
});
if (res.status === 403) {
const resetHeader = res.headers.get("X-RateLimit-Reset");
const resetTimestamp = resetHeader ? parseInt(resetHeader, 10) : Math.floor(Date.now() / 1000) + 3600;
throw new RateLimitError(resetTimestamp);
throw new RateLimitError(getRateLimitReset(res));
}
if (!res.ok) {
const body = await res.text().catch(() => "Unknown error");
Expand Down
Loading