Skip to content

Metadata Validation#4

Merged
amackillop merged 3 commits intomainfrom
austin/mdk-197/metadata-validation
Dec 22, 2025
Merged

Metadata Validation#4
amackillop merged 3 commits intomainfrom
austin/mdk-197/metadata-validation

Conversation

@amackillop
Copy link
Contributor

@amackillop amackillop commented Dec 22, 2025

Implements validation for checkout metadata.

Validation Rules

The validation enforces the following security constraints:

  • Key count limit: Maximum 50 keys per metadata object
  • Key format: Only alphanumeric characters, underscores, and hyphens allowed
  • Key length: Maximum 100 characters per key
  • Value encoding: Must be valid UTF-8
  • Control characters: Blocks null bytes and problematic control characters (allows tab, newline, carriage return)
  • Size limit: Total serialized JSON size must not exceed 1KB

Benefits

  • Prevents injection attacks via null bytes and control characters
  • Limits data size to prevent DoS attacks
  • Ensures data integrity with UTF-8 validation

Introduces a generic Result<T, E> discriminated union type for
type-safe error handling without exceptions. This pattern allows
functions to return either success (ok: true, value: T) or failure
(ok: false, error: E) in a way that TypeScript can narrow.

Helper functions ok() and err() simplify creating Result instances.
Implements comprehensive validation for checkout metadata to prevent
security issues before database persistence. Validates key count, key
format/length, value encoding, control characters, and total serialized
size.

Validation collects all errors and reports them together, making it
easier for users to fix multiple issues. Error messages include the
specific key name for value-related errors.
@amackillop amackillop force-pushed the austin/mdk-197/metadata-validation branch from edf9f99 to 736f3ae Compare December 22, 2025 16:51
@amackillop amackillop force-pushed the austin/mdk-197/metadata-validation branch from 736f3ae to 01d1159 Compare December 22, 2025 16:52
* Returns all validation errors found, allowing users to fix multiple issues at once.
*
* @param metadata - The metadata object to validate, or undefined
* @returns A Result containing either success (ok: true) or an array of validation errors (ok: false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or an array of validation errors

yeah, good call.

the key is set by our user, but the value can come from the user’s visitor, so abuse might not be the user’s fault. we need to tell them exactly which metadata entry failed and why, so they can surface a clear error to the visitor and handle it cleanly, instead of things breaking with no explanation.

on the mdk-checkout side, we should add strongly typed errors to createCheckout so our users can reliably tell what went wrong and respond accordingly (unrelated to this issue, but right now it may fail because they didn't set the apiKey / mnemonic OR because the metadata validation failed OR because VSS is down. we need to tell them what's going on. right now it just fails silently)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'm a big fan of modelling the error domain.

Copy link
Contributor

@martinsaposnic martinsaposnic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love the haskellish Result

@amackillop amackillop merged commit d91dc50 into main Dec 22, 2025
2 checks passed
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.

2 participants