Validationο
Robot validation and security checks.
Robot Validatorο
- class linkforge.core.RobotValidator(checks=None)[source]ο
Bases:
objectValidates robot structure for URDF export and simulation.
Runs a configurable registry of
ValidationCheckinstances. By default, all standard checks run in dependency order. Callers can pass a custom list to run only a specific subset of validation rules.Example
>>> from linkforge.core.models import Robot, Link >>> from linkforge.core import RobotValidator >>> robot = Robot(name="test_robot") >>> robot.add_link(Link(name="base_link")) >>> # Validating a robot >>> result = RobotValidator().validate(robot) >>> if result.is_valid: ... print("Robot is valid!") ... else: ... print(f"Found {result.error_count} errors")
- Parameters:
checks (
list[ValidationCheck] |None)
- DEFAULT_CHECKS: list[type[ValidationCheck]] = [<class 'linkforge.core.validation.checks.HasLinksCheck'>, <class 'linkforge.core.validation.checks.DuplicateNameCheck'>, <class 'linkforge.core.validation.checks.JointReferenceCheck'>, <class 'linkforge.core.validation.checks.TreeStructureCheck'>, <class 'linkforge.core.validation.checks.MassPropertiesCheck'>, <class 'linkforge.core.validation.checks.GeometryCheck'>, <class 'linkforge.core.validation.checks.Ros2ControlCheck'>, <class 'linkforge.core.validation.checks.MimicChainCheck'>, <class 'linkforge.core.validation.checks.SemanticCheck'>, <class 'linkforge.core.validation.checks.SemanticConsistencyCheck'>]ο
- __init__(checks=None)[source]ο
Initialize validator.
- Parameters:
checks (
list[ValidationCheck] |None) β Optional custom list of check instances to run. Defaults toDEFAULT_CHECKS(all standard checks).
Validation Resultο
- class linkforge.core.ValidationResult(issues=<factory>, robot_name='')[source]ο
Bases:
objectResult of robot validation.
- property is_valid: boolο
Check if validation passed (no errors).
Note: Warnings donβt block validity.
- add_error(title, message, affected_objects=None, suggestion=None, code=None, auto_fix=None)[source]ο
Add an error to the validation result.
Securityο
Security validation functions for robot model parsing.
This module provides security checks to prevent malicious robot description files from accessing unauthorized file system locations or causing other security issues.
- linkforge.core.validation.security.validate_mesh_path(mesh_filepath, source_directory, allow_absolute=False, sandbox_root=None)[source]ο
Validate that a mesh file path is safe to access.
This function prevents path traversal attacks by ensuring that mesh paths stay within the robot modelβs directory or its subdirectories.
Security Note: Absolute paths are discouraged for portability and security. Use allow_absolute=True only when loading trusted robot description files.
- Parameters:
mesh_filepath (
Path) β The mesh file path from the URDF (may be relative or absolute)source_directory (
Path) β The directory containing the robot model fileallow_absolute (
bool) β If True, allows absolute paths (default: False for security)sandbox_root (
Path|None) β The root directory for the sandbox. If None, source_directory is used. Access is restricted to files within this root and its subdirectories.
- Return type:
- Returns:
The validated absolute path to the mesh file
- Raises:
RobotSecurityError β If the mesh path attempts to escape the source directory
RobotSecurityError β If absolute paths are not allowed but one is provided
- linkforge.core.validation.security.is_suspicious_location(path)[source]ο
Check if a path resolves to a suspicious system location.
- linkforge.core.validation.security.validate_package_uri(uri)[source]ο
Validate a ROS package:// URI.
- Parameters:
uri (
str) β The package URI to validate (e.g., βpackage://my_robot/meshes/arm.stlβ)- Return type:
- Returns:
The validated URI
- Raises:
RobotSecurityError β If the URI is malformed or contains suspicious components
Usage Examplesο
Validate Robotο
from linkforge.core import RobotValidator
validator = RobotValidator()
result = validator.validate(robot)
if result.is_valid:
print(" Robot is valid!")
else:
print(" Validation errors:")
for error in result.errors:
print(f" - {error}")
print("\nWarnings:")
for warning in result.warnings:
print(f" - {warning}")
Security Checksο
from linkforge.core.validation import validate_mesh_path, find_sandbox_root
from linkforge.core import RobotSecurityError
from pathlib import Path
# Validate mesh path (prevents path traversal)
try:
validate_mesh_path(Path("../../etc/passwd"), Path("/tmp")) # Raises RobotSecurityError
except RobotSecurityError as e:
print(f"Security error: {e}")
# Valid paths within sandbox
urdf_dir = Path("/my_robot/urdf")
validate_mesh_path(Path("meshes/robot.stl"), urdf_dir) # OK
# Auto-detect sandbox root for sibling folder access
urdf_file = Path("/my_robot/urdf/robot.urdf")
sandbox = find_sandbox_root(urdf_file) # Returns /my_robot
validate_mesh_path(Path("../meshes/part.stl"), urdf_dir, sandbox_root=sandbox) # OK
Validation Checksο
The validator performs the following checks:
Structure Validationο
Robot has a name
At least one link exists
All links have unique names
All joints have unique names
Tree structure is valid (no cycles, single root)
Link Validationο
Links have inertial properties (mass > 0)
Inertia tensors are physically valid
Visual and collision geometries are valid
Joint Validationο
Parent and child links exist
Joint limits are valid (lower β€ upper)
Axis is non-zero for revolute/prismatic joints
Mimic joints reference existing joints
Sensor Validationο
Sensors are attached to existing links
Sensor-specific info is provided
Update rates are positive
Security Validationο
Mesh paths donβt escape the sandbox root
Sandbox root auto-detection for sibling folders
Numeric values are within safe ranges
XML depth is limited (prevents XML bombs)