feat(security): implement Next.js Edge Middleware for admin routes - #1255
Merged
ayomideadeniran merged 1 commit intoSep 3, 2026
Merged
Conversation
|
@martinshub-tech Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
|
@martinshub-tech is attempting to deploy a commit to the Ayomide Adeniran's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@martinshub-tech PR under review — I will get back to you if I find any wrong implementations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1087
Description: This PR implements Next.js Edge Middleware to securely protect administrative routes, directly fulfilling the required technical specifications and edge deployment considerations.
Key Features & Implementation Details:
JWT & Session Validation: Validates JWT signatures and session nonces (x-nonce) directly on the Next.js Edge runtime using standard Web Crypto API (crypto.subtle.verify), with zero origin server overhead.
IP Allowlisting & Geo-fencing: Enforces region restrictions (ALLOWED_ADMIN_COUNTRIES) and IP whitelists (ALLOWED_ADMIN_IPS) for all paths matching /admin*.
Bot Detection & Rate Limiting: Introduces early request termination for automated malicious agents, paired with an efficient in-memory token-bucket mechanism deployed at the edge layer.
Secure Redirect Handling: Preserves return paths on unauthenticated requests and automatically validates the returnTo URLs against open-redirect vulnerabilities.
Testing Notes:
JWT_SECRET must be set in your .env to validate admin tokens locally.
Adjust ALLOWED_ADMIN_IPS (comma-separated list of IPs) to simulate the IP allowlist. By default, localhost traffic is permitted.
Run pnpm dev and visit /admin to verify that unauthorized requests successfully redirect to /login?returnTo=/admin