Skip to content

feat(crypto): add immutable crypto configuration validation (#1729) - #1839

Open
Paranoa-dev wants to merge 1 commit into
Stellar-Mail:mainfrom
Paranoa-dev:feat/crypto-config-validation
Open

feat(crypto): add immutable crypto configuration validation (#1729)#1839
Paranoa-dev wants to merge 1 commit into
Stellar-Mail:mainfrom
Paranoa-dev:feat/crypto-config-validation

Conversation

@Paranoa-dev

Copy link
Copy Markdown

Creates an immutable, validated crypto configuration object (CryptoConfig) that consolidates algorithm identifiers, size limits, key resolver availability, clock source, and runtime primitive checks into a single validated configuration. Invalid combinations fail before any crypto operation is served.

Problem

Algorithm identifiers, limits, and runtime capabilities are currently implicit in code and not validated as one configuration. Misconfigured deployments may fail only when a user attempts to send or open a message.

Solution

src/services/crypto/config.ts � Crypto Configuration Validation

New types and interfaces:

  • CryptoConfig â�� The full, immutable, frozen configuration object
  • CryptoConfigInput â�� Builder input with optional overrides
  • CryptoEnvironment â�� "development" | "production" deployment mode
  • CryptoLimits â�� Body and attachment size/count limits
  • CryptoPrimitives â�� Runtime primitive availability (subtleCrypto, getRandomValues)
  • KeyResolverConfig / ClockConfig â�� Availability metadata for key resolvers and clocks

Validation functions:

  • validateEnvironment() â�� Checks environment string is "development" or "production"
  • validateEnvelopeVersion() â�� Validates version against the suite registry
  • validateSuites() â�� Validates all suite names are registered and supported for the given version
  • validateLimits() â�� Enforces min/max bounds on all limit fields (finite numbers, integer attachments)
  • validatePrimitives() â�� Production requires crypto.subtle and crypto.getRandomValues; development allows test overrides
  • detectPrimitives() â�� Runtime detection of Web Crypto availability (frozen result)

Builder and singleton:

  • buildCryptoConfig(input?) â�� Builds and validates an immutable config. Returns valid: false with frozen errors array on failure. No secrets in errors.
  • getCryptoConfig() / resetCryptoConfig() â�� Cached singleton for the default (production) config

Key design decisions:

  • All output is Object.freeze()d â�� truly immutable after construction
  • Secret values (keys, tokens, credentials) are never stored or derivable from the configuration
  • Production requires real Web Crypto primitives; development mode allows injectable test overrides
  • Limits have explicit floor (MIN_LIMITS) and ceiling (MAX_LIMITS) values to prevent misconfiguration
  • Suites are cross-validated against both the registry and the envelope version

tests/unit/crypto/config.test.ts � 72 Tests

Comprehensive test coverage across:

  • Valid configurations (default, custom limits, explicit suites, partial overrides)
  • Immutability (frozen objects at every level)
  • Invalid configurations (unknown version, unregistered suites, out-of-bounds limits, non-finite values, NaN, Infinity)
  • Production primitive requirements (missing subtleCrypto / getRandomValues)
  • Secret leakage prevention (no high-entropy blobs, no secret markers in config or errors)
  • Individual validation functions (validateLimits, validateSuites, validateEnvelopeVersion, validatePrimitives, validateEnvironment)
  • Runtime detection (detectPrimitives)
  • Singleton caching (getCryptoConfig / resetCryptoConfig)
  • Integration: multiple invalid combinations fail before serving

Acceptance Criteria

  • Invalid combinations fail before serving crypto operations
  • Secret values are never included in validation errors
  • Development and production requirements are explicit
  • Tests cover valid and invalid configurations (72 tests, all passing)

Implementation Scope

  • Primary area: src/services/crypto/config.ts
  • Tests: tests/unit/crypto/config.test.ts
  • No changes to: API, Soroban contract, relay, UI, routing, or unrelated feature folders

Test Results

� tests/unit/crypto/config.test.ts (72 tests) � all passing

The pre-existing crypto test failures (crypto is not defined) are due to the vitest Node.js environment lacking globalThis.crypto and are unrelated to this change.

Closes #1729

…Mail#1729)

Create CryptoConfig for validating suites, limits, key resolvers, clocks,
and runtime primitives as one immutable configuration. Invalid combinations
now fail before any crypto operation is served. Secret values are never
included in validation errors. Production requires Web Crypto; development
allows test overrides.
@Paranoa-dev

Copy link
Copy Markdown
Author

Good day Maintainer
i would love to get some feedback from the pr i just pushed
the failed client checks are an embedded error with the repo and not part of the code i pushed

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.

Add crypto configuration validation at application startup

1 participant