Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling and validation for posted personal info length #730

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion app/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
SPDX-License-Identifier: Apache-2.0
"""

from typing import Literal

from fastapi import status
from pydantic import BaseModel

from app.utils.contract_error_code import REVERT_CODE_MAP, error_code_msg

Expand All @@ -28,6 +31,15 @@ class AppError(Exception):
code_list: list[int] | None = None


class RecordErrorDetail(BaseModel):
row_num: int
error_reason: Literal["PersonalInfoExceedsSizeLimit"]


class InvalidUploadErrorDetail(BaseModel):
record_error_details: list[RecordErrorDetail]


################################################
# 400_BAD_REQUEST
################################################
Expand Down Expand Up @@ -88,11 +100,34 @@ class MultipleTokenTransferNotAllowedError(BadRequestError):


class OperationNotPermittedForOlderIssuers(BadRequestError):
"""An operation not permitted for older issuers"""
"""
An operation not permitted for older issuers
"""

code = 10


class PersonalInfoExceedsSizeLimit(BadRequestError):
"""
Personal info exceeds size limit
"""

code = 11


class BatchPersonalInfoRegistrationValidationError(BadRequestError):
"""
The uploaded list of batch personal info registration has invalid records
"""

code = 12
detail: InvalidUploadErrorDetail

def __init__(self, detail: InvalidUploadErrorDetail):
self.record_error_details = detail
super().__init__(detail)


class OperationNotAllowedStateError(BadRequestError):
"""
Error returned when server-side data is not ready to process the request
Expand Down
51 changes: 47 additions & 4 deletions app/routers/issuer/bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
SPDX-License-Identifier: Apache-2.0
"""

import json
import uuid
from datetime import UTC, datetime
from typing import Annotated, List, Optional, Sequence
Expand Down Expand Up @@ -49,12 +50,16 @@
from app.database import DBAsyncSession
from app.exceptions import (
AuthorizationError,
BatchPersonalInfoRegistrationValidationError,
ContractRevertError,
InvalidParameterError,
InvalidUploadErrorDetail,
MultipleTokenTransferNotAllowedError,
NonTransferableTokenError,
OperationNotAllowedStateError,
OperationNotSupportedVersionError,
PersonalInfoExceedsSizeLimit,
RecordErrorDetail,
SendTransactionError,
TokenNotExistError,
)
Expand Down Expand Up @@ -2519,6 +2524,7 @@ async def register_bond_token_holder_extra_info(
InvalidParameterError,
SendTransactionError,
ContractRevertError,
PersonalInfoExceedsSizeLimit,
),
)
async def register_bond_token_holder_personal_info(
Expand Down Expand Up @@ -2589,6 +2595,13 @@ async def register_bond_token_holder_personal_info(
await db.merge(_off_personal_info)
await db.commit()
else:
# Check the length of personal info content
if (
len(json.dumps(input_personal_info).encode("utf-8"))
> config.PERSONAL_INFO_MESSAGE_SIZE_LIMIT
):
raise PersonalInfoExceedsSizeLimit

token_contract = await IbetStraightBondContract(token_address).get()
try:
personal_info_contract = PersonalInfoContract(
Expand Down Expand Up @@ -2699,7 +2712,12 @@ async def list_all_bond_token_batch_personal_info_registration(
operation_id="InitiateBondTokenBatchPersonalInfoRegistration",
response_model=BatchRegisterPersonalInfoUploadResponse,
responses=get_routers_responses(
422, 401, 404, AuthorizationError, InvalidParameterError
422,
401,
404,
AuthorizationError,
InvalidParameterError,
BatchPersonalInfoRegistrationValidationError,
),
)
async def initiate_bond_token_batch_personal_info_registration(
Expand Down Expand Up @@ -2757,14 +2775,39 @@ async def initiate_bond_token_batch_personal_info_registration(
batch.status = BatchRegisterPersonalInfoUploadStatus.PENDING.value
db.add(batch)

for personal_info in personal_info_list:
errs = []
bulk_register_record_list = []

for i, personal_info in enumerate(personal_info_list):
bulk_register_record = BatchRegisterPersonalInfo()
bulk_register_record.upload_id = batch_id
bulk_register_record.token_address = token_address
bulk_register_record.account_address = personal_info.account_address
bulk_register_record.personal_info = personal_info.model_dump()
bulk_register_record.status = 0
db.add(bulk_register_record)
bulk_register_record.personal_info = personal_info.model_dump()

# Check the length of personal info content
if (
personal_info.data_source == PersonalInfoDataSource.ON_CHAIN
and len(json.dumps(bulk_register_record.personal_info).encode("utf-8"))
> config.PERSONAL_INFO_MESSAGE_SIZE_LIMIT
):
errs.append(
RecordErrorDetail(
row_num=i,
error_reason="PersonalInfoExceedsSizeLimit",
)
)

if not errs:
bulk_register_record_list.append(bulk_register_record)

if len(errs) > 0:
raise BatchPersonalInfoRegistrationValidationError(
detail=InvalidUploadErrorDetail(record_error_details=errs)
)

db.add_all(bulk_register_record_list)

await db.commit()

Expand Down
50 changes: 46 additions & 4 deletions app/routers/issuer/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
SPDX-License-Identifier: Apache-2.0
"""

import json
import uuid
from datetime import UTC, datetime
from decimal import Decimal
Expand Down Expand Up @@ -50,12 +51,16 @@
from app.database import DBAsyncSession
from app.exceptions import (
AuthorizationError,
BatchPersonalInfoRegistrationValidationError,
ContractRevertError,
InvalidParameterError,
InvalidUploadErrorDetail,
MultipleTokenTransferNotAllowedError,
NonTransferableTokenError,
OperationNotAllowedStateError,
OperationNotSupportedVersionError,
PersonalInfoExceedsSizeLimit,
RecordErrorDetail,
SendTransactionError,
TokenNotExistError,
)
Expand Down Expand Up @@ -2447,6 +2452,7 @@ async def register_share_token_holder_extra_info(
InvalidParameterError,
SendTransactionError,
ContractRevertError,
PersonalInfoExceedsSizeLimit,
),
)
async def register_share_token_holder_personal_info(
Expand Down Expand Up @@ -2517,6 +2523,13 @@ async def register_share_token_holder_personal_info(
await db.merge(_off_personal_info)
await db.commit()
else:
# Check the length of personal info content
if (
len(json.dumps(input_personal_info).encode("utf-8"))
> config.PERSONAL_INFO_MESSAGE_SIZE_LIMIT
):
raise PersonalInfoExceedsSizeLimit

token_contract = await IbetShareContract(token_address).get()
try:
personal_info_contract = PersonalInfoContract(
Expand Down Expand Up @@ -2627,7 +2640,12 @@ async def list_all_share_token_batch_personal_info_registration(
operation_id="InitiateShareTokenBatchPersonalInfoRegistration",
response_model=BatchRegisterPersonalInfoUploadResponse,
responses=get_routers_responses(
422, 401, 404, AuthorizationError, InvalidParameterError
422,
401,
404,
AuthorizationError,
InvalidParameterError,
BatchPersonalInfoRegistrationValidationError,
),
)
async def initiate_share_token_batch_personal_info_registration(
Expand Down Expand Up @@ -2685,14 +2703,38 @@ async def initiate_share_token_batch_personal_info_registration(
batch.status = BatchRegisterPersonalInfoUploadStatus.PENDING.value
db.add(batch)

for personal_info in personal_info_list:
errs = []
bulk_register_record_list = []

for i, personal_info in enumerate(personal_info_list):
bulk_register_record = BatchRegisterPersonalInfo()
bulk_register_record.upload_id = batch_id
bulk_register_record.token_address = token_address
bulk_register_record.account_address = personal_info.account_address
bulk_register_record.personal_info = personal_info.model_dump()
bulk_register_record.status = 0
db.add(bulk_register_record)
bulk_register_record.personal_info = personal_info.model_dump()

# Check the length of personal info content
if (
len(json.dumps(bulk_register_record.personal_info).encode("utf-8"))
> config.PERSONAL_INFO_MESSAGE_SIZE_LIMIT
):
errs.append(
RecordErrorDetail(
row_num=i,
error_reason="PersonalInfoExceedsSizeLimit",
)
)

if not errs:
bulk_register_record_list.append(bulk_register_record)

if len(errs) > 0:
raise BatchPersonalInfoRegistrationValidationError(
detail=InvalidUploadErrorDetail(record_error_details=errs)
)

db.add_all(bulk_register_record_list)

await db.commit()

Expand Down
6 changes: 6 additions & 0 deletions batch/processor_batch_register_personal_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ async def process(self):
await self.__sink_on_finish_register_process(
db_session=db_session, record_id=batch_data.id, status=2
)
except ValueError:
# for ValueError: Plaintext is too long
LOG.warning(f"Failed to send transaction: id=<{batch_data.id}>")
await self.__sink_on_finish_register_process(
db_session=db_session, record_id=batch_data.id, status=2
)
await db_session.commit()

error_registration_list = await self.__get_registration_data(
Expand Down
6 changes: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@
# Average block generation interval
EXPECTED_BLOCKS_PER_SEC = float(os.environ.get("EXPECTED_BLOCKS_PER_SEC", 0.1))

# Maximum message size for name registration for ibet PersonalInfo contract:
# ( key bit length / 8 ) - ( 2 * hash function output length + 2 ) = 1238
# key bit length: 10240
# hash function output length: 20
PERSONAL_INFO_MESSAGE_SIZE_LIMIT = int((10240 / 8) - (2 * 20 + 2))


####################################################
# Web3 settings
Expand Down
Loading