Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix(backend): enforce ownership on user history and private User fields (#6409)

Mir Arif Hasan9cc980bc4feb
diff --git a/packages/hoppscotch-backend/src/mock-server/mock-server.resolver.ts b/packages/hoppscotch-backend/src/mock-server/mock-server.resolver.ts
index 423e814e6..cba6f3081 100644
--- a/packages/hoppscotch-backend/src/mock-server/mock-server.resolver.ts
+++ b/packages/hoppscotch-backend/src/mock-server/mock-server.resolver.ts
@@ -48,8 +48,8 @@ export class MockServerResolver {
if (E.isLeft(creator)) throwErr(creator.left);
return {
...creator.right,
- currentGQLSession: JSON.stringify(creator.right.currentGQLSession),
- currentRESTSession: JSON.stringify(creator.right.currentRESTSession),
+ currentGQLSession: null,
+ currentRESTSession: null,
};
}
diff --git a/packages/hoppscotch-backend/src/published-docs/published-docs.service.spec.ts b/packages/hoppscotch-backend/src/published-docs/published-docs.service.spec.ts
index 1b1680235..3b43b6fe1 100644
--- a/packages/hoppscotch-backend/src/published-docs/published-docs.service.spec.ts
+++ b/packages/hoppscotch-backend/src/published-docs/published-docs.service.spec.ts
@@ -856,8 +856,8 @@ describe('getPublishedDocsCreator', () => {
const expectedUser = {
...user,
- currentGQLSession: JSON.stringify(user.currentGQLSession),
- currentRESTSession: JSON.stringify(user.currentRESTSession),
+ currentGQLSession: null,
+ currentRESTSession: null,
};
expect(result).toEqualRight(expectedUser);
diff --git a/packages/hoppscotch-backend/src/published-docs/published-docs.service.ts b/packages/hoppscotch-backend/src/published-docs/published-docs.service.ts
index d84e2399b..14b1da51b 100644
--- a/packages/hoppscotch-backend/src/published-docs/published-docs.service.ts
+++ b/packages/hoppscotch-backend/src/published-docs/published-docs.service.ts
@@ -253,8 +253,8 @@ export class PublishedDocsService {
const creator = user
? {
...user,
- currentGQLSession: JSON.stringify(user.currentGQLSession),
- currentRESTSession: JSON.stringify(user.currentRESTSession),
+ currentGQLSession: null,
+ currentRESTSession: null,
}
: null;
diff --git a/packages/hoppscotch-backend/src/team-invitation/team-invitation.resolver.ts b/packages/hoppscotch-backend/src/team-invitation/team-invitation.resolver.ts
index b9f21a8b2..5520cec88 100644
--- a/packages/hoppscotch-backend/src/team-invitation/team-invitation.resolver.ts
+++ b/packages/hoppscotch-backend/src/team-invitation/team-invitation.resolver.ts
@@ -65,8 +65,8 @@ export class TeamInvitationResolver {
return {
...user.value,
- currentGQLSession: JSON.stringify(user.value.currentGQLSession),
- currentRESTSession: JSON.stringify(user.value.currentRESTSession),
+ currentGQLSession: null,
+ currentRESTSession: null,
};
}
diff --git a/packages/hoppscotch-backend/src/team/team-member.resolver.ts b/packages/hoppscotch-backend/src/team/team-member.resolver.ts
index c8b9b2207..595c9de80 100644
--- a/packages/hoppscotch-backend/src/team/team-member.resolver.ts
+++ b/packages/hoppscotch-backend/src/team/team-member.resolver.ts
@@ -17,8 +17,8 @@ export class TeamMemberResolver {
return {
...member.value,
- currentRESTSession: JSON.stringify(member.value.currentRESTSession),
- currentGQLSession: JSON.stringify(member.value.currentGQLSession),
+ currentRESTSession: null,
+ currentGQLSession: null,
};
}
}
diff --git a/packages/hoppscotch-backend/src/user-environment/user.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user.resolver.ts
index c1d8d31d4..d55637f3a 100644
--- a/packages/hoppscotch-backend/src/user-environment/user.resolver.ts
+++ b/packages/hoppscotch-backend/src/user-environment/user.resolver.ts
@@ -1,9 +1,13 @@
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
+import { UseGuards } from '@nestjs/common';
import { User } from 'src/user/user.model';
import { UserEnvironment } from './user-environments.model';
import { UserEnvironmentsService } from './user-environments.service';
import * as E from 'fp-ts/Either';
import { throwErr } from '../utils';
+import { GqlAuthGuard } from '../guards/gql-auth.guard';
+import { GqlUser } from '../decorators/gql-user.decorator';
+import { USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS } from '../errors';
@Resolver(() => User)
export class UserEnvsUserResolver {
@@ -12,16 +16,26 @@ export class UserEnvsUserResolver {
@ResolveField(() => [UserEnvironment], {
description: 'Returns a list of users personal environments',
})
- async environments(@Parent() user: User): Promise<UserEnvironment[]> {
+ @UseGuards(GqlAuthGuard)
+ async environments(
+ @Parent() user: User,
+ @GqlUser() requestingUser: User,
+ ): Promise<UserEnvironment[]> {
+ if (requestingUser?.uid !== user.uid) return [];
return await this.userEnvironmentsService.fetchUserEnvironments(user.uid);
}
@ResolveField(() => UserEnvironment, {
description: 'Returns the users global environments',
})
+ @UseGuards(GqlAuthGuard)
async globalEnvironments(
@Parent() user: User,
+ @GqlUser() requestingUser: User,
): Promise<UserEnvironment | string> {
+ if (requestingUser?.uid !== user.uid) {
+ throwErr(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
+ }
const userEnvironment =
await this.userEnvironmentsService.fetchUserGlobalEnvironment(user.uid);
if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left);
diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts
index c9ccba49e..bf27ff43c 100644
--- a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts
+++ b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts
@@ -369,6 +369,53 @@ describe('UserHistoryService', () => {
userHistory,
);
});
+ test('Should scope the lookup and the update to the requesting user (ownership enforcement)', async () => {
+ const executedOn = new Date();
+
+ mockPrisma.userHistory.findFirst.mockResolvedValueOnce({
+ userUid: 'abc',
+ id: '1',
+ request: [{}],
+ responseMetadata: [{}],
+ reqType: ReqType.REST,
+ executedOn,
+ isStarred: false,
+ });
+ mockPrisma.userHistory.update.mockResolvedValueOnce({
+ userUid: 'abc',
+ id: '1',
+ request: [{}],
+ responseMetadata: [{}],
+ reqType: ReqType.REST,
+ executedOn,
+ isStarred: true,
+ });
+
+ await userHistoryService.toggleHistoryStarStatus('abc', '1');
+
+ expect(mockPrisma.userHistory.findFirst).toHaveBeenCalledWith({
+ where: { id: '1', userUid: 'abc' },
+ });
+ expect(mockPrisma.userHistory.update).toHaveBeenCalledWith({
+ where: { id: '1', userUid: 'abc' },
+ data: { isStarred: true },
+ });
+ });
+ test('Should resolve left when the history entry is not owned by the requesting user', async () => {
+ // Scoped lookup finds nothing because the entry belongs to another user
+ mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null);
+
+ const result = await userHistoryService.toggleHistoryStarStatus(
+ 'attacker',
+ '1',
+ );
+
+ expect(result).toEqualLeft(USER_HISTORY_NOT_FOUND);
+ // The lookup must be scoped to the requesting user's uid
+ expect(mockPrisma.userHistory.findFirst).toHaveBeenCalledWith({
+ where: { id: '1', userUid: 'attacker' },
+ });
+ });
});
describe('removeRequestFromHistory', () => {
test('Should resolve right and delete request from users history', async () => {
@@ -433,6 +480,38 @@ describe('UserHistoryService', () => {
userHistory,
);
});
+ test('Should scope the delete to the requesting user (ownership enforcement)', async () => {
+ mockPrisma.userHistory.delete.mockResolvedValueOnce({
+ userUid: 'abc',
+ id: '1',
+ request: [{}],
+ responseMetadata: [{}],
+ reqType: ReqType.REST,
+ executedOn: date,
+ isStarred: false,
+ });
+
+ await userHistoryService.removeRequestFromHistory('abc', '1');
+
+ expect(mockPrisma.userHistory.delete).toHaveBeenCalledWith({
+ where: { id: '1', userUid: 'abc' },
+ });
+ });
+ test('Should resolve left when deleting a history entry not owned by the requesting user', async () => {
+ // Prisma throws when no row matches the user-scoped where clause
+ mockPrisma.userHistory.delete.mockRejectedValueOnce(new Error('P2025'));
+
+ const result = await userHistoryService.removeRequestFromHistory(
+ 'attacker',
+ '1',
+ );
+
+ expect(result).toEqualLeft(USER_HISTORY_NOT_FOUND);
+ // The delete must be scoped to the requesting user's uid
+ expect(mockPrisma.userHistory.delete).toHaveBeenCalledWith({
+ where: { id: '1', userUid: 'attacker' },
+ });
+ });
});
describe('deleteAllUserHistory', () => {
test('Should resolve right and delete all user REST history for a request type', async () => {
diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.ts
index 19d65ae4f..c16978db6 100644
--- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts
+++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts
@@ -99,7 +99,7 @@ export class UserHistoryService {
* @returns an Either of updated `UserHistory` or Error
*/
async toggleHistoryStarStatus(uid: string, id: string) {
- const userHistory = await this.fetchUserHistoryByID(id);
+ const userHistory = await this.fetchUserHistoryByID(id, uid);
if (O.isNone(userHistory)) {
return E.left(USER_HISTORY_NOT_FOUND);
}
@@ -108,6 +108,7 @@ export class UserHistoryService {
const updatedHistory = await this.prisma.userHistory.update({
where: {
id: id,
+ userUid: uid,
},
data: {
isStarred: !userHistory.value.isStarred,
@@ -142,6 +143,7 @@ export class UserHistoryService {
const delUserHistory = await this.prisma.userHistory.delete({
where: {
id: id,
+ userUid: uid,
},
});
@@ -205,14 +207,16 @@ export class UserHistoryService {
}
/**
- * Fetch a user history based on history ID.
+ * Fetch a user history based on history ID, scoped to its owner.
* @param id User History ID
- * @returns an `UserHistory` object
+ * @param uid UID of the user the history entry must belong to
+ * @returns an `UserHistory` object owned by the given user, or `O.none`
*/
- async fetchUserHistoryByID(id: string) {
+ async fetchUserHistoryByID(id: string, uid: string) {
const userHistory = await this.prisma.userHistory.findFirst({
where: {
id: id,
+ userUid: uid,
},
});
if (userHistory == null) return O.none;
diff --git a/packages/hoppscotch-backend/src/user-history/user.resolver.ts b/packages/hoppscotch-backend/src/user-history/user.resolver.ts
index b6758d159..3c4648143 100644
--- a/packages/hoppscotch-backend/src/user-history/user.resolver.ts
+++ b/packages/hoppscotch-backend/src/user-history/user.resolver.ts
@@ -1,9 +1,12 @@
import { Args, Parent, ResolveField, Resolver } from '@nestjs/graphql';
+import { UseGuards } from '@nestjs/common';
import { User } from '../user/user.model';
import { UserHistoryService } from './user-history.service';
import { UserHistory } from './user-history.model';
import { ReqType } from 'src/types/RequestTypes';
import { PaginationArgs } from '../types/input-types.args';
+import { GqlAuthGuard } from '../guards/gql-auth.guard';
+import { GqlUser } from '../decorators/gql-user.decorator';
@Resolver(() => User)
export class UserHistoryUserResolver {
@@ -12,10 +15,13 @@ export class UserHistoryUserResolver {
@ResolveField(() => [UserHistory], {
description: 'Returns a users REST history',
})
+ @UseGuards(GqlAuthGuard)
async RESTHistory(
@Parent() user: User,
+ @GqlUser() requestingUser: User,
@Args() args: PaginationArgs,
): Promise<UserHistory[]> {
+ if (requestingUser?.uid !== user.uid) return [];
return await this.userHistoryService.fetchUserHistory(
user.uid,
args.take,
@@ -25,10 +31,13 @@ export class UserHistoryUserResolver {
@ResolveField(() => [UserHistory], {
description: 'Returns a users GraphQL history',
})
+ @UseGuards(GqlAuthGuard)
async GQLHistory(
@Parent() user: User,
+ @GqlUser() requestingUser: User,
@Args() args: PaginationArgs,
): Promise<UserHistory[]> {
+ if (requestingUser?.uid !== user.uid) return [];
return await this.userHistoryService.fetchUserHistory(
user.uid,
args.take,
diff --git a/packages/hoppscotch-backend/src/user-settings/user.resolver.ts b/packages/hoppscotch-backend/src/user-settings/user.resolver.ts
index b5f63de6e..d191e03cf 100644
--- a/packages/hoppscotch-backend/src/user-settings/user.resolver.ts
+++ b/packages/hoppscotch-backend/src/user-settings/user.resolver.ts
@@ -1,9 +1,13 @@
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
+import { UseGuards } from '@nestjs/common';
import { User } from 'src/user/user.model';
import { UserSettings } from './user-settings.model';
import { UserSettingsService } from './user-settings.service';
import * as E from 'fp-ts/Either';
import { throwErr } from 'src/utils';
+import { GqlAuthGuard } from '../guards/gql-auth.guard';
+import { GqlUser } from '../decorators/gql-user.decorator';
+import { USER_SETTINGS_NOT_FOUND } from '../errors';
@Resolver(() => User)
export class UserSettingsUserResolver {
@@ -12,7 +16,9 @@ export class UserSettingsUserResolver {
@ResolveField(() => UserSettings, {
description: 'Returns user settings',
})
- async settings(@Parent() user: User) {
+ @UseGuards(GqlAuthGuard)
+ async settings(@Parent() user: User, @GqlUser() requestingUser: User) {
+ if (requestingUser?.uid !== user.uid) throwErr(USER_SETTINGS_NOT_FOUND);
const userSettings = await this.userSettingsService.fetchUserSettings(user);
if (E.isLeft(userSettings)) throwErr(userSettings.left);