Skip to content

feat: add comprehensive input validation for token operations (#1534)#1549

Open
NETIZEN-11 wants to merge 2 commits into
hyperledger-labs:mainfrom
NETIZEN-11:1534-validation
Open

feat: add comprehensive input validation for token operations (#1534)#1549
NETIZEN-11 wants to merge 2 commits into
hyperledger-labs:mainfrom
NETIZEN-11:1534-validation

Conversation

@NETIZEN-11
Copy link
Copy Markdown

@NETIZEN-11 NETIZEN-11 commented Apr 13, 2026

Summary

Implements comprehensive input validation for token operations (Issue, Transfer, Redeem) to ensure data integrity,
security, and consistency.

Changes

New Validation Package (token/services/validation/)

  • ValidateAmount() - validates token amounts (non-zero, within max limit)
  • ValidateAddress() - validates recipient addresses (non-empty, max 256 bytes)
  • ValidateTokenType() - validates token types (non-empty)
  • ValidateMetadata() - validates metadata fields (key presence, 10KB size limit)
  • ValidateTransferValues() - validates transfer values and owners arrays
  • ValidateRedeemValue() - validates redeem values

Error Types with Codes

  • InvalidAmountError
  • InvalidAddressError
  • InvalidMetadataError
  • InvalidTokenTypeError
  • ValidationError

Enhanced token/request.go

Issue() - Added:

  • Metadata size validation (10KB limit per value)

Transfer() - Added:

  • Token type validation (non-empty)
  • Index-specific zero value error messages
  • Owners/values length mismatch validation
  • All owners must be defined validation
  • Metadata size validation

Redeem() - Added:

  • Token type validation (non-empty)
  • Value zero validation
  • Metadata size validation

validateMetadata() - New helper function for metadata validation

Test Results

All unit tests pass:
PASS ok github.com/hyperledger-labs/fabric-token-sdk/token/services/validation
PASS ok github.com/hyperledger-labs/fabric-token-sdk/token

Acceptance Criteria Met

  • Validate token amounts (non-negative, within limits)
  • Sanitize and validate recipient addresses
  • Validate metadata fields (type, length, format)
  • Provide clear and specific error messages for validation failures
  • Add unit tests for all validation scenarios
  • Custom exceptions for validation failures

Related Issue

Fixes #1534

Test plan

  • Run unit tests: go test ./token/services/validation/...
  • Run request tests: go test ./token -run TestRequest
  • Build verification: go build ./token/...

@NETIZEN-11
Copy link
Copy Markdown
Author

@adecaro Would love a review on this. Thanks!

@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 29, 2026

Hi @NETIZEN-11 , sorry for this very late reply. I'm reviewing it now 🙏

Comment thread token/services/selector/sherdlock/fetcher.go
@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 29, 2026

Hi @NETIZEN-11 , I'm not sure the PR achieves the goals stated in the Issue. There are many validation functions but they are not used.
Some more general comments:

  • Typed errors are more than enough, no need for error codes.
  • When possible, use always constants instead of magic values.

Thanks for the effort 🙏

@NETIZEN-11 NETIZEN-11 force-pushed the 1534-validation branch 2 times, most recently from 1545d91 to b94f38b Compare May 2, 2026 04:04
- Add validation package with functions for amount, address, token type, metadata
- Add custom error types for validation failures
- Use validation in Issue, Transfer, and Redeem methods
- Fix test compilation errors in sherdlock
- Fix mutex unlock bug in cachedFetcher.update()

Signed-off-by: Nitesh <nitesh@example.com>
Signed-off-by: Nitesh Kumar <niteshkumar121411@gmail.com>
@NETIZEN-11 NETIZEN-11 requested a review from adecaro May 2, 2026 04:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add comprehensive input validation for token operations

2 participants