-
Notifications
You must be signed in to change notification settings - Fork 8
Secure backend data access with signed auth tokens and RBAC #48
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,30 @@ | ||
| const dotenv = require("dotenv"); | ||
| const { z } = require("zod"); | ||
| import dotenv from 'dotenv'; | ||
| import { z } from 'zod'; | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| const envSchema = z.object({ | ||
| VITE_SUPABASE_URL: z.string().url(), | ||
| VITE_SUPABASE_ANON_KEY: z.string().min(10), | ||
| PORT: z.string().optional() | ||
| PORT: z.string().optional(), | ||
| AUTH_TOKEN_SECRET: z.string().min(16).optional(), | ||
| AUTH_TOKEN_TTL_SECONDS: z.string().regex(/^\d+$/).optional(), | ||
| CORS_ALLOW_ORIGIN: z.string().optional(), | ||
| LOGIN_RATE_LIMIT_MAX_ATTEMPTS: z.string().regex(/^\d+$/).optional(), | ||
| LOGIN_RATE_LIMIT_WINDOW_MS: z.string().regex(/^\d+$/).optional(), | ||
| LOGIN_RATE_LIMIT_BLOCK_MS: z.string().regex(/^\d+$/).optional(), | ||
| }); | ||
|
|
||
| const result = envSchema.safeParse(process.env); | ||
|
|
||
| if (!result.success) { | ||
| console.error("\n❌ Invalid environment configuration:\n"); | ||
| console.error('\n❌ Invalid environment configuration:\n'); | ||
|
|
||
| result.error.errors.forEach((err) => { | ||
| console.error(`- ${err.path.join(".")}: ${err.message}`); | ||
| console.error(`- ${err.path.join('.')}: ${err.message}`); | ||
| }); | ||
|
|
||
| process.exit(1); // 🔥 FAIL FAST | ||
| process.exit(1); | ||
| } | ||
|
|
||
| module.exports = result.data; | ||
| export default result.data; |
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
Oops, something went wrong.
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.
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.
🟡 Admin-only endpoints return 403 instead of 401 for unauthenticated requests
The
GET /api/bills/:spotIdandDELETE /api/users/:userIdendpoints return HTTP 403 (Forbidden) when the request has no token or an invalid/expired token, instead of HTTP 401 (Unauthorized). Per HTTP semantics, 401 means "you are not authenticated" while 403 means "you are authenticated but lack permission." Returning 403 for unauthenticated requests conflates these two conditions.Root Cause and Impact
At
backend/server.js:339-345andbackend/server.js:350-354, the checkif (!authedUser || authedUser.role !== 'admin')collapses both the unauthenticated case (!authedUser) and the unauthorized case (role !== 'admin') into a single 403 response.Compare with the other protected endpoints like
GET /api/ordersatbackend/server.js:252-256andPOST /api/ordersatbackend/server.js:300-303, which correctly return 401 first for unauthenticated users, and only check authorization afterward.For example, a request with no
Authorizationheader toGET /api/bills/spot-1returns{"error": "Forbidden"}with status 403, when it should return{"error": "Unauthorized"}with status 401. This makes it harder for API clients to distinguish between "need to log in" vs "logged in but insufficient privileges," and breaks the pattern established by the other endpoints in this same PR.Was this helpful? React with 👍 or 👎 to provide feedback.