fix(api): secure featured hunt routes and use postgres datastore - #1040
Open
Devadakene wants to merge 3 commits into
Open
fix(api): secure featured hunt routes and use postgres datastore#1040Devadakene wants to merge 3 commits into
Devadakene wants to merge 3 commits into
Conversation
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 #858
Objective
This PR resolves a critical vulnerability and reliability issue with the "Featured Hunt" API routes. Previously, the
/api/admin/featuredand/api/admin/featured/rotateendpoints lacked proper authentication, allowing anonymous users to change the featured hunt. Additionally, the featured hunt state was being written to a local JSON file (featuredHuntServer.json) insideprocess.cwd(). This file-based approach silently failed in our serverless deployment environment because the filesystem is read-only, leading to state regressions upon every deployment.This PR enforces admin authorization across these endpoints and migrates the featured hunt state to a robust PostgreSQL datastore.
Changes Implemented
1. Enforced Admin Authentication
apps/web/app/api/admin/featured/route.ts,apps/web/app/api/admin/featured/rotate/route.tsassertAdminAuth(req)utility at the top of theGETandPOSThandlers for both the main featured route and the rotate route.401 Unauthorizedor403 Forbiddenresponse.2. Migrated State to PostgreSQL
apps/web/lib/featuredHuntDb.ts(and related migration scripts)app_settingstable under thefeatured_hunt_idkey.3. Improved Error Handling
apps/web/lib/featuredHuntDb.ts,apps/web/app/api/admin/featured/rotate/route.tslogger.errorswallow pattern. ThewriteFeaturedIdfunction now executes direct raw SQL queries viagetDb()and allows any database connection or query failures to throw exceptions naturally.withErrorHandlinghigher-order wrapper, converting them into standardizedHTTP 500 Internal Server Errorresponses with appropriate request IDs for tracing.4. Consistent Application to Rotate Endpoint
apps/web/app/api/admin/featured/rotate/route.tsreadFeaturedIdandwriteFeaturedIddatabase abstractions.Acceptance Criteria Validated
app_settingstable) instead of a file underprocess.cwd().logger.error./api/admin/featured/rotateendpoint.How to Test
POSTrequest to/api/admin/featuredand/api/admin/featured/rotatewithout valid admin credentials. Verify that the request is rejected.POSTrequest with ahuntIdto/api/admin/featured. Verify that the request succeeds and the new featured hunt ID is returned in a subsequentGETrequest.app_settingstable in your local Postgres database and verify that thefeatured_hunt_idkey exists and matches the ID you set./api/admin/featured/rotateendpoint as an admin. Verify that the featured hunt successfully advances to the next active seeded hunt, and that the database is updated accordingly.