Security and Data Exposure
Validation is a correctness boundary, not an authorization boundary. A valid request can still be forbidden, and an invalid request must never reach privileged code.
Server authority
Client rules and JSON Schema are advisory data. They tell a client what the server expects, but the server must run the validator again. Never treat a client-side pass as authorization or as proof that the payload is safe.
Error responses
ValidationError can carry a message, a message key, an error code, parameters, and custom state.
Treat all of it as data that may reach an untrusted caller.
- Keep
ValidationProblemOptions.IncludeErrorCodesandIncludeSeveritiesat their defaults (false) unless the client needs them. - Do not interpolate secrets, connection strings, internal identifiers, SQL, or raw user input into custom messages.
- Prefer stable message keys over free-form messages when the response is public.
- Remember that
ValidationError.CustomStateis not serialized by the ASP.NET Core adapter, but it is visible to in-process consumers. - Return the same shape for “not found” and “not authorized” where the distinction would leak information.
builder.Services.AddValidationEndpoints(options =>
{
options.IncludeErrorCodes = false;
options.IncludeSeverities = false;
options.Title = "Request validation failed";
});
Client rules
ClientRulesWriter exports validator names, member names, rule kinds, message keys, and parameters.
That is useful for forms and dangerous for internal models.
- Use
ClientRulesOptions.IncludedValidatorsas an allow-list; export only public request models. - Do not export validators for admin, billing, or internal-only endpoints.
- Conditions are exported as
hasCondition, not as predicates; do not assume the client can reproduce server logic. - If you use
MessageResolverto pre-resolve messages, confirm the text contains no internal data. - Version the payload with
SchemaVersionand reject unknown versions on the client.
var options = new ClientRulesOptions
{
SchemaVersion = "1.0"
};
options.IncludedValidators.Add(typeof(CreateOrderValidator).FullName!);
var json = ClientRulesWriter.WriteToString([new CreateOrderValidator()], options);
Schema and OpenAPI
JSON Schema and OpenAPI expose the shape and constraints of your contracts.
- Generate schemas only for public DTOs.
- Do not publish internal endpoints or admin-only models in the public OpenAPI document.
- Treat
x-validation-rulesand pattern constraints as public information. - Keep custom naming policies aligned with the transformer; a mismatch can silently omit constraints.
Regular expressions and custom predicates
Matches(pattern)compiles the pattern and applies a one-second timeout. A timeout propagates asRegexMatchTimeoutException; keep patterns bounded and handle it at the boundary if the input is untrusted.Matches(Regex)uses theRegexinstance you supply. If you pass a custom instance, supply a timeout and avoid catastrophic backtracking.Must,MustAsync,Custom, andCustomAsyncrun arbitrary code. Keep them deterministic, bounded, and cancellation-aware. A custom delegate exception becomes a validation failure whose message names the exception type; override the message if that type name is sensitive.- Expensive async rules (database or network calls) should be rate-limited and protected by the request cancellation token.
Multi-tenancy
Validators are stateless after configuration and are safe to reuse across requests. Tenant-specific
state must come from the instance or from ValidationContext:
- Use
context.Rootfor the root instance. - Use
context.RootDatafor a per-walk cache; it is pooled and not shared across calls. - Use
context.ServiceProviderfor scoped services supplied by the caller. - Never capture a tenant identifier,
HttpContext, or a scoped service in a singleton validator.
Logging
- Log the member path, message key, error code, and a correlation ID.
- Do not log the rejected value, the full request body, or message parameters that may contain PII.
- Separate 400 (validation) from 500 (server fault) metrics.
- Alert on
AsyncValidatorInvokedSynchronouslyException; it means an async validator reached a sync adapter.
Supply chain
- Pin package versions and review preview upgrades.
- Keep all Jeninnet.Validation packages on the same version.
- Restore from the official NuGet feed; verify the package owner and signature if your policy requires it.
- Run the AOT/trimming publish in CI for deployments that depend on it.