# Deserialization of Untrusted Data (CWE-502) The product deserializes untrusted data without sufficiently verifying that the resulting data will be valid. **Stack:** Go - Prevalence: Medium 3 languages covered - Impact: Critical 3 critical-severity rules - Prevention: Documented 7 fix examples **OWASP:** Software and Data Integrity Failures (A08:2021-Software and Data Integrity Failures) - #8 ## Description Many programming languages allow the serialization of objects for storage or transmission. When untrusted data is deserialized, it can lead to code execution, denial of service, or other unintended consequences. ## Prevention Prevention strategies for Deserialization of Untrusted Data based on 2 Shoulder detection rules. ### Go Use strict typed structs instead of interface{} and avoid gob with untrusted data Validate all training data against strict schemas and apply content moderation before ingestion ## Warning Signs - [HIGH] Untrusted data is deserialized without validation - [HIGH] truly dangerous deserialization in Go - [HIGH] Untrusted data flows to ... without validation - [HIGH] untrusted data flowing into AI/LLM fine-tuning or training processes without validation ## Consequences - Execute Unauthorized Code - DoS: Crash/Exit/Restart - Modify Application Data ## Mitigations - Avoid deserialization of untrusted data if possible - If deserialization is necessary, use safer formats like JSON - Implement integrity checks such as digital signatures - Isolate deserialization in low privilege environments ## Detection - Total rules: 7 - Critical: 3 - Languages: go, javascript, typescript, python ## Rules by Language ### Go (2 rules) - **Insecure Deserialization** [HIGH]: Detects truly dangerous deserialization in Go. Unlike Java or Python, Go's encoding/json is safe (data-only parsing, no code execution). This rule focuses on: - gob.Decoder: Can instantiate arbitrary types, potential RCE (CRITICAL) - json/yaml/xml to interface{}: Type confusion risk when combined with untrusted input (MEDIUM) Note: json.Unmarshal to typed structs is NOT flagged as it cannot execute code. - Remediation: Use strict struct types instead of interface{} and validate after unmarshaling. ```go type User struct { Name string `json:"name"` Email string `json:"email"` } var user User if err := json.Unmarshal(input, &user); err != nil { return err } ``` Learn more: https://shoulder.dev/learn/go/cwe-502/unsafe-deserialization - **LLM Training Data Poisoning** [HIGH]: Detects untrusted data flowing into AI/LLM fine-tuning or training processes without validation. - Remediation: Validate all training data against strict schemas before ingestion. ```go if err := validate.Struct(doc); err != nil { return errors.New("validation failed") } ``` Learn more: https://shoulder.dev/learn/go/cwe-502/llm-training-data-poisoning