---
title: "iOS Serialisation and Encoding"
section: "Mobile"
sectionSlug: "mobile-pentesting"
sourcePath: "src/mobile-pentesting/ios-pentesting/ios-serialisation-and-encoding.md"
sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/mobile-pentesting/ios-pentesting/ios-serialisation-and-encoding.md"
sha: "188de82beb54e70956b2952367a0af91d26758b8"
isIndex: false
modified: true
license: "CC-BY-NC-4.0"
---
# iOS Serialisation and Encoding
OWASP's object-serialization guidance provides additional code and testing context.<sup>[[1]](#references)</sup>
## Object Serialization in iOS Development
In iOS, **object serialization** converts objects into a representation that can be stored or transmitted and reconstructed later. `NSCoding` and `NSSecureCoding` support keyed archives for Objective-C-compatible object graphs, commonly producing `Data`/`NSData`; Swift value types commonly use `Codable` instead.<sup>[[2]](#references)</sup><sup>[[3]](#references)</sup>
### **`NSCoding`** Implementation
To implement `NSCoding`, a class must inherit from `NSObject` or be marked as `@objc`. This protocol mandates the implementation of two methods for encoding and decoding instance variables:
```swift
class CustomPoint: NSObject, NSCoding {
var x: Double = 0.0
var name: String = ""
func encode(with aCoder: NSCoder) {
aCoder.encode(x, forKey: "x")
aCoder.encode(name, forKey: "name")
}
required convenience init?(coder aDecoder: NSCoder) {
guard let name = aDecoder.decodeObject(forKey: "name") as? String else { return nil }
self.init(x: aDecoder.decodeDouble(forKey: "x"), name: name)
}
}
```
### **Enhancing Security with `NSSecureCoding`**
To reduce object-substitution attacks during unarchiving, **`NSSecureCoding`** requires callers to declare the expected classes and conforming classes to opt in. It improves type safety but does not encrypt or authenticate the archive, so untrusted or sensitive data may still require separate confidentiality and integrity controls.<sup>[[2]](#references)</sup>
```swift
static var supportsSecureCoding: Bool {
return true
}
let obj = decoder.decodeObject(of: MyClass.self, forKey: "myKey")
```
## Data Archiving with `NSKeyedArchiver`
`NSKeyedArchiver` and `NSKeyedUnarchiver` encode and decode object graphs. The following legacy APIs illustrate older code that may still appear during review, but Apple deprecated them; new code should use secure archiving/unarchiving APIs that specify the expected root class.<sup>[[3]](#references)</sup>
```swift
NSKeyedArchiver.archiveRootObject(customPoint, toFile: "/path/to/archive")
let customPoint = NSKeyedUnarchiver.unarchiveObjectWithFile("/path/to/archive") as? CustomPoint
```
### Using `Codable` for Simplified Serialization
Swift's `Codable` protocol combines `Decodable` and `Encodable`, facilitating the encoding and decoding of objects like `String`, `Int`, `Double`, etc., without extra effort:
```swift
struct CustomPointStruct: Codable {
var x: Double
var name: String
}
```
This approach supports straightforward serialization to and from property lists and JSON.<sup>[[4]](#references)</sup>
## JSON and XML Encoding Alternatives
Beyond native support, several third-party libraries offer JSON and XML encoding/decoding capabilities, each with its own performance characteristics and security considerations. It's imperative to carefully select these libraries, especially to mitigate vulnerabilities like XXE (XML External Entities) attacks by configuring parsers to prevent external entity processing.
### Security Considerations
When serializing data, especially to the file system, it's essential to be vigilant about the potential inclusion of sensitive information. Serialized data, if intercepted or improperly handled, can expose applications to risks such as unauthorized actions or data leakage. Encrypting and signing serialized data is recommended to enhance security.
## References
- [1] [OWASP MASTG - Object Serialization](https://mas.owasp.org/MASTG-KNOW-0075/)
- [2] [Apple - `NSSecureCoding`](https://developer.apple.com/documentation/foundation/nssecurecoding)
- [3] [Apple - `NSKeyedArchiver`](https://developer.apple.com/documentation/foundation/nskeyedarchiver)
- [4] [Apple - Encoding and Decoding Custom Types](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types)