# Deserialization of Untrusted Data (CWE-502) The product deserializes untrusted data without sufficiently verifying that the resulting data will be valid. **Stack:** Go - Prevalence: Średnia Pokryto 3 języków - Impact: Krytyczny 3 reguł o krytycznym poziomie - Prevention: Udokumentowane 7 przykładów poprawek **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 Strategie zapobiegania dla Deserialization of Untrusted Data oparte na 2 regułach detekcji Shoulder. ### 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 - Wykonanie nieautoryzowanego kodu - DoS: awaria / wyjście / restart - Modyfikacja danych aplikacji ## Mitigations - Jeśli to możliwe, unikaj deserializacji niezaufanych danych - Jeśli deserializacja jest konieczna, stosuj bezpieczniejsze formaty, takie jak JSON - Wdroż kontrole integralności, np. podpisy cyfrowe - Izoluj deserializację w środowiskach o niskich uprawnieniach ## 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