Skip to content

Commit 16f24a6

Browse files
committed
feat: validate assignment id
1 parent 7fbdbd8 commit 16f24a6

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

packages/apps/fortune/exchange-oracle/server/src/common/guards/signature.auth.spec.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { faker } from '@faker-js/faker';
22
import { ChainId, EscrowUtils } from '@human-protocol/sdk';
3-
import { ExecutionContext } from '@nestjs/common';
3+
import { ExecutionContext, HttpException } from '@nestjs/common';
44
import { Reflector } from '@nestjs/core';
55
import { Test, TestingModule } from '@nestjs/testing';
66

@@ -110,8 +110,9 @@ describe('SignatureAuthGuard', () => {
110110
reflector.get = jest.fn().mockReturnValue([AuthSignatureRole.Worker]);
111111

112112
mockRequest.headers[HEADER_SIGNATURE_KEY] = 'validSignature';
113+
const assignmentId = faker.number.int();
113114
mockRequest.body = {
114-
assignment_id: '1',
115+
assignment_id: assignmentId,
115116
};
116117
(verifySignature as jest.Mock).mockReturnValue(true);
117118
assignmentRepository.findOneById.mockResolvedValue({
@@ -120,10 +121,10 @@ describe('SignatureAuthGuard', () => {
120121

121122
const result = await guard.canActivate(context);
122123
expect(result).toBeTruthy();
123-
expect(assignmentRepository.findOneById).toHaveBeenCalledWith('1');
124+
expect(assignmentRepository.findOneById).toHaveBeenCalledWith(assignmentId);
124125
});
125126

126-
it('should throw AuthError if assignment is not found for Worker role', async () => {
127+
it('should throw BadRequest error if assignment id is not number', async () => {
127128
reflector.get = jest.fn().mockReturnValue([AuthSignatureRole.Worker]);
128129

129130
mockRequest.headers[HEADER_SIGNATURE_KEY] = 'validSignature';
@@ -133,6 +134,21 @@ describe('SignatureAuthGuard', () => {
133134
(verifySignature as jest.Mock).mockReturnValue(true);
134135
assignmentRepository.findOneById.mockResolvedValue(null);
135136

137+
const resultPromise = guard.canActivate(context);
138+
await expect(resultPromise).rejects.toBeInstanceOf(HttpException);
139+
await expect(resultPromise).rejects.toThrow('Invalid assignment id');
140+
});
141+
142+
it('should throw AuthError if assignment is not found for Worker role', async () => {
143+
reflector.get = jest.fn().mockReturnValue([AuthSignatureRole.Worker]);
144+
145+
mockRequest.headers[HEADER_SIGNATURE_KEY] = 'validSignature';
146+
mockRequest.body = {
147+
assignment_id: 1,
148+
};
149+
(verifySignature as jest.Mock).mockReturnValue(true);
150+
assignmentRepository.findOneById.mockResolvedValue(null);
151+
136152
const resultPromise = guard.canActivate(context);
137153
await expect(resultPromise).rejects.toBeInstanceOf(ValidationError);
138154
await expect(resultPromise).rejects.toThrow(ErrorAssignment.NotFound);

packages/apps/fortune/exchange-oracle/server/src/common/guards/signature.auth.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export class SignatureAuthGuard implements CanActivate {
4141
const oracleAdresses: string[] = [];
4242

4343
if (roles.includes(AuthSignatureRole.Worker)) {
44+
if (!Number.isInteger(data.assignment_id)) {
45+
throw new HttpException('Invalid assignment id', HttpStatus.BAD_REQUEST);
46+
}
47+
4448
const assignment = await this.assignmentRepository.findOneById(
4549
data.assignment_id,
4650
);

0 commit comments

Comments
 (0)