# YAML Deserialization via yaml.load()

Language: Python
Severity: Critical
CWE: CWE-502

## Source
8

## Flow
8-9

## Sink
9

## Vulnerable Code
```python
import yaml
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/iot/device/provision', methods=['POST'])
def provision_iot_device():
    device_manifest = request.data.decode('utf-8')
    device_config = yaml.load(device_manifest, Loader=yaml.Loader)
    device_id = device_config.get('device_id', 'unknown')
    firmware_ver = device_config.get('firmware_version', '1.0.0')
    telemetry_interval = device_config.get('telemetry_interval', 60)
    print(f"Provisioning device {device_id} with firmware {firmware_ver}")
    return jsonify({"status": "provisioned", "device_id": device_id, "interval": telemetry_interval})
```

## Explanation

The application uses yaml.load() with yaml.Loader to deserialize untrusted YAML data from HTTP POST requests. This allows arbitrary Python object instantiation, enabling remote code execution through specially crafted YAML payloads that exploit Python's object serialization mechanisms.

## Remediation

The fix replaces yaml.load() with yaml.safe_load(), which only deserializes standard YAML tags (strings, numbers, lists, dicts) and prevents instantiation of arbitrary Python objects. An additional type check ensures the parsed result is a dictionary before accessing keys, providing defense against unexpected input formats.

## Secure Code
```python
import yaml
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/iot/device/provision', methods=['POST'])
def provision_iot_device():
    device_manifest = request.data.decode('utf-8')
    device_config = yaml.safe_load(device_manifest)
    if not isinstance(device_config, dict):
        return jsonify({"error": "Invalid device manifest format"}), 400
    device_id = device_config.get('device_id', 'unknown')
    firmware_ver = device_config.get('firmware_version', '1.0.0')
    telemetry_interval = device_config.get('telemetry_interval', 60)
    print(f"Provisioning device {device_id} with firmware {firmware_ver}")
    return jsonify({"status": "provisioned", "device_id": device_id, "interval": telemetry_interval})
```
