-
Notifications
You must be signed in to change notification settings - Fork 0
🔒 fix: secure card API endpoint with NextAuth #372
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { getAuthenticatedUser } from "@/lib/apiUtils"; | ||
|
|
||
| vi.mock("@/lib/apiUtils", async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import("@/lib/apiUtils")>(); | ||
| return { | ||
| ...actual, | ||
| getAuthenticatedUser: vi.fn().mockResolvedValue({ username: "alice", token: "token" }), | ||
| }; | ||
| }); | ||
|
Comment on lines
+4
to
+10
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/app/api/card/[username]/route.test.ts
Line: 4-10
Comment:
**401 パスのテストが欠落している**
`getAuthenticatedUser` は常に認証済みユーザーを返すようにモックされていますが、`null` を返した場合(未認証)に `401 Unauthorized` が返されることを確認するテストケースがありません。この PR の主要な変更点であるにもかかわらず、そのパスがテストで検証されていない状態です。`vi.mocked(getAuthenticatedUser).mockResolvedValueOnce(null)` を使って未認証シナリオのテストを追加してください。
How can I resolve this? If you propose a fix, please make it concise. |
||
|
|
||
| vi.mock("@/lib/cardDataFetcher", () => ({ | ||
| fetchCardData: vi.fn(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,8 +2,9 @@ import { RateLimiter } from "@/lib/rateLimit"; | |||||||||||||||||
| import { fetchCardData } from "@/lib/cardDataFetcher"; | ||||||||||||||||||
| import { parseCardQueryParams, renderCardResponse, renderErrorCardResponse } from "@/lib/cardRenderer"; | ||||||||||||||||||
| import { getClientIp } from "@/lib/rateLimit"; | ||||||||||||||||||
| import { getAuthenticatedUser } from "@/lib/apiUtils"; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| export const runtime = "edge"; | ||||||||||||||||||
| const rateLimiter = new RateLimiter(50, 60 * 1000); // 50 requests per minute | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -15,6 +16,10 @@ export async function GET( | |||||||||||||||||
| { params }: { params: Promise<{ username: string }> } | ||||||||||||||||||
| ): Promise<Response> { | ||||||||||||||||||
| const { username } = await params; | ||||||||||||||||||
| const user = await getAuthenticatedUser(); | ||||||||||||||||||
| if (!user) { | ||||||||||||||||||
| return new Response("Unauthorized", { status: 401 }); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+19
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/app/api/card/[username]/route.ts
Line: 19-22
Comment:
**`user.token` が取得されているが一切使用されていない**
`getAuthenticatedUser()` は `{ username, token }` を返しますが、`token` はその後まったく利用されていません。`fetchCardData` は `process.env.GITHUB_TOKEN` を直接参照するため、認証済みユーザーの OAuth トークンが使われていない状態です。意図的であれば問題ありませんが、`_user` として命名しておくと「意図的に未使用」であることが明示されます。
```suggestion
const _user = await getAuthenticatedUser();
if (!_user) {
return new Response("Unauthorized", { status: 401 });
}
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||
| const url = new URL(request.url); | ||||||||||||||||||
| const options = parseCardQueryParams(url.searchParams); | ||||||||||||||||||
| const allowedOrigin = process.env.APP_URL || "http://localhost:3000"; | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the repository guidelines, mock implementations returning Promises should use async functions and have explicit return types to maintain type safety and readability.
References