| title | Type Registration & Security |
|---|---|
| sidebar_position | 3 |
| id | type_registration |
| license | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
This page covers type registration mechanisms and security configurations.
In strict mode, only registered types can be deserialized. This prevents arbitrary code execution:
import pyfory
# Strict mode (recommended for production)
f = pyfory.Fory(strict=True)
class SafeClass:
def __init__(self, data):
self.data = data
# Must register types in strict mode
f.register(SafeClass, typename="com.example.SafeClass")
# Now serialization works
obj = SafeClass("safe data")
data = f.serialize(obj)
result = f.deserialize(data)
# Unregistered types will raise an exception
class UnsafeClass:
pass
# This will fail in strict mode
try:
f.serialize(UnsafeClass())
except Exception as e:
print("Security protection activated!")fory.register(MyClass, type_id=100)fory.register(MyClass, typename="com.example.MyClass")fory.register(MyClass, type_id=100, serializer=MySerializer(fory, MyClass))type_id = 100
for model_class in [User, Order, Product, Invoice]:
fory.register(model_class, type_id=type_id)
type_id += 1# Always use strict=True in production
fory = pyfory.Fory(strict=True)
# Explicitly register allowed types
fory.register(UserModel, type_id=100)
fory.register(OrderModel, type_id=101)strict=False, Fory will deserialize arbitrary types, which can pose security risks if data comes from untrusted sources. Only use strict=False in controlled environments where you trust the data source completely.
# Only in trusted environments
fory = pyfory.Fory(xlang=False, ref=True, strict=False)If you do need to use strict=False, configure a DeserializationPolicy:
from pyfory import DeserializationPolicy
class SafePolicy(DeserializationPolicy):
def validate_class(self, cls, is_local, **kwargs):
# Block dangerous modules
if cls.__module__ in {'subprocess', 'os', '__builtin__'}:
raise ValueError(f"Blocked: {cls}")
return None
fory = pyfory.Fory(xlang=False, strict=False, policy=SafePolicy())Limit deserialization depth to prevent stack overflow attacks:
fory = pyfory.Fory(
strict=True,
max_depth=100 # Adjust based on your data structure depth
)- Always use
strict=Truein production - Use
type_idfor performance,typenamefor flexibility - Register all types upfront before any serialization
- Set appropriate
max_depthbased on your data structures - Use
DeserializationPolicywhenstrict=Falseis necessary
- Configuration - Fory parameters
- Security - DeserializationPolicy details
- Custom Serializers - Custom serialization