Most advice about obfuscating serialized types is a single warning: do not rename your DTOs, the names are the contract. That is true for System.Text.Json, Newtonsoft.Json, and DataContract — the member name is literally the key written to the wire. MessagePack-CSharp is more interesting, because it has two contract styles that behave in opposite ways. One never puts a member name on the wire and is completely rename-safe. The other writes the member names as map keys and breaks the moment you rename them — and it breaks in the worst way, because an in-process round-trip test still passes. Knowing which style a type uses is the whole game.
Two ways to key a MessagePack object
MessagePack is a binary format of maps, arrays and scalars. MessagePack-CSharp lets you map a CLR type onto it in two fundamentally different ways. The first is integer keys:
[MessagePackObject]
public sealed class Telemetry
{
[Key(0)] public int DeviceId { get; set; }
[Key(1)] public string Status { get; set; } = "";
}
The second is string keys — either explicitly with [Key("...")], or by asking MessagePack to use the property names for you:
[MessagePackObject(keyAsPropertyName: true)]
public sealed class Telemetry
{
public int DeviceId { get; set; }
public string Status { get; set; } = "";
}
These compile to different wire formats, and that difference is the entire story for obfuscation.
The bytes decide everything
Serialize new Telemetry { DeviceId = 7, Status = "ok" } with integer keys and you get a five-byte MessagePack array:
92 07 A2 6F 6B
└┬ └┬ └──┬───┘
│ │ └─ "ok" (A2 = fixstr len 2, then 6F 6B)
│ └─ 7 (positional: array[0] = DeviceId)
└─ fixarray, 2 elements
The field DeviceId is identified by its position in the array — index 0 — exactly like a protobuf tag number. The string "DeviceId" appears nowhere. Serialize the same object with keyAsPropertyName and you get a map instead, and the member names are right there as UTF-8:
82 A8 44 65 76 69 63 65 49 64 07 A6 53 74 61 74 75 73 A2 6F 6B
└┬ └───────────┬──────────┘ └┬ └──────┬───────┘ └┬ └──┬──┘
│ "DeviceId" │ "Status" │ "ok"
│ (A8 = fixstr len 8) 7 (A6 = fixstr len 6) │
└─ fixmap, 2 pairs └─ value
44 65 76 69 63 65 49 64 is literally the ASCII for DeviceId. The member name is the contract.
Why integer keys are rename-safe
Because the name never reaches the wire, Nebula is free to rewrite it. After renaming, a decompiler sees:
[MessagePackObject]
public sealed class b
{
[Key(0)] public int a { get; set; }
[Key(1)] public string c { get; set; } = "";
}
The [Key(0)] / [Key(1)] numbers are untouched — they are data in the attribute blob, not identifiers — so the serialized bytes are byte-for-byte identical to the un-obfuscated build. An old client that still has the original Telemetry reads the renamed server's output perfectly, because both sides agree that array slot 0 is the device id. This is the same property that makes protobuf-net rename-safe, and for the same reason: the contract is a number, and the name is only a label for your source tree. Integer keys are also the most compact MessagePack layout, so you lose nothing by preferring them.
Why string keys break — quietly
Now rename the keyAsPropertyName version. DeviceId becomes a, Status becomes b, and the map MessagePack writes changes to:
82 A1 61 07 A1 62 A2 6F 6B
└┬─┘ └┬─┘
"a" "b"
Any blob written by a previous build still has the keys "DeviceId" and "Status". When the renamed build reads it, those keys match no member — a and b stay at their defaults, and the data is silently lost. A second service written in another language, keyed on the documented names, breaks the same way.
Here is the trap that makes this dangerous rather than merely wrong. MessagePack-CSharp builds its formatter with the DynamicObjectResolver, which reads the member names by reflection, at formatter-generation time, and bakes the resulting UTF-8 key bytes into the generated serializer and deserializer. After obfuscation, reflection reports the new names, so the one obfuscated build writes "a"/"b" and reads "a"/"b". A unit test that serializes and immediately deserializes in the same process round-trips perfectly and goes green. Nothing fails until data crosses a version or service boundary — a cache entry from last week, a queued message from another node, a document written by the un-obfuscated staging build. Your tests cannot see that boundary, so they cannot warn you.
The ContractlessStandardResolver — which serializes plain POCOs with no [MessagePackObject] at all, using property names as string keys — has exactly this hazard and no attributes to signal it. Treat any type serialized contractlessly as name-fragile.
The explicit string key hides nothing
It is tempting to think [Key("DeviceId")] lets you rename the member while keeping the wire stable — and it does keep the wire stable, which is a real compatibility win. But understand what it does not buy you:
[MessagePackObject]
public sealed class Telemetry
{
[Key("DeviceId")] public int a { get; set; } // renamed member…
[Key("Status")] public string c { get; set; } = ""; // …original name still on the wire
}
The literal "DeviceId" is now stored in the custom-attribute blob in the assembly metadata, and it is still written to every serialized map. So the original, meaningful name survives both the decompiler and the wire. Explicit string keys are the right tool when you need to rename the code but preserve an existing on-disk or on-the-network contract — they are not a tool for hiding what the fields mean. If secrecy of the field names matters, integer keys are the only style that actually removes the names.
Scoping it in nebula.config.json
Put the whole assembly on full renaming and protection, then exempt only the specific types that use string keys or contractless serialization. A realistic configuration:
{
"schemaVersion": 1,
"inputs": [ "bin/Release/net8.0/Acme.Fleet.dll" ],
"outputDirectory": "protected",
"rename": { "enabled": true, "scope": "all" },
"stringEncryption": { "enabled": true },
"controlFlow": { "enabled": true },
"keep": [
{
"type": "Acme.Fleet.Contracts.*",
"members": "names",
"reason": "MessagePack keyAsPropertyName — map keys are member names"
}
]
}
"members": "names" preserves the member names for those contracts while still letting Nebula apply string encryption and control-flow transforms to their method bodies — you protect the logic, not the field names the serializer needs. Everything outside the keep-list gets renamed freely. Then protect the assembly:
nebula protect --config nebula.config.json
The better long-term fix is to migrate those contracts to integer keys. It is a one-time edit — add [Key(N)] to each member and drop keyAsPropertyName — after which the type is rename-safe, needs no keep rule, and serializes smaller. Keep the string-key keep-list for the contracts you genuinely cannot change, such as a wire format already shared with a third party.
Verify, don't assume
After protecting, open the output in Glass.NET and look at the serialized types. For an integer-keyed contract you should see renamed members carrying intact [Key(0)] numbers. For anything still on string keys, the keep-list should have left the member names readable — and if a [Key("...")] or [MessagePackObject(keyAsPropertyName: true)] type shows up renamed, that is a bug waiting to surface in production, because the round-trip test will never catch it. The surest regression test is not an in-process round-trip; it is deserializing a blob captured from the previous build and asserting the fields populate.
The rule of thumb
MessagePack rename-safety is a property of the key style, not the library. Integer keys carry positions, string keys carry names — and only names break under renaming. Prefer [Key(N)] for everything you obfuscate; it is rename-safe, compact, and leaks nothing. For the string-keyed contracts you cannot change, keep-list their names and protect their bodies, and remember that an explicit [Key("...")] preserves compatibility without preserving secrecy. Above all, do not trust a green round-trip test: the only honest check reads yesterday's bytes with today's build.
See Nebula.NET for the full transform set and the nebula.config.json reference.
Top comments (0)