feat: Bound metadata byte length and reject invalid encodings (Closes #1065) - #1081
Conversation
Add visible-ASCII validation to settlement and registry contracts to prevent non-encoding-safe strings from being persisted on-chain. Settlement contract: - Add callora-validators dependency - Add InvalidEncoding error variant (code 34) - Create validation module with require_valid_offering_id, require_valid_price, and require_valid_message helpers - Apply validation to set_price (offering_id, price) and broadcast (message) entry points - 15 unit tests for validation module Registry contract: - Add callora-validators dependency - Add InvalidEncoding error variant (code 10) - Enhance validate_offering_id and validate_metadata to reject non-visible-ASCII, leading/trailing whitespace, and empty strings - 13 integration tests for encoding validation Validation policy (consistent with callora-validators): - Non-empty - Within byte-length bounds (64 for offering IDs, 32 for prices, 256 for metadata/messages) - All bytes in visible ASCII range (0x20..=0x7E) - No leading or trailing whitespace Closes CalloraOrg#1065 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
|
Hey @sheyman546! 👋 It looks like this PR isn't linked to any issue. If this PR is for one of the issues assigned to you as part of a Wave, please link it to ensure your contribution is tracked properly. You can do this by adding a keyword to the PR description (e.g.,
|
|
Thanks for the substantive implementation. Before this can be merged, please add a GitHub-recognized issue-closing reference in the PR description, such as |
|
Please link this pull request to the issue it addresses by adding a closing reference such as in the PR description. Once linked, we can verify it against the intended issue. |
|
Please link this pull request to the issue it addresses by adding a closing reference such as Closes # in the PR description. Once linked, we can verify it against the intended issue. |
What it fixes
Value-moving contracts accept arbitrary
Stringparameters for offering IDs, prices, metadata, and broadcast messages without validating byte length or encoding. This allows non-visible-ASCII bytes (control characters, DEL, zero-width characters) to be persisted on-chain, which can:Root cause
The
settlementcontract'sset_priceandbroadcastfunctions, and theregistrycontract'sregister_offeringfunction, acceptStringparameters with only minimal or no validation:set_price: No validation onoffering_idorpricestrings — any bytes acceptedbroadcast: No validation onmessagestring — any bytes acceptedvalidate_metadata: Only checks empty/length, not encodingvalidate_offering_id: Only checks empty/length, not encodingThe
callora_validatorscrate already providesis_visible_ascii_metadataandnormalize_visible_asciifunctions that enforce the correct policy, but neither the settlement nor registry contracts use them.The fix
Settlement contract
callora-validatorsdependency toCargo.tomlInvalidEncodingerror variant (code 34) toSettlementErrorvalidation.rsmodule with three validation helpers:require_valid_offering_id— enforces 1..=64 bytes, visible ASCII, no leading/trailing whitespacerequire_valid_price— enforces 1..=32 bytes, visible ASCII, no leading/trailing whitespacerequire_valid_message— enforces 1..=256 bytes, visible ASCII, no leading/trailing whitespaceset_priceandbroadcastentry points viaunwrap_or_else(|e| env.panic_with_error(e))Registry contract
callora-validatorsdependency toCargo.tomlInvalidEncodingerror variant (code 10) toRegistryErrorvalidate_offering_idto callis_visible_ascii_metadataafter length checkvalidate_metadatato callis_visible_ascii_metadataafter length checkValidation policy (consistent with
callora_validators)Why this approach
callora_validatorsInvalidEncodingis sufficient for caller branchingWhat could break
InvalidEncodinginstead of silently persisting. This is the intended behavior — any existing non-ASCII data was already problematic for off-chain systems.SettlementError, so existing error-code branches are unaffected.RegistryError, so existing error-code branches are unaffected.How it was tested
Compilation verification
Both
callora-settlementandcallora-registrycompile successfully withcargo check. Fullcargo testwas blocked by a Windows toolchain issue (dlltool.exenot found forgetrandom/windows-sys), but the code logic is verified through:Test coverage
Settlement validation tests (15):
Registry encoding tests (13):
Follow-up worth filing separately
callora-validatorsto remaining contracts —checkpoint(Symbol metadata),hot(Symbol action tags),freeze(Symbol reason) could benefit from similar validationdlltool.exemissing preventscargo teston Windows; consider adding MinGW/LLVM to the dev environmentcargo fmt --checkreports ~40 files with formatting differences; a separate formatting-only PR would clean these up