Skip to content

fix: require authentication for GET /api/newsletter stats endpoint (#351)#353

Open
Siddh2024 wants to merge 5 commits into
Darshan3690:mainfrom
Siddh2024:fix/issue-351-newsletter-auth
Open

fix: require authentication for GET /api/newsletter stats endpoint (#351)#353
Siddh2024 wants to merge 5 commits into
Darshan3690:mainfrom
Siddh2024:fix/issue-351-newsletter-auth

Conversation

@Siddh2024

Copy link
Copy Markdown
Contributor

Description

Fixes #351

The \GET /api/newsletter\ endpoint was exposing subscriber data (emails, names, dates, sources) without any authentication. Any visitor could enumerate the full subscriber list.

Changes

  • Added \�uth\ import from @clerk/nextjs/server\
  • Added authentication check at the start of the \GET\ handler
  • Returns \401 Unauthorized\ if no authenticated user

Security Impact

  • ✅ Subscriber data is now protected behind Clerk authentication
  • ✅ Only authenticated users can access subscriber statistics
  • ✅ Prevents unauthorized enumeration of the subscriber list

@vercel

vercel Bot commented May 31, 2026

Copy link
Copy Markdown

@Siddh2024 is attempting to deploy a commit to the Darshan Rajput's projects Team on Vercel.

A member of the Team first needs to authorize it.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cb1b80716e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +207 to +211
const { userId } = await auth();

if (!userId) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Restrict newsletter stats to admins

For any regular signed-in Clerk user, this check only verifies that auth() returned a userId, so GET /api/newsletter still returns recentSubscribers with subscriber emails/names/sources. Since this handler is marked admin-only and sign-up is public in the app, the sensitive subscriber list remains exposed to all authenticated users unless you also validate an admin role/metadata or move it behind an admin-only route.

Useful? React with 👍 / 👎.

@Darshan3690 Darshan3690 added level:advanced Hard technical work points 55 quality:clean Good quality code Multiplier ×1.2 quality:exceptional Excellent contribution ×1.5 type:refactor Bonus +10 type:performance Bonus +15 type:security Bonus +20 gssoc:approved Valid PR, points counted gssoc'26 This contribution/project is part of GirlScript Summer of Code 2026 mentor:Darshan3690 under gssoc'26 as mentor labels May 31, 2026
@vercel

vercel Bot commented May 31, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
the-dev-pocket-961a Error Error Jun 5, 2026 4:18am

Copilot AI left a comment

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@Darshan3690

Copy link
Copy Markdown
Owner

@Siddh2024
Thanks for catching this.

You're right — the current implementation only checks whether the request is authenticated (userId exists), but it doesn't verify that the user has admin privileges. Since the endpoint returns subscriber information, authenticated non-admin users could still access sensitive data.

Could you please update the implementation to enforce admin authorization (e.g., validate admin role/metadata or use our existing admin access helper) before returning newsletter stats?

Once that's addressed, I'll re-review and merge.

@Siddh2024

Copy link
Copy Markdown
Contributor Author

@Darshan3690 I've added the admin authorization check as requested. The GET handler now:

  1. Authenticates via �uth() — returns 401 if unauthenticated
  2. Fetches user metadata via clerkClient.users.getUser(userId)
  3. Checks admin role — returns 403 if publicMetadata.role !== admin

This ensures only users with the admin role can access newsletter subscriber data. The update is pushed to the ix/issue-351-newsletter-auth branch.

Copilot AI left a comment

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@Darshan3690

Copy link
Copy Markdown
Owner

resolve conflicts. @Siddh2024

@Siddh2024

Copy link
Copy Markdown
Contributor Author

resolve conflicts. @Siddh2024
@Darshan3690 , done .

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


// GET /api/newsletter/stats - Get newsletter statistics (admin only)
export async function GET(request: NextRequest) {
export async function GET(_request: NextRequest) {

// GET /api/newsletter/stats - Get newsletter statistics (admin only)
export async function GET(request: NextRequest) {
export async function GET(_request: NextRequest) {
@Darshan3690

Copy link
Copy Markdown
Owner

@Siddh2024 check copilot comment

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

Labels

gssoc:approved Valid PR, points counted gssoc'26 This contribution/project is part of GirlScript Summer of Code 2026 level:advanced Hard technical work points 55 mentor:Darshan3690 under gssoc'26 as mentor quality:clean Good quality code Multiplier ×1.2 quality:exceptional Excellent contribution ×1.5 type:performance Bonus +15 type:refactor Bonus +10 type:security Bonus +20

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GET /api/newsletter exposes all subscriber emails without authentication

3 participants