Security
Use this page when a Java reader accepts bytes from outside the application's trust boundary. Fory reconstructs application values; it does not authenticate the sender, protect transport integrity, or decide whether a valid value is authorized for a business operation.
Application boundary
Before deserialization:
- Authenticate the sender and protect message integrity at the transport or storage layer.
- Enforce request or file size, timeout, and concurrency limits outside Fory.
- Register only the application types the endpoint accepts and configure the reader before its first root operation.
- Validate the deserialized value against application authorization and domain rules before use.
Built-in safeguards
Keep class registration enabled for production and any untrusted payload source:
Fory fory = Fory.builder()
.requireClassRegistration(true)
.withMaxDepth(50)
.withMaxGraphMemoryBytes(128L * 1024 * 1024)
.withMaxUnbackedContainerItems(8192)
.build();
Security-related options:
requireClassRegistration(true)restricts deserialization to registered classes.withMaxDepth(...)rejects unexpectedly deep object graphs.withMaxGraphMemoryBytes(...)sets an approximate gate for materialized graph memory during one root deserialization. The estimate mainly covers collections, maps, arrays, structs, and objects; Fory core primitive arrays and primitive lists count their primitive storage from the decoded length. It skips leaf values such as strings, primitive scalars, and dedicated binary values that do not use a primitive-array serializer. Actual process memory can be higher than this limit. Leaf values remain protected by byte-availability checks: if the unread input does not contain enough bytes, Fory will not read or create that leaf value. The default is a fixed128 MiB; set a positive byte limit when trusted workloads need a larger or smaller gate.withMaxUnbackedContainerItems(...)limits count-driven collection and map work whose repeated read bodies do not consume proportional input. The default is8192; zero is a strict limit.withMaxTypeFields(...)andwithMaxTypeMetaBytes(...)bound the field count and encoded body size of one received remote metadata body.withMaxSchemaVersionsPerType(...)andwithMaxAverageSchemaVersionsPerType(...)bound accepted remote metadata versions without changing registration, dynamic loading, or schema-evolution semantics.withDeserializeUnknownClass(false)avoids materializing unknown classes from metadata.checkJdkClassSerializable(true)keeps the JDK serializability check forjava.*classes.- Class registration warnings can be useful during security audits; use
suppressClassRegistrationWarnings(false)when you need to surface unexpected types.
Use requireClassRegistration(false) only for trusted payloads, and pair it with a TypeChecker
allow list when dynamic class loading is required.
Verification
Add negative tests for the boundary as well as normal round trips. Verify that the configured reader rejects unexpected application types, excessive nesting, resource-limit violations, and malformed input. After a failed read, verify that a valid root can still be read with the same Fory instance.
See Configuration for the complete option reference and Type Registration for the Fory registration API.