fix(api): allow creating and editing Sources in Local App Mode#2602
fix(api): allow creating and editing Sources in Local App Mode#2602brandon-pereira wants to merge 1 commit into
Conversation
In Local App Mode the auth middleware injects a plain string team id onto req.user instead of a Mongoose ObjectId. The sources POST/PUT handlers called teamId.toJSON(), which only exists on ObjectId, throwing 'TypeError: teamId.toJSON is not a function' and returning HTTP 500 when saving a Source. Use teamId.toString(), which works for both string and ObjectId team ids, and add regression tests covering the string-team-id path. Fixes HDX-4713 Co-authored-by: Brandon Pereira <brandon-pereira@users.noreply.github.com>
🦋 Changeset detectedLatest commit: fda1316 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
E2E Test Results✅ All tests passed • 224 passed • 3 skipped • 1494s
Tests ran across 4 shards in parallel. |
🟡 Tier 3 — StandardIntroduces new logic, modifies core functionality, or touches areas with non-trivial risk. Why this tier:
Review process: Full human review — logic, architecture, edge cases. Stats
|
Deep Review✅ No critical issues found. The core change ( 🟡 P2 -- recommended
🔵 P3 nitpicks (3)
Reviewers (7): correctness, security, api-contract, testing, maintainability, project-standards, kieran-typescript. Testing gaps:
|
Summary
Since
@hyperdx/app@v2.22.0, creating or editing a Source from Team Settings → Data → Sources fails withHTTP 500 "Failed to update source"when running HyperDX in Local App Mode (IS_LOCAL_APP_MODE="DANGEROUSLY_is_local_app_mode💀").Root cause: In Local App Mode, the auth middleware (
isUserAuthenticated) injects a plain string team id ("_local_team_") ontoreq.user.teaminstead of a MongooseObjectId. ThePOST /sourcesandPUT /sources/:idhandlers built the request body withteam: teamId.toJSON()..toJSON()only exists onObjectId, not on strings, so the handler threw:GET /sourceswas unaffected because it already usedteamId.toString(), which is why listing sources worked but saving them did not.Fix: Use
teamId.toString()in both the create and update handlers. It returns the correct value for both astringteam id (Local App Mode) and anObjectId(normal auth). Theteamvalue in the body is also overridden downstream by the controllers, so this is safe.How to test on Vercel preview
N/A — non-UI change (API-only bug fix).
Testing
Added two regression tests in
packages/api/src/routers/api/__tests__/sources.test.tsthat mount the sources router with a middleware emulating the Local App Mode string team id and exercisePOST /andPUT /:id.teamId.toJSON()code, both tests fail with the exact error from the issue (TypeError: teamId.toJSON is not a function).References
Linear Issue: HDX-4713