Skip to content

🔒 Fix missing authentication on card API endpoint#380

Open
is0692vs wants to merge 1 commit into
mainfrom
jules-11135183214603674343-22eec3bb
Open

🔒 Fix missing authentication on card API endpoint#380
is0692vs wants to merge 1 commit into
mainfrom
jules-11135183214603674343-22eec3bb

Conversation

@is0692vs

@is0692vs is0692vs commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

🎯 What: Added authentication checks to the src/app/api/card/[username]/route.ts API endpoint.

⚠️ Risk: The API endpoint was previously completely unauthenticated and accessible by anyone. This could be exploited to consume application resources, bypass intended access controls, or expose rate-limiting capacity to unauthorized actors (even though the underlying data fetched is public).

🛡️ Solution: Integrated the existing getAuthenticatedUser() utility to protect the route. If a user is not authenticated, the route now correctly returns a 401 Unauthorized status with a rendered error card image. Modified the route to remove the unsupported Edge runtime declaration (since authentication checks require Node APIs here) and updated the corresponding test suite.


PR created automatically by Jules for task 11135183214603674343 started by @is0692vs

Greptile Summary

このPRは /api/card/[username] エンドポイントに getAuthenticatedUser() による認証チェックを追加し、未認証リクエストに 401 を返すよう変更します。また、Node.js APIが必要なためEdgeランタイム宣言を削除し、テストスイートを更新しています。

  • 認証ガードの追加: 未認証時に 401 + エラーカード画像を返す処理を追加。他のダッシュボードAPIと同じ getAuthenticatedUser() を利用しており、パターン自体は既存コードと一致しています。
  • キャッシュ設定の問題: 401 レスポンスに public, s-maxage=60 が適用されており、CDN が 401 をキャッシュした場合、認証済みユーザーへのリクエストも同じキャッシュを受け取るリスクがあります。no-store への変更が必要です。
  • テスト結果ファイルのコミット: test-results/.last-run.json が失敗状態のまま新規追加されており、.gitignore への追加も必要です。

Confidence Score: 3/5

認証チェック自体の実装は正しいが、401 レスポンスのキャッシュ設定を修正せずにマージすると CDN キャッシュ汚染が発生する可能性があり、このままのマージは推奨しない。

401 レスポンスに public, s-maxage=60 が設定されていることで、CDN が認証失敗レスポンスをキャッシュし、認証済みユーザーにも返してしまうリスクがある。また、テスト結果ファイルが失敗状態でコミットされている点も信頼性を下げる要因となっている。

主に src/app/api/card/[username]/route.ts のキャッシュ設定と、test-results/.last-run.json の取り扱いに注意が必要。

Security Review

  • CDNキャッシュ汚染による認証バイパス (src/app/api/card/[username]/route.ts): 401 Unauthorized レスポンスに Cache-Control: public, s-maxage=60 が設定されており、CDN がこのレスポンスをキャッシュする場合がある。未認証リクエストが先に届いた場合、後続の認証済みリクエストにもキャッシュ済み 401 が返ることで、実質的にサービス拒否が発生しうる。no-store への変更で対処可能。

Important Files Changed

Filename Overview
src/app/api/card/[username]/route.ts getAuthenticatedUser() による認証チェックを追加し、edge ランタイム宣言を削除。ただし 401 レスポンスに public, s-maxage=60 キャッシュを適用しているため、CDN キャッシュ汚染のリスクあり。
src/app/api/card/[username]/route.test.ts 認証テストケースを追加し、既存テストに beforeEach で認証済みユーザーのモックを設定。テスト構造は適切だが、test-results/.last-run.json が失敗状態でコミットされている点が懸念。
test-results/.last-run.json テスト失敗状態("status": "failed")のファイルが新規追加されており、.gitignore にも含まれていないため不要なファイルが管理対象になっている。

Sequence Diagram

sequenceDiagram
    participant Client as クライアント
    participant CDN as CDN (Edge Network)
    participant Route as /api/card/[username]
    participant Auth as getAuthenticatedUser()
    participant Fetcher as fetchCardData()

    Client->>CDN: GET /api/card/alice
    CDN->>Route: キャッシュミス → フォワード

    Route->>Auth: セッション確認
    alt 未認証
        Auth-->>Route: null
        Route-->>CDN: "401 + Cache-Control: public, s-maxage=60"
        CDN-->>Client: 401 (キャッシュ保存)
        Note over CDN: 60秒間キャッシュ汚染
        Client->>CDN: GET /api/card/alice (認証済み)
        CDN-->>Client: キャッシュ済み401を返却
    else 認証済み
        Auth-->>Route: "{username, token}"
        Route->>Fetcher: fetchCardData(username)
        Fetcher-->>Route: CardData
        Route-->>CDN: "200 + Cache-Control: public, s-maxage=1800"
        CDN-->>Client: カード画像
    end
Loading
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
src/app/api/card/[username]/route.ts:24-34
**401レスポンスへのパブリックキャッシュ適用が危険**

`ERROR_CACHE = "public, s-maxage=60, stale-while-revalidate=120"` を 401 レスポンスにそのまま使用しています。Vercel の Edge Network など CDN は `s-maxage` が明示的に指定されている場合に 401 レスポンスをキャッシュする場合があります。認証していないリクエストが先に届くと、その CDN キャッシュが 60 秒間残り、同じ URL を後からリクエストした認証済みユーザーにも 401 が返ってしまいます。認証の成否はリクエストごとの Session 依存なので、401 には `no-store` または `private` を使うべきです。

```suggestion
    const user = await getAuthenticatedUser();
    if (!user) {
        return renderErrorCardResponse({
            message: "Unauthorized",
            options,
            status: 401,
            cacheControl: "no-store",
            fontUrl,
            allowedOrigin,
        });
    }
```

### Issue 2 of 2
test-results/.last-run.json:1-4
**テスト失敗状態のファイルがコミットされています**

`test-results/.last-run.json``"status": "failed"` はテスト実行が失敗した状態でコミットされていることを示しています。`failedTests` 配列は空ですが、テストランナー自体が正常に完了していない可能性があります。また、このファイルは `.gitignore` に含まれておらず、今後も生成のたびにコミット対象になる恐れがあります。`.gitignore``test-results/` を追加することを推奨します。

Reviews (1): Last reviewed commit: "🔒 Fix missing authentication on card AP..." | Re-trigger Greptile

Greptile also left 2 inline comments on this PR.

Context used:

  • Context used - 日本語で!!! (source)

Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel

vercel Bot commented Jun 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
github-user-summary Ignored Ignored Jun 12, 2026 7:22am

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@is0692vs, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 16 minutes and 3 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 75a5530d-b2eb-40e7-a17e-23a33126ad84

📥 Commits

Reviewing files that changed from the base of the PR and between a475ecb and e4c5661.

📒 Files selected for processing (3)
  • src/app/api/card/[username]/route.test.ts
  • src/app/api/card/[username]/route.ts
  • test-results/.last-run.json
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch jules-11135183214603674343-22eec3bb

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces authentication checks to the GET /api/card/[username] API route using getAuthenticatedUser, returning a 401 Unauthorized response if the user is not authenticated. It also adds corresponding unit tests to verify this behavior and updates existing tests to mock a successful authentication. Additionally, the Edge runtime configuration was removed from the route. Feedback indicates that a temporary test execution artifact (test-results/.last-run.json) was accidentally committed and should be removed and added to .gitignore.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +1 to +4
{
"status": "failed",
"failedTests": []
} No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The test-results/.last-run.json file is a temporary test execution artifact and should not be committed to the repository. Please remove this file and add test-results/ to your .gitignore file to prevent it from being tracked in the future.

@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment on lines +24 to +34
const user = await getAuthenticatedUser();
if (!user) {
return renderErrorCardResponse({
message: "Unauthorized",
options,
status: 401,
cacheControl: ERROR_CACHE,
fontUrl,
allowedOrigin,
});
}

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.

P1 security 401レスポンスへのパブリックキャッシュ適用が危険

ERROR_CACHE = "public, s-maxage=60, stale-while-revalidate=120" を 401 レスポンスにそのまま使用しています。Vercel の Edge Network など CDN は s-maxage が明示的に指定されている場合に 401 レスポンスをキャッシュする場合があります。認証していないリクエストが先に届くと、その CDN キャッシュが 60 秒間残り、同じ URL を後からリクエストした認証済みユーザーにも 401 が返ってしまいます。認証の成否はリクエストごとの Session 依存なので、401 には no-store または private を使うべきです。

Suggested change
const user = await getAuthenticatedUser();
if (!user) {
return renderErrorCardResponse({
message: "Unauthorized",
options,
status: 401,
cacheControl: ERROR_CACHE,
fontUrl,
allowedOrigin,
});
}
const user = await getAuthenticatedUser();
if (!user) {
return renderErrorCardResponse({
message: "Unauthorized",
options,
status: 401,
cacheControl: "no-store",
fontUrl,
allowedOrigin,
});
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/app/api/card/[username]/route.ts
Line: 24-34

Comment:
**401レスポンスへのパブリックキャッシュ適用が危険**

`ERROR_CACHE = "public, s-maxage=60, stale-while-revalidate=120"` を 401 レスポンスにそのまま使用しています。Vercel の Edge Network など CDN は `s-maxage` が明示的に指定されている場合に 401 レスポンスをキャッシュする場合があります。認証していないリクエストが先に届くと、その CDN キャッシュが 60 秒間残り、同じ URL を後からリクエストした認証済みユーザーにも 401 が返ってしまいます。認証の成否はリクエストごとの Session 依存なので、401 には `no-store` または `private` を使うべきです。

```suggestion
    const user = await getAuthenticatedUser();
    if (!user) {
        return renderErrorCardResponse({
            message: "Unauthorized",
            options,
            status: 401,
            cacheControl: "no-store",
            fontUrl,
            allowedOrigin,
        });
    }
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +1 to +4
{
"status": "failed",
"failedTests": []
} No newline at end of file

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.

P2 テスト失敗状態のファイルがコミットされています

test-results/.last-run.json"status": "failed" はテスト実行が失敗した状態でコミットされていることを示しています。failedTests 配列は空ですが、テストランナー自体が正常に完了していない可能性があります。また、このファイルは .gitignore に含まれておらず、今後も生成のたびにコミット対象になる恐れがあります。.gitignoretest-results/ を追加することを推奨します。

Prompt To Fix With AI
This is a comment left during a code review.
Path: test-results/.last-run.json
Line: 1-4

Comment:
**テスト失敗状態のファイルがコミットされています**

`test-results/.last-run.json``"status": "failed"` はテスト実行が失敗した状態でコミットされていることを示しています。`failedTests` 配列は空ですが、テストランナー自体が正常に完了していない可能性があります。また、このファイルは `.gitignore` に含まれておらず、今後も生成のたびにコミット対象になる恐れがあります。`.gitignore``test-results/` を追加することを推奨します。

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant