From 6e80a914bf15689432e689c64799b74e1513c868 Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Tue, 3 Nov 2026 08:30:19 -0500 Subject: [PATCH 1/2] fix: ignore and in dashboard PATCH bodies --- .changeset/dashboard-patch-team-immutable.md | 5 ++ packages/api/src/controllers/dashboard.ts | 15 ++++- packages/api/src/models/dashboard.ts | 15 ++++- .../api/__tests__/dashboard.int.test.ts | 63 +++++++++++++++++++ packages/api/src/routers/api/dashboards.ts | 13 +++- 5 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 .changeset/dashboard-patch-team-immutable.md diff --git a/.changeset/dashboard-patch-team-immutable.md b/.changeset/dashboard-patch-team-immutable.md new file mode 100644 index 0000000000..bf1e156e16 --- /dev/null +++ b/.changeset/dashboard-patch-team-immutable.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/api': patch +--- + +fix: ignore `team` and `createdBy` in dashboard PATCH bodies so a dashboard can't be moved to another team or have its owner rewritten diff --git a/packages/api/src/controllers/dashboard.ts b/packages/api/src/controllers/dashboard.ts index 4518f54ca7..e5b1878f72 100644 --- a/packages/api/src/controllers/dashboard.ts +++ b/packages/api/src/controllers/dashboard.ts @@ -249,6 +249,19 @@ export async function updateDashboard( updates: Partial>, userId?: ObjectId, ) { + // Defense-in-depth: callers pass spread objects into the update doc below. + // A smuggled `team`/`createdBy` here would move the document between + // tenants / forge ownership (the model marks them immutable, but the + // write below reaches Mongo directly). + const { + team: _ignoredTeam, + createdBy: _ignoredCreatedBy, + ...safeUpdates + } = updates as Record; + if (_ignoredTeam !== undefined || _ignoredCreatedBy !== undefined) { + throw new Error('updateDashboard: team/createdBy are not updatable'); + } + const oldDashboard = await getDashboard(dashboardId, teamId); if (oldDashboard == null) { @@ -261,7 +274,7 @@ export async function updateDashboard( team: teamId, }, { - ...updates, + ...safeUpdates, tags: updates.tags && uniq(updates.tags), updatedBy: userId, }, diff --git a/packages/api/src/models/dashboard.ts b/packages/api/src/models/dashboard.ts index 6468b05d7b..34b859b6ea 100644 --- a/packages/api/src/models/dashboard.ts +++ b/packages/api/src/models/dashboard.ts @@ -25,7 +25,17 @@ export default mongoose.model( required: true, }, tiles: { type: mongoose.Schema.Types.Mixed, required: true }, - team: { type: mongoose.Schema.Types.ObjectId, ref: 'Team' }, + // Immutable: `team` pins a dashboard to its owning tenant. Client PATCH + // bodies are re-parsed against DashboardWithoutIdSchema (which declares + // no `team`), so a smuggled key never reaches the update doc — this is + // defense-in-depth for any other caller that spreads into an update. + // The provisioner sets it via $setOnInsert only, which immutability + // permits. + team: { + type: mongoose.Schema.Types.ObjectId, + ref: 'Team', + immutable: true, + }, tags: { type: [String], default: [], @@ -39,10 +49,13 @@ export default mongoose.model( required: false, }, containers: { type: mongoose.Schema.Types.Array, required: false }, + // Immutable: ownership attribution is set once at creation; updates + // may only change `updatedBy`. createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: false, + immutable: true, }, updatedBy: { type: mongoose.Schema.Types.ObjectId, diff --git a/packages/api/src/routers/api/__tests__/dashboard.int.test.ts b/packages/api/src/routers/api/__tests__/dashboard.int.test.ts index 8a6d185739..2abf2baf64 100644 --- a/packages/api/src/routers/api/__tests__/dashboard.int.test.ts +++ b/packages/api/src/routers/api/__tests__/dashboard.int.test.ts @@ -2638,4 +2638,67 @@ describe('dashboard router', () => { }); }); }); + + describe('PATCH /:id server-owned fields', () => { + // Security regression: `team` and `createdBy` are server-owned. A PATCH + // body that smuggled them used to move a dashboard into another team's + // namespace (cross-tenant injection) and forge ownership attribution — + // zod-express-middleware's validateRequest validates without replacing + // req.body, and the handler used to spread req.body into the update. + // Now the handler re-parses the body against + // DashboardWithoutIdSchema.partial(), which declares neither field. + it('ignores smuggled team and createdBy on PATCH', async () => { + const victimTeamId = new mongoose.Types.ObjectId(); + const victimUserId = new mongoose.Types.ObjectId(); + + const created = await agent + .post('/dashboards') + .send(MOCK_DASHBOARD) + .expect(200); + + const updated = await agent + .patch(`/dashboards/${created.body.id}`) + .send({ + name: 'Hijacked Dashboard', + team: victimTeamId.toString(), + createdBy: victimUserId.toString(), + }) + .expect(200); + + // API response must not echo the forged values + expect(updated.body.team).toBe(team._id.toString()); + + // Ground truth: the persisted document stays in the owning team + const stored = await Dashboard.findById(created.body.id).lean(); + expect(String(stored?.team)).toBe(team._id.toString()); + expect(String(stored?.createdBy)).toBe(user._id.toString()); + + // And the owner can still reach it (a real move would orphan it) + const list = await agent.get('/dashboards').expect(200); + const listedIds = list.body.map((d: { _id: string }) => d._id); + expect(listedIds).toContain(created.body.id); + }); + + // Same mechanism via a client that round-trips a full GET response: the + // extra keys ride along in the spread and must still be dropped. + it('drops server-owned keys from a round-tripped PATCH body', async () => { + const created = await agent + .post('/dashboards') + .send(MOCK_DASHBOARD) + .expect(200); + const before = await agent.get('/dashboards').expect(200); + const full = before.body.find( + (d: { _id: string }) => d._id === created.body.id, + ); + + await agent + .patch(`/dashboards/${created.body.id}`) + .send({ ...full, name: 'Updated Name' }) + .expect(200); + + const stored = await Dashboard.findById(created.body.id).lean(); + expect(String(stored?.team)).toBe(team._id.toString()); + expect(String(stored?.createdBy)).toBe(user._id.toString()); + }); + }); }); diff --git a/packages/api/src/routers/api/dashboards.ts b/packages/api/src/routers/api/dashboards.ts index 778fd4914d..544679ef92 100644 --- a/packages/api/src/routers/api/dashboards.ts +++ b/packages/api/src/routers/api/dashboards.ts @@ -212,9 +212,16 @@ router.patch( return res.sendStatus(404); } - // Only omit undefined values, keep null (which signals field removal) - // `provisioned` is server-owned — see the POST handler above. - const updates = _.omitBy(_.omit(req.body, 'provisioned'), _.isUndefined); + // Re-parse instead of omitting keys by name: DashboardWithoutIdSchema + // declares no `team`/`createdBy`/`provisioned`, so parsing drops them + // along with every other unknown key. All three are server-owned — + // `team`/`provisioned` for tenant isolation and the provisioner, and + // `createdBy` for ownership attribution (a smuggled `team` used to move + // a dashboard into another team's namespace; see the POST handler's + // comment for why validateRequest alone can't enforce this). + // Nulls survive: the schema's optional fields are `.nullable()`, and a + // null still signals field removal downstream. + const updates = DashboardWithoutIdSchema.partial().parse(req.body); const sourceError = await validateFilterSources( teamId, From 0c165631ddcf7524c2531a53747b4aae653249b3 Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Tue, 3 Nov 2026 08:38:58 -0500 Subject: [PATCH 2/2] fix: Drop createdBy from saved search PATCH requests --- .changeset/dashboard-patch-team-immutable.md | 2 +- packages/api/src/controllers/savedSearch.ts | 6 +++- packages/api/src/models/savedSearch.ts | 4 +++ .../api/__tests__/savedSearch.int.test.ts | 30 +++++++++++++++++++ packages/api/src/routers/api/savedSearch.ts | 16 +++++++--- 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/.changeset/dashboard-patch-team-immutable.md b/.changeset/dashboard-patch-team-immutable.md index bf1e156e16..2b7f5b6ab9 100644 --- a/.changeset/dashboard-patch-team-immutable.md +++ b/.changeset/dashboard-patch-team-immutable.md @@ -2,4 +2,4 @@ '@hyperdx/api': patch --- -fix: ignore `team` and `createdBy` in dashboard PATCH bodies so a dashboard can't be moved to another team or have its owner rewritten +fix: ignore `team` and `createdBy` in dashboard and saved search PATCH bodies so they can't be moved to another team or have their owner rewritten diff --git a/packages/api/src/controllers/savedSearch.ts b/packages/api/src/controllers/savedSearch.ts index ac66b1cb47..6222b46725 100644 --- a/packages/api/src/controllers/savedSearch.ts +++ b/packages/api/src/controllers/savedSearch.ts @@ -60,10 +60,14 @@ export function updateSavedSearch( savedSearch: SavedSearchWithoutId, userId?: string, ) { + // Callers may pass a merged stored document, which carries `createdBy`. + // Drop it so a spread can never rewrite ownership attribution. + const { createdBy: _createdBy, ...safeSavedSearch } = + savedSearch as SavedSearchWithoutId & { createdBy?: unknown }; return SavedSearch.findOneAndUpdate( { _id: savedSearchId, team: teamId }, { - ...savedSearch, + ...safeSavedSearch, team: teamId, updatedBy: userId, }, diff --git a/packages/api/src/models/savedSearch.ts b/packages/api/src/models/savedSearch.ts index afda2efcb8..54e49ea657 100644 --- a/packages/api/src/models/savedSearch.ts +++ b/packages/api/src/models/savedSearch.ts @@ -41,10 +41,12 @@ const savedSearchSchema = new Schema< ISavedSearchMethods >( { + // Immutable: `team` pins a saved search to its owning tenant. team: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Team', + immutable: true, }, name: String, @@ -59,10 +61,12 @@ const savedSearchSchema = new Schema< }, tags: [String], filters: [{ type: mongoose.Schema.Types.Mixed }], + // Immutable: ownership attribution is set once at creation. createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: false, + immutable: true, }, updatedBy: { type: mongoose.Schema.Types.ObjectId, diff --git a/packages/api/src/routers/api/__tests__/savedSearch.int.test.ts b/packages/api/src/routers/api/__tests__/savedSearch.int.test.ts index 6f0abfd317..6a51905ee5 100644 --- a/packages/api/src/routers/api/__tests__/savedSearch.int.test.ts +++ b/packages/api/src/routers/api/__tests__/savedSearch.int.test.ts @@ -189,6 +189,36 @@ describe('savedSearch router', () => { expect(dbRecord?.createdBy?.toString()).toBe(user._id.toString()); }); + it('ignores smuggled team and createdBy on PATCH', async () => { + const otherUser = await User.create({ + email: 'other@test.com', + name: 'Other User', + }); + const otherTeamId = '679b12d6cf282580fc63aad5'; + const created = await agent + .post('/saved-search') + .send(MOCK_SAVED_SEARCH) + .expect(200); + + await agent + .patch(`/saved-search/${created.body._id}`) + .send({ + name: 'updated name', + team: otherTeamId, + createdBy: otherUser._id.toString(), + }) + .expect(200); + + const dbRecord = await SavedSearch.findById(created.body._id); + expect(dbRecord?.name).toBe('updated name'); + expect(dbRecord?.team.toString()).toBe(team._id.toString()); + expect(dbRecord?.createdBy?.toString()).toBe(user._id.toString()); + + const savedSearches = await agent.get('/saved-search').expect(200); + const listed = savedSearches.body.find(s => s._id === created.body._id); + expect(listed.createdBy).toMatchObject({ email: user.email }); + }); + it('sets createdBy on alerts created from a saved search and populates it in list', async () => { // Create a saved search const savedSearch = await agent diff --git a/packages/api/src/routers/api/savedSearch.ts b/packages/api/src/routers/api/savedSearch.ts index 7ad0672133..867a0bf8be 100644 --- a/packages/api/src/routers/api/savedSearch.ts +++ b/packages/api/src/routers/api/savedSearch.ts @@ -19,6 +19,10 @@ import { objectIdSchema } from '@/utils/zod'; const router = express.Router(); +const updateSavedSearchBodySchema = SavedSearchSchema.partial().extend({ + name: z.string().trim().min(1).optional(), +}); + type SavedSearchListExpRes = express.Response; router.get('/', async (req, res: SavedSearchListExpRes, next) => { @@ -60,9 +64,7 @@ router.post( router.patch( '/:id', validateRequest({ - body: SavedSearchSchema.partial().extend({ - name: z.string().trim().min(1).optional(), - }), + body: updateSavedSearchBodySchema, params: z.object({ id: objectIdSchema, }), @@ -81,7 +83,13 @@ router.patch( return; } - const updates = _.omitBy(req.body, _.isNil); + // Re-parse: validateRequest validates without replacing req.body, so + // server-owned keys like `team`/`createdBy` would otherwise reach the + // update. Parsing drops every key the schema doesn't declare. + const updates = _.omitBy( + updateSavedSearchBodySchema.parse(req.body), + _.isNil, + ); const updatedSavedSearch = await updateSavedSearch( teamId.toString(),