Skip to content

Commit

Permalink
Feat: Malformed request get rejected gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
AvbrehtLuka committed Dec 5, 2024
1 parent 46c57a6 commit c5156f1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/dtos/generic/generic.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export enum AttestationResponseStatus {
* Attestation request is invalid.
*/
INVALID = 'INVALID',
/**
* Attestation request malformed.
*/
MALFORMED = 'MALFORMED',
/**
* Attestation request cannot be confirmed neither rejected by the verifier at the moment.
*/
Expand Down
5 changes: 2 additions & 3 deletions src/services/address-validity-verifier.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChainType } from '@flarenetwork/mcc';
import { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { IConfig } from '../config/configuration';
import {
Expand All @@ -9,8 +9,7 @@ import {
AttestationResponseDTO_AddressValidity_Response,
} from '../dtos/attestation-types/AddressValidity.dto';
import {
AttestationResponse,
AttestationResponseStatus,
AttestationResponseStatus
} from '../dtos/generic/generic.dto';
import { serializeBigInts } from '../external-libs/utils';
import { verifyAddressBTC } from '../verification/address-validity/address-validity-btc';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
BalanceDecreasingTransaction_Request,
BalanceDecreasingTransaction_Response,
} from '../dtos/attestation-types/BalanceDecreasingTransaction.dto';
import { AttestationResponse } from '../dtos/generic/generic.dto';
import { serializeBigInts } from '../external-libs/utils';
import {
BtcIndexerQueryManager,
Expand Down
53 changes: 34 additions & 19 deletions src/services/common/verifier-base.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChainType, ZERO_BYTES_32 } from '@flarenetwork/mcc';
import { HttpException, HttpStatus } from '@nestjs/common';
import { HttpException, HttpStatus, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
AttestationTypeOptions,
Expand All @@ -9,7 +9,10 @@ import {
} from '../../config/configuration';

import { EntityManager } from 'typeorm';
import { AttestationTypeBase_Request, AttestationTypeBase_Response } from '../../dtos/attestation-types/AttestationTypeBase.dto';
import {
AttestationTypeBase_Request,
AttestationTypeBase_Response,
} from '../../dtos/attestation-types/AttestationTypeBase.dto';
import {
AttestationResponse,
AttestationResponseEncoded,
Expand All @@ -35,13 +38,13 @@ interface IVerificationServiceConfig {
interface IVerificationServiceWithIndexerConfig
extends IVerificationServiceConfig {
indexerQueryManager:
| typeof DogeIndexerQueryManager
| typeof BtcIndexerQueryManager
| typeof XrpIndexerQueryManager;
| typeof DogeIndexerQueryManager
| typeof BtcIndexerQueryManager
| typeof XrpIndexerQueryManager;
}

export interface ITypeSpecificVerificationServiceConfig
extends Omit<IVerificationServiceWithIndexerConfig, 'attestationName'> { }
extends Omit<IVerificationServiceWithIndexerConfig, 'attestationName'> {}

export abstract class BaseVerifierService<
Req extends AttestationTypeBase_Request,
Expand Down Expand Up @@ -73,17 +76,20 @@ export abstract class BaseVerifierService<
if (
request.attestationType !== encodeAttestationName(this.attestationName) ||
request.sourceId !==
encodeAttestationName((this.isTestnet ? 'test' : '') + this.source)
encodeAttestationName((this.isTestnet ? 'test' : '') + this.source)
) {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
error: `Attestation type and source id combination not supported: (${request.attestationType
}, ${request.sourceId}). This source supports attestation type '${this.attestationName
}' (${encodeAttestationName(this.attestationName)}) and source id '${(this.isTestnet ? 'test' : '') + this.source
}' (${encodeAttestationName(
(this.isTestnet ? 'test' : '') + this.source,
)}).`,
error: `Attestation type and source id combination not supported: (${
request.attestationType
}, ${request.sourceId}). This source supports attestation type '${
this.attestationName
}' (${encodeAttestationName(this.attestationName)}) and source id '${
(this.isTestnet ? 'test' : '') + this.source
}' (${encodeAttestationName(
(this.isTestnet ? 'test' : '') + this.source,
)}).`,
},
HttpStatus.BAD_REQUEST,
);
Expand Down Expand Up @@ -112,12 +118,21 @@ export abstract class BaseVerifierService<
public async verifyEncodedRequestFDC(
abiEncodedRequest: string,
): Promise<AttestationResponseEncoded> {
const requestJSON = this.store.parseRequest<
{
messageIntegrityCode: string;
} & Req
>(abiEncodedRequest);
const response = await this.verifyRequestInternal(requestJSON);
let response: AttestationResponse<Res>;
try {
const requestJSON = this.store.parseRequest<
{
messageIntegrityCode: string;
} & Req
>(abiEncodedRequest);
response = await this.verifyRequestInternal(requestJSON);
} catch (error) {
Logger.debug(`Error parsing request: ${abiEncodedRequest}`);
Logger.error(`Error parsing request: ${error}`);
return {
status: AttestationResponseStatus.MALFORMED,
};
}
if (
response.status !== AttestationResponseStatus.VALID ||
!response.response
Expand Down

0 comments on commit c5156f1

Please sign in to comment.